Skip to content

Commit ff90167

Browse files
committed
Added 'additionalProperties: true' support
1 parent 3f70dc5 commit ff90167

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
const fastSafeStringify = require('fast-safe-stringify')
4+
35
function build (schema) {
46
/* eslint no-new-func: "off" */
57
var code = `
@@ -51,7 +53,9 @@ function build (schema) {
5153
;
5254
return ${main}
5355
`
54-
56+
if (schema.additionalProperties === true) {
57+
return (new Function('fastSafeStringify', code))(fastSafeStringify)
58+
}
5559
return (new Function(code))()
5660
}
5761

@@ -197,6 +201,11 @@ function addPatternProperties (pp, ap) {
197201

198202
function additionalProperty (ap) {
199203
let code = ''
204+
if (ap === true) {
205+
return `
206+
json += $asString(keys[i]) + ':' + fastSafeStringify(obj[keys[i]]) + ','
207+
`
208+
}
200209
let type = ap.type
201210
if (type === 'object') {
202211
code += buildObject(ap, '', 'buildObjectAP')
@@ -251,9 +260,6 @@ function buildObject (schema, code, name) {
251260
if (schema.patternProperties) {
252261
code += addPatternProperties(schema.patternProperties, schema.additionalProperties)
253262
} else if (schema.additionalProperties && !schema.patternProperties) {
254-
if (schema.additionalProperties === true) {
255-
throw new TypeError('additionalProperties must be an object, see the docs for more info')
256-
}
257263
code += addAdditionalProperties(schema.additionalProperties)
258264
}
259265

0 commit comments

Comments
 (0)