From 53694e776adfc0b3f8b49101d1477f091c1dc0d9 Mon Sep 17 00:00:00 2001 From: Julian Berman Date: Tue, 24 Aug 2021 10:21:41 +0100 Subject: Fix items' instance path as well. --- jsonschema/_validators.py | 9 ++++--- jsonschema/tests/test_validators.py | 52 +++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) diff --git a/jsonschema/_validators.py b/jsonschema/_validators.py index 49f0145..4adc3c2 100644 --- a/jsonschema/_validators.py +++ b/jsonschema/_validators.py @@ -1,5 +1,4 @@ from fractions import Fraction -from itertools import islice from urllib.parse import urldefrag, urljoin import re @@ -72,8 +71,12 @@ def items(validator, items, instance, schema): message = f"Expected at most {prefix} items, but found {len(instance)}" yield ValidationError(message) else: - for item in islice(instance, prefix, None): - yield from validator.descend(instance=item, schema=items) + for index in range(prefix, len(instance)): + yield from validator.descend( + instance=instance[index], + schema=items, + path=index, + ) def additionalItems(validator, aI, instance, schema): diff --git a/jsonschema/tests/test_validators.py b/jsonschema/tests/test_validators.py index 7f5f387..026ee85 100644 --- a/jsonschema/tests/test_validators.py +++ b/jsonschema/tests/test_validators.py @@ -1163,6 +1163,58 @@ class TestValidationErrorDetails(TestCase): ), ) + def test_prefixItems_with_items(self): + schema = { + "items": {"type": "string"}, + "prefixItems": [{}], + } + validator = validators.Draft202012Validator(schema) + e1, e2 = validator.iter_errors(["foo", 2, "bar", 4, "baz"]) + self.assertEqual( + ( + e1.message, + e1.validator, + e1.validator_value, + e1.instance, + e1.absolute_path, + e1.schema, + e1.schema_path, + e1.json_path, + ), + ( + "2 is not of type 'string'", + "type", + "string", + 2, + deque([1]), + {"type": "string"}, + deque(["items", "type"]), + "$[1]", + ), + ) + self.assertEqual( + ( + e2.message, + e2.validator, + e2.validator_value, + e2.instance, + e2.absolute_path, + e2.schema, + e2.schema_path, + e2.json_path, + ), + ( + "4 is not of type 'string'", + "type", + "string", + 4, + deque([3]), + {"type": "string"}, + deque(["items", "type"]), + "$[3]", + ), + ) + class MetaSchemaTestsMixin(object): # TODO: These all belong upstream -- cgit v1.2.1