summaryrefslogtreecommitdiff
path: root/jsonschema
diff options
context:
space:
mode:
authorJulian Berman <Julian@GrayVines.com>2021-08-23 10:37:14 +0100
committerJulian Berman <Julian@GrayVines.com>2021-08-23 10:37:14 +0100
commitaca914c0508bca7bb862f060b503bb01a3b98d92 (patch)
treee3c536ab94709fce40e950e9d6a897e760fb30ff /jsonschema
parente38e5cfa97065cb5dbbcdf0e05ca2db3dbc06751 (diff)
downloadjsonschema-aca914c0508bca7bb862f060b503bb01a3b98d92.tar.gz
Fix the schema path used by prefixItems errors.
Also simplify its implementation, what it's doing is just zip.
Diffstat (limited to 'jsonschema')
-rw-r--r--jsonschema/_validators.py9
-rw-r--r--jsonschema/tests/test_validators.py51
2 files changed, 53 insertions, 7 deletions
diff --git a/jsonschema/_validators.py b/jsonschema/_validators.py
index 34baa09..7f05505 100644
--- a/jsonschema/_validators.py
+++ b/jsonschema/_validators.py
@@ -97,8 +97,7 @@ def additionalItems(validator, aI, instance, schema):
elif not aI and len(instance) > len(schema.get("items", [])):
error = "Additional items are not allowed (%s %s unexpected)"
yield ValidationError(
- error %
- extras_msg(instance[len(schema.get("items", [])):]),
+ error % extras_msg(instance[len(schema.get("items", [])):]),
)
@@ -450,7 +449,5 @@ def prefixItems(validator, prefixItems, instance, schema):
if not validator.is_type(instance, "array"):
return
- for k, v in enumerate(instance[:min(len(prefixItems), len(instance))]):
- yield from validator.descend(
- v, prefixItems[k], schema_path="prefixItems",
- )
+ for (index, each), subschema in zip(enumerate(instance), prefixItems):
+ yield from validator.descend(each, subschema, schema_path=index)
diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py
index 85f6b4f..2406cb1 100644
--- a/jsonschema/tests/test_validators.py
+++ b/jsonschema/tests/test_validators.py
@@ -418,7 +418,7 @@ class TestValidationErrorMessages(TestCase):
message = self.message_for(instance=[1, 2, 3], schema={"maxItems": 2})
self.assertEqual(message, "[1, 2, 3] is too long")
- def test_prefixItems(self):
+ def test_prefixItems_with_items(self):
message = self.message_for(
instance=[1, 2, "foo", 5],
schema={"items": False, "prefixItems": [{}, {}]},
@@ -1114,6 +1114,55 @@ class TestValidationErrorDetails(TestCase):
),
)
+ def test_prefixItems(self):
+ schema = {"prefixItems": [{"type": "string"}, {}, {}, {"maximum": 3}]}
+ validator = validators.Draft202012Validator(schema)
+ type_error, min_error = validator.iter_errors([1, 2, "foo", 5])
+ self.assertEqual(
+ (
+ type_error.message,
+ type_error.validator,
+ type_error.validator_value,
+ type_error.instance,
+ type_error.absolute_path,
+ type_error.schema,
+ type_error.schema_path,
+ type_error.json_path,
+ ),
+ (
+ "1 is not of type 'string'",
+ "type",
+ "string",
+ 1,
+ deque([]),
+ {"type": "string"},
+ deque(["prefixItems", 0, "type"]),
+ "$",
+ ),
+ )
+ self.assertEqual(
+ (
+ min_error.message,
+ min_error.validator,
+ min_error.validator_value,
+ min_error.instance,
+ min_error.absolute_path,
+ min_error.schema,
+ min_error.schema_path,
+ min_error.json_path,
+ ),
+ (
+ "5 is greater than the maximum of 3",
+ "maximum",
+ 3,
+ 5,
+ deque([]),
+ {"maximum": 3},
+ deque(["prefixItems", 3, "maximum"]),
+ "$",
+ ),
+ )
+
class MetaSchemaTestsMixin(object):
# TODO: These all belong upstream