Skip to content

Commit 017d4e5

Browse files
Merge pull request #499 from json-schema-org/ether/dynamic-scope-enter-leave
test that dynamic scopes are not always in scope
2 parents 6327a3b + aa621ed commit 017d4e5

File tree

2 files changed

+116
-2
lines changed

2 files changed

+116
-2
lines changed

tests/draft-future/dynamicRef.json

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
]
7979
},
8080
{
81-
"description": "A $dynamicRef should resolve to the first $dynamicAnchor that is encountered when the schema is evaluated",
81+
"description": "A $dynamicRef should resolve to the first $dynamicAnchor still in scope that is encountered when the schema is evaluated",
8282
"schema": {
8383
"$id": "https://test.json-schema.org/typical-dynamic-resolution/root",
8484
"$ref": "list",
@@ -384,5 +384,62 @@
384384
"valid": false
385385
}
386386
]
387+
},
388+
{
389+
"description": "after leaving a dynamic scope, it should not be used by a $dynamicRef",
390+
"schema": {
391+
"$id": "https://test.json-schema.org/dynamic-ref-leaving-dynamic-scope/main",
392+
"if": {
393+
"$id": "first_scope",
394+
"$defs": {
395+
"thingy": {
396+
"$comment": "this is first_scope#thingy",
397+
"$dynamicAnchor": "thingy",
398+
"type": "number"
399+
}
400+
}
401+
},
402+
"then": {
403+
"$id": "second_scope",
404+
"$ref": "start",
405+
"$defs": {
406+
"thingy": {
407+
"$comment": "this is second_scope#thingy, the final destination of the $dynamicRef",
408+
"$dynamicAnchor": "thingy",
409+
"type": "null"
410+
}
411+
}
412+
},
413+
"$defs": {
414+
"start": {
415+
"$comment": "this is the landing spot from $ref",
416+
"$id": "start",
417+
"$dynamicRef": "inner_scope#thingy"
418+
},
419+
"thingy": {
420+
"$comment": "this is the first stop for the $dynamicRef",
421+
"$id": "inner_scope",
422+
"$dynamicAnchor": "thingy",
423+
"type": "string"
424+
}
425+
}
426+
},
427+
"tests": [
428+
{
429+
"description": "string matches /$defs/thingy, but the $dynamicRef does not stop here",
430+
"data": "a string",
431+
"valid": false
432+
},
433+
{
434+
"description": "first_scope is not in dynamic scope for the $dynamicRef",
435+
"data": 42,
436+
"valid": false
437+
},
438+
{
439+
"description": "/then/$defs/thingy is the final stop for the $dynamicRef",
440+
"data": null,
441+
"valid": true
442+
}
443+
]
387444
}
388445
]

tests/draft2020-12/dynamicRef.json

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
]
7979
},
8080
{
81-
"description": "A $dynamicRef should resolve to the first $dynamicAnchor that is encountered when the schema is evaluated",
81+
"description": "A $dynamicRef should resolve to the first $dynamicAnchor still in scope that is encountered when the schema is evaluated",
8282
"schema": {
8383
"$id": "https://test.json-schema.org/typical-dynamic-resolution/root",
8484
"$ref": "list",
@@ -384,5 +384,62 @@
384384
"valid": false
385385
}
386386
]
387+
},
388+
{
389+
"description": "after leaving a dynamic scope, it should not be used by a $dynamicRef",
390+
"schema": {
391+
"$id": "https://test.json-schema.org/dynamic-ref-leaving-dynamic-scope/main",
392+
"if": {
393+
"$id": "first_scope",
394+
"$defs": {
395+
"thingy": {
396+
"$comment": "this is first_scope#thingy",
397+
"$dynamicAnchor": "thingy",
398+
"type": "number"
399+
}
400+
}
401+
},
402+
"then": {
403+
"$id": "second_scope",
404+
"$ref": "start",
405+
"$defs": {
406+
"thingy": {
407+
"$comment": "this is second_scope#thingy, the final destination of the $dynamicRef",
408+
"$dynamicAnchor": "thingy",
409+
"type": "null"
410+
}
411+
}
412+
},
413+
"$defs": {
414+
"start": {
415+
"$comment": "this is the landing spot from $ref",
416+
"$id": "start",
417+
"$dynamicRef": "inner_scope#thingy"
418+
},
419+
"thingy": {
420+
"$comment": "this is the first stop for the $dynamicRef",
421+
"$id": "inner_scope",
422+
"$dynamicAnchor": "thingy",
423+
"type": "string"
424+
}
425+
}
426+
},
427+
"tests": [
428+
{
429+
"description": "string matches /$defs/thingy, but the $dynamicRef does not stop here",
430+
"data": "a string",
431+
"valid": false
432+
},
433+
{
434+
"description": "first_scope is not in dynamic scope for the $dynamicRef",
435+
"data": 42,
436+
"valid": false
437+
},
438+
{
439+
"description": "/then/$defs/thingy is the final stop for the $dynamicRef",
440+
"data": null,
441+
"valid": true
442+
}
443+
]
387444
}
388445
]

0 commit comments

Comments
 (0)