Made validator inheritable by base classes.
This commit is contained in:
parent
98a44c6601
commit
c2c4461930
|
@ -2,10 +2,7 @@
|
|||
|
||||
import copy
|
||||
|
||||
import jsonschema
|
||||
|
||||
import model
|
||||
import exceptions
|
||||
|
||||
|
||||
def model_factory(schema, base_class=model.Model):
|
||||
|
@ -15,16 +12,9 @@ def model_factory(schema, base_class=model.Model):
|
|||
"""
|
||||
schema = copy.deepcopy(schema)
|
||||
|
||||
def validator(obj):
|
||||
"""Apply a JSON schema to an object"""
|
||||
try:
|
||||
jsonschema.validate(obj, schema)
|
||||
except jsonschema.ValidationError as exc:
|
||||
raise exceptions.ValidationError(str(exc))
|
||||
|
||||
class Model(base_class):
|
||||
def __init__(self, *args, **kwargs):
|
||||
kwargs.setdefault('validator', validator)
|
||||
self.__dict__['schema'] = schema
|
||||
base_class.__init__(self, *args, **kwargs)
|
||||
|
||||
Model.__name__ = str(schema['name'])
|
||||
|
|
|
@ -3,15 +3,13 @@
|
|||
import copy
|
||||
|
||||
import jsonpatch
|
||||
import jsonschema
|
||||
|
||||
import exceptions
|
||||
|
||||
|
||||
class Model(dict):
|
||||
def __init__(self, *args, **kwargs):
|
||||
# Load the validator from the kwargs
|
||||
#self.__dict__['validator'] = kwargs.pop('validator', self.default_validator)
|
||||
|
||||
# we overload setattr so set this manually
|
||||
d = dict(*args, **kwargs)
|
||||
|
||||
|
@ -104,5 +102,9 @@ class Model(dict):
|
|||
original = self.__dict__['__original__']
|
||||
return jsonpatch.make_patch(original, dict(self)).to_string()
|
||||
|
||||
def default_validator(self, *args, **kwargs):
|
||||
return True
|
||||
def validator(self, obj):
|
||||
"""Apply a JSON schema to an object"""
|
||||
try:
|
||||
jsonschema.validate(obj, self.schema)
|
||||
except jsonschema.ValidationError as exc:
|
||||
raise exceptions.ValidationError(str(exc))
|
||||
|
|
Loading…
Reference in New Issue