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/anchor.markdown
+56-68Lines changed: 56 additions & 68 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -22,91 +22,79 @@ related:
22
22
keyword: $dynamicAnchor
23
23
---
24
24
25
-
The `$anchor` keyword is used to assign a unique identifier to a subschema within its schema resource. This identifier can then be referenced elsewhere using the `$ref` keyword.
25
+
The `$anchor` keyword associates a subschema with the given URI fragment
26
+
identifier, which can be referenced using the [`$ref`]({{< ref
27
+
"2020-12/core/ref" >}}) keyword. The fragment identifier is resolved against
28
+
the URI of the schema resource. Therefore, using this keyword to declare the
29
+
same anchor more than once within the same schema resource results in an
30
+
invalid schema.
26
31
27
-
* The `$anchor` keyword allows for the creation of plain reusable name fragments that aren't tied to specific structural locations, offering a flexible alternative to using JSON Pointer fragments, which require knowledge of the schema's structure.
28
-
* An anchor is resolved against the base URI of its schema resource.
32
+
{{<learning-more>}}
29
33
30
-
## Examples
34
+
JSON Schema anchors were inspired by how web browsers [automatically
{{<instance-pass `An instance with integer is valid`>}}
106
-
99
94
+
{{<instance-pass `An object value with non-empty first and last names is valid`>}}
95
+
{ "firstName": "John", "lastName": "Doe" }
107
96
{{</instance-pass>}}
108
97
109
-
{{<instance-fail `An instance with boolean is invalid`>}}
110
-
true
98
+
{{<instance-fail `An object value with empty first and last names is invalid`>}}
99
+
{ "firstName": "", "lastName": "" }
111
100
{{</instance-fail>}}
112
-
- Here the URI Reference of `foo` subschema is resolved to `https://example.com/nested` and the named anchor is used in the URI fragment to reference this subschema.
<smallclass="d-block mt-2 pt-1 border-top text-muted"><b>Hint:</b> Use the <ahref="https://github.com/sourcemeta/jsonschema/blob/main/docs/metaschema.markdown"><code>jsonschema metaschema</code></a>command to catch keywords set to invalid values</small>
44
+
<smallclass="d-block mt-2 pt-1 border-top text-muted"><b>Hint:</b> Use the <ahref="https://github.com/sourcemeta/jsonschema/blob/main/docs/metaschema.markdown"><code>jsonschema metaschema</code></a>and <ahref="https://github.com/sourcemeta/jsonschema/blob/main/docs/lint.markdown"><code>jsonschema lint</code></a> commands to catch keywords set to invalid values</small>
45
45
{{ else }}
46
46
{{ errorf "Keyword is missing value description" }}
0 commit comments