@@ -141,13 +141,17 @@ function $asRegExp (reg) {
141
141
return '"' + reg + '"'
142
142
}
143
143
144
- function addPatternProperties ( pp , ap ) {
144
+ function addPatternProperties ( schema ) {
145
+ var pp = schema . patternProperties
145
146
let code = `
146
147
var keys = Object.keys(obj)
147
148
for (var i = 0; i < keys.length; i++) {
148
149
if (properties[keys[i]]) continue
149
150
`
150
151
Object . keys ( pp ) . forEach ( ( regex , index ) => {
152
+ if ( pp [ regex ] [ '$ref' ] ) {
153
+ pp [ regex ] = refFinder ( pp [ regex ] [ '$ref' ] , schema )
154
+ }
151
155
var type = pp [ regex ] . type
152
156
code += `
153
157
if (/${ regex } /.test(keys[i])) {
@@ -189,8 +193,8 @@ function addPatternProperties (pp, ap) {
189
193
}
190
194
`
191
195
} )
192
- if ( ap ) {
193
- code += additionalProperty ( ap )
196
+ if ( schema . additionalProperties ) {
197
+ code += additionalProperty ( schema )
194
198
}
195
199
196
200
code += `
@@ -199,13 +203,18 @@ function addPatternProperties (pp, ap) {
199
203
return code
200
204
}
201
205
202
- function additionalProperty ( ap ) {
206
+ function additionalProperty ( schema ) {
207
+ var ap = schema . additionalProperties
203
208
let code = ''
204
209
if ( ap === true ) {
205
210
return `
206
211
json += $asString(keys[i]) + ':' + fastSafeStringify(obj[keys[i]]) + ','
207
212
`
208
213
}
214
+ if ( ap [ '$ref' ] ) {
215
+ ap = refFinder ( ap [ '$ref' ] , schema )
216
+ }
217
+
209
218
let type = ap . type
210
219
if ( type === 'object' ) {
211
220
code += buildObject ( ap , '' , 'buildObjectAP' )
@@ -241,26 +250,41 @@ function additionalProperty (ap) {
241
250
return code
242
251
}
243
252
244
- function addAdditionalProperties ( ap ) {
253
+ function addAdditionalProperties ( schema ) {
245
254
return `
246
255
var keys = Object.keys(obj)
247
256
for (var i = 0; i < keys.length; i++) {
248
257
if (properties[keys[i]]) continue
249
- ${ additionalProperty ( ap ) }
258
+ ${ additionalProperty ( schema ) }
250
259
}
251
260
`
252
261
}
253
262
263
+ function refFinder ( ref , schema ) {
264
+ // Split file from walk
265
+ ref = ref . split ( '#' )
266
+ // If external file
267
+ if ( ref [ 0 ] ) {
268
+ schema = require ( ref [ 0 ] )
269
+ }
270
+ const walk = ref [ 1 ] . split ( '/' )
271
+ let code = 'return schema'
272
+ for ( let i = 1 ; i < walk . length ; i ++ ) {
273
+ code += `['${ walk [ i ] } ']`
274
+ }
275
+ return ( new Function ( 'schema' , code ) ) ( schema )
276
+ }
277
+
254
278
function buildObject ( schema , code , name ) {
255
279
code += `
256
280
function ${ name } (obj) {
257
281
var json = '{'
258
282
`
259
283
260
284
if ( schema . patternProperties ) {
261
- code += addPatternProperties ( schema . patternProperties , schema . additionalProperties )
285
+ code += addPatternProperties ( schema )
262
286
} else if ( schema . additionalProperties && ! schema . patternProperties ) {
263
- code += addAdditionalProperties ( schema . additionalProperties )
287
+ code += addAdditionalProperties ( schema )
264
288
}
265
289
266
290
var laterCode = ''
@@ -273,6 +297,10 @@ function buildObject (schema, code, name) {
273
297
json += '${ $asString ( key ) } :'
274
298
`
275
299
300
+ if ( schema . properties [ key ] [ '$ref' ] ) {
301
+ schema . properties [ key ] = refFinder ( schema . properties [ key ] [ '$ref' ] , schema )
302
+ }
303
+
276
304
const result = nested ( laterCode , name , '.' + key , schema . properties [ key ] )
277
305
278
306
code += result . code
0 commit comments