Skip to content

Commit 2777982

Browse files
committed
Allow specifying deep additionalProperties=true
1 parent f4011e6 commit 2777982

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

index.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,26 @@ function build (schema, options) {
7474
;
7575
return ${main}
7676
`
77-
if (schema.additionalProperties === true) {
77+
if (hasAdditionalPropertiesTrue(schema)) {
7878
return (new Function('fastSafeStringify', code))(fastSafeStringify)
7979
}
8080
return (new Function(code))()
8181
}
8282

83+
function hasAdditionalPropertiesTrue (schema) {
84+
if (schema.additionalProperties === true) { return true }
85+
86+
var objectKeys = Object.keys(schema)
87+
for (var i = 0; i < objectKeys.length; i++) {
88+
var value = schema[objectKeys[i]]
89+
if (typeof value === 'object') {
90+
if (hasAdditionalPropertiesTrue(value)) { return true }
91+
}
92+
}
93+
94+
return false
95+
}
96+
8397
function $asNull () {
8498
return 'null'
8599
}

test/additionalProperties.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,3 +237,20 @@ test('very nested additionalProperties', (t) => {
237237
let obj = [{ ap: { nested: { moarNested: { finally: { value: 'str' } } } } }]
238238
t.equal('[{"ap":{"nested":{"moarNested":{"finally":{"value":"str"}}}}}]', stringify(obj))
239239
})
240+
241+
test('nested additionalProperties set to true', (t) => {
242+
t.plan(1)
243+
const stringify = build({
244+
title: 'nested additionalProperties=true',
245+
type: 'object',
246+
properties: {
247+
ap: {
248+
type: 'object',
249+
additionalProperties: true
250+
}
251+
}
252+
})
253+
254+
let obj = { ap: { value: 'string', someNumber: 42 } }
255+
t.equal('{"ap":{"value":"string","someNumber":42}}', stringify(obj))
256+
})

0 commit comments

Comments
 (0)