$ref : URI Reference
$ref
URI ReferenceThis keyword is used to reference a statically identified schema.
Value | This keyword must be set to an absolute URI or a relative reference as defined by RFC 3986, where its fragment (if any) can consist of a JSON Pointer as defined by RFC 6901 |
---|---|
Kind | Applicator |
Applies To | Any |
Dialect | 2020-12 |
Changed In | Draft 6 2019-09 |
Introduced In | Draft 3 |
Vocabulary | Core |
Specification | https://json-schema.org/draft/2020-12/json-schema-core.html#section-8.2.3.1 |
Metaschema | https://json-schema.org/draft/2020-12/meta/core |
Official Tests | |
Default | None |
Annotation | None |
Affected By | |
Affects | None |
Also See |
|
The $ref
keyword is used to statically reference a schema. This is useful for avoiding code duplication and promoting modularity when describing complex data structures.
Common Pitfall
Because of how URI resolution works, a reference to an absolute URI does not necessarily mean the reference points to a remote resource. Conversely, a reference to a relative URI does not necessarily mean the reference points to the current schema resource.
When encountering a reference, a JSON Schema implementation will first resolve it into an absolute URI given the base URI of the schema. If the resulting destination is present in the schema, it will be a local reference. Otherwise, a remote reference.
Digging Deeper
URIs play a central role in JSON Schema. Going through the URI RFC 3986 specification is a must for gaining a deeper understanding of references, identifiers, and anchors. More specifically, we recommend carefully studying URI resolution, URLs vs URNs, and the difference between a URI and a URI Reference.
Additionally, a JSON Schema reference URI may contain a JSON Pointer. For this reason, we recommend reading the JSON Pointer RFC 6901 specification, primarily its proposed URI fragment identifier representation.
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/schemas/product.json",
"type": "object",
"properties": {
"productId": { "type": "integer" },
"name": { "$ref": "string" }
},
"required": [ "productId", "name" ],
"$defs": {
"string": {
"$id": "string",
"type": "string"
}
}
}
{
"productId": 123,
"name": "Widget"
}
{
"productId": 217,
"name": 999
}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/schemas/order.json",
"type": "object",
"properties": {
"items": {
"type": "array",
"items": { "$ref": "schemas/product.json" }
}
}
}
{
"items": [
{ "productId": 123, "name": "Widget" },
{ "productId": 456, "name": "Gadget" }
]
}
// Assuming http://example.com/schemas/product.json defines the product schema
{
"items": [
{ "name": "Widget" },
{ "productId": 456 }
]
}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/schemas/product.json",
"$ref": "https://example.com/schemas/product.json#/$defs/string",
"$defs": {
"string": { "type": "string" }
}
}
"John Doe"
true
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com/schemas/product.json",
"$ref": "https://example.com/schemas/product.json#string",
"$defs": {
"string": { "$anchor": "string", "type": "boolean" }
}
}
false
99
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com",
"type": "object",
"properties": {
"name": { "$ref": "#/$defs/string" }
},
"required": [ "name" ],
"$defs": {
"string": { "type": "string" }
}
}
{
"name": "John Doe"
}
{
"name": true
}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://example.com",
"type": "object",
"properties": {
"counter": { "$ref": "#counter" }
},
"required": [ "counter" ],
"$defs": {
"string": { "$anchor": "counter", "type": "number" }
}
}
{
"counter": 51
}
{
"counter": "59"
}
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "urn:example:vehicle",
"$ref": "urn:example:phone",
"$defs": {
"phone": {
"$id": "urn:example:phone",
"type": "number"
}
}
}
7843559621
{
"phone": 9866548907
}