Skip to content

Commit df9a264

Browse files
authored
Merge pull request #37 from SimenB/fastSafeStringify-undefined
If a value is undefined, it shouldn't be passed to fastSafeStringify
2 parents 9ef997e + 6a03de8 commit df9a264

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
@@ -237,7 +237,8 @@ function additionalProperty (schema, externalSchema, fullSchema) {
237237
let code = ''
238238
if (ap === true) {
239239
return `
240-
json += $asString(keys[i]) + ':' + fastSafeStringify(obj[keys[i]]) + ','
240+
if (obj[keys[i]] !== undefined)
241+
json += $asString(keys[i]) + ':' + fastSafeStringify(obj[keys[i]]) + ','
241242
`
242243
}
243244
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)