Skip to content

Commit 53694e7

Browse files
committed
Fix items' instance path as well.
1 parent 60d689c commit 53694e7

File tree

2 files changed

+58
-3
lines changed

2 files changed

+58
-3
lines changed

jsonschema/_validators.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from fractions import Fraction
2-
from itertools import islice
32
from urllib.parse import urldefrag, urljoin
43
import re
54

@@ -72,8 +71,12 @@ def items(validator, items, instance, schema):
7271
message = f"Expected at most {prefix} items, but found {len(instance)}"
7372
yield ValidationError(message)
7473
else:
75-
for item in islice(instance, prefix, None):
76-
yield from validator.descend(instance=item, schema=items)
74+
for index in range(prefix, len(instance)):
75+
yield from validator.descend(
76+
instance=instance[index],
77+
schema=items,
78+
path=index,
79+
)
7780

7881

7982
def additionalItems(validator, aI, instance, schema):

jsonschema/tests/test_validators.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,6 +1163,58 @@ def test_prefixItems(self):
11631163
),
11641164
)
11651165

1166+
def test_prefixItems_with_items(self):
1167+
schema = {
1168+
"items": {"type": "string"},
1169+
"prefixItems": [{}],
1170+
}
1171+
validator = validators.Draft202012Validator(schema)
1172+
e1, e2 = validator.iter_errors(["foo", 2, "bar", 4, "baz"])
1173+
self.assertEqual(
1174+
(
1175+
e1.message,
1176+
e1.validator,
1177+
e1.validator_value,
1178+
e1.instance,
1179+
e1.absolute_path,
1180+
e1.schema,
1181+
e1.schema_path,
1182+
e1.json_path,
1183+
),
1184+
(
1185+
"2 is not of type 'string'",
1186+
"type",
1187+
"string",
1188+
2,
1189+
deque([1]),
1190+
{"type": "string"},
1191+
deque(["items", "type"]),
1192+
"$[1]",
1193+
),
1194+
)
1195+
self.assertEqual(
1196+
(
1197+
e2.message,
1198+
e2.validator,
1199+
e2.validator_value,
1200+
e2.instance,
1201+
e2.absolute_path,
1202+
e2.schema,
1203+
e2.schema_path,
1204+
e2.json_path,
1205+
),
1206+
(
1207+
"4 is not of type 'string'",
1208+
"type",
1209+
"string",
1210+
4,
1211+
deque([3]),
1212+
{"type": "string"},
1213+
deque(["items", "type"]),
1214+
"$[3]",
1215+
),
1216+
)
1217+
11661218

11671219
class MetaSchemaTestsMixin(object):
11681220
# TODO: These all belong upstream

0 commit comments

Comments
 (0)