{
"$id": "https://example.com/schemas/customer",
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"first_name": {
"type": "string"
},
"last_name": {
"type": "string"
},
"shipping_address": {
"$id": "https://example.com/schemas/address",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"street_address": {
"type": "string"
},
"city": {
"type": "string"
},
"state": {
"enum": [
"CA",
"NY",
"... etc ..."
]
}
},
"required": [
"street_address",
"city",
"state"
],
"definitions": {
"state": {
"enum": [
"CA",
"NY",
"... etc ..."
]
}
}
},
"billing_address": {
"$id": "https://example.com/schemas/address",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"street_address": {
"type": "string"
},
"city": {
"type": "string"
},
"state": {
"enum": [
"CA",
"NY",
"... etc ..."
]
}
},
"required": [
"street_address",
"city",
"state"
],
"definitions": {
"state": {
"enum": [
"CA",
"NY",
"... etc ..."
]
}
}
}
},
"required": [
"first_name",
"last_name",
"shipping_address",
"billing_address"
],
"$defs": {
"address": {
"$id": "https://example.com/schemas/address",
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"properties": {
"street_address": {
"type": "string"
},
"city": {
"type": "string"
},
"state": {
"enum": [
"CA",
"NY",
"... etc ..."
]
}
},
"required": [
"street_address",
"city",
"state"
],
"definitions": {
"state": {
"enum": [
"CA",
"NY",
"... etc ..."
]
}
}
}
}
}
Version used
15.3.5
The input json used
test.json
This isbundling example
from json-schema.org.
{ "$id": "https://example.com/schemas/customer", "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": { "first_name": { "type": "string" }, "last_name": { "type": "string" }, "shipping_address": { "$ref": "/schemas/address" }, "billing_address": { "$ref": "/schemas/address" } }, "required": ["first_name", "last_name", "shipping_address", "billing_address"], "$defs": { "address": { "$id": "https://example.com/schemas/address", "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "street_address": { "type": "string" }, "city": { "type": "string" }, "state": { "$ref": "#/definitions/state" } }, "required": ["street_address", "city", "state"], "definitions": { "state": { "enum": ["CA", "NY", "... etc ..."] } } } } }bundling
program
test.cjs
result
Result of bundling beautified by jq
{ "$id": "https://example.com/schemas/customer", "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": { "first_name": { "type": "string" }, "last_name": { "type": "string" }, "shipping_address": { "$ref": "#" }, "billing_address": { "$ref": "#" } }, "required": [ "first_name", "last_name", "shipping_address", "billing_address" ], "$defs": { "address": { "$id": "https://example.com/schemas/address", "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "street_address": { "type": "string" }, "city": { "type": "string" }, "state": { "$ref": "#/definitions/state" } }, "required": [ "street_address", "city", "state" ], "definitions": { "state": { "enum": [ "CA", "NY", "... etc ..." ] } } } } }problem
Notice the
{"$ref":"#"}for theshipping_addressandbilling_addressproperties.expected behaviour
Either don't change it, or present valid json pointer references which are applied to the top level document everywhere, which would be good for libraries like RJSF, but might be complicated? (This would probably mean altering the compound schemas to not contain
$idany more and changing all the references in them. Probably ok or should bundle guarantee the compound schemas are unchanged?)dereferencing
program
test.cjs
result
Result of bundling
{ "$id": "https://example.com/schemas/customer", "$schema": "https://json-schema.org/draft/2020-12/schema", "type": "object", "properties": { "first_name": { "type": "string" }, "last_name": { "type": "string" }, "shipping_address": { "$id": "https://example.com/schemas/address", "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "street_address": { "type": "string" }, "city": { "type": "string" }, "state": { "enum": [ "CA", "NY", "... etc ..." ] } }, "required": [ "street_address", "city", "state" ], "definitions": { "state": { "enum": [ "CA", "NY", "... etc ..." ] } } }, "billing_address": { "$id": "https://example.com/schemas/address", "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "street_address": { "type": "string" }, "city": { "type": "string" }, "state": { "enum": [ "CA", "NY", "... etc ..." ] } }, "required": [ "street_address", "city", "state" ], "definitions": { "state": { "enum": [ "CA", "NY", "... etc ..." ] } } } }, "required": [ "first_name", "last_name", "shipping_address", "billing_address" ], "$defs": { "address": { "$id": "https://example.com/schemas/address", "$schema": "http://json-schema.org/draft-07/schema#", "type": "object", "properties": { "street_address": { "type": "string" }, "city": { "type": "string" }, "state": { "enum": [ "CA", "NY", "... etc ..." ] } }, "required": [ "street_address", "city", "state" ], "definitions": { "state": { "enum": [ "CA", "NY", "... etc ..." ] } } } } }problems
None I can spot. So json-schema-ref-parser already knows how to resolve those local references in a compound document.