diff --git a/lib/validator.js b/lib/validator.js index 57e053e3..d96d89a4 100644 --- a/lib/validator.js +++ b/lib/validator.js @@ -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 } } diff --git a/test/standalone-mode.test.js b/test/standalone-mode.test.js index abb489b3..0c11fa4c 100644 --- a/test/standalone-mode.test.js +++ b/test/standalone-mode.test.js @@ -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 })