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

Nested object validation error #168

Open
gabriel-turturica opened this issue Sep 9, 2021 · 4 comments
Open

Nested object validation error #168

gabriel-turturica opened this issue Sep 9, 2021 · 4 comments
Labels

Comments

@gabriel-turturica
Copy link

file schema.json

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "dataType": { "$ref": "#/$defs/datatypes-enum" }
  },
  "required": ["dataType"],
  "$defs": {
    "datatypes-enum": {
      "oneOf": [
        {
          "type": "string",
          "enum": ["bool", "boolA"]
        },
        {
          "type": "object",
          "patternProperties": {
            "^[a-zA-Z0-9_]+$": { "$ref": "#/$defs/datatypes-enum" }
          }
        }
      ]
    }
  }
}

file data.json

{
  "dataType": { "test": { "test": { "test": "bool" } } }
}

Expected results when validating data.json against schema.json: valid file (verified using https://www.jsonschemavalidator.net/ and jsonschema in python).
Results I get: "schema with # /$defs/datatypes-enum/oneOf/0 already inserted".

Any advice would be appreciated.

@pboettch
Copy link
Owner

pboettch commented Sep 9, 2021

Interesting.

Your schema is using recursion. If your schema is valid then there is a bug in this project which prevents this schema to be parsed.

@pboettch pboettch added the bug label Sep 9, 2021
@pboettch
Copy link
Owner

pboettch commented Sep 9, 2021

OK, I found the problem: $defs is not the correct name for defintions in draft7, (it is definitions), so $defs is considered an unknown keyword - which is allowed to contain schemas for validation in draft7, if they are referenced somewhere within the schema - otherwise they are left unconsidered.

Because of that, this library parses unknown keywords-referenced-schemas on demand, which is different to known keywords (such as definitions) which are de-referenced, once everything is loaded.

What happens with the other validators if you name $defs to something unknown?

One solution/workaround here would be to consider $defs as a valid keyword, or you to change your schema to definitions.

Or parsing unknown keyword-schemas lazily as well - but this might be harder.

@gabriel-turturica
Copy link
Author

Your debugging seems to be on point. I have changed from $defs to definitions and everything works fine. Thanks for the feedback.

@pboettch
Copy link
Owner

pboettch commented Sep 9, 2021

Glad you found a workaround, but the error you got is still a bug and needs to be fixed.

@pboettch pboettch reopened this Sep 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants