Incorrect SchemaError for enum schema with unique elements
See original GitHub issue# Draft7 for convenience; the same applies to 3, 4, and 6.
# We can have a unique list of things that compare equal, e.g. this works:
jsonschema.Draft7Validator.check_schema({'enum': [False, 0.0]})
# But wrap them in a one-element list, and we get a `SchemaError` instead!
jsonschema.Draft7Validator.check_schema({'enum': [[False], [0.0]]})
There are IMO two bugs here:
-
JSON values are being tested for equality using Python object semantics, rather than comparing them as JSON (e.g. via a canonical encoding).
-
The json-schema specification says
The value of this keyword MUST be an array. This array SHOULD have at least one element. Elements in the array SHOULD be unique.
Validation therefore ought to allow schema authors to use empty or non-unique arrays, because the RFC 2119 definition of
SHOULD
explicitly permits ignoring such clauses after considering the circumstances.
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Json schema validation error - Stack Overflow
Therefore you receive an error about wrong type. JSON schema type MUST be single value or array of such strings: "array","boolean","integer"," ...
Read more >Handling Validation Errors - python-jsonschema
In case an invalid schema itself is encountered, a SchemaError is raised. exception jsonschema.exceptions.SchemaError(message, validator=<unset>, path=(), ...
Read more >Re: Dependencies between two "enums" - Google Groups
The problem here is with your schema. First you tell it to reference to one definition _but then_ you constrain it with an...
Read more >Understanding JSON Schema
element is unique. In this case, the JSON snippet is valid if it matches any of the given types. Here is a simple...
Read more >schema - PyPI
validate(data). This method may raise SchemaError exception, which will tell Schema that that piece of data is invalid, otherwise—it will continue validating.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@Julian thanks for the “for archeological purposes”, rest assured it’s worth adding as people still run into this strange mismatch years later 😄
I mentioned this briefly on https://github.com/json-schema-org/json-schema-spec/issues/717#issuecomment-1299112660 but just for archaeological purposes, this change was just partially “backed out” for drafts 3 and 4 where indeed the original behavior (MUST) was correct, those drafts contain stronger language. It’s definitely correct on newer ones though (so thanks again for filing it!)