Skip to content

Commit 4af917c

Browse files
scmorsemcollina
authored andcommitted
Support nested -erences (#188)
1 parent 03082eb commit 4af917c

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,38 @@ const externalSchema = {
402402
strings: require('./string-def.json')
403403
}
404404

405+
const stringify = fastJson(schema, { schema: externalSchema })
406+
```
407+
External definitions can also reference each other.
408+
Example:
409+
```javascript
410+
const schema = {
411+
title: 'Example Schema',
412+
type: 'object',
413+
properties: {
414+
foo: {
415+
$ref: 'strings#/definitions/foo'
416+
}
417+
}
418+
}
419+
420+
const externalSchema = {
421+
strings: {
422+
definitions: {
423+
foo: {
424+
$ref: 'things#/definitions/foo'
425+
}
426+
}
427+
},
428+
things: {
429+
definitions: {
430+
foo: {
431+
type: 'string'
432+
}
433+
}
434+
}
435+
}
436+
405437
const stringify = fastJson(schema, { schema: externalSchema })
406438
```
407439

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ function addPatternProperties (schema, externalSchema, fullSchema) {
310310
`
311311
Object.keys(pp).forEach((regex, index) => {
312312
if (pp[regex].$ref) {
313-
pp[regex] = refFinder(pp[regex].$ref, fullSchema, externalSchema, fullSchema)
313+
pp[regex] = refFinder(pp[regex].$ref, fullSchema, externalSchema)
314314
}
315315
var type = pp[regex].type
316316
code += `
@@ -513,7 +513,8 @@ function refFinder (ref, schema, externalSchema) {
513513
}
514514
}
515515
}
516-
return (new Function('schema', code))(schema)
516+
const result = (new Function('schema', code))(schema)
517+
return result.$ref ? refFinder(result.$ref, schema, externalSchema) : result
517518
}
518519

519520
function sanitizeKey (key) {

test/ref.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -804,3 +804,45 @@ test('ref external to relative definition', (t) => {
804804

805805
t.equal(output, '{"fooParent":{"foo":"bar"}}')
806806
})
807+
808+
test('ref to nested ref definition', (t) => {
809+
t.plan(2)
810+
811+
const externalSchema = {
812+
'a:b:c1': {
813+
$id: 'a:b:c1',
814+
type: 'object',
815+
definitions: {
816+
foo: { $ref: 'a:b:c2#/definitions/foo' }
817+
}
818+
},
819+
'a:b:c2': {
820+
$id: 'a:b:c2',
821+
type: 'object',
822+
definitions: {
823+
foo: { type: 'string' }
824+
}
825+
}
826+
}
827+
828+
const schema = {
829+
type: 'object',
830+
required: ['foo'],
831+
properties: {
832+
foo: { $ref: 'a:b:c1#/definitions/foo' }
833+
}
834+
}
835+
836+
const object = { foo: 'foo' }
837+
const stringify = build(schema, { schema: externalSchema })
838+
const output = stringify(object)
839+
840+
try {
841+
JSON.parse(output)
842+
t.pass()
843+
} catch (e) {
844+
t.fail()
845+
}
846+
847+
t.equal(output, '{"foo":"foo"}')
848+
})

0 commit comments

Comments
 (0)