Skip to content

Fix error paths when additionalItems is used with items keyword #123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 25, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion jsonschema/_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ 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 index, item in enumerate(instance[len_items:], start=len_items):
for error in validator.descend(item, aI, path=index):
yield error
elif not aI and len(instance) > len(schema.get("items", [])):
Expand Down
17 changes: 17 additions & 0 deletions jsonschema/tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,23 @@ def test_additionalItems(self):
self.assertEqual(e1.validator, "type")
self.assertEqual(e2.validator, "minimum")

def test_additionalItems_with_items(self):
instance = ["foo", "bar", 1]
schema = {
"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), [1])
self.assertEqual(list(e2.path), [2])

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


class TestErrorTree(unittest.TestCase):
def setUp(self):
Expand Down