2
2
3
3
const fastSafeStringify = require ( 'fast-safe-stringify' )
4
4
5
- function build ( schema ) {
5
+ function build ( schema , options ) {
6
+ options = options || { }
6
7
/* eslint no-new-func: "off" */
7
8
var code = `
8
9
'use strict'
@@ -26,7 +27,7 @@ function build (schema) {
26
27
switch ( schema . type ) {
27
28
case 'object' :
28
29
main = '$main'
29
- code = buildObject ( schema , code , main )
30
+ code = buildObject ( schema , code , main , options . schema )
30
31
break
31
32
case 'string' :
32
33
main = $asString . name
@@ -43,7 +44,7 @@ function build (schema) {
43
44
break
44
45
case 'array' :
45
46
main = '$main'
46
- code = buildArray ( schema , code , main )
47
+ code = buildArray ( schema , code , main , options . schema )
47
48
break
48
49
default :
49
50
throw new Error ( `${ schema . type } unsupported` )
@@ -141,7 +142,7 @@ function $asRegExp (reg) {
141
142
return '"' + reg + '"'
142
143
}
143
144
144
- function addPatternProperties ( schema ) {
145
+ function addPatternProperties ( schema , externalSchema ) {
145
146
var pp = schema . patternProperties
146
147
let code = `
147
148
var keys = Object.keys(obj)
@@ -150,19 +151,19 @@ function addPatternProperties (schema) {
150
151
`
151
152
Object . keys ( pp ) . forEach ( ( regex , index ) => {
152
153
if ( pp [ regex ] [ '$ref' ] ) {
153
- pp [ regex ] = refFinder ( pp [ regex ] [ '$ref' ] , schema )
154
+ pp [ regex ] = refFinder ( pp [ regex ] [ '$ref' ] , schema , externalSchema )
154
155
}
155
156
var type = pp [ regex ] . type
156
157
code += `
157
158
if (/${ regex } /.test(keys[i])) {
158
159
`
159
160
if ( type === 'object' ) {
160
- code += buildObject ( pp [ regex ] , '' , 'buildObjectPP' + index )
161
+ code += buildObject ( pp [ regex ] , '' , 'buildObjectPP' + index , externalSchema )
161
162
code += `
162
163
json += $asString(keys[i]) + ':' + buildObjectPP${ index } (obj[keys[i]]) + ','
163
164
`
164
165
} else if ( type === 'array' ) {
165
- code += buildArray ( pp [ regex ] , '' , 'buildArrayPP' + index )
166
+ code += buildArray ( pp [ regex ] , '' , 'buildArrayPP' + index , externalSchema )
166
167
code += `
167
168
json += $asString(keys[i]) + ':' + buildArrayPP${ index } (obj[keys[i]]) + ','
168
169
`
@@ -194,7 +195,7 @@ function addPatternProperties (schema) {
194
195
`
195
196
} )
196
197
if ( schema . additionalProperties ) {
197
- code += additionalProperty ( schema )
198
+ code += additionalProperty ( schema , externalSchema )
198
199
}
199
200
200
201
code += `
@@ -203,7 +204,7 @@ function addPatternProperties (schema) {
203
204
return code
204
205
}
205
206
206
- function additionalProperty ( schema ) {
207
+ function additionalProperty ( schema , externalSchema ) {
207
208
var ap = schema . additionalProperties
208
209
let code = ''
209
210
if ( ap === true ) {
@@ -212,17 +213,17 @@ function additionalProperty (schema) {
212
213
`
213
214
}
214
215
if ( ap [ '$ref' ] ) {
215
- ap = refFinder ( ap [ '$ref' ] , schema )
216
+ ap = refFinder ( ap [ '$ref' ] , schema , externalSchema )
216
217
}
217
218
218
219
let type = ap . type
219
220
if ( type === 'object' ) {
220
- code += buildObject ( ap , '' , 'buildObjectAP' )
221
+ code += buildObject ( ap , '' , 'buildObjectAP' , externalSchema )
221
222
code += `
222
223
json += $asString(keys[i]) + ':' + buildObjectAP(obj[keys[i]]) + ','
223
224
`
224
225
} else if ( type === 'array' ) {
225
- code += buildArray ( ap , '' , 'buildArrayAP' )
226
+ code += buildArray ( ap , '' , 'buildArrayAP' , externalSchema )
226
227
code += `
227
228
json += $asString(keys[i]) + ':' + buildArrayAP(obj[keys[i]]) + ','
228
229
`
@@ -250,22 +251,22 @@ function additionalProperty (schema) {
250
251
return code
251
252
}
252
253
253
- function addAdditionalProperties ( schema ) {
254
+ function addAdditionalProperties ( schema , externalSchema ) {
254
255
return `
255
256
var keys = Object.keys(obj)
256
257
for (var i = 0; i < keys.length; i++) {
257
258
if (properties[keys[i]]) continue
258
- ${ additionalProperty ( schema ) }
259
+ ${ additionalProperty ( schema , externalSchema ) }
259
260
}
260
261
`
261
262
}
262
263
263
- function refFinder ( ref , schema ) {
264
+ function refFinder ( ref , schema , externalSchema ) {
264
265
// Split file from walk
265
266
ref = ref . split ( '#' )
266
267
// If external file
267
268
if ( ref [ 0 ] ) {
268
- schema = require ( ref [ 0 ] )
269
+ schema = externalSchema [ ref [ 0 ] ]
269
270
}
270
271
const walk = ref [ 1 ] . split ( '/' )
271
272
let code = 'return schema'
@@ -275,16 +276,16 @@ function refFinder (ref, schema) {
275
276
return ( new Function ( 'schema' , code ) ) ( schema )
276
277
}
277
278
278
- function buildObject ( schema , code , name ) {
279
+ function buildObject ( schema , code , name , externalSchema ) {
279
280
code += `
280
281
function ${ name } (obj) {
281
282
var json = '{'
282
283
`
283
284
284
285
if ( schema . patternProperties ) {
285
- code += addPatternProperties ( schema )
286
+ code += addPatternProperties ( schema , externalSchema )
286
287
} else if ( schema . additionalProperties && ! schema . patternProperties ) {
287
- code += addAdditionalProperties ( schema )
288
+ code += addAdditionalProperties ( schema , externalSchema )
288
289
}
289
290
290
291
var laterCode = ''
@@ -298,10 +299,10 @@ function buildObject (schema, code, name) {
298
299
`
299
300
300
301
if ( schema . properties [ key ] [ '$ref' ] ) {
301
- schema . properties [ key ] = refFinder ( schema . properties [ key ] [ '$ref' ] , schema )
302
+ schema . properties [ key ] = refFinder ( schema . properties [ key ] [ '$ref' ] , schema , externalSchema )
302
303
}
303
304
304
- const result = nested ( laterCode , name , '.' + key , schema . properties [ key ] )
305
+ const result = nested ( laterCode , name , '.' + key , schema . properties [ key ] , externalSchema )
305
306
306
307
code += result . code
307
308
laterCode = result . laterCode
@@ -336,15 +337,15 @@ function buildObject (schema, code, name) {
336
337
return code
337
338
}
338
339
339
- function buildArray ( schema , code , name ) {
340
+ function buildArray ( schema , code , name , externalSchema ) {
340
341
code += `
341
342
function ${ name } (obj) {
342
343
var json = '['
343
344
`
344
345
345
346
var laterCode = ''
346
347
347
- const result = nested ( laterCode , name , '[i]' , schema . items )
348
+ const result = nested ( laterCode , name , '[i]' , schema . items , externalSchema )
348
349
349
350
code += `
350
351
const l = obj.length
@@ -370,7 +371,7 @@ function buildArray (schema, code, name) {
370
371
return code
371
372
}
372
373
373
- function nested ( laterCode , name , key , schema ) {
374
+ function nested ( laterCode , name , key , schema , externalSchema ) {
374
375
var code = ''
375
376
var funcName
376
377
const type = schema . type
@@ -398,14 +399,14 @@ function nested (laterCode, name, key, schema) {
398
399
break
399
400
case 'object' :
400
401
funcName = ( name + key ) . replace ( / [ - . \[ \] ] / g, '' )
401
- laterCode = buildObject ( schema , laterCode , funcName )
402
+ laterCode = buildObject ( schema , laterCode , funcName , externalSchema )
402
403
code += `
403
404
json += ${ funcName } (obj${ key } )
404
405
`
405
406
break
406
407
case 'array' :
407
408
funcName = ( name + key ) . replace ( / [ - . \[ \] ] / g, '' )
408
- laterCode = buildArray ( schema , laterCode , funcName )
409
+ laterCode = buildArray ( schema , laterCode , funcName , externalSchema )
409
410
code += `
410
411
json += ${ funcName } (obj${ key } )
411
412
`
0 commit comments