summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Gonzalez (pegasus) <gonvaled@gonvaled.com>2013-05-21 08:19:12 +0200
committerDaniel Gonzalez (pegasus) <gonvaled@gonvaled.com>2013-05-21 08:19:12 +0200
commited0f98ee8f94890b4d330c5e47fb5c33c98cc09d (patch)
tree43ecd5cda1f59f450b9e0fc077f25cc622888f92
parent0f534a089c104c0e01f4e238174a6007e5739b37 (diff)
downloadjsonschema-ed0f98ee8f94890b4d330c5e47fb5c33c98cc09d.tar.gz
Added validator_for to provide direct access to validators
-rw-r--r--docs/creating.rst2
-rw-r--r--jsonschema/__init__.py2
-rw-r--r--jsonschema/tests/test_validators.py23
-rw-r--r--jsonschema/validators.py12
4 files changed, 34 insertions, 5 deletions
diff --git a/docs/creating.rst b/docs/creating.rst
index 03b07c5..e18226f 100644
--- a/docs/creating.rst
+++ b/docs/creating.rst
@@ -45,6 +45,8 @@ Creating or Extending Validators
:returns: an :class:`jsonschema.IValidator`
+.. autofunction:: validate_for
+
.. autofunction:: validates
diff --git a/jsonschema/__init__.py b/jsonschema/__init__.py
index 4514d8f..9ace769 100644
--- a/jsonschema/__init__.py
+++ b/jsonschema/__init__.py
@@ -17,7 +17,7 @@ from jsonschema._format import (
)
from jsonschema.validators import (
ErrorTree, Draft3Validator, Draft4Validator, RefResolver, ValidatorMixin,
- validate, validates,
+ validator_for, validate, validates,
)
diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py
index 5162f6f..ba7da70 100644
--- a/jsonschema/tests/test_validators.py
+++ b/jsonschema/tests/test_validators.py
@@ -8,8 +8,8 @@ from jsonschema import FormatChecker, ValidationError
from jsonschema.compat import PY3
from jsonschema.tests.compat import mock, unittest
from jsonschema.validators import (
- RefResolutionError, UnknownType, ErrorTree, Draft3Validator,
- Draft4Validator, RefResolver, ValidatorMixin, create, extend, validate,
+ _unset, RefResolutionError, UnknownType, ErrorTree, Draft3Validator,
+ Draft4Validator, RefResolver, ValidatorMixin, create, extend, validator_for, validate,
)
@@ -624,6 +624,25 @@ class TestDraft4Validator(ValidatorTestMixin, unittest.TestCase):
validator_class = Draft4Validator
+class TestValidatorFor(unittest.TestCase):
+ def do_it(self, schema_url, expected_draft):
+ schema = {"$schema" : schema_url}
+ with mock.patch.object(expected_draft, "check_schema") as chk_schema:
+ validator_for(schema)
+ chk_schema.assert_called_once_with(schema)
+
+ def test_validator_for(self):
+ self.do_it("http://json-schema.org/draft-03/schema#", Draft3Validator)
+ self.do_it("http://json-schema.org/draft-03/schema", Draft3Validator)
+ self.do_it("http://json-schema.org/draft-04/schema#", Draft4Validator)
+ self.do_it("http://json-schema.org/draft-04/schema", Draft4Validator)
+
+ def test_validator_unset(self):
+ schema = {}
+ validator = validator_for(schema)
+ self.assertEqual(validator, _unset)
+
+
class TestValidate(unittest.TestCase):
def test_draft3_validator_is_chosen(self):
schema = {"$schema" : "http://json-schema.org/draft-03/schema#"}
diff --git a/jsonschema/validators.py b/jsonschema/validators.py
index c4424b4..9a68440 100644
--- a/jsonschema/validators.py
+++ b/jsonschema/validators.py
@@ -473,8 +473,16 @@ class ErrorTree(object):
return len(self.errors) + child_errors
+def validator_for(schema, default=_unset):
+ cls = meta_schemas.get(schema.get("$schema", ""), default)
+ if not cls is _unset:
+ cls.check_schema(schema)
+ return cls
+
+
def validate(instance, schema, cls=None, *args, **kwargs):
if cls is None:
- cls = meta_schemas.get(schema.get("$schema", ""), Draft4Validator)
- cls.check_schema(schema)
+ cls = validator_for(schema)
+ else:
+ cls.check_schema(schema)
cls(schema, *args, **kwargs).validate(instance)