Nesting an array inside an object in JSON schema always returns valid (even when it's not)
Directory structure is as follows:
├── main.json
├── objects.json
└── test.json
main.json:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "1.1.0",
"title": "My Main Schema",
"description": "reprex schema",
"type": "object",
"properties": {
"the_object": {
"type": "object",
"properties": {
"my_nested_object": {
"$ref": "./objects.json#/properties/objects"
}
}
}
}
}
objects.json:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "objects",
"title": "My Main Schema",
"description": "reprex schema",
"type": "object",
"properties": {
"objects": {
"type": "array",
"items": {
"type": "string",
"enum": ["a","b","c"]
}
}
}
}
test.json:
{
"the_object": {
"my_nested_object": ["d", "e"]
}
}
jsonvalidate::json_validate("test.json", "main.json")
This will always evaluate to TRUE, disregarding the mismatch between the values in the array in test_json/the_object/my_nested_object and the enum specified in the objects reference. It seems like nested objects disregard the specified constraints of referenced schema?
Change main.json to say
{
"$schema": "http://json-schema.org/draft-04/schema#",
"$id": "1.1.0",
"title": "My Main Schema",
"description": "reprex schema",
"type": "object",
"properties": {
"the_object": {
"type": "object",
"properties": {
"my_nested_object": {
"$ref": "objects.json#/properties/objects"
}
}
}
}
}
and validate with
jsonvalidate::json_validate("test.json", "main.json", engine = "ajv")
Several things are going on here that are not great:
- We need to move towards deprecating imjv, or warning if people use it. The schema validator is not as powerful and not all features that we've added recently are supported on it. We use ajv for everything, and you will need that for more recent json schema versions
- We should detect use of references with imjv and warn that they are being ignored.
- We should also allow
./pathto be treated aspath; I've not seen that before
Pull requests on any of these welcome, otherwise I'll look at these issues next time we work on the package
Thank you for the tip @richfitz!
What are imjv specific references and how do they differ from ajv? I might be able to PR that.
Changing the paths and using ajv seems to be validating as expected now.