From 7e27685d2a973a34b42770c87dc561478cb7107e Mon Sep 17 00:00:00 2001 From: Luan Taraschi <130802253+luantaraschi@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:35:30 -0300 Subject: [PATCH 1/2] fix: write null instead of undefined for values JSON cannot represent Schemas without a type (an empty schema, `true`, or an array without `items`) and the extra items allowed by `additionalItems: true` are serialized with `JSON.stringify(value)`. For `undefined`, functions and symbols that call returns `undefined`, which was appended to the output as the text `undefined`, so `[1, undefined]` with `{ type: 'array' }` became `[1,undefined]`, which is not valid JSON. Fall back to `null` in those places, which is what `JSON.stringify` writes for such values inside arrays. --- index.js | 11 +++++++---- test/any.test.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 0d5fa26b..6744a77a 100644 --- a/index.js +++ b/index.js @@ -1179,7 +1179,7 @@ function buildArray (context, location, input) { if (i) { json += JSON_STR_COMMA } - json += JSON.stringify(obj[i]) + json += JSON.stringify(obj[i]) ?? JSON_STR_NULL }` } } else { @@ -1269,7 +1269,7 @@ function buildArray (context, location, input) { inlinedCode += ` for (let i = ${itemsSchema.length}; i < arrayLength_${objVar}; i++) { !addComma_${localUid} && (addComma_${localUid} = true) || (json += JSON_STR_COMMA) - json += JSON.stringify(${objVar}[i]) + json += JSON.stringify(${objVar}[i]) ?? JSON_STR_NULL }` } } else { @@ -1473,7 +1473,10 @@ function buildSingleTypeSerializer (context, location, input) { return buildArray(context, location, input) } case undefined: - return `json += JSON.stringify(${input})` + // JSON.stringify returns undefined for undefined, functions and symbols. + // Write null instead, as JSON.stringify does for array items, so the + // output stays valid JSON. + return `json += JSON.stringify(${input}) ?? JSON_STR_NULL` default: throw new Error(`${schema.type} unsupported`) } @@ -1761,7 +1764,7 @@ function buildValue (context, location, input) { let schema = location.schema if (typeof schema === 'boolean') { - return `json += JSON.stringify(${input})` + return `json += JSON.stringify(${input}) ?? JSON_STR_NULL` } if (schema.$ref) { diff --git a/test/any.test.js b/test/any.test.js index a73aa0c1..7d5b6261 100644 --- a/test/any.test.js +++ b/test/any.test.js @@ -114,6 +114,45 @@ test('empty schema on array', (t) => { t.assert.equal(stringify([1, true, 'hello', [], { x: 1 }]), '[1,true,"hello",[],{"x":1}]') }) +test('empty schema on array writes null for values JSON cannot represent', (t) => { + t.plan(4) + + const stringify = build({ + type: 'array', + items: {} + }) + + t.assert.equal(stringify([1, undefined, 2]), '[1,null,2]') + t.assert.equal(stringify([() => {}, Symbol('s')]), '[null,null]') + t.assert.equal(stringify(new Array(2)), '[null,null]') + t.assert.equal(build({ type: 'array' })([undefined]), '[null]') +}) + +test('additionalItems true writes null for values JSON cannot represent', (t) => { + t.plan(1) + + const stringify = build({ + type: 'array', + items: [{ type: 'integer' }], + additionalItems: true + }) + + t.assert.equal(stringify([1, undefined, () => {}]), '[1,null,null]') +}) + +test('empty schema never writes undefined', (t) => { + t.plan(3) + + t.assert.equal(build({})(undefined), 'null') + t.assert.equal(build(true)(() => {}), 'null') + t.assert.equal(build({ + type: 'object', + properties: { + x: {} + } + })({ x: () => {} }), '{"x":null}') +}) + test('empty schema on anyOf', (t) => { t.plan(4) From b466fb1a65b030428ccda8b14e93fe23f75e9192 Mon Sep 17 00:00:00 2001 From: Luan Taraschi <130802253+luantaraschi@users.noreply.github.com> Date: Wed, 18 Nov 2026 19:43:44 -0300 Subject: [PATCH 2/2] fix: write null for additional properties whose toJSON returns undefined With `additionalProperties: true` the value goes straight to `JSON.stringify`, so an object whose `toJSON()` returns `undefined` still came out as `{"x":undefined}`. Use the same `null` fallback there. Also note in the README how values JSON cannot represent are written. --- README.md | 2 ++ index.js | 2 +- test/additionalProperties.test.js | 12 ++++++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 8e709af9..c078a0c4 100644 --- a/README.md +++ b/README.md @@ -239,6 +239,8 @@ const obj = { console.log(stringify(obj)) // '{"mail":"mail@example.com"}' ``` +A property set to `undefined` is left out as well. Values that JSON cannot represent (functions, symbols, objects whose `toJSON()` returns `undefined`) are written as `null` when they reach a schema without a `type`, such as `{}` or `true`. `JSON.stringify` also writes `null` for them inside arrays, but leaves the key out inside objects. Additional and pattern properties holding a function or a symbol are still left out. + #### Defaults `fast-json-stringify` supports `default` jsonschema key in order to serialize a value diff --git a/index.js b/index.js index 6744a77a..c96c8bbc 100644 --- a/index.js +++ b/index.js @@ -690,7 +690,7 @@ function buildExtraObjectPropertiesSerializer (context, location, addComma, objV if (additionalPropertiesSchema === true) { code += ` ${addComma} - json += asString(key) + JSON_STR_COLONS + JSON.stringify(value) + json += asString(key) + JSON_STR_COLONS + (JSON.stringify(value) ?? JSON_STR_NULL) ` } else { const propertyLocation = location.getPropertyLocation('additionalProperties') diff --git a/test/additionalProperties.test.js b/test/additionalProperties.test.js index 980dd00b..03654157 100644 --- a/test/additionalProperties.test.js +++ b/test/additionalProperties.test.js @@ -417,3 +417,15 @@ test('additionalProperties set to false without declared properties ignores unma t.assert.equal(out, '{"str1":"a"}') t.assert.deepStrictEqual(JSON.parse(out), { str1: 'a' }) }) + +test('additionalProperties set to true writes null when toJSON returns undefined', (t) => { + t.plan(2) + const stringify = build({ + type: 'object', + additionalProperties: true + }) + + const out = stringify({ x: { toJSON () { return undefined } }, y: 1 }) + t.assert.equal(out, '{"x":null,"y":1}') + t.assert.deepStrictEqual(JSON.parse(out), { x: null, y: 1 }) +})