Skip to content

Commit 400391a

Browse files
committed
Update recursive schema example for $dynamicAnchor
This updates for the change of $recursiveRef (with a boolean) to $dynamicAnchor (with a plain name fragment), and provides a more detailed example of how the dynamic scope is used.
1 parent f9b3e86 commit 400391a

File tree

1 file changed

+52
-17
lines changed

1 file changed

+52
-17
lines changed

jsonschema-core.xml

Lines changed: 52 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3444,15 +3444,15 @@ https://example.com/schemas/common#/$defs/count/minimum
34443444
{
34453445
"$schema": "https://json-schema.org/draft/2019-09/schema",
34463446
"$id": "https://example.com/tree",
3447-
"$dynamicAnchor": true,
3447+
"$dynamicAnchor": "node",
34483448
34493449
"type": "object",
34503450
"properties": {
34513451
"data": true,
34523452
"children": {
34533453
"type": "array",
34543454
"items": {
3455-
"$dynamicRef": "#"
3455+
"$dynamicRef": "#node"
34563456
}
34573457
}
34583458
}
@@ -3462,7 +3462,7 @@ https://example.com/schemas/common#/$defs/count/minimum
34623462
{
34633463
"$schema": "https://json-schema.org/draft/2019-09/schema",
34643464
"$id": "https://example.com/strict-tree",
3465-
"$dynamicAnchor": true,
3465+
"$dynamicAnchor": node,
34663466
34673467
"$ref": "tree",
34683468
"unevaluatedProperties": false
@@ -3475,32 +3475,67 @@ https://example.com/schemas/common#/$defs/count/minimum
34753475
]]>
34763476
</artwork>
34773477
</figure>
3478+
<t>
3479+
When we load these two schemas, we will notice the "$dynamicAnchor"
3480+
named "node" (note the lack of "#" as this is just the name)
3481+
present in each, resulting in the following full schema URIs:
3482+
<list style="symbols">
3483+
<t>"https://example.com/tree#node"</t>
3484+
<t>"https://example.com/strict-tree#node"</t>
3485+
</list>
3486+
In addition, JSON Schema implementations keep track of the fact
3487+
that these fragments were created with "$dynamicAnchor".
3488+
</t>
34783489
<t>
34793490
If we apply the "strict-tree" schema to the instance, we will follow
34803491
the "$ref" to the "tree" schema, examine its "children" subschema,
3481-
and find the "$dynamicAnchor" in its "items" subschema.
3492+
and find the "$dynamicRef": to "#node" (note the "#" for URI fragment syntax)
3493+
in its "items" subschema. That reference resolves to
3494+
"https://example.com/tree#node", which is a URI with a fragment
3495+
created by "$dynamicAnchor". Therefore we must examine the dynamic
3496+
scope before following the reference.
3497+
</t>
3498+
<t>
34823499
At this point, the dynamic path is
3483-
"#/$ref/properties/children/items/$dynamicRef".
3500+
"#/$ref/properties/children/items/$dynamicRef", with a dynamic scope
3501+
containing (from the outermost scope to the innermost):
3502+
<list style="numbers">
3503+
<t>"https://example.com/strict-tree#"</t>
3504+
<t>"https://example.com/tree#"</t>
3505+
<t>"https://example.com/tree#/properties/children"</t>
3506+
<t>"https://example.com/tree#/properties/children/items"</t>
3507+
</list>
34843508
</t>
34853509
<t>
3486-
The base URI at this point is "https://example.com/tree", so the
3487-
"$dynamicRef" initially resolves to "https://example.com/tree#".
3488-
Since "$dynamicAnchor" is true, we examine the dynamic path to
3489-
see if there is a different base URI to use. We find
3490-
"$dynamicAnchor" with a true value at the dynamic paths of
3491-
"#" and "#/$ref".
3510+
Since we are looking for a plain name fragment, which can be
3511+
defined anywhere within a schema resource, the JSON Pointer fragments
3512+
are irrelevant to this check. That means that we can remove those
3513+
fragments and eliminate consecutive duplicates, producing:
3514+
<list style="numbers">
3515+
<t>"https://example.com/strict-tree"</t>
3516+
<t>"https://example.com/tree"</t>
3517+
</list>
34923518
</t>
34933519
<t>
3494-
The outermost is "#", which is the root schema of the "strict-tree"
3495-
schema, so we use its base URI of "https://example.com/strict-tree",
3496-
which produces a final resolved URI of
3497-
"https://example.com/strict-tree#" for the "$dynamicRef".
3520+
In this case, the outermost resource also has a "node" fragment
3521+
defined by "$dynamicAnchor". Therefore instead of resolving the
3522+
"$dynamicRef" to "https://example.com/tree#node", we resolve it to
3523+
"https://example.com/strict-tree#node".
34983524
</t>
34993525
<t>
35003526
This way, the recursion in the "tree" schema recurses to the root
35013527
of "strict-tree", instead of only applying "strict-tree" to the
35023528
instance root, but applying "tree" to instance children.
35033529
</t>
3530+
<t>
3531+
This example shows both "$dynamicAnchor"s in the same place
3532+
in each schema, specifically the resource root schema.
3533+
Since plain-name fragments are independent of the JSON structure,
3534+
this would work just as well if one or both of the node schema objects
3535+
were moved under "$defs". It is the matching "$dynamicAnchor" values
3536+
which tell us how to resolve the dynamic reference, not any sort of
3537+
correlation in JSON structure.
3538+
</t>
35043539
</section>
35053540

35063541
<section title="Working with vocabularies">
@@ -3600,7 +3635,7 @@ https://example.com/schemas/common#/$defs/count/minimum
36003635
{
36013636
"$schema": "https://json-schema.org/draft/2019-09/schema",
36023637
"$id": "https://example.com/meta/general-use-example",
3603-
"$dynamicAnchor": true,
3638+
"$dynamicAnchor": "meta",
36043639
"$vocabulary": {
36053640
"https://json-schema.org/draft/2019-09/vocab/core": true,
36063641
"https://json-schema.org/draft/2019-09/vocab/applicator": true,
@@ -3635,7 +3670,7 @@ https://example.com/schemas/common#/$defs/count/minimum
36353670
{
36363671
"$schema": "https://json-schema.org/draft/2019-09/schema",
36373672
"$id": "https://example.com/meta/example-vocab",
3638-
"$dynamicAnchor": true,
3673+
"$dynamicAnchor": "meta",
36393674
"$vocabulary": {
36403675
"https://example.com/vocab/example-vocab": true,
36413676
},

0 commit comments

Comments
 (0)