Skip to content

Commit b5755ff

Browse files
committed
Add new recursive schema example.
1 parent 48fb19b commit b5755ff

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

jsonschema-core.xml

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,6 +1595,15 @@
15951595
a reference to its own root, identified by the empty fragment
15961596
URI reference ("#").
15971597
</t>
1598+
<t>
1599+
For an example using these keyword, see appendix
1600+
<xref target="recursive-example" format="counter" />.
1601+
<cref>
1602+
The difference between the hyper-schema meta-schema in previous
1603+
drafts and an this draft dramatically demonstrates the utility
1604+
of these keywords.
1605+
</cref>
1606+
</t>
15981607
<section title='Dynamically recursive references with "$recursiveRef"'>
15991608
<t>
16001609
The value of the "$recursiveRef" property MUST be a string which is
@@ -3307,6 +3316,81 @@ User-Agent: product-name/5.4.1 so-cool-json-schema/1.0.2 curl/7.43.0
33073316
</t>
33083317
</section>
33093318

3319+
<section title="Example of recursive schema extension" anchor="recursive-example">
3320+
<figure>
3321+
<preamble>
3322+
Consider the following two schemas describing a simple
3323+
recursive tree structure, where each node in the tree
3324+
can have a "data" field of any type. The first schema
3325+
allows and ignores other instance properties. The second is
3326+
more strict and only allows the "data" and "children" properties.
3327+
An example instance with "data" misspelled as "daat" is also shown.
3328+
</preamble>
3329+
<artwork>
3330+
<![CDATA[
3331+
// tree schema, extensible
3332+
{
3333+
"$schema": "https://json-schema.org/draft/2019-08/schema",
3334+
"$id": "https://example.com/tree",
3335+
"$recursiveAnchor": true,
3336+
3337+
"type": "object",
3338+
"properties": {
3339+
"data": true,
3340+
"children": {
3341+
"type": "array",
3342+
"items": {
3343+
"$recursiveRef": "#"
3344+
}
3345+
}
3346+
}
3347+
}
3348+
3349+
// strict-tree schema, guards against misspelled properties
3350+
{
3351+
"$schema": "https://json-schema.org/draft/2019-08/schema",
3352+
"$id": "https://example.com/strict-tree",
3353+
"$recursiveAnchor": true,
3354+
3355+
"$ref": "tree",
3356+
"unevaluatedProperties": false
3357+
}
3358+
3359+
// instance with misspelled field
3360+
{
3361+
"children": [ { "daat": 1 } ]
3362+
}
3363+
]]>
3364+
</artwork>
3365+
</figure>
3366+
<t>
3367+
If we apply the "strict-tree" schema to the instance, we will follow
3368+
the "$ref" to the "tree" schema, examine its "children" subschema,
3369+
and find the "$recursiveAnchor" in its "items" subschema.
3370+
At this point, the dynamic path is
3371+
"#/$ref/properties/children/items/$recursiveRef".
3372+
</t>
3373+
<t>
3374+
The base URI at this point is "https://example.com/tree", so the
3375+
"$recursiveRef" initially resolves to "https://example.com/tree#".
3376+
Since "$recursiveAnchor" is true, we examine the dynamic path to
3377+
see if there is a different base URI to use. We find
3378+
"$recursiveAnchor" with a true value at the dynamic paths of
3379+
"#" and "#/$ref".
3380+
</t>
3381+
<t>
3382+
The outermost is "#", which is the root schema of the "strict-tree"
3383+
schema, so we use its base URI of "https://example.com/strict-tree",
3384+
which produces a final resolved URI of
3385+
"https://example.com/strict-tree#" for the "$recursiveRef".
3386+
</t>
3387+
<t>
3388+
This way, the recursion in the "tree" schema recurses to the root
3389+
of "strict-tree", instead of only applying "strict-tree" to the
3390+
instance root, but applying "tree" to instance children.
3391+
</t>
3392+
</section>
3393+
33103394
<section title="Working with vocabularies">
33113395
<section title="Best practices for vocabulary and meta-schema authors">
33123396
<t>

0 commit comments

Comments
 (0)