Extending validators with custom callables not associated with specific properties #1288
Replies: 1 comment
|
The mapping passed to If the check must run once for every instance even when the schema is from jsonschema import Draft202012Validator, ValidationError
def business_errors(instance):
# Replace this example with your check across the instance.
if instance == 'blocked':
yield ValidationError('Custom check failed')
def iter_errors(instance, schema):
yield from business_errors(instance)
yield from Draft202012Validator(schema).iter_errors(instance)
errors = list(iter_errors('blocked', {}))This runs your check at the root once per call and also collects normal schema errors. It does not automatically run the business check again for every nested subschema; add traversal explicitly if that is what you need. I tested jsonschema 4.26.0: an extension keyword was not called for If you can require a keyword in each schema, |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hi,
It appears that to extend a validator class, one must pass in a mapping from schema property names to callables with custom validation logic. What if the callable I want to inject isn't associated with a singular schema property and must always run regardless of what schema properties exist?
All reactions