Skip to content

Commit 6eb3ac0

Browse files
committed
Additional properties support
1 parent 77daa51 commit 6eb3ac0

File tree

1 file changed

+64
-4
lines changed

1 file changed

+64
-4
lines changed

index.js

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ function $asRegExp (reg) {
137137
return '"' + reg + '"'
138138
}
139139

140-
function addPatternProperties (pp) {
140+
function addPatternProperties (pp, ap) {
141141
let code = `
142142
var keys = Object.keys(obj)
143143
for (var i = 0; i < keys.length; i++) {
@@ -176,27 +176,85 @@ function addPatternProperties (pp) {
176176
`
177177
} else {
178178
code += `
179-
throw new Error('Cannot coerce ' + obj[keys[i]] + ' to ${type}')
179+
throw new Error('Cannot coerce ' + obj[keys[i]] + ' to ${type}')
180180
`
181181
}
182+
182183
code += `
184+
continue
183185
}
184186
`
185187
})
188+
if (ap) {
189+
code += additionalProperty(ap)
190+
}
191+
186192
code += `
187193
}
188-
if (Object.keys(properties).length === 0) json = json.substring(0, json.length - 1)
189194
`
190195
return code
191196
}
192197

198+
function additionalProperty (ap) {
199+
let code = ''
200+
let type = ap.type
201+
if (type === 'object') {
202+
code += buildObject(ap, '', 'buildObjectAP')
203+
code += `
204+
json += $asString(keys[i]) + ':' + buildObjectAP(obj[keys[i]]) + ','
205+
`
206+
} else if (type === 'array') {
207+
code += buildArray(ap, '', 'buildArrayAP')
208+
code += `
209+
json += $asString(keys[i]) + ':' + buildArrayAP(obj[keys[i]]) + ','
210+
`
211+
} else if (type === 'null') {
212+
code += `
213+
json += $asString(keys[i]) +':null,'
214+
`
215+
} else if (type === 'string') {
216+
code += `
217+
json += $asString(keys[i]) + ':' + $asString(obj[keys[i]]) + ','
218+
`
219+
} else if (type === 'number' || type === 'integer') {
220+
code += `
221+
json += $asString(keys[i]) + ':' + $asNumber(obj[keys[i]]) + ','
222+
`
223+
} else if (type === 'boolean') {
224+
code += `
225+
json += $asString(keys[i]) + ':' + $asBoolean(obj[keys[i]]) + ','
226+
`
227+
} else {
228+
code += `
229+
throw new Error('Cannot coerce ' + obj[keys[i]] + ' to ${type}')
230+
`
231+
}
232+
return code
233+
}
234+
235+
function addAdditionalProperties (ap) {
236+
return `
237+
var keys = Object.keys(obj)
238+
for (var i = 0; i < keys.length; i++) {
239+
if (properties[keys[i]]) continue
240+
${additionalProperty(ap)}
241+
}
242+
`
243+
}
244+
193245
function buildObject (schema, code, name) {
194246
code += `
195247
function ${name} (obj) {
196248
var json = '{'
197249
`
250+
198251
if (schema.patternProperties) {
199-
code += addPatternProperties(schema.patternProperties)
252+
code += addPatternProperties(schema.patternProperties, schema.additionalProperties)
253+
} 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+
}
257+
code += addAdditionalProperties(schema.additionalProperties)
200258
}
201259

202260
var laterCode = ''
@@ -232,7 +290,9 @@ function buildObject (schema, code, name) {
232290
`
233291
})
234292

293+
// Removes the comma if is the last element of the string (in case there are not properties)
235294
code += `
295+
if (json[json.length - 1] === ',') json = json.substring(0, json.length - 1)
236296
json += '}'
237297
return json
238298
}

0 commit comments

Comments
 (0)