diff options
author | Julian Berman <Julian@GrayVines.com> | 2021-08-24 10:21:41 +0100 |
---|---|---|
committer | Julian Berman <Julian@GrayVines.com> | 2021-08-24 10:21:41 +0100 |
commit | 53694e776adfc0b3f8b49101d1477f091c1dc0d9 (patch) | |
tree | 5a6590b272280aad273a6398a8b70ee1609e988a | |
parent | 60d689ca25012cc223166407f73348c86d13a1b4 (diff) | |
download | jsonschema-53694e776adfc0b3f8b49101d1477f091c1dc0d9.tar.gz |
Fix items' instance path as well.
-rw-r--r-- | jsonschema/_validators.py | 9 | ||||
-rw-r--r-- | 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 |