Skip to content

Commit f020278

Browse files
committed
fix: finder for JSON schema id as top level function
1 parent 5550e5e commit f020278

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

index.js

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -424,25 +424,25 @@ function addAdditionalProperties (schema, externalSchema, fullSchema) {
424424
`
425425
}
426426

427+
function idFinder (schema, searchedId) {
428+
let objSchema
429+
const explore = (schema, searchedId) => {
430+
Object.keys(schema || {}).forEach((key, i, a) => {
431+
if (key === '$id' && schema[key] === searchedId) {
432+
objSchema = schema
433+
} else if (objSchema === undefined && typeof schema[key] === 'object') {
434+
explore(schema[key], searchedId)
435+
}
436+
})
437+
}
438+
explore(schema, searchedId)
439+
return objSchema
440+
}
441+
427442
function refFinder (ref, schema, externalSchema) {
428443
// Split file from walk
429444
ref = ref.split('#')
430445

431-
const searchForId = (schema, searchedId) => {
432-
let objSchema
433-
const explore = (schema, searchedId) => {
434-
Object.keys(schema || {}).forEach((key, i, a) => {
435-
if (key === '$id' && schema[key] === searchedId) {
436-
objSchema = schema
437-
} else if (typeof schema[key] === 'object') {
438-
explore(schema[key], searchedId)
439-
}
440-
})
441-
}
442-
explore(schema, searchedId)
443-
return objSchema
444-
}
445-
446446
// If external file
447447
if (ref[0]) {
448448
schema = externalSchema[ref[0]]
@@ -451,12 +451,13 @@ function refFinder (ref, schema, externalSchema) {
451451
var code = 'return schema'
452452
// If it has a path
453453
if (ref[1]) {
454+
// ref[1] could contain a JSON pointer - ex: #/definitions/num
455+
// or plan name fragment id - ex: #customId
454456
var walk = ref[1].split('/')
455-
var wl = walk.length
456-
if (wl === 1) {
457-
return searchForId(schema, `#${ref[1]}`)
457+
if (walk.length === 1) {
458+
return idFinder(schema, `#${ref[1]}`)
458459
} else {
459-
for (var i = 1; i < wl; i++) {
460+
for (var i = 1; i < walk.length; i++) {
460461
code += `['${walk[i]}']`
461462
}
462463
}

0 commit comments

Comments
 (0)