Skip to content

Commit f79bad5

Browse files
committed
Properly preserve applicable_validators in extended validators.
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
1 parent 52c2419 commit f79bad5

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

CHANGELOG.rst

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
v4.18.3
2+
=======
3+
4+
* Properly preserve ``applicable_validators`` in extended validators.
5+
Specifically, validators extending early drafts where siblings of ``$ref`` were ignored will properly ignore siblings in the extended validator.
6+
17
v4.18.2
28
=======
39

jsonschema/tests/test_validators.py

+17
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,23 @@ def id_of(schema):
285285
Derived = validators.extend(Original)
286286
self.assertEqual(Derived.ID_OF(Derived.META_SCHEMA), correct_id)
287287

288+
def test_extend_applicable_validators(self):
289+
"""
290+
Extending a validator preserves its notion of applicable validators.
291+
"""
292+
293+
schema = {
294+
"$defs": {"test": {"type": "number"}},
295+
"$ref": "#/$defs/test",
296+
"maximum": 1
297+
}
298+
299+
draft4 = validators.Draft4Validator(schema)
300+
self.assertTrue(draft4.is_valid(37)) # as $ref ignores siblings
301+
302+
Derived = validators.extend(validators.Draft4Validator)
303+
self.assertTrue(Derived(schema).is_valid(37))
304+
288305

289306
class TestValidationErrorMessages(TestCase):
290307
def message_for(self, instance, schema, *args, **kwargs):

jsonschema/validators.py

+3
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,8 @@ class Validator:
219219
FORMAT_CHECKER = format_checker_arg
220220
ID_OF = staticmethod(id_of)
221221

222+
_APPLICABLE_VALIDATORS = applicable_validators
223+
222224
schema: referencing.jsonschema.Schema = field(repr=reprlib.repr)
223225
_ref_resolver = field(default=None, repr=False, alias="resolver")
224226
format_checker: _format.FormatChecker | None = field(default=None)
@@ -570,6 +572,7 @@ def extend(
570572
type_checker=type_checker,
571573
format_checker=format_checker,
572574
id_of=validator.ID_OF,
575+
applicable_validators=validator._APPLICABLE_VALIDATORS,
573576
)
574577

575578

0 commit comments

Comments
 (0)