When passed into AJV this then results in.
MissingRefError: can't resolve reference #/oneOf/0/allOf/0 from id sub2.schema.json
{
'$id': 'root.schema.json',
'$schema': 'https://json-schema.org/draft/2019-09/schema',
type: 'object',
oneOf: [
{
'$id': 'sub1.schema.json',
'$schema': 'https://json-schema.org/draft/2019-09/schema',
type: 'object',
allOf: [
{
'$id': 'base.schema.json',
'$schema': 'https://json-schema.org/draft/2019-09/schema',
type: 'object',
properties: { foo: { type: 'string' } }
},
{ properties: { name: { type: 'string', const: 'sub1' } } }
]
},
{
'$id': 'sub2.schema.json',
'$schema': 'https://json-schema.org/draft/2019-09/schema',
type: 'object',
allOf: [
{ '$ref': '#/oneOf/0/allOf/0' },
{ properties: { name: { type: 'string', const: 'sub2' } } }
]
},
{
'$id': 'sub3.schema.json',
'$schema': 'https://json-schema.org/draft/2019-09/schema',
type: 'object',
allOf: [
{ '$ref': '#/oneOf/0/allOf/0' },
{ properties: { name: { type: 'string', const: 'sub3' } } }
]
}
]
}
Sample
https://github.com/phawxby/json-ref-example/blob/main/src/example.ts
What I think is happening here is that the repetition of the reference to
root.schema.jsonis being resolved down to$refsin later references. However because those sub schemas (sub1.schema.json) include as$idthat changes the base uri for resolution. The result is#/oneOf/0/allOf/0is being resolved fromsub2.schema.jsonand not fromroot.schema.json.When passed into AJV this then results in.
Removing the
$idfromsub*.schema.jsonwhich prevents the base uri from being changed corrects the issue.Compiled output