@@ -18,3 +18,170 @@ related:
1818 - vocabulary : core
1919 keyword : $schema
2020---
21+
22+ The [ ` $id ` ] ({{< ref "draft7/core/id" >}}) keyword explicitly turns a schema
23+ into a _ schema resource_ (a schema that is associated with a URI). Relative
24+ URIs are resolved against the _ current_ base URI, which is either the closest
25+ parent [ ` $id ` ] ({{< ref "draft7/core/id" >}}) keyword (applicable in the case
26+ of compound schemas), or the base URI as determined by the context on which the
27+ schema is declared (i.e. serving a schema over HTTP _ may_ implicitly award it
28+ such URL as the base).
29+
30+ {{<learning-more >}}
31+
32+ This keyword directly applies (without modifications or extensions) the concept
33+ of URIs to schemas. ** If you are having a hard time following the concepts
34+ discussed in this page, it is probably because you don't have a strong grasp of
35+ URIs yet** (a notably hard but universal pre-requisite!).
36+
37+ To learn more about URIs, we strongly suggest studying the [ IETF RFC
38+ 3986] ( https://www.rfc-editor.org/info/rfc3986 ) URI standard. To avoid
39+ confusion, note that there is also a [ WHATWG URL
40+ Standard] ( https://url.spec.whatwg.org ) that targets URLs in the context of web
41+ browsers. However, JSON Schema only implements and expects the IETF original
42+ standard.
43+
44+ {{</learning-more >}}
45+
46+ {{<common-pitfall >}}
47+
48+ In JSON Schema, schema identifiers are merely identifiers and no behaviour is
49+ imposed on them. In particular, JSON Schema does not guarantee that a schema
50+ with an HTTP URL identifier is actually resolvable at such URL. To avoid
51+ surprises, JSON Schema implementations must be careful with automatically
52+ sending remote network requests when encountering supposely resolvable schema
53+ identifiers.
54+
55+ {{</common-pitfall >}}
56+
57+ {{<best-practice >}}
58+
59+ It is strongly recommended for every schema file to explicitly declare an
60+ _ absolute_ URI using this keyword. By doing so, you completely avoid various
61+ complex URI resolution edge cases, mainly when the base URI is implicit and
62+ context-dependent.
63+
64+ If you are serving schemas over the network (i.e. over HTTP), it is common to
65+ set this keyword to the target URL. However, if your schemas are not accessible
66+ over the network, prefer using a
67+ [ URN] ( https://en.wikipedia.org/wiki/Uniform_Resource_Name ) (with a valid
68+ namespace registered by
69+ [ IANA] ( https://www.iana.org/assignments/urn-namespaces/urn-namespaces.xhtml ) )
70+ or a non-locatable URI scheme such as a [ Tag URI] ( https://www.taguri.org ) .
71+
72+ {{</best-practice >}}
73+
74+ To debug the role of the [ ` $id ` ] ({{< ref "draft7/core/id" >}}) keyword on
75+ a schema (particularly schemas with embedded resources), try the [ `jsonschema
76+ inspect`] ( https://github.com/sourcemeta/jsonschema/blob/main/docs/inspect.markdown )
77+ command. This command prints detailed information about each schema resource,
78+ subschema, location, and reference present in the schema. For example:
79+
80+ ``` sh
81+ $ jsonschema inspect schema.json
82+ (RESOURCE) URI: https://example.com/schema
83+ Type : Static
84+ Root : https://example.com/schema
85+ Pointer :
86+ Base : https://example.com/schema
87+ Relative Pointer :
88+ Dialect : http://json-schema.org/draft-07/schema#
89+ Base Dialect : http://json-schema.org/draft-07/schema#
90+ Parent : < NONE>
91+ Instance Location :
92+
93+ ...
94+
95+ (SUBSCHEMA) URI: https://example.com/schema#/properties/foo
96+ Type : Static
97+ Root : https://example.com/schema
98+ Pointer : /properties/foo
99+ Base : https://example.com/schema
100+ Relative Pointer : /properties/foo
101+ Dialect : http://json-schema.org/draft-07/schema#
102+ Base Dialect : http://json-schema.org/draft-07/schema#
103+ Parent :
104+ Instance Location : /foo
105+
106+ ...
107+
108+ (REFERENCE) ORIGIN: /$schema
109+ Type : Static
110+ Destination : http://json-schema.org/draft-07/schema
111+ - (w/o fragment) : http://json-schema.org/draft-07/schema
112+ - (fragment) : < NONE>
113+ ```
114+
115+ ## Examples
116+
117+ {{<schema ` A schema that declares a potentially resolvable HTTP absolute URL identifier ` >}}
118+ {
119+ "$schema": "http://json-schema.org/draft-07/schema# ",
120+ "$id": "https://example.com/schemas/even-number.json ",
121+ "type": "number",
122+ "multipleOf": 2
123+ }
124+ {{</schema >}}
125+
126+ {{<schema ` A schema that declares a non-resolvable Tag URI identifier ` >}}
127+ {
128+ "$schema": "http://json-schema.org/draft-07/schema# ",
129+ "$id": "tag: example .com,2025: even-number ",
130+ "type": "number",
131+ "multipleOf": 2
132+ }
133+ {{</schema >}}
134+
135+ {{<schema ` A schema that declares a non-resolvable URN example identifier ` >}}
136+ {
137+ "$schema": "http://json-schema.org/draft-07/schema# ",
138+ "$id": "urn:example: even-number ",
139+ "type": "number",
140+ "multipleOf": 2
141+ }
142+ {{</schema >}}
143+
144+ {{<schema ` A schema that uses fragment identifiers to create reusable anchors ` >}}
145+ {
146+ "$schema": "http://json-schema.org/draft-07/schema# ",
147+ "$id": "https://example.com/schemas/user.json ",
148+ "type": "object",
149+ "properties": {
150+ "name": { "$ref": "#nonEmptyString" },
151+ "email": { "$ref": "#nonEmptyString" }
152+ },
153+ "definitions": {
154+ "nonEmptyString": {
155+ "$id": "#nonEmptyString",
156+ "type": "string",
157+ "minLength": 1
158+ }
159+ }
160+ }
161+ {{</schema >}}
162+
163+ {{<schema ` A compound schema that declares relative and absolute nested URIs ` >}}
164+ {
165+ "$schema": "http://json-schema.org/draft-07/schema# ",
166+ "$comment": "This is the root schema resource",
167+ "$id": "https://example.com/schemas/root.json ",
168+ "properties": {
169+ "foo": {
170+ "$comment": "The resolved URI of this nested schema resource is https://example.com/schemas/foo.json ",
171+ "$id": "foo.json"
172+ },
173+ "bar": {
174+ "$comment": "The resolved URI of this nested schema resource is https://example.com/schemas/bar.json ",
175+ "$id": "/schemas/bar.json"
176+ },
177+ "baz": {
178+ "$comment": "The resolved URI of this nested schema resource is https://absolute.example/baz.json ",
179+ "$id": "https://absolute.example/baz.json ",
180+ "items": {
181+ "$comment": "The resolved URI of this nested schema resource is https://absolute.example/deep ",
182+ "$id": "deep"
183+ }
184+ }
185+ }
186+ }
187+ {{</schema >}}
0 commit comments