Skip to content
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

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

Open
yogat3ch opened this issue Feb 8, 2024 · 2 comments

Comments

@yogat3ch
Copy link

yogat3ch commented Feb 8, 2024

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
Copy link
Member

richfitz commented Feb 8, 2024

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 ./path to be treated as path; 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

@yogat3ch
Copy link
Author

yogat3ch commented Feb 9, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants