Skip to content

Commit fffbb9a

Browse files
committed
Start polishing $dynamicAnchor
Signed-off-by: Juan Cruz Viotti <[email protected]>
1 parent cc7b2eb commit fffbb9a

File tree

1 file changed

+56
-2
lines changed

1 file changed

+56
-2
lines changed

content/2020-12/core/dynamicAnchor.markdown

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,61 @@ related:
2222
keyword: $anchor
2323
---
2424

25-
The `$dynamicAnchor` keyword allows the creation of plain name fragments that are not tied to a particular structural location within a schema. This is particularly useful for making subschemas reusable and relocatable without needing to update JSON Pointer references. Unlike `$anchor`, `$dynamicAnchor` indicates an extension point when used with the `$dynamicRef` keyword, facilitating the extension of recursive schemas without imposing specific semantics on that extension. Without `$dynamicRef`, `$dynamicAnchor` behaves the same as `$anchor`.
25+
26+
The [`$dynamicAnchor`]({{< ref "2020-12/core/dynamicanchor" >}}) keyword is an
27+
extension of the [`$anchor`]({{< ref "2020-12/core/anchor" >}}) keyword that
28+
not only associates a subschema with the given URI fragment identifier, but
29+
also records this association in the [dynamic
30+
scope](https://json-schema.org/blog/posts/dynamicref-and-generics). When
31+
resolving a dynamic anchor using the [`$dynamicRef`]({{< ref
32+
"2020-12/core/dynamicref" >}}) keyword, the base URI of the origin is not
33+
considered. Instead, evaluation jumps to the first encountered occurence of the
34+
given dynamic anchor in the [stack of schema
35+
resources](https://json-schema.org/blog/posts/understanding-lexical-dynamic-scopes#the-dynamic-scope-as-a-stack)
36+
traversed so far.
37+
38+
In other words, **think of a schema declaring a dynamic anchor as offering an
39+
overridable extension hook**. For example, a schema that declares a dynamic
40+
anchor `foo` says: _"I allow any parent schema to re-define this `foo`
41+
subschema as they desire"_, while keeping the rest of the schema intact.
42+
43+
{{<best-practice>}}
44+
45+
Use dynamic references when you need to define generic and extensible schemas.
46+
In fact, the dynamic referencing mechanism is a direct translation of generic
47+
programming facilities like [C++ template
48+
parameters](https://en.cppreference.com/w/cpp/language/template_parameters) and
49+
[Java generics](https://en.wikipedia.org/wiki/Generics_in_Java) to JSON Schema.
50+
See the blog post [Using Dynamic References to Support Generic
51+
Types](https://json-schema.org/blog/posts/dynamicref-and-generics#using-dynamic-references-to-support-generic-types)
52+
by Greg Dennis (co-author of the JSON Schema specification) for a hands-on
53+
discussion of this concept.
54+
55+
{{</best-practice>}}
56+
57+
{{<learning-more>}}
58+
59+
The official JSON Schema meta-schemas define, by convention, a dynamic anchor
60+
called `meta`. This is a fundamental building block for schema extensibility.
61+
The meta-schema of every vocabulary (official or third-party) hooks into this
62+
dynamic anchor to extend the recursive definition of what constitutes a valid
63+
schema for the given dialect.
64+
65+
More specifically, by relying on the `meta` dynamic anchor, a vocabulary
66+
meta-schema can validate the presence of a new keyword and have those
67+
constraints be automatically discovered and applied by any applicator of any
68+
other vocabulary (even future ones).
69+
70+
{{</learning-more>}}
71+
72+
{{<common-pitfall>}}
73+
74+
The schema resource from where the dynamic anchor lookup originates _must_
75+
declare such dynamic anchor. Otherwise, the schema would be unusable until
76+
another schema provides a definition for dynamic such anchor. This rule is
77+
informally referred to as the _bookending requirement_.
78+
79+
{{</common-pitfall>}}
2680

2781
## Examples
2882

@@ -61,4 +115,4 @@ The `$dynamicAnchor` keyword allows the creation of plain name fragments that ar
61115
[ "foo", 42 ]
62116
{{</instance-fail>}}
63117

64-
_**Note:** A `$dynamicRef` referencing a `$dynamicAnchor` within the same schema resource functions similarly to a standard `$ref` referencing an `$anchor`. Similarly, a `$dynamicRef` referencing an `$anchor` within the same schema resource behaves like a typical `$ref` referencing an `$anchor`. Likewise, a `$ref` targeting a `$dynamicAnchor` within the same schema resource behaves like a regular `$ref` targeting an `$anchor`._
118+
_**Note:** A `$dynamicRef` referencing a `$dynamicAnchor` within the same schema resource functions similarly to a standard `$ref` referencing an `$anchor`. Similarly, a `$dynamicRef` referencing an `$anchor` within the same schema resource behaves like a typical `$ref` referencing an `$anchor`. Likewise, a `$ref` targeting a `$dynamicAnchor` within the same schema resource behaves like a regular `$ref` targeting an `$anchor`._

0 commit comments

Comments
 (0)