Best practice for own TypeChecker that overrides is_type #1408
Replies: 1 comment
|
For the stated goal—skip the schema's from jsonschema import validators
def skip_link_type(validator_cls):
original_type = validator_cls.VALIDATORS["type"]
def validate_type(validator, expected_types, instance, schema):
if not isinstance(instance, YamlLink):
yield from original_type(
validator, expected_types, instance, schema
)
return validators.extend(
validator_cls, validators={"type": validate_type}
)
# Use your existing YamlLink definition and schema registry.
base_cls = validators.validator_for(schema)
base_cls.check_schema(schema)
validator_cls = skip_link_type(base_cls)
validator = validator_cls(schema, registry=SCHEMA_REGISTRY)This leaves If you want to change actual type classification instead, the public API is The example bypasses only References: extending keyword handlers, public type checker API. AI-assisted response. A standalone reproduction executed with jsonschema 4.25.1 for Draft7 and Draft202012: links bypass |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Hello,
I'm trying to ignore type checking for a specific class instance that represents a Link in our YAML Schema. Right now I'm creating a new type checker, but to make it work as expected I have to create it with the "original" types taken from the private instance attribute
_type_checkersof the original type checker.Is this the best way to override the
is_typefunction of a TypeChecker? I didn't want to monkey patch the function in the original TypeChecker.All reactions