Skip to content

Commit 6a03de8

Browse files
committed
If a value is undefined, it shouldn't be passed to fastSafeStringify
1 parent b4d8301 commit 6a03de8

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ function additionalProperty (schema, externalSchema, fullSchema) {
231231
let code = ''
232232
if (ap === true) {
233233
return `
234-
json += $asString(keys[i]) + ':' + fastSafeStringify(obj[keys[i]]) + ','
234+
if (obj[keys[i]] !== undefined)
235+
json += $asString(keys[i]) + ':' + fastSafeStringify(obj[keys[i]]) + ','
235236
`
236237
}
237238
if (ap['$ref']) {

test/additionalProperties.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,20 @@ test('nested additionalProperties set to true', (t) => {
254254
let obj = { ap: { value: 'string', someNumber: 42 } }
255255
t.equal('{"ap":{"value":"string","someNumber":42}}', stringify(obj))
256256
})
257+
258+
test('field passed to fastSafeStringify as undefined should be removed', (t) => {
259+
t.plan(1)
260+
const stringify = build({
261+
title: 'nested additionalProperties=true',
262+
type: 'object',
263+
properties: {
264+
ap: {
265+
type: 'object',
266+
additionalProperties: true
267+
}
268+
}
269+
})
270+
271+
let obj = { ap: { value: 'string', someNumber: undefined } }
272+
t.equal('{"ap":{"value":"string"}}', stringify(obj))
273+
})

0 commit comments

Comments
 (0)