Skip to content

Commit

Permalink
Fix regression with ALLOW_EXTRA and Any validator (alecthomas#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdce8p authored Jul 2, 2024
1 parent 23e1783 commit b24b80d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
1 change: 1 addition & 0 deletions voluptuous/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
... 'Users': {'snmp_community': 'monkey'}}}}
True
"""

# flake8: noqa
# fmt: off
from voluptuous.schema_builder import *
Expand Down
5 changes: 2 additions & 3 deletions voluptuous/schema_builder.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

# fmt: off
from __future__ import annotations

Expand Down Expand Up @@ -363,10 +362,10 @@ def validate_mapping(path, iterable, out):
if remove_key:
# remove key
continue
elif error:
errors.append(error)
elif self.extra == ALLOW_EXTRA:
out[key] = value
elif error:
errors.append(error)
elif self.extra != REMOVE_EXTRA:
errors.append(er.Invalid('extra keys not allowed', key_path))
# else REMOVE_EXTRA: ignore the key so it's removed from output
Expand Down
17 changes: 17 additions & 0 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1704,6 +1704,23 @@ def as_int(a):
assert str(ctx.value.errors[1]) == "expecting a number @ data['four']"


def test_key3():
schema = Schema(
{
Any("name", "area"): str,
"domain": str,
},
extra=ALLOW_EXTRA,
)
schema(
{
"name": "one",
"domain": "two",
"additional_key": "extra",
}
)


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

Expand Down

0 comments on commit b24b80d

Please sign in to comment.