|
1595 | 1595 | a reference to its own root, identified by the empty fragment
|
1596 | 1596 | URI reference ("#").
|
1597 | 1597 | </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> |
1598 | 1607 | <section title='Dynamically recursive references with "$recursiveRef"'>
|
1599 | 1608 | <t>
|
1600 | 1609 | 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
|
3307 | 3316 | </t>
|
3308 | 3317 | </section>
|
3309 | 3318 |
|
| 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 | + |
3310 | 3394 | <section title="Working with vocabularies">
|
3311 | 3395 | <section title="Best practices for vocabulary and meta-schema authors">
|
3312 | 3396 | <t>
|
|
0 commit comments