Permalink
Browse files

Don't validate anything except if present.

Closes #81
  • Loading branch information...
1 parent abd0b2d commit 9da70c69a90b6827b03d105afca99bbb87ae3d7e @Julian committed Mar 10, 2013
Showing with 22 additions and 4 deletions.
  1. +7 −1 jsonschema.py
  2. +15 −3 tests.py
View
@@ -208,7 +208,13 @@ def iter_errors(self, instance, _schema=None):
_schema = self.schema
with self.resolver.in_scope(_schema.get("id", "")):
- for k, v in iteritems(_schema):
+ ref = _schema.get("$ref")
+ if ref is not None:
+ validators = [("$ref", ref)]
+ else:
+ validators = iteritems(_schema)
+
+ for k, v in validators:
validator_attr = "validate_%s" % (k.lstrip("$"),)
validator = getattr(self, validator_attr, None)
View
@@ -25,9 +25,9 @@
pypy_version_info = None
from jsonschema import (
- PY3, SchemaError, UnknownType, ValidationError, ErrorTree,
- Draft3Validator, Draft4Validator, FormatChecker, draft3_format_checker,
- draft4_format_checker, RefResolver, validate, FormatError
+ PY3, FormatError, SchemaError, UnknownType, ValidationError, ErrorTree,
+ Draft3Validator, Draft4Validator, FormatChecker, RefResolver,
+ ValidatorMixin, draft3_format_checker, draft4_format_checker, validate,
)
@@ -613,6 +613,18 @@ def test_draft4_validator_is_the_default(self):
chk_schema.assert_called_once_with({})
+class TestValidatorMixin(unittest.TestCase):
+ def test_if_ref_is_present_then_the_schema_is_replaced(self):
+ class Validator(ValidatorMixin):
+ validate_ref = mock.Mock(return_value=[])
+ validate_type = mock.Mock(return_value=[])
+
+ Validator({"$ref" : "foo", "type" : "quux"}).validate(1)
+
+ self.assertTrue(Validator.validate_ref.called)
+ self.assertFalse(Validator.validate_type.called)
+
+
class TestRefResolver(unittest.TestCase):
def setUp(self):
self.base_uri = ""

0 comments on commit 9da70c6

Please sign in to comment.