ropensci / jsonvalidate

:heavy_check_mark::interrobang: Validate JSON
https://docs.ropensci.org/jsonvalidate
Other
48 stars 14 forks source link

Nesting an array inside an object in JSON schema always returns valid (even when it's not) #78

Open yogat3ch opened 7 months ago

yogat3ch commented 7 months ago

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?

richfitz commented 7 months ago

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:

Pull requests on any of these welcome, otherwise I'll look at these issues next time we work on the package

yogat3ch commented 7 months ago

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.