diff options
author | yinyinl <yinyinl@users.noreply.github.com> | 2020-02-09 03:11:04 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-09 12:11:04 +0100 |
commit | 6f41194ab675efa451470f8abfebfa6bc72403fe (patch) | |
tree | 18b4c1a386bf2511eac2646aaff5d11a82505a1d | |
parent | 303d9a52a7837b49162445a6220989c9373aea88 (diff) | |
download | warlock-6f41194ab675efa451470f8abfebfa6bc72403fe.tar.gz |
Maintain the validator instance to speedup schema validation process (#55)
-rw-r--r-- | warlock/core.py | 8 | ||||
-rw-r--r-- | warlock/model.py | 6 |
2 files changed, 10 insertions, 4 deletions
diff --git a/warlock/core.py b/warlock/core.py index a6dd3a4..2e933e1 100644 --- a/warlock/core.py +++ b/warlock/core.py @@ -17,6 +17,7 @@ import copy from . import model +from jsonschema.validators import validator_for def model_factory(schema, base_class=model.Model, name=None, resolver=None): @@ -32,6 +33,13 @@ def model_factory(schema, base_class=model.Model, name=None, resolver=None): def __init__(self, *args, **kwargs): self.__dict__["schema"] = schema self.__dict__["resolver"] = resolver + + cls = validator_for(self.schema) + if resolver is not None: + self.__dict__["validator_instance"] = cls(schema, resolver=resolver) + else: + self.__dict__["validator_instance"] = cls(schema) + base_class.__init__(self, *args, **kwargs) if resolver is not None: diff --git a/warlock/model.py b/warlock/model.py index 62eb330..54bcbd7 100644 --- a/warlock/model.py +++ b/warlock/model.py @@ -127,9 +127,7 @@ class Model(dict): def validate(self, obj): """Apply a JSON schema to an object""" try: - if self.resolver is not None: - jsonschema.validate(obj, self.schema, resolver=self.resolver) - else: - jsonschema.validate(obj, self.schema) + self.validator_instance.validate(obj) + except jsonschema.ValidationError as exc: raise exceptions.ValidationError(str(exc)) |