Skip to content

Commit db4b64a

Browse files
committed
support
1 parent d7c6066 commit db4b64a

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

index.js

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,17 @@ function $asRegExp (reg) {
141141
return '"' + reg + '"'
142142
}
143143

144-
function addPatternProperties (pp, ap) {
144+
function addPatternProperties (schema) {
145+
var pp = schema.patternProperties
145146
let code = `
146147
var keys = Object.keys(obj)
147148
for (var i = 0; i < keys.length; i++) {
148149
if (properties[keys[i]]) continue
149150
`
150151
Object.keys(pp).forEach((regex, index) => {
152+
if (pp[regex]['$ref']) {
153+
pp[regex] = refFinder(pp[regex]['$ref'], schema)
154+
}
151155
var type = pp[regex].type
152156
code += `
153157
if (/${regex}/.test(keys[i])) {
@@ -189,8 +193,8 @@ function addPatternProperties (pp, ap) {
189193
}
190194
`
191195
})
192-
if (ap) {
193-
code += additionalProperty(ap)
196+
if (schema.additionalProperties) {
197+
code += additionalProperty(schema)
194198
}
195199

196200
code += `
@@ -199,13 +203,18 @@ function addPatternProperties (pp, ap) {
199203
return code
200204
}
201205

202-
function additionalProperty (ap) {
206+
function additionalProperty (schema) {
207+
var ap = schema.additionalProperties
203208
let code = ''
204209
if (ap === true) {
205210
return `
206211
json += $asString(keys[i]) + ':' + fastSafeStringify(obj[keys[i]]) + ','
207212
`
208213
}
214+
if (ap['$ref']) {
215+
ap = refFinder(ap['$ref'], schema)
216+
}
217+
209218
let type = ap.type
210219
if (type === 'object') {
211220
code += buildObject(ap, '', 'buildObjectAP')
@@ -241,26 +250,41 @@ function additionalProperty (ap) {
241250
return code
242251
}
243252

244-
function addAdditionalProperties (ap) {
253+
function addAdditionalProperties (schema) {
245254
return `
246255
var keys = Object.keys(obj)
247256
for (var i = 0; i < keys.length; i++) {
248257
if (properties[keys[i]]) continue
249-
${additionalProperty(ap)}
258+
${additionalProperty(schema)}
250259
}
251260
`
252261
}
253262

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+
254278
function buildObject (schema, code, name) {
255279
code += `
256280
function ${name} (obj) {
257281
var json = '{'
258282
`
259283

260284
if (schema.patternProperties) {
261-
code += addPatternProperties(schema.patternProperties, schema.additionalProperties)
285+
code += addPatternProperties(schema)
262286
} else if (schema.additionalProperties && !schema.patternProperties) {
263-
code += addAdditionalProperties(schema.additionalProperties)
287+
code += addAdditionalProperties(schema)
264288
}
265289

266290
var laterCode = ''
@@ -273,6 +297,10 @@ function buildObject (schema, code, name) {
273297
json += '${$asString(key)}:'
274298
`
275299

300+
if (schema.properties[key]['$ref']) {
301+
schema.properties[key] = refFinder(schema.properties[key]['$ref'], schema)
302+
}
303+
276304
const result = nested(laterCode, name, '.' + key, schema.properties[key])
277305

278306
code += result.code

0 commit comments

Comments
 (0)