Skip to content

Commit

Permalink
Remove value enumeration when validating empty list (#397)
Browse files Browse the repository at this point in the history
The behaviour seems to have been introduced in commit
95489bd that modified the schema of an empty list in order
to provide a consistent behaviour between Schema({}) and Schema([]).

The commit introduced an enumeration of the incorrect values rather
than the expected behaviour of showing the key with the incorrect
value.

This commit provides the expected behaviour, and is also consistent
with the 'pathless' behaviour currently implemented, meaning that

- Schema([])([1]) -> 'not a valid value @ data[1]'
- Schema({'key': []})({'key': [1]) -> 'not a valid dictionary value @
data['key'].

Finally, as new unit test is provided to ensure that the new behaviour
remains intact.
  • Loading branch information
monopolis committed Oct 12, 2020
1 parent b1aaa3e commit 10a78d0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions voluptuous/schema_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,11 @@ def validate_sequence(path, data):
if not isinstance(data, seq_type):
raise er.SequenceTypeInvalid('expected a %s' % seq_type_name, path)

# Empty seq schema, allow any data.
# Empty seq schema, reject any data.
if not schema:
if data:
raise er.MultipleInvalid([
er.ValueInvalid('not a valid value', [value]) for value in data
er.ValueInvalid('not a valid value', path if path else data)
])
return data

Expand Down
14 changes: 14 additions & 0 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1528,3 +1528,17 @@ def test_any_with_discriminant():
assert_equal(str(e), 'expected bool for dictionary value @ data[\'implementation\'][\'c-value\']')
else:
assert False, "Did not raise correct Invalid"


def test_empty_list_raises_error_of_key_not_values():
""" https://github.com/alecthomas/voluptuous/issues/397 """
schema = Schema({
Required('variables', default=[]): []
})

try:
schema({'variables': ['x']})
except MultipleInvalid as e:
assert_equal(str(e), "not a valid value for dictionary value @ data['variables']")
else:
assert False, "Did not raise correct Invalid"

0 comments on commit 10a78d0

Please sign in to comment.