Skip to content

Commit aca914c

Browse files
committed
Fix the schema path used by prefixItems errors.
Also simplify its implementation, what it's doing is just zip.
1 parent e38e5cf commit aca914c

File tree

2 files changed

+53
-7
lines changed

2 files changed

+53
-7
lines changed

jsonschema/_validators.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,7 @@ def additionalItems(validator, aI, instance, schema):
9797
elif not aI and len(instance) > len(schema.get("items", [])):
9898
error = "Additional items are not allowed (%s %s unexpected)"
9999
yield ValidationError(
100-
error %
101-
extras_msg(instance[len(schema.get("items", [])):]),
100+
error % extras_msg(instance[len(schema.get("items", [])):]),
102101
)
103102

104103

@@ -450,7 +449,5 @@ def prefixItems(validator, prefixItems, instance, schema):
450449
if not validator.is_type(instance, "array"):
451450
return
452451

453-
for k, v in enumerate(instance[:min(len(prefixItems), len(instance))]):
454-
yield from validator.descend(
455-
v, prefixItems[k], schema_path="prefixItems",
456-
)
452+
for (index, each), subschema in zip(enumerate(instance), prefixItems):
453+
yield from validator.descend(each, subschema, schema_path=index)

jsonschema/tests/test_validators.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ def test_maxItems(self):
418418
message = self.message_for(instance=[1, 2, 3], schema={"maxItems": 2})
419419
self.assertEqual(message, "[1, 2, 3] is too long")
420420

421-
def test_prefixItems(self):
421+
def test_prefixItems_with_items(self):
422422
message = self.message_for(
423423
instance=[1, 2, "foo", 5],
424424
schema={"items": False, "prefixItems": [{}, {}]},
@@ -1114,6 +1114,55 @@ def test_ref(self):
11141114
),
11151115
)
11161116

1117+
def test_prefixItems(self):
1118+
schema = {"prefixItems": [{"type": "string"}, {}, {}, {"maximum": 3}]}
1119+
validator = validators.Draft202012Validator(schema)
1120+
type_error, min_error = validator.iter_errors([1, 2, "foo", 5])
1121+
self.assertEqual(
1122+
(
1123+
type_error.message,
1124+
type_error.validator,
1125+
type_error.validator_value,
1126+
type_error.instance,
1127+
type_error.absolute_path,
1128+
type_error.schema,
1129+
type_error.schema_path,
1130+
type_error.json_path,
1131+
),
1132+
(
1133+
"1 is not of type 'string'",
1134+
"type",
1135+
"string",
1136+
1,
1137+
deque([]),
1138+
{"type": "string"},
1139+
deque(["prefixItems", 0, "type"]),
1140+
"$",
1141+
),
1142+
)
1143+
self.assertEqual(
1144+
(
1145+
min_error.message,
1146+
min_error.validator,
1147+
min_error.validator_value,
1148+
min_error.instance,
1149+
min_error.absolute_path,
1150+
min_error.schema,
1151+
min_error.schema_path,
1152+
min_error.json_path,
1153+
),
1154+
(
1155+
"5 is greater than the maximum of 3",
1156+
"maximum",
1157+
3,
1158+
5,
1159+
deque([]),
1160+
{"maximum": 3},
1161+
deque(["prefixItems", 3, "maximum"]),
1162+
"$",
1163+
),
1164+
)
1165+
11171166

11181167
class MetaSchemaTestsMixin(object):
11191168
# TODO: These all belong upstream

0 commit comments

Comments
 (0)