Skip to content

Commit d8d7ec0

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 940586a commit d8d7ec0

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
@@ -3434,15 +3434,15 @@ https://example.com/schemas/common#/$defs/count/minimum
34343434
{
34353435
"$schema": "https://json-schema.org/draft/2019-09/schema",
34363436
"$id": "https://example.com/tree",
3437-
"$dynamicAnchor": true,
3437+
"$dynamicAnchor": "node",
34383438
34393439
"type": "object",
34403440
"properties": {
34413441
"data": true,
34423442
"children": {
34433443
"type": "array",
34443444
"items": {
3445-
"$dynamicRef": "#"
3445+
"$dynamicRef": "#node"
34463446
}
34473447
}
34483448
}
@@ -3452,7 +3452,7 @@ https://example.com/schemas/common#/$defs/count/minimum
34523452
{
34533453
"$schema": "https://json-schema.org/draft/2019-09/schema",
34543454
"$id": "https://example.com/strict-tree",
3455-
"$dynamicAnchor": true,
3455+
"$dynamicAnchor": node,
34563456
34573457
"$ref": "tree",
34583458
"unevaluatedProperties": false
@@ -3465,32 +3465,67 @@ https://example.com/schemas/common#/$defs/count/minimum
34653465
]]>
34663466
</artwork>
34673467
</figure>
3468+
<t>
3469+
When we load these two schemas, we will notice the "$dynamicAnchor"
3470+
named "node" (note the lack of "#" as this is just the name)
3471+
present in each, resulting in the following full schema URIs:
3472+
<list style="symbols">
3473+
<t>"https://example.com/tree#node"</t>
3474+
<t>"https://example.com/strict-tree#node"</t>
3475+
</list>
3476+
In addition, JSON Schema implementations keep track of the fact
3477+
that these fragments were created with "$dynamicAnchor".
3478+
</t>
34683479
<t>
34693480
If we apply the "strict-tree" schema to the instance, we will follow
34703481
the "$ref" to the "tree" schema, examine its "children" subschema,
3471-
and find the "$dynamicAnchor" in its "items" subschema.
3482+
and find the "$dynamicRef": to "#node" (note the "#" for URI fragment syntax)
3483+
in its "items" subschema. That reference resolves to
3484+
"https://example.com/tree#node", which is a URI with a fragment
3485+
created by "$dynamicAnchor". Therefore we must examine the dynamic
3486+
scope before following the reference.
3487+
</t>
3488+
<t>
34723489
At this point, the dynamic path is
3473-
"#/$ref/properties/children/items/$dynamicRef".
3490+
"#/$ref/properties/children/items/$dynamicRef", with a dynamic scope
3491+
containing (from the outermost scope to the innermost):
3492+
<list style="numbers">
3493+
<t>"https://example.com/strict-tree#"</t>
3494+
<t>"https://example.com/tree#"</t>
3495+
<t>"https://example.com/tree#/properties/children"</t>
3496+
<t>"https://example.com/tree#/properties/children/items"</t>
3497+
</list>
34743498
</t>
34753499
<t>
3476-
The base URI at this point is "https://example.com/tree", so the
3477-
"$dynamicRef" initially resolves to "https://example.com/tree#".
3478-
Since "$dynamicAnchor" is true, we examine the dynamic path to
3479-
see if there is a different base URI to use. We find
3480-
"$dynamicAnchor" with a true value at the dynamic paths of
3481-
"#" and "#/$ref".
3500+
Since we are looking for a plain name fragment, which can be
3501+
defined anywhere within a schema resource, the JSON Pointer fragments
3502+
are irrelevant to this check. That means that we can remove those
3503+
fragments and eliminate consecutive duplicates, producing:
3504+
<list style="numbers">
3505+
<t>"https://example.com/strict-tree"</t>
3506+
<t>"https://example.com/tree"</t>
3507+
</list>
34823508
</t>
34833509
<t>
3484-
The outermost is "#", which is the root schema of the "strict-tree"
3485-
schema, so we use its base URI of "https://example.com/strict-tree",
3486-
which produces a final resolved URI of
3487-
"https://example.com/strict-tree#" for the "$dynamicRef".
3510+
In this case, the outermost resource also has a "node" fragment
3511+
defined by "$dynamicAnchor". Therefore instead of resolving the
3512+
"$dynamicRef" to "https://example.com/tree#node", we resolve it to
3513+
"https://example.com/strict-tree#node".
34883514
</t>
34893515
<t>
34903516
This way, the recursion in the "tree" schema recurses to the root
34913517
of "strict-tree", instead of only applying "strict-tree" to the
34923518
instance root, but applying "tree" to instance children.
34933519
</t>
3520+
<t>
3521+
This example shows both "$dynamicAnchor"s in the same place
3522+
in each schema, specifically the resource root schema.
3523+
Since plain-name fragments are independent of the JSON structure,
3524+
this would work just as well if one or both of the node schema objects
3525+
were moved under "$defs". It is the matching "$dynamicAnchor" values
3526+
which tell us how to resolve the dynamic reference, not any sort of
3527+
correlation in JSON structure.
3528+
</t>
34943529
</section>
34953530

34963531
<section title="Working with vocabularies">
@@ -3590,7 +3625,7 @@ https://example.com/schemas/common#/$defs/count/minimum
35903625
{
35913626
"$schema": "https://json-schema.org/draft/2019-09/schema",
35923627
"$id": "https://example.com/meta/general-use-example",
3593-
"$dynamicAnchor": true,
3628+
"$dynamicAnchor": "meta",
35943629
"$vocabulary": {
35953630
"https://json-schema.org/draft/2019-09/vocab/core": true,
35963631
"https://json-schema.org/draft/2019-09/vocab/applicator": true,
@@ -3625,7 +3660,7 @@ https://example.com/schemas/common#/$defs/count/minimum
36253660
{
36263661
"$schema": "https://json-schema.org/draft/2019-09/schema",
36273662
"$id": "https://example.com/meta/example-vocab",
3628-
"$dynamicAnchor": true,
3663+
"$dynamicAnchor": "meta",
36293664
"$vocabulary": {
36303665
"https://example.com/vocab/example-vocab": true,
36313666
},

0 commit comments

Comments
 (0)