@@ -137,7 +137,7 @@ function $asRegExp (reg) {
137
137
return '"' + reg + '"'
138
138
}
139
139
140
- function addPatternProperties ( pp ) {
140
+ function addPatternProperties ( pp , ap ) {
141
141
let code = `
142
142
var keys = Object.keys(obj)
143
143
for (var i = 0; i < keys.length; i++) {
@@ -176,27 +176,85 @@ function addPatternProperties (pp) {
176
176
`
177
177
} else {
178
178
code += `
179
- throw new Error('Cannot coerce ' + obj[keys[i]] + ' to ${ type } ')
179
+ throw new Error('Cannot coerce ' + obj[keys[i]] + ' to ${ type } ')
180
180
`
181
181
}
182
+
182
183
code += `
184
+ continue
183
185
}
184
186
`
185
187
} )
188
+ if ( ap ) {
189
+ code += additionalProperty ( ap )
190
+ }
191
+
186
192
code += `
187
193
}
188
- if (Object.keys(properties).length === 0) json = json.substring(0, json.length - 1)
189
194
`
190
195
return code
191
196
}
192
197
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
+
193
245
function buildObject ( schema , code , name ) {
194
246
code += `
195
247
function ${ name } (obj) {
196
248
var json = '{'
197
249
`
250
+
198
251
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 )
200
258
}
201
259
202
260
var laterCode = ''
@@ -232,7 +290,9 @@ function buildObject (schema, code, name) {
232
290
`
233
291
} )
234
292
293
+ // Removes the comma if is the last element of the string (in case there are not properties)
235
294
code += `
295
+ if (json[json.length - 1] === ',') json = json.substring(0, json.length - 1)
236
296
json += '}'
237
297
return json
238
298
}
0 commit comments