diff options
author | David Johnson <david.johnson@oerc.ox.ac.uk> | 2016-06-25 10:46:42 -0700 |
---|---|---|
committer | Brian Waldon <brian@waldon.cc> | 2016-06-25 10:49:15 -0700 |
commit | 64a771d151a5defb9bca82042f2c78d513b33aa9 (patch) | |
tree | 6564e9d7d5afd866a469198778c7e7683c26b797 /warlock | |
parent | 9d89de02df78a5315ec9e75145eb4a6578c05598 (diff) | |
download | warlock-64a771d151a5defb9bca82042f2c78d513b33aa9.tar.gz |
Allow a resolver to be set on the Model
Diffstat (limited to 'warlock')
-rw-r--r-- | warlock/core.py | 7 | ||||
-rw-r--r-- | warlock/model.py | 5 |
2 files changed, 10 insertions, 2 deletions
diff --git a/warlock/core.py b/warlock/core.py index affdaa9..c4486c7 100644 --- a/warlock/core.py +++ b/warlock/core.py @@ -19,19 +19,24 @@ import copy from . import model -def model_factory(schema, base_class=model.Model, name=None): +def model_factory(schema, resolver=None, base_class=model.Model, name=None): """Generate a model class based on the provided JSON Schema :param schema: dict representing valid JSON schema :param name: A name to give the class, if `name` is not in `schema` """ schema = copy.deepcopy(schema) + resolver = resolver class Model(base_class): def __init__(self, *args, **kwargs): self.__dict__['schema'] = schema + self.__dict__['resolver'] = resolver base_class.__init__(self, *args, **kwargs) + if resolver is not None: + Model.resolver = resolver + if name is not None: Model.__name__ = name elif 'name' in schema: diff --git a/warlock/model.py b/warlock/model.py index 666128c..1fe2606 100644 --- a/warlock/model.py +++ b/warlock/model.py @@ -137,6 +137,9 @@ class Model(dict): def validate(self, obj): """Apply a JSON schema to an object""" try: - jsonschema.validate(obj, self.schema) + if self.resolver is not None: + jsonschema.validate(obj, self.schema, resolver=self.resolver) + else: + jsonschema.validate(obj, self.schema) except jsonschema.ValidationError as exc: raise exceptions.ValidationError(str(exc)) |