Add back compatibility with jsonschema <3.0.0

Resolves: https://pagure.io/pungi/issue/1667
Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
This commit is contained in:
Lubomír Sedlář 2023-05-26 10:07:54 +02:00 committed by lsedlar
parent 6e72de7efe
commit e888e76992

View File

@ -227,8 +227,18 @@ def validate(config, offline=False, schema=None):
DefaultValidator = _extend_with_default_and_alias(
jsonschema.Draft4Validator, offline=offline
)
if hasattr(jsonschema.Draft4Validator, "TYPE_CHECKER"):
# jsonschema >= 3.0 has new interface for checking types
validator = DefaultValidator(schema)
else:
validator = DefaultValidator(
schema,
{
"array": (tuple, list),
"regex": six.string_types,
"url": six.string_types,
},
)
errors = []
warnings = []
@ -444,13 +454,16 @@ def _extend_with_default_and_alias(validator_class, offline=False):
context=all_errors,
)
kwargs = {}
if hasattr(validator_class, "TYPE_CHECKER"):
# jsonschema >= 3
def is_array(checker, instance):
return isinstance(instance, (tuple, list))
def is_string_type(checker, instance):
return isinstance(instance, six.string_types)
type_checker = validator_class.TYPE_CHECKER.redefine_many(
kwargs["type_checker"] = validator_class.TYPE_CHECKER.redefine_many(
{"array": is_array, "regex": is_string_type, "url": is_string_type}
)
@ -464,7 +477,7 @@ def _extend_with_default_and_alias(validator_class, offline=False):
"additionalProperties": _validate_additional_properties,
"anyOf": _validate_any_of,
},
type_checker=type_checker,
**kwargs
)