Skip to content

Commit c6e85ac

Browse files
committed
Revise $id initial explanation
Signed-off-by: Juan Cruz Viotti <jv@jviotti.com>
1 parent 2bcf935 commit c6e85ac

1 file changed

Lines changed: 41 additions & 17 deletions

File tree

content/2020-12/core/id.markdown

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,58 @@ related:
2929
keyword: $vocabulary
3030
---
3131

32-
{{<learning-more>}}
33-
Generally, `schema` and `schema resource` might create confusion. Let's clarify the terminology first:
34-
35-
**Schema**: This refers to the entire JSON boolean or JSON object passed to an evaluator.
32+
The `$id` keyword explicitly turns a schema into a _schema resource_ (a schema
33+
that is associated with a URI). Relative URIs are resolved against the
34+
_current_ base URI, which is either the closest parent `$id` keyword
35+
(applicable in the case of compound schemas) or the base URI as determined by
36+
the context on which the schema is declared (i.e. serving a schema over HTTP
37+
_may_ implicitly award it such URL as the base).
3638

37-
**Schema Resource**: A schema may consist of one or more schema resources (`$id` boundaries). When you introduce nested schema objects with `$id` in your schema, you create new schema resources.
39+
Note that you cannot set this keyword to a URI that contains a fragment
40+
identifier. Instead, fragment identifiers must be set with the [`$anchor`]({{<
41+
ref "2020-12/core/anchor" >}}) keyword.
3842

39-
**Schema Object**: This is a single subschema in the schema tree, considering only its immediate keywords and not including nested subschemas.
43+
{{<learning-more>}}
4044

45+
This keyword directly applies (without modifications or extensions) the concept
46+
of URIs to schemas. **If you are having a hard time following the concepts
47+
discussed in this page, it is probably because you don't have a strong grasp of
48+
URIs yet** (a notably hard but universal pre-requisite!).
4149

42-
_Relationships_:
43-
* A _schema_ has one or more schema resources.
44-
* A _schema resource_ has one or more schema objects.
45-
* A _schema object_ has one or more keywords.
50+
To learn more about URIs, we strongly suggest studying the [IETF RFC
51+
3986](https://www.rfc-editor.org/info/rfc3986) URI standard. To avoid
52+
confusion, note that there is also a [WHATWG URL
53+
Standard](https://url.spec.whatwg.org) that targets URLs in the context of web
54+
browsers. However, JSON Schema only implements and expects the IETF original
55+
standard.
4656

47-
_**Note**: A schema resource does not include its children schema resources, as they are conceptually distinct entities (just like html iframes in a web page), despite being nested. However, all of them are part of the same schema. Refer to the last example for clarification._
4857
{{</learning-more>}}
4958

50-
The `$id` keyword declares the URI for a schema, usually set at the top level. However, any subschema has the flexibility to declare its own `$id` to distinguish itself with a distinct URI. Each subschema with an `$id` in a compound schema is called a _schema resource_.
59+
{{<common-pitfall>}}
60+
61+
In JSON Schema, schema identifiers are merely identifiers and no behaviour is
62+
imposed on them. In particular, JSON Schema does not guarantee that a schema
63+
with an HTTP URL identifier is actually resolvable at such URL. To avoid
64+
surprises, JSON Schema implementations must be careful with automatically
65+
sending remote network requests when encountering supposely resolvable schema
66+
identifiers.
67+
68+
{{</common-pitfall>}}
5169

52-
* The top-level schema resource is referred to as the root schema resource.
53-
* The identifier of the root schema resource, if set, must be an absolute URI.
54-
* The presence of an identifier sets a new base URI for such schema resource.
70+
{{<best-practice>}}
5571

56-
It's worth noting that if the `$id` identifier is a URL, it's common for the URL to respond with the schema when accessed through a web browser, but this behavior is not mandatory; the URL primarily serves as an identifier. Additionally, for non-locatable URIs, such as those not intended for direct accessibility over the declared protocol (e.g., HTTPS), it is advisable to consider using URNs.
72+
It is strongly recommended for every schema file to explicitly declare an
73+
_absolute_ URI using this keyword. By doing so, you completely avoid various
74+
complex URI resolution edge cases, mainly when the base URI is implicit and
75+
context-dependent.
5776

58-
_**Note:** Check out the [URI RFC](https://datatracker.ietf.org/doc/html/rfc3986) to gain a deeper understanding of how resolution works, providing valuable insights into the essential role of URIs in JSON Schema._
77+
If you are serving schemas over the network (i.e. over HTTP), a common practice
78+
is to set this keyword to the expected URL. However, if your schemas are not
79+
accessible over the network, prefer using a [URN (Uniform Resource
80+
Name)](https://en.wikipedia.org/wiki/Uniform_Resource_Name) or a non-locatable
81+
URI scheme such as a [Tag URI](https://www.taguri.org).
5982

83+
{{</best-practice>}}
6084

6185
## Examples
6286

0 commit comments

Comments
 (0)