Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class Validator {
const ajvSchema = clone(schema)
this.convertSchemaToAjvFormat(ajvSchema)
this.ajv.addSchema(ajvSchema, schemaKey)
this._ajvSchemas[schemaKey] = schema
this._ajvSchemas[schemaKey] = ajvSchema
}
}

Expand Down
32 changes: 32 additions & 0 deletions test/standalone-mode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,38 @@ test('inline validators with external oneOf refs and toJSON values', async (t) =
t.assert.throws(() => stringify({ value: true }), /does not match schema definition/)
})

test('restored validator handles toJSON values', async (t) => {
t.plan(3)

after(async () => {
await fs.promises.rm(destination, { force: true })
})

const code = fjs({
type: 'object',
properties: {
value: {
oneOf: [
{ type: 'string', format: 'date-time' },
{ type: 'integer' }
]
}
}
}, { mode: 'standalone', inlineValidators: false })

const destination = path.resolve(tmpDir, 'standalone-restored-toJSON.js')

await fs.promises.writeFile(destination, code)
const stringify = require(destination)

t.assert.equal(
stringify({ value: new Date('2020-01-02T03:04:05.000Z') }),
'{"value":"2020-01-02T03:04:05.000Z"}'
)
t.assert.equal(stringify({ value: 42 }), '{"value":42}')
t.assert.throws(() => stringify({ value: true }), /does not match schema definition/)
})

test('does not emit unused inline validators', (t) => {
const code = build({ mode: 'standalone', inlineValidators: true })

Expand Down
Loading