Skip to content

Commit 8b3d849

Browse files
authored
support for empty schemas in additionalProperties (#294)
1 parent 8681de0 commit 8b3d849

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,11 @@ function addPatternProperties (location) {
442442
${addComma}
443443
json += $asString(keys[i]) + ':' + $asBoolean(obj[keys[i]])
444444
`
445+
} else if (type === undefined) {
446+
code += `
447+
${addComma}
448+
json += $asString(keys[i]) + ':' + $asAny(obj[keys[i]])
449+
`
445450
} else {
446451
code += `
447452
throw new Error('Cannot coerce ' + obj[keys[i]] + ' to ' + ${JSON.stringify(type)})
@@ -535,6 +540,11 @@ function additionalProperty (location) {
535540
${addComma}
536541
json += $asString(keys[i]) + ':' + $asBoolean(obj[keys[i]])
537542
`
543+
} else if (type === undefined) {
544+
code += `
545+
${addComma}
546+
json += $asString(keys[i]) + ':' + $asAny(obj[keys[i]])
547+
`
538548
} else {
539549
code += `
540550
throw new Error('Cannot coerce ' + obj[keys[i]] + ' to ' + ${JSON.stringify(type)})

test/additionalProperties.test.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,31 @@ test('additionalProperties - array coerce', (t) => {
161161
t.equal('{"foo":["t","r","u","e"],"ofoo":[],"arrfoo":["1","2"],"objfoo":[]}', stringify(obj))
162162
})
163163

164+
test('additionalProperties with empty schema', (t) => {
165+
t.plan(1)
166+
const stringify = build({
167+
type: 'object',
168+
additionalProperties: {}
169+
})
170+
171+
const obj = { a: 1, b: true, c: null }
172+
t.equal('{"a":1,"b":true,"c":null}', stringify(obj))
173+
})
174+
175+
test('additionalProperties with nested empty schema', (t) => {
176+
t.plan(1)
177+
const stringify = build({
178+
type: 'object',
179+
properties: {
180+
data: { type: 'object', additionalProperties: {} }
181+
},
182+
required: ['data']
183+
})
184+
185+
const obj = { data: { a: 1, b: true, c: null } }
186+
t.equal('{"data":{"a":1,"b":true,"c":null}}', stringify(obj))
187+
})
188+
164189
test('nested additionalProperties', (t) => {
165190
t.plan(1)
166191
const stringify = build({

0 commit comments

Comments
 (0)