diff --git a/warlock/core.py b/warlock/core.py index 5717399..44771e2 100644 --- a/warlock/core.py +++ b/warlock/core.py @@ -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']) diff --git a/warlock/model.py b/warlock/model.py index 6dff70a..47dfeeb 100644 --- a/warlock/model.py +++ b/warlock/model.py @@ -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))