summaryrefslogtreecommitdiff
path: root/jsonschema
diff options
context:
space:
mode:
authorJulian Berman <Julian@GrayVines.com>2022-07-12 05:29:47 +0200
committerJulian Berman <Julian@GrayVines.com>2022-07-12 05:29:47 +0200
commit72c3200d9b14139c9ab3094d0da4c0dd68acafe3 (patch)
tree171c20e5a09ee446c5e381db9fb096438eb928c5 /jsonschema
parent8819f463d7e01b2f7e3f8ca3a010d38301aa7e69 (diff)
downloadjsonschema-72c3200d9b14139c9ab3094d0da4c0dd68acafe3.tar.gz
Fix best_match's `type` matching when it's an array.v4.7.2
Closes: #973
Diffstat (limited to 'jsonschema')
-rw-r--r--jsonschema/exceptions.py12
-rw-r--r--jsonschema/tests/test_exceptions.py27
2 files changed, 36 insertions, 3 deletions
diff --git a/jsonschema/exceptions.py b/jsonschema/exceptions.py
index 07beb2d..927468f 100644
--- a/jsonschema/exceptions.py
+++ b/jsonschema/exceptions.py
@@ -143,11 +143,17 @@ class _Error(Exception):
def _matches_type(self):
try:
- expected_type = self.schema["type"]
+ expected = self.schema["type"]
except (KeyError, TypeError):
return False
- else:
- return self._type_checker.is_type(self.instance, expected_type)
+
+ if isinstance(expected, str):
+ return self._type_checker.is_type(self.instance, expected)
+
+ return any(
+ self._type_checker.is_type(self.instance, expected_type)
+ for expected_type in expected
+ )
class ValidationError(_Error):
diff --git a/jsonschema/tests/test_exceptions.py b/jsonschema/tests/test_exceptions.py
index c3c5255..d641ac5 100644
--- a/jsonschema/tests/test_exceptions.py
+++ b/jsonschema/tests/test_exceptions.py
@@ -158,6 +158,33 @@ class TestBestMatch(TestCase):
best = self.best_match_of(instance={"foo": "bar"}, schema=reordered)
self.assertEqual(best.validator, "minLength")
+ def test_it_prioritizes_matching_union_types(self):
+ schema = {
+ "properties": {
+ "foo": {
+ "anyOf": [
+ {"type": ["array", "object"], "minItems": 2},
+ {"type": ["integer", "string"], "minLength": 10},
+ ],
+ },
+ },
+ }
+ best = self.best_match_of(instance={"foo": "bar"}, schema=schema)
+ self.assertEqual(best.validator, "minLength")
+
+ reordered = {
+ "properties": {
+ "foo": {
+ "anyOf": [
+ {"type": "string", "minLength": 10},
+ {"type": "array", "minItems": 2},
+ ],
+ },
+ },
+ }
+ best = self.best_match_of(instance={"foo": "bar"}, schema=reordered)
+ self.assertEqual(best.validator, "minLength")
+
def test_boolean_schemas(self):
schema = {"properties": {"foo": False}}
best = self.best_match_of(instance={"foo": "bar"}, schema=schema)