Skip to content

Commit a840b16

Browse files
authored
Fix creation of invalid JSON when serializing functions or symbols (#299) (#300)
1 parent e61de64 commit a840b16

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ function additionalProperty (location) {
475475
let code = ''
476476
if (ap === true) {
477477
return `
478-
if (obj[keys[i]] !== undefined) {
478+
if (obj[keys[i]] !== undefined && typeof obj[keys[i]] !== 'function' && typeof obj[keys[i]] !== 'symbol') {
479479
${addComma}
480480
json += $asString(keys[i]) + ':' + JSON.stringify(obj[keys[i]])
481481
}

test/additionalProperties.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,20 @@ test('property without type but with enum, will acts as additionalProperties wit
309309
const obj = { ap: { additional: 'field' } }
310310
t.equal('{"ap":{}}', stringify(obj))
311311
})
312+
313+
test('function and symbol references are not serialized as undefined', (t) => {
314+
t.plan(1)
315+
const stringify = build({
316+
title: 'additionalProperties',
317+
type: 'object',
318+
additionalProperties: true,
319+
properties: {
320+
str: {
321+
type: 'string'
322+
}
323+
}
324+
})
325+
326+
const obj = { str: 'x', test: 'test', meth: () => 'x', sym: Symbol('x') }
327+
t.equal('{"str":"x","test":"test"}', stringify(obj))
328+
})

0 commit comments

Comments
 (0)