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 multiprocessing
|
||||||
import os.path
|
import os.path
|
||||||
import platform
|
import platform
|
||||||
import distro
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
import jsonschema
|
import jsonschema
|
||||||
@ -228,8 +227,18 @@ def validate(config, offline=False, schema=None):
|
|||||||
DefaultValidator = _extend_with_default_and_alias(
|
DefaultValidator = _extend_with_default_and_alias(
|
||||||
jsonschema.Draft4Validator, offline=offline
|
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(
|
validator = DefaultValidator(
|
||||||
schema,
|
schema,
|
||||||
|
{
|
||||||
|
"array": (tuple, list),
|
||||||
|
"regex": six.string_types,
|
||||||
|
"url": six.string_types,
|
||||||
|
},
|
||||||
)
|
)
|
||||||
errors = []
|
errors = []
|
||||||
warnings = []
|
warnings = []
|
||||||
@ -445,37 +454,20 @@ def _extend_with_default_and_alias(validator_class, offline=False):
|
|||||||
context=all_errors,
|
context=all_errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
kwargs = {}
|
||||||
|
if hasattr(validator_class, "TYPE_CHECKER"):
|
||||||
|
# jsonschema >= 3
|
||||||
def is_array(checker, instance):
|
def is_array(checker, instance):
|
||||||
return isinstance(instance, (tuple, list))
|
return isinstance(instance, (tuple, list))
|
||||||
|
|
||||||
def is_string_type(checker, instance):
|
def is_string_type(checker, instance):
|
||||||
return isinstance(instance, six.string_types)
|
return isinstance(instance, six.string_types)
|
||||||
|
|
||||||
# RHEL9 has newer version of package jsonschema
|
kwargs["type_checker"] = validator_class.TYPE_CHECKER.redefine_many(
|
||||||
# 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(
|
|
||||||
{"array": is_array, "regex": is_string_type, "url": is_string_type}
|
{"array": is_array, "regex": is_string_type, "url": is_string_type}
|
||||||
)
|
)
|
||||||
|
|
||||||
validator = jsonschema.validators.extend(
|
return jsonschema.validators.extend(
|
||||||
validator_class,
|
validator_class,
|
||||||
{
|
{
|
||||||
"properties": properties_validator,
|
"properties": properties_validator,
|
||||||
@ -485,9 +477,8 @@ def _extend_with_default_and_alias(validator_class, offline=False):
|
|||||||
"additionalProperties": _validate_additional_properties,
|
"additionalProperties": _validate_additional_properties,
|
||||||
"anyOf": _validate_any_of,
|
"anyOf": _validate_any_of,
|
||||||
},
|
},
|
||||||
type_checker=type_checker,
|
**kwargs
|
||||||
)
|
)
|
||||||
return validator
|
|
||||||
|
|
||||||
|
|
||||||
class ConfigDeprecation(jsonschema.exceptions.ValidationError):
|
class ConfigDeprecation(jsonschema.exceptions.ValidationError):
|
||||||
|
Loading…
Reference in New Issue
Block a user