Skip to content

Commit 311e1a5

Browse files
committed
Fix externalSchema, added options object
1 parent db4b64a commit 311e1a5

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

index.js

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const fastSafeStringify = require('fast-safe-stringify')
44

5-
function build (schema) {
5+
function build (schema, options) {
6+
options = options || {}
67
/* eslint no-new-func: "off" */
78
var code = `
89
'use strict'
@@ -26,7 +27,7 @@ function build (schema) {
2627
switch (schema.type) {
2728
case 'object':
2829
main = '$main'
29-
code = buildObject(schema, code, main)
30+
code = buildObject(schema, code, main, options.schema)
3031
break
3132
case 'string':
3233
main = $asString.name
@@ -43,7 +44,7 @@ function build (schema) {
4344
break
4445
case 'array':
4546
main = '$main'
46-
code = buildArray(schema, code, main)
47+
code = buildArray(schema, code, main, options.schema)
4748
break
4849
default:
4950
throw new Error(`${schema.type} unsupported`)
@@ -141,7 +142,7 @@ function $asRegExp (reg) {
141142
return '"' + reg + '"'
142143
}
143144

144-
function addPatternProperties (schema) {
145+
function addPatternProperties (schema, externalSchema) {
145146
var pp = schema.patternProperties
146147
let code = `
147148
var keys = Object.keys(obj)
@@ -150,19 +151,19 @@ function addPatternProperties (schema) {
150151
`
151152
Object.keys(pp).forEach((regex, index) => {
152153
if (pp[regex]['$ref']) {
153-
pp[regex] = refFinder(pp[regex]['$ref'], schema)
154+
pp[regex] = refFinder(pp[regex]['$ref'], schema, externalSchema)
154155
}
155156
var type = pp[regex].type
156157
code += `
157158
if (/${regex}/.test(keys[i])) {
158159
`
159160
if (type === 'object') {
160-
code += buildObject(pp[regex], '', 'buildObjectPP' + index)
161+
code += buildObject(pp[regex], '', 'buildObjectPP' + index, externalSchema)
161162
code += `
162163
json += $asString(keys[i]) + ':' + buildObjectPP${index}(obj[keys[i]]) + ','
163164
`
164165
} else if (type === 'array') {
165-
code += buildArray(pp[regex], '', 'buildArrayPP' + index)
166+
code += buildArray(pp[regex], '', 'buildArrayPP' + index, externalSchema)
166167
code += `
167168
json += $asString(keys[i]) + ':' + buildArrayPP${index}(obj[keys[i]]) + ','
168169
`
@@ -194,7 +195,7 @@ function addPatternProperties (schema) {
194195
`
195196
})
196197
if (schema.additionalProperties) {
197-
code += additionalProperty(schema)
198+
code += additionalProperty(schema, externalSchema)
198199
}
199200

200201
code += `
@@ -203,7 +204,7 @@ function addPatternProperties (schema) {
203204
return code
204205
}
205206

206-
function additionalProperty (schema) {
207+
function additionalProperty (schema, externalSchema) {
207208
var ap = schema.additionalProperties
208209
let code = ''
209210
if (ap === true) {
@@ -212,17 +213,17 @@ function additionalProperty (schema) {
212213
`
213214
}
214215
if (ap['$ref']) {
215-
ap = refFinder(ap['$ref'], schema)
216+
ap = refFinder(ap['$ref'], schema, externalSchema)
216217
}
217218

218219
let type = ap.type
219220
if (type === 'object') {
220-
code += buildObject(ap, '', 'buildObjectAP')
221+
code += buildObject(ap, '', 'buildObjectAP', externalSchema)
221222
code += `
222223
json += $asString(keys[i]) + ':' + buildObjectAP(obj[keys[i]]) + ','
223224
`
224225
} else if (type === 'array') {
225-
code += buildArray(ap, '', 'buildArrayAP')
226+
code += buildArray(ap, '', 'buildArrayAP', externalSchema)
226227
code += `
227228
json += $asString(keys[i]) + ':' + buildArrayAP(obj[keys[i]]) + ','
228229
`
@@ -250,22 +251,22 @@ function additionalProperty (schema) {
250251
return code
251252
}
252253

253-
function addAdditionalProperties (schema) {
254+
function addAdditionalProperties (schema, externalSchema) {
254255
return `
255256
var keys = Object.keys(obj)
256257
for (var i = 0; i < keys.length; i++) {
257258
if (properties[keys[i]]) continue
258-
${additionalProperty(schema)}
259+
${additionalProperty(schema, externalSchema)}
259260
}
260261
`
261262
}
262263

263-
function refFinder (ref, schema) {
264+
function refFinder (ref, schema, externalSchema) {
264265
// Split file from walk
265266
ref = ref.split('#')
266267
// If external file
267268
if (ref[0]) {
268-
schema = require(ref[0])
269+
schema = externalSchema[ref[0]]
269270
}
270271
const walk = ref[1].split('/')
271272
let code = 'return schema'
@@ -275,16 +276,16 @@ function refFinder (ref, schema) {
275276
return (new Function('schema', code))(schema)
276277
}
277278

278-
function buildObject (schema, code, name) {
279+
function buildObject (schema, code, name, externalSchema) {
279280
code += `
280281
function ${name} (obj) {
281282
var json = '{'
282283
`
283284

284285
if (schema.patternProperties) {
285-
code += addPatternProperties(schema)
286+
code += addPatternProperties(schema, externalSchema)
286287
} else if (schema.additionalProperties && !schema.patternProperties) {
287-
code += addAdditionalProperties(schema)
288+
code += addAdditionalProperties(schema, externalSchema)
288289
}
289290

290291
var laterCode = ''
@@ -298,10 +299,10 @@ function buildObject (schema, code, name) {
298299
`
299300

300301
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)
302303
}
303304

304-
const result = nested(laterCode, name, '.' + key, schema.properties[key])
305+
const result = nested(laterCode, name, '.' + key, schema.properties[key], externalSchema)
305306

306307
code += result.code
307308
laterCode = result.laterCode
@@ -336,15 +337,15 @@ function buildObject (schema, code, name) {
336337
return code
337338
}
338339

339-
function buildArray (schema, code, name) {
340+
function buildArray (schema, code, name, externalSchema) {
340341
code += `
341342
function ${name} (obj) {
342343
var json = '['
343344
`
344345

345346
var laterCode = ''
346347

347-
const result = nested(laterCode, name, '[i]', schema.items)
348+
const result = nested(laterCode, name, '[i]', schema.items, externalSchema)
348349

349350
code += `
350351
const l = obj.length
@@ -370,7 +371,7 @@ function buildArray (schema, code, name) {
370371
return code
371372
}
372373

373-
function nested (laterCode, name, key, schema) {
374+
function nested (laterCode, name, key, schema, externalSchema) {
374375
var code = ''
375376
var funcName
376377
const type = schema.type
@@ -398,14 +399,14 @@ function nested (laterCode, name, key, schema) {
398399
break
399400
case 'object':
400401
funcName = (name + key).replace(/[-.\[\]]/g, '')
401-
laterCode = buildObject(schema, laterCode, funcName)
402+
laterCode = buildObject(schema, laterCode, funcName, externalSchema)
402403
code += `
403404
json += ${funcName}(obj${key})
404405
`
405406
break
406407
case 'array':
407408
funcName = (name + key).replace(/[-.\[\]]/g, '')
408-
laterCode = buildArray(schema, laterCode, funcName)
409+
laterCode = buildArray(schema, laterCode, funcName, externalSchema)
409410
code += `
410411
json += ${funcName}(obj${key})
411412
`

0 commit comments

Comments
 (0)