Skip to content

Commit 990bae7

Browse files
committed
Use an explicit default provided to validator_for in all cases.
Previously it was only used when $schema was not present, but clearly it should be used even when it is but the URI is not recognized. Combined with #981 this *hopefully* now handles the remaining downstream users with subclasses.
1 parent a8c3b14 commit 990bae7

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

CHANGELOG.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
v4.10.3
2+
-------
3+
4+
* ``jsonschema.validators.validator_for`` now properly uses the explicitly
5+
provided default validator even if the ``$schema`` URI is not found.
6+
17
v4.10.2
28
-------
39

jsonschema/tests/test_validators.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1906,6 +1906,10 @@ def test_does_not_warn_if_meta_schema_is_unspecified(self):
19061906
validators.validator_for(schema={}, default={})
19071907
self.assertFalse(w)
19081908

1909+
def test_validator_for_custom_default_with_schema(self):
1910+
schema, default = {"$schema": "mailto:[email protected]"}, object()
1911+
self.assertIs(validators.validator_for(schema, default), default)
1912+
19091913

19101914
class TestValidate(TestCase):
19111915
def assertUses(self, schema, Validator):

jsonschema/validators.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
exceptions,
2828
)
2929

30+
_UNSET = _utils.Unset()
31+
3032
_VALIDATORS: dict[str, typing.Any] = {}
3133
_META_SCHEMAS = _utils.URIDict()
3234
_VOCABULARIES: list[tuple[str, typing.Any]] = []
@@ -1078,7 +1080,7 @@ def validate(instance, schema, cls=None, *args, **kwargs):
10781080
raise error
10791081

10801082

1081-
def validator_for(schema, default=_LATEST_VERSION):
1083+
def validator_for(schema, default=_UNSET):
10821084
"""
10831085
Retrieve the validator class appropriate for validating the given schema.
10841086
@@ -1099,16 +1101,20 @@ def validator_for(schema, default=_LATEST_VERSION):
10991101
If unprovided, the default is to return the latest supported
11001102
draft.
11011103
"""
1104+
1105+
DefaultValidator = _LATEST_VERSION if default is _UNSET else default
1106+
11021107
if schema is True or schema is False or "$schema" not in schema:
1103-
return default
1108+
return DefaultValidator
11041109
if schema["$schema"] not in _META_SCHEMAS:
1105-
warn(
1106-
(
1107-
"The metaschema specified by $schema was not found. "
1108-
"Using the latest draft to validate, but this will raise "
1109-
"an error in the future."
1110-
),
1111-
DeprecationWarning,
1112-
stacklevel=2,
1113-
)
1114-
return _META_SCHEMAS.get(schema["$schema"], _LATEST_VERSION)
1110+
if default is _UNSET:
1111+
warn(
1112+
(
1113+
"The metaschema specified by $schema was not found. "
1114+
"Using the latest draft to validate, but this will raise "
1115+
"an error in the future."
1116+
),
1117+
DeprecationWarning,
1118+
stacklevel=2,
1119+
)
1120+
return _META_SCHEMAS.get(schema["$schema"], DefaultValidator)

0 commit comments

Comments
 (0)