Add back compatibility with jsonschema <3.0.0
Resolves: https://pagure.io/pungi/issue/1667
Signed-off-by: Lubomír Sedlář <lsedlar@redhat.com>
(cherry picked from commit e888e76992
(centos_master))
This commit is contained in:
parent
42f1c62528
commit
997e372f25
@ -39,7 +39,6 @@ from __future__ import print_function
|
||||
import multiprocessing
|
||||
import os.path
|
||||
import platform
|
||||
import distro
|
||||
import re
|
||||
|
||||
import jsonschema
|
||||
@ -228,9 +227,19 @@ def validate(config, offline=False, schema=None):
|
||||
DefaultValidator = _extend_with_default_and_alias(
|
||||
jsonschema.Draft4Validator, offline=offline
|
||||
)
|
||||
validator = DefaultValidator(
|
||||
schema,
|
||||
)
|
||||
|
||||
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 = []
|
||||
for error in validator.iter_errors(config):
|
||||
@ -445,49 +454,31 @@ def _extend_with_default_and_alias(validator_class, offline=False):
|
||||
context=all_errors,
|
||||
)
|
||||
|
||||
def is_array(checker, instance):
|
||||
return isinstance(instance, (tuple, list))
|
||||
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)
|
||||
def is_string_type(checker, instance):
|
||||
return isinstance(instance, six.string_types)
|
||||
|
||||
# RHEL9 has newer version of package jsonschema
|
||||
# which has another way of working with validators
|
||||
if float(distro.linux_distribution()[1]) < 9:
|
||||
validator = jsonschema.validators.extend(
|
||||
validator_class,
|
||||
{
|
||||
"properties": properties_validator,
|
||||
"deprecated": error_on_deprecated,
|
||||
"type": validate_regex_type,
|
||||
"required": _validate_required,
|
||||
"additionalProperties": _validate_additional_properties,
|
||||
"anyOf": _validate_any_of,
|
||||
},
|
||||
)
|
||||
validator.DEFAULT_TYPES.update({
|
||||
"array": (list, tuple),
|
||||
"regex": six.string_types,
|
||||
"url": six.string_types,
|
||||
})
|
||||
else:
|
||||
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}
|
||||
)
|
||||
|
||||
validator = jsonschema.validators.extend(
|
||||
validator_class,
|
||||
{
|
||||
"properties": properties_validator,
|
||||
"deprecated": error_on_deprecated,
|
||||
"type": validate_regex_type,
|
||||
"required": _validate_required,
|
||||
"additionalProperties": _validate_additional_properties,
|
||||
"anyOf": _validate_any_of,
|
||||
},
|
||||
type_checker=type_checker,
|
||||
)
|
||||
return validator
|
||||
return jsonschema.validators.extend(
|
||||
validator_class,
|
||||
{
|
||||
"properties": properties_validator,
|
||||
"deprecated": error_on_deprecated,
|
||||
"type": validate_regex_type,
|
||||
"required": _validate_required,
|
||||
"additionalProperties": _validate_additional_properties,
|
||||
"anyOf": _validate_any_of,
|
||||
},
|
||||
**kwargs
|
||||
)
|
||||
|
||||
|
||||
class ConfigDeprecation(jsonschema.exceptions.ValidationError):
|
||||
|
Loading…
Reference in New Issue
Block a user