-
-
Notifications
You must be signed in to change notification settings - Fork 581
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
Enhance jsonschema.exceptions.best_match to prefer deeper errors #698
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Thanks for the feedback. More detailed example below. Configuration
Schema
Instance
Code
Actual output
Potential output
|
I haven't gotten a chance to have a look at your last message, but in that new example it seems more likely there's something we may be able to improve (unclear; best_match is always a bit of a heuristic, and one has to be careful that making one example simpler doesn't make another more complex) But I think from what I see it's possible |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
This comment was marked as outdated.
A mostly simplified version of this example is: schema = {
"anyOf": [
{"$ref": "#/definitions/B"},
{"$ref": "#/definitions/C"}
],
"definitions": {
"B": {
"properties": {
"schema": { "const": "B" },
"child" : { "$ref": "#" }
},
},
"C": {
"properties": {
"schema": { "const": "C" },
"foo": { "type" : "string" },
"child" : { "$ref": "#" }
},
}
}
}
instance = {
"schema": "B",
"child" : {
"schema": "B",
"child" : {"schema": "C", "foo" : 2}
}
}
from pprint import pformat, pprint
from textwrap import indent
from jsonschema import Draft7Validator, exceptions
Draft7Validator.check_schema(schema)
validator = Draft7Validator(schema)
errors = list(validator.iter_errors(instance))
best = exceptions.best_match(errors)
pprint(
[
(each.message, "#/" + "/".join(each.absolute_path), "#/" + "/".join(map(str, each.absolute_schema_path)))
for each in best.context[2].context[0].context
]
) The issue is essentially that neither error really is deeper. They're both at the same level -- specifically the above produces:
i.e. they're at the same level both in the instance and schema. I'm inclined to close this as much as I agree the other message would be better for this case, I can't think of a heuristic that actually would get us there. I'm open to suggestions, but what's in the title here isn't it, and we already do indeed take depth into account. |
Given the schema below, the following instance fails with a single error:
{'schema': 'C', 'kind': {'schema': 'D', 'foo': 2}}: {'schema': 'C', 'kind': {'schema': 'D', 'foo': 2}} is not valid under any of the given schemas
While it is true that the instance does not match either definitions
B
orC
, could the validator indicate that the instance matched until it reached/kind/foo
. Otherwise, schema validation will always fail on the top levelanyOf
, which is not very helpful.Perhaps this is already possible and I missed it, or there is a way to design the schema differently?
Sample instance
Schema
The text was updated successfully, but these errors were encountered: