Skip to content

Commit

Permalink
Properly preserve applicable_validators in extended validators.
Browse files Browse the repository at this point in the history
Specifically, a validator extending an early JSON Schema draft where
siblings of `$ref` are ignored will properly ignore siblings in the
extended validator as well.

Closes: #1125
  • Loading branch information
Julian committed Jul 13, 2023
1 parent 52c2419 commit f79bad5
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
@@ -1,3 +1,9 @@
v4.18.3
=======

* Properly preserve ``applicable_validators`` in extended validators.
Specifically, validators extending early drafts where siblings of ``$ref`` were ignored will properly ignore siblings in the extended validator.

v4.18.2
=======

Expand Down
17 changes: 17 additions & 0 deletions jsonschema/tests/test_validators.py
Expand Up @@ -285,6 +285,23 @@ def id_of(schema):
Derived = validators.extend(Original)
self.assertEqual(Derived.ID_OF(Derived.META_SCHEMA), correct_id)

def test_extend_applicable_validators(self):
"""
Extending a validator preserves its notion of applicable validators.
"""

schema = {
"$defs": {"test": {"type": "number"}},
"$ref": "#/$defs/test",
"maximum": 1
}

draft4 = validators.Draft4Validator(schema)
self.assertTrue(draft4.is_valid(37)) # as $ref ignores siblings

Derived = validators.extend(validators.Draft4Validator)
self.assertTrue(Derived(schema).is_valid(37))


class TestValidationErrorMessages(TestCase):
def message_for(self, instance, schema, *args, **kwargs):
Expand Down
3 changes: 3 additions & 0 deletions jsonschema/validators.py
Expand Up @@ -219,6 +219,8 @@ class Validator:
FORMAT_CHECKER = format_checker_arg
ID_OF = staticmethod(id_of)

_APPLICABLE_VALIDATORS = applicable_validators

schema: referencing.jsonschema.Schema = field(repr=reprlib.repr)
_ref_resolver = field(default=None, repr=False, alias="resolver")
format_checker: _format.FormatChecker | None = field(default=None)
Expand Down Expand Up @@ -570,6 +572,7 @@ def extend(
type_checker=type_checker,
format_checker=format_checker,
id_of=validator.ID_OF,
applicable_validators=validator._APPLICABLE_VALIDATORS,
)


Expand Down

0 comments on commit f79bad5

Please sign in to comment.