diff options
-rw-r--r-- | jsonschema/tests/test_validators.py | 2 | ||||
-rw-r--r-- | jsonschema/validators.py | 91 |
2 files changed, 56 insertions, 37 deletions
diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index f8f982c..1aaaceb 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -1201,14 +1201,12 @@ class AntiDraft6LeakMixin(object): self.Validator.check_schema(False) self.assertIn("False is not of type", str(e.exception)) - @unittest.skip("This test fails, but it shouldn't.") def test_True_is_not_a_schema_even_if_you_forget_to_check(self): resolver = validators.RefResolver("", {}) with self.assertRaises(Exception) as e: self.Validator(True, resolver=resolver).validate(12) self.assertNotIsInstance(e.exception, exceptions.ValidationError) - @unittest.skip("This test fails, but it shouldn't.") def test_False_is_not_a_schema_even_if_you_forget_to_check(self): resolver = validators.RefResolver("", {}) with self.assertRaises(Exception) as e: diff --git a/jsonschema/validators.py b/jsonschema/validators.py index 650b5d1..f8af491 100644 --- a/jsonschema/validators.py +++ b/jsonschema/validators.py @@ -131,6 +131,51 @@ def _id_of(schema): return schema.get(u"$id", u"") +def _no_boolean_errors_from(validator, instance, schema): + ref = schema.get(u"$ref") + if ref is not None: + fn = validator.VALIDATORS["$ref"] + errors = fn(validator, ref, instance, schema) or () + for error in errors: + # set details if not already set by the called fn + error._set(instance=instance, schema=schema) + yield error + else: + for k, v in iteritems(schema): + fn = validator.VALIDATORS.get(k) + if fn is None: + continue + errors = fn(validator, v, instance, schema) or () + for error in errors: + # set details if not already set by the called fn + error._set( + validator=k, + validator_value=v, + instance=instance, + schema=schema, + ) + if k != u"$ref": + error.schema_path.appendleft(k) + yield error + + +def _errors_from(validator, instance, schema): + if schema is True: + return + elif schema is False: + yield exceptions.ValidationError( + "False schema does not allow %r" % (instance,), + validator=None, + validator_value=None, + instance=instance, + schema=schema, + ) + return + else: + for error in _no_boolean_errors_from(validator, instance, schema): + yield error + + def create( meta_schema, validators=(), @@ -138,6 +183,7 @@ def create( default_types=None, type_checker=None, id_of=_id_of, + errors_from=_errors_from, ): """ Create a new validator class. @@ -190,6 +236,12 @@ def create( A function that given a schema, returns its ID. + errors_from (callable): + + A function that given a validator, instance and schema, + returns the iterable of validation errors that are + applicable. + Returns: a new `jsonschema.IValidator` class @@ -270,45 +322,12 @@ def create( if _schema is None: _schema = self.schema - if _schema is True: - return - elif _schema is False: - yield exceptions.ValidationError( - "False schema does not allow %r" % (instance,), - validator=None, - validator_value=None, - instance=instance, - schema=_schema, - ) - return - scope = id_of(_schema) if scope: self.resolver.push_scope(scope) try: - ref = _schema.get(u"$ref") - if ref is not None: - validators = [(u"$ref", ref)] - else: - validators = iteritems(_schema) - - for k, v in validators: - validator = self.VALIDATORS.get(k) - if validator is None: - continue - - errors = validator(self, v, instance, _schema) or () - for error in errors: - # set details if not already set by the called fn - error._set( - validator=k, - validator_value=v, - instance=instance, - schema=_schema, - ) - if k != u"$ref": - error.schema_path.appendleft(k) - yield error + for error in errors_from(self, instance, _schema): + yield error finally: if scope: self.resolver.pop_scope() @@ -442,6 +461,7 @@ Draft3Validator = create( type_checker=_types.draft3_type_checker, version="draft3", id_of=lambda schema: schema.get(u"id", ""), + errors_from=_no_boolean_errors_from, ) Draft4Validator = create( @@ -477,6 +497,7 @@ Draft4Validator = create( type_checker=_types.draft4_type_checker, version="draft4", id_of=lambda schema: schema.get(u"id", ""), + errors_from=_no_boolean_errors_from, ) Draft6Validator = create( |