Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions jsonschema/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,10 @@ def additionalItems(validator, aI, instance, schema):
):
return

len_items = len(schema.get("items", []))
if validator.is_type(aI, "object"):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can provide len_items here as the start argument to enumerate rather than adding manually.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool, didn't know that.

for index, item in enumerate(instance[len(schema.get("items", [])):]):
for error in validator.descend(item, aI, path=index):
for index, item in enumerate(instance[len_items:]):
for error in validator.descend(item, aI, path=index+len_items):
yield error
elif not aI and len(instance) > len(schema.get("items", [])):
error = "Additional items are not allowed (%s %s unexpected)"
Expand Down
8 changes: 4 additions & 4 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,18 +484,18 @@ def test_patternProperties(self):
self.assertEqual(e2.validator, "minimum")

def test_additionalItems(self):
instance = ["foo", 1]
instance = ["foo", "bar", 1]
schema = {
"items": [],
"items": [{}],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was correct as-is, wasn't it, it just covered a case with empty items? If so we should leave the old test case and just add this as a new one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, will do.

"additionalItems" : {"type": "integer", "minimum": 5}
}

validator = Draft3Validator(schema)
errors = validator.iter_errors(instance)
e1, e2 = sorted_errors(errors)

self.assertEqual(list(e1.path), [0])
self.assertEqual(list(e2.path), [1])
self.assertEqual(list(e1.path), [1])
self.assertEqual(list(e2.path), [2])

self.assertEqual(e1.validator, "type")
self.assertEqual(e2.validator, "minimum")
Expand Down