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

Add context field to ValidationError, to improve oneOf and anyOf validation errors #649

Open
jpmckinney opened this issue Dec 22, 2024 · 2 comments

Comments

@jpmckinney
Copy link
Contributor

In the case where no subschema is valid, the ValidationError instances in python-jsonschema set a context attribute for allOf and anyOf validation errors as documented and in the code.

This context attribute contains all the validation errors from the subschemas. This is very useful, because the default error message is not sufficiently informative: "[frequently a very large JSON blob] is not valid under any of the schemas listed in the 'oneOf' keyword".

Instead, an application could report the specific errors under each of the subschemas (or, it could have some way to determine which subschema was most relevant, and only report its errors). It might then end up reporting a single, specific validation error within the subschema, which is much more actionable. For example, "'name' is not a string".

Adding this sort of context attribute (I have no opinion on naming) would allow applications to improve the error message.

@jpmckinney jpmckinney changed the title Improve oneOf and anyOf validation errors by adding context attribute to error instance Add context field to ValidationError, to improve oneOf and anyOf validation errors Dec 22, 2024
@Stranger6667
Copy link
Owner

This is super useful indeed, and I would be happy to have a mechanism to improve error messages for such cases (personally I saw a lot of opaque error messages of this sort while validating Open API with jsonschema).

The biggest challenge is avoiding a performance hit by collecting all the related errors. The first idea that comes to mind is to re-validate on demand and enable this "collection" logic, so this way the users who don't need this context won't be affected. Not sure though

@jpmckinney
Copy link
Contributor Author

Could there be an argument passed to validator_for (and other initializers) that either:

  1. gets passed to OneOfValidator via the ctx argument, such that it uses iter_errors instead of is_valid when checking subschemas (this means some extra if-statements in the oneOf validator)
  2. gets passed to get_for_draft, such that a different oneOf validator is chosen, that uses iter_errors (no extra instructions from if-statements, but probably some code duplication)

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

No branches or pull requests

2 participants