Skip to content

some tests of the interactions between $id, $anchor and $ref #383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions tests/draft2019-09/anchor.json
Original file line number Diff line number Diff line change
Expand Up @@ -169,5 +169,36 @@
"valid": false
}
]
},
{
"description": "non-schema object containing an $anchor property",
"schema": {
"$defs": {
"const_not_anchor": {
"const": {
"$anchor": "not_a_real_anchor"
}
}
},
"if": {
"const": "skip not_a_real_anchor"
},
"then": true,
"else" : {
"$ref": "#/$defs/const_not_anchor"
}
},
"tests": [
{
"description": "skip traversing definition for a valid result",
"data": "skip not_a_real_anchor",
"valid": true
},
{
"description": "const at const_not_anchor does not match",
"data": 1,
"valid": false
}
]
}
]
31 changes: 31 additions & 0 deletions tests/draft2019-09/id.json
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,36 @@
"valid": false
}
]
},
{
"description": "non-schema object containing an $id property",
"schema": {
"$defs": {
"const_not_id": {
"const": {
"$id": "not_a_real_id"
}
}
},
"if": {
"const": "skip not_a_real_id"
},
"then": true,
"else" : {
"$ref": "#/$defs/const_not_id"
}
},
"tests": [
{
"description": "skip traversing definition for a valid result",
"data": "skip not_a_real_id",
"valid": true
},
{
"description": "const at const_not_id does not match",
"data": 1,
"valid": false
}
]
}
]
65 changes: 65 additions & 0 deletions tests/draft2019-09/ref.json
Original file line number Diff line number Diff line change
Expand Up @@ -575,5 +575,70 @@
"valid": false
}
]
},
{
"description": "order of evaluation: $id and $ref",
"schema": {
"$comment": "$id must be evaluated before $ref to get the proper $ref destination",
"$id": "/base/base.json",
"$ref": "int.json",
"$defs": {
"bigint": {
"$comment": "canonical uri: /base/int.json",
"$id": "int.json",
"maximum": 10
},
"smallint": {
"$comment": "canonical uri: /int.json",
"$id": "/int.json",
"maximum": 2
}
}
},
"tests": [
{
"description": "data is valid against first definition",
"data": 5,
"valid": true
},
{
"description": "data is invalid against first definition",
"data": 50,
"valid": false
}
]
},
{
"description": "order of evaluation: $id and $anchor and $ref",
"schema": {
"$comment": "$id must be evaluated before $ref to get the proper $ref destination",
"$id": "/base.json",
"$ref": "#bigint",
"$defs": {
"bigint": {
"$comment": "canonical uri: /base.json#/$defs/bigint; another valid uri for this location: /base.json#bigint",
"$anchor": "bigint",
"maximum": 10
},
"smallint": {
"$comment": "canonical uri: /#/$defs/smallint; another valid uri for this location: /#bigint",
"$id": "/",
"$anchor": "bigint",
"maximum": 2
}
}
},
"tests": [
{
"description": "data is valid against first definition",
"data": 5,
"valid": true
},
{
"description": "data is invalid against first definition",
"data": 50,
"valid": false
}
]
}
]
63 changes: 62 additions & 1 deletion tests/draft6/id.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,67 @@
"valid": false
}
]
},
{
"description": "non-schema object containing a plain-name $id property",
Copy link
Member

@gregsdennis gregsdennis Jun 28, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This uses if/then/else which weren't introduced to draft 7. We need to remove these from draft 6.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind opening an issue, I'll remove them if someone doesn't get to it first

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix in #562

"schema": {
"$defs": {
"const_not_anchor": {
"const": {
"$id": "#not_a_real_anchor"
}
}
},
"if": {
"const": "skip not_a_real_anchor"
},
"then": true,
"else" : {
"$ref": "#/$defs/const_not_anchor"
}
},
"tests": [
{
"description": "skip traversing definition for a valid result",
"data": "skip not_a_real_anchor",
"valid": true
},
{
"description": "const at const_not_anchor does not match",
"data": 1,
"valid": false
}
]
},
{
"description": "non-schema object containing an $id property",
"schema": {
"$defs": {
"const_not_id": {
"const": {
"$id": "not_a_real_id"
}
}
},
"if": {
"const": "skip not_a_real_id"
},
"then": true,
"else" : {
"$ref": "#/$defs/const_not_id"
}
},
"tests": [
{
"description": "skip traversing definition for a valid result",
"data": "skip not_a_real_id",
"valid": true
},
{
"description": "const at const_not_id does not match",
"data": 1,
"valid": false
}
]
}

]
63 changes: 62 additions & 1 deletion tests/draft7/id.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,67 @@
"valid": false
}
]
},
{
"description": "non-schema object containing a plain-name $id property",
"schema": {
"$defs": {
"const_not_anchor": {
"const": {
"$id": "#not_a_real_anchor"
}
}
},
"if": {
"const": "skip not_a_real_anchor"
},
"then": true,
"else" : {
"$ref": "#/$defs/const_not_anchor"
}
},
"tests": [
{
"description": "skip traversing definition for a valid result",
"data": "skip not_a_real_anchor",
"valid": true
},
{
"description": "const at const_not_anchor does not match",
"data": 1,
"valid": false
}
]
},
{
"description": "non-schema object containing an $id property",
"schema": {
"$defs": {
"const_not_id": {
"const": {
"$id": "not_a_real_id"
}
}
},
"if": {
"const": "skip not_a_real_id"
},
"then": true,
"else" : {
"$ref": "#/$defs/const_not_id"
}
},
"tests": [
{
"description": "skip traversing definition for a valid result",
"data": "skip not_a_real_id",
"valid": true
},
{
"description": "const at const_not_id does not match",
"data": 1,
"valid": false
}
]
}

]
64 changes: 64 additions & 0 deletions tests/draft7/ref.json
Original file line number Diff line number Diff line change
Expand Up @@ -644,5 +644,69 @@
"valid": false
}
]
},
{
"description": "order of evaluation: $id and $ref",
"schema": {
"$comment": "$id must be evaluated before $ref to get the proper $ref destination",
"$id": "/base/base.json",
"$ref": "int.json",
"$defs": {
"bigint": {
"$comment": "canonical uri: /base/int.json",
"$id": "int.json",
"maximum": 10
},
"smallint": {
"$comment": "canonical uri: /int.json",
"$id": "/int.json",
"maximum": 2
}
}
},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is wrong. $id in the presence of $ref in draft7 will do nothing. consequently, attempts to resolve the ref to "/int.json" will fail.

It looks like I copied this test erroneously from draft2019-09, and it was never tested.

"tests": [
{
"description": "data is valid against first definition",
"data": 5,
"valid": true
},
{
"description": "data is invalid against first definition",
"data": 50,
"valid": false
}
]
},
{
"description": "order of evaluation: plain-name $id and $ref",
"schema": {
"$comment": "$id must be evaluated before $ref to get the proper $ref destination",
"$id": "/base.json",
"$ref": "#bigint",
"$defs": {
"bigint": {
"$comment": "canonical uri: /base.json#/$defs/bigint; another valid uri for this location: /base.json#bigint",
"$id": "#bigint",
"maximum": 10
},
"smallint": {
"$comment": "canonical uri: /#/$defs/smallint; another valid uri for this location: /#bigint",
"$id": "/#bigint",
"maximum": 2
}
}
},
"tests": [
{
"description": "data is valid against first definition",
"data": 5,
"valid": true
},
{
"description": "data is invalid against first definition",
"data": 50,
"valid": false
}
]
}
]