-
Hi! I've tried to adapt the validating-with-additional-types example to use a new type, rather than redefine an existing type, but I'm not having much luck. Here's a simple reproduction: from jsonschema import validators
from jsonschema.exceptions import SchemaError
from jsonschema.validators import Draft202012Validator
try:
def validate_my_type(checker, instance):
return True
type_checker = Draft202012Validator.TYPE_CHECKER.redefine(
"my_type", validate_my_type,
)
CustomValidator = validators.extend(
Draft202012Validator,
type_checker=type_checker,
)
CustomValidator.check_schema(schema={"type": "my_type"})
print("validation succeeded")
except SchemaError as e:
print(f"validation failed: {e}") The resulting output is:
Is |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
It's incorrect in the sense that that isn't allowed in Draft 2020 -- the set of types are fixed and not extendable, so you're getting an error telling you "your schema is invalid" (which it is -- the metaschema also constrains the types to a fixed list which is what you're seeing there). You could of course create your own dialect exactly like 2020 but with this restriction removed, for example by calling Better is to not do this though and to invent your own keyword instead, so that your new dialect is not invalid under 2020 and instead extends it, and doesn't do illegal things with the |
Beta Was this translation helpful? Give feedback.
It's incorrect in the sense that that isn't allowed in Draft 2020 -- the set of types are fixed and not extendable, so you're getting an error telling you "your schema is invalid" (which it is -- the metaschema also constrains the types to a fixed list which is what you're seeing there).
You could of course create your own dialect exactly like 2020 but with this restriction removed, for example by calling
jsonschema.validators.create
with some new metaschema that's the 2020 one but just with that condition removed and with otherwise all the same logic.Better is to not do this though and to invent your own keyword instead, so that your new dialect is not invalid under 2020 and instead ext…