From 8eb992454c7a85127fae1a73991542995f103133 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=BCrg=C3=BCn=20Day=C4=B1o=C4=9Flu?= Date: Wed, 21 Oct 2026 23:12:27 +0200 Subject: [PATCH] fix: keep converted schemas in standalone validator state Validator#addSchema registered the converted schema with Ajv but stored the original one in the state used by standalone mode. The restored validator therefore lacked the fjs_type conversion and rejected values serialized through toJSON, such as Date for date-time strings, when inlineValidators is false. --- lib/validator.js | 2 +- test/standalone-mode.test.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/validator.js b/lib/validator.js index 124b5d59..96f20824 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 })