You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/2020-12/core/ref.markdown
+96-24Lines changed: 96 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -32,33 +32,105 @@ related:
32
32
keyword: $defs
33
33
---
34
34
35
-
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.
36
-
37
-
{{<common-pitfall>}} Because of how URI resolution works, a reference to an
38
-
absolute URI does not necessarily mean the reference points to a remote
39
-
resource. Conversely, a reference to a relative URI does not necessarily mean
40
-
the reference points to the current schema resource.
41
-
42
-
When encountering a reference, a JSON Schema implementation will first resolve
43
-
it into an absolute URI given the base URI of the schema. If the resulting
44
-
destination is present in the schema, it will be a local reference. Otherwise,
45
-
a remote reference.
46
-
{{</common-pitfall>}}
47
-
48
-
{{<learning-more>}} URIs play a central role in JSON Schema. Going through the
49
-
URI [RFC 3986](https://datatracker.ietf.org/doc/html/rfc3986) specification is a
50
-
must for gaining a deeper understanding of references, identifiers, and
51
-
anchors. More specifically, we recommend carefully studying [URI
52
-
resolution](https://datatracker.ietf.org/doc/html/rfc3986#section-5), URLs vs
53
-
URNs, and the difference between a URI and a URI Reference.
54
-
55
-
Additionally, a JSON Schema reference URI may contain a JSON Pointer. For this
56
-
reason, we recommend reading the JSON Pointer
57
-
[RFC 6901](https://www.rfc-editor.org/rfc/rfc6901) specification, primarily its
58
-
proposed [URI fragment identifier
35
+
The `$ref` keyword enables a schema to reference another schema by its URI,
36
+
effectively importing its keywords into the current evaluation process. This
37
+
keyword is the cornerstone of schema composition, allowing complex schemas to
38
+
be created out of simpler ones. A reference may set its URI fragment to a [JSON
39
+
Pointer](https://www.rfc-editor.org/rfc/rfc6901) that determines the
40
+
destination of the reference after first resolving the rest of the URI.
41
+
42
+
{{<common-pitfall>}}
43
+
44
+
**Avoid referencing other schema files using their file paths**. While some
45
+
implementations support this by automatically constructing schema URIs that
46
+
make use of the `file://` scheme, this is not enforced behaviour. The only
47
+
standard and guaranteed mechanism of declaring a schema URI for identification
48
+
and referencing purposes is through the [`$id`]({{< ref "2020-12/core/id" >}})
49
+
keyword.
50
+
51
+
{{</common-pitfall>}}
52
+
53
+
{{<common-pitfall>}}
54
+
55
+
The target of a reference must be a schema. Referencing a JSON value that is
56
+
not unambiguously recognised as a schema leads to undefined behaviour. This
57
+
not only includes referencing arbitrary JSON files (the obvious case), but also
58
+
referencing parts of a schema that a JSON Schema evaluator does not consider to
59
+
be a subschema. For example, referencing the contents of the [`examples`]({{<
60
+
ref "2020-12/meta-data/examples" >}}) keyword.
61
+
62
+
{{</common-pitfall>}}
63
+
64
+
References are either _internal_ (pointing at schemas within the same schema
65
+
definition) or _external_ (pointing at schema resources outside the given
66
+
schema definition). If the reference is a relative URI, it is resolved against
67
+
the _current_ base URI, which is either the closest parent URI as set by the
68
+
[`$id`]({{< ref "2020-12/core/id" >}}) keyword, or the base URI as determined
69
+
by the context on which the schema is declared. Schema wrappers like OpenAPI
70
+
are notable examples of the latter. A relative reference from a schema embedded
71
+
in an OpenAPI specification is resolved from the root of the API specification,
72
+
and not from the root of the schema.
73
+
74
+
{{<best-practice>}}
75
+
76
+
It is highly recommended to make use of _external_ references to break down
77
+
complex monolithic schemas into smaller schema files. If you need a monolithic
78
+
schema, you can automatically inline external references using the [`jsonschema
0 commit comments