Greedy (?) object typing leads to semantically wrong error message #812
Labels
Enhancement
Some new desired functionality
Error Reporting
Issues related to clearer or more robust validation error reporting
Hi, thanks for a great project!
We've stumbled across an issue with misleading error messages, although I'm not sure if there is a useful fix.
jsonschema
version:3.2.0
3.8.5
When you define two objects (e.g.,
person
andentity
), which share a number of properties but define others which are mutually exclusive across the two, and one object (say,entity
) also requires fields where the other doesn't, properties are reported in aValidationError
asunexpected
when from the user perspective, they aren't.Expected behaviour
Given the definitions
person
(properties:person_property
(optional),alias
(optional))entity
(properties:entity_property
(required),alias
(optional))and data that introduces an additional property
BREAK_ENTITY
(which is disallowed via"additionalProperties": false
) to an otherwise validentity
data object, I expect the error message to raise the additional key, but not any other (semantically valid) keys.Actual behaviour
The error messages raises two unexpected keys: the additional one (correct), and
entity_property
(incorrect).Without having had a look at the source code, I assume that the additional property trips the error, and then trying to type the object and collecting unexpected properties is greedy, and in this case assumes
person
whereas we're looking atentity
really, which would be valid if only the additional property was removed. I guess changing the heuristic to find the best match may come at a performance penalty though, as you'd have to look at all potential best matches?This also happens independently from which of the two objects is defined first, but depends on the order of objects in
anyOf
.Reproducing the issue
(Minimalized (well, more or less) schema
Valid data
Invalid
person
data (yields expected message)The following invalid
person
data breaks and yields the expected error message:jsonschema.exceptions.ValidationError: Additional properties are not allowed ('BREAK_PERSON' was unexpected)
.ISSUE: Invalid
entity
data (yields unexpected message)The following invalid
entity
data yields the expected error, albeit with an unexpected error messagejsonschema.exceptions.ValidationError: Additional properties are not allowed ('BREAK_ENTITY', 'entity_property' were unexpected)
(, 'entity_property'
being the unexpected part, full trace further below)Trace
Linebreaks included for legibility.
The text was updated successfully, but these errors were encountered: