Skip to content
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

Fix regression with ALLOW_EXTRA and Any validator #522

Merged
merged 2 commits into from
Jul 2, 2024
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
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
Loading