summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Berman <Julian@GrayVines.com>2022-08-18 09:50:07 +0300
committerJulian Berman <Julian@GrayVines.com>2022-08-18 09:50:07 +0300
commit990bae77c368fe885492dede9ef5adfbb950924c (patch)
tree89fccaa6d1feb437d0f5662d6e1ed68e1c72cd1f
parenta8c3b140a0134aa8c07fd7720d4e40f1bf759545 (diff)
downloadjsonschema-990bae77c368fe885492dede9ef5adfbb950924c.tar.gz
Use an explicit default provided to validator_for in all cases.v4.10.3
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.
-rw-r--r--CHANGELOG.rst6
-rw-r--r--jsonschema/tests/test_validators.py4
-rw-r--r--jsonschema/validators.py30
3 files changed, 28 insertions, 12 deletions
diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 59fe78c..61668ae 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -1,3 +1,9 @@
+v4.10.3
+-------
+
+* ``jsonschema.validators.validator_for`` now properly uses the explicitly
+ provided default validator even if the ``$schema`` URI is not found.
+
v4.10.2
-------
diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py
index e6452d8..4ebc8db 100644
--- a/jsonschema/tests/test_validators.py
+++ b/jsonschema/tests/test_validators.py
@@ -1906,6 +1906,10 @@ class TestValidatorFor(TestCase):
validators.validator_for(schema={}, default={})
self.assertFalse(w)
+ def test_validator_for_custom_default_with_schema(self):
+ schema, default = {"$schema": "mailto:foo@example.com"}, object()
+ self.assertIs(validators.validator_for(schema, default), default)
+
class TestValidate(TestCase):
def assertUses(self, schema, Validator):
diff --git a/jsonschema/validators.py b/jsonschema/validators.py
index ab510d3..79a8da3 100644
--- a/jsonschema/validators.py
+++ b/jsonschema/validators.py
@@ -27,6 +27,8 @@ from jsonschema import (
exceptions,
)
+_UNSET = _utils.Unset()
+
_VALIDATORS: dict[str, typing.Any] = {}
_META_SCHEMAS = _utils.URIDict()
_VOCABULARIES: list[tuple[str, typing.Any]] = []
@@ -1078,7 +1080,7 @@ def validate(instance, schema, cls=None, *args, **kwargs):
raise error
-def validator_for(schema, default=_LATEST_VERSION):
+def validator_for(schema, default=_UNSET):
"""
Retrieve the validator class appropriate for validating the given schema.
@@ -1099,16 +1101,20 @@ def validator_for(schema, default=_LATEST_VERSION):
If unprovided, the default is to return the latest supported
draft.
"""
+
+ DefaultValidator = _LATEST_VERSION if default is _UNSET else default
+
if schema is True or schema is False or "$schema" not in schema:
- return default
+ return DefaultValidator
if schema["$schema"] not in _META_SCHEMAS:
- warn(
- (
- "The metaschema specified by $schema was not found. "
- "Using the latest draft to validate, but this will raise "
- "an error in the future."
- ),
- DeprecationWarning,
- stacklevel=2,
- )
- return _META_SCHEMAS.get(schema["$schema"], _LATEST_VERSION)
+ if default is _UNSET:
+ warn(
+ (
+ "The metaschema specified by $schema was not found. "
+ "Using the latest draft to validate, but this will raise "
+ "an error in the future."
+ ),
+ DeprecationWarning,
+ stacklevel=2,
+ )
+ return _META_SCHEMAS.get(schema["$schema"], DefaultValidator)