Skip to content

Commit 4a9c8f8

Browse files
authored
Fix bug with Any validator and REMOVE_EXTRA (#524)
1 parent a7a55f8 commit 4a9c8f8

2 files changed

Lines changed: 80 additions & 5 deletions

File tree

voluptuous/schema_builder.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,11 +364,13 @@ def validate_mapping(path, iterable, out):
364364
continue
365365
elif self.extra == ALLOW_EXTRA:
366366
out[key] = value
367+
elif self.extra == REMOVE_EXTRA:
368+
# ignore the key so it's removed from output
369+
continue
367370
elif error:
368371
errors.append(error)
369-
elif self.extra != REMOVE_EXTRA:
372+
else:
370373
errors.append(er.Invalid('extra keys not allowed', key_path))
371-
# else REMOVE_EXTRA: ignore the key so it's removed from output
372374

373375
# for any required keys left that weren't found and don't have defaults:
374376
for key in required_keys:

voluptuous/tests/tests.py

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pytest
99

1010
from voluptuous import (
11-
ALLOW_EXTRA, PREVENT_EXTRA, All, AllInvalid, Any, Clamp, Coerce, Contains,
11+
ALLOW_EXTRA, PREVENT_EXTRA, REMOVE_EXTRA, All, AllInvalid, Any, Clamp, Coerce, Contains,
1212
ContainsInvalid, Date, Datetime, Email, EmailInvalid, Equal, ExactSequence,
1313
Exclusive, Extra, FqdnUrl, In, Inclusive, InInvalid, Invalid, IsDir, IsFile, Length,
1414
Literal, LiteralInvalid, Marker, Match, MatchInvalid, Maybe, MultipleInvalid, NotIn,
@@ -1704,22 +1704,95 @@ def as_int(a):
17041704
assert str(ctx.value.errors[1]) == "expecting a number @ data['four']"
17051705

17061706

1707-
def test_key3():
1707+
def test_any_with_extra_allow():
17081708
schema = Schema(
17091709
{
17101710
Any("name", "area"): str,
17111711
"domain": str,
17121712
},
17131713
extra=ALLOW_EXTRA,
17141714
)
1715-
schema(
1715+
1716+
result = schema(
1717+
{
1718+
"name": "one",
1719+
"domain": "two",
1720+
"additional_key": "extra",
1721+
}
1722+
)
1723+
1724+
assert result == {
1725+
"name": "one",
1726+
"domain": "two",
1727+
"additional_key": "extra",
1728+
}
1729+
1730+
1731+
def test_any_with_extra_remove():
1732+
schema = Schema(
1733+
{
1734+
Any("name", "area"): str,
1735+
"domain": str,
1736+
},
1737+
extra=REMOVE_EXTRA,
1738+
)
1739+
1740+
result = schema(
17161741
{
17171742
"name": "one",
17181743
"domain": "two",
17191744
"additional_key": "extra",
17201745
}
17211746
)
17221747

1748+
assert result == {
1749+
"name": "one",
1750+
"domain": "two",
1751+
}
1752+
1753+
1754+
def test_any_with_extra_prevent():
1755+
schema = Schema(
1756+
{
1757+
Any("name", "area"): str,
1758+
"domain": str,
1759+
},
1760+
extra=PREVENT_EXTRA,
1761+
)
1762+
1763+
with pytest.raises(MultipleInvalid) as ctx:
1764+
schema(
1765+
{
1766+
"name": "one",
1767+
"domain": "two",
1768+
"additional_key": "extra",
1769+
}
1770+
)
1771+
1772+
assert len(ctx.value.errors) == 1
1773+
assert str(ctx.value.errors[0]) == "not a valid value @ data['additional_key']"
1774+
1775+
1776+
def test_any_with_extra_none():
1777+
schema = Schema(
1778+
{
1779+
Any("name", "area"): str,
1780+
"domain": str,
1781+
},
1782+
)
1783+
1784+
with pytest.raises(MultipleInvalid) as ctx:
1785+
schema(
1786+
{
1787+
"name": "one",
1788+
"domain": "two",
1789+
"additional_key": "extra",
1790+
}
1791+
)
1792+
1793+
assert len(ctx.value.errors) == 1
1794+
assert str(ctx.value.errors[0]) == "not a valid value @ data['additional_key']"
1795+
17231796

17241797
def test_coerce_enum():
17251798
"""Test Coerce Enum"""

0 commit comments

Comments
 (0)