Don't validate anything except if present.

Closes #81
This commit is contained in:
Julian Berman
2013-03-10 14:13:01 -04:00
parent abd0b2dd1c
commit 9da70c69a9
2 changed files with 22 additions and 4 deletions

View File

@@ -208,7 +208,13 @@ class ValidatorMixin(object):
_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 File

@@ -25,9 +25,9 @@ except ImportError:
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 @@ class TestValidate(unittest.TestCase):
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 = ""