Fix jsonschema deprecation warning

This commit is contained in:
allegroai 2019-11-15 22:00:21 +02:00
parent 0a45d2094f
commit 603d42792d

View File

@ -31,6 +31,22 @@ def schema_property(name):
return init return init
# Support both jsonschema >= 3.0.0 and <= 2.6.0
_CustomValidator = None
try:
from jsonschema import TypeChecker, Draft7Validator
def _is_array(checker, instance):
return isinstance(instance, (list, tuple))
_CustomValidator = jsonschema.validators.extend(
Draft7Validator,
type_checker=Draft7Validator.TYPE_CHECKER.redefine("array", _is_array)
)
except ImportError:
pass
class DataModel(object): class DataModel(object):
""" Data Model""" """ Data Model"""
_schema = None _schema = None
@ -66,11 +82,18 @@ class DataModel(object):
} }
def validate(self, schema=None): def validate(self, schema=None):
jsonschema.validate( if _CustomValidator is None:
self.to_dict(), jsonschema.validate(
schema or self._schema, self.to_dict(),
types=dict(array=(list, tuple), integer=six.integer_types), schema or self._schema,
) types=dict(array=(list, tuple), integer=six.integer_types),
)
else:
jsonschema.validate(
self.to_dict(),
schema or self._schema,
cls=_CustomValidator,
)
def __repr__(self): def __repr__(self):
return '<{}.{}: {}>'.format( return '<{}.{}: {}>'.format(