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 bug with Any validator and REMOVE_EXTRA #524

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 4 additions & 2 deletions voluptuous/schema_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,11 +364,13 @@ def validate_mapping(path, iterable, out):
continue
elif self.extra == ALLOW_EXTRA:
out[key] = value
elif self.extra == REMOVE_EXTRA:
# ignore the key so it's removed from output
continue
elif error:
errors.append(error)
elif self.extra != REMOVE_EXTRA:
else:
errors.append(er.Invalid('extra keys not allowed', key_path))
# else REMOVE_EXTRA: ignore the key so it's removed from output

# for any required keys left that weren't found and don't have defaults:
for key in required_keys:
Expand Down
79 changes: 76 additions & 3 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import pytest

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


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

result = schema(
{
"name": "one",
"domain": "two",
"additional_key": "extra",
}
)

assert result == {
"name": "one",
"domain": "two",
"additional_key": "extra",
}


def test_any_with_extra_remove():
schema = Schema(
{
Any("name", "area"): str,
"domain": str,
},
extra=REMOVE_EXTRA,
)

result = schema(
{
"name": "one",
"domain": "two",
"additional_key": "extra",
}
)

assert result == {
"name": "one",
"domain": "two",
}


def test_any_with_extra_prevent():
schema = Schema(
{
Any("name", "area"): str,
"domain": str,
},
extra=PREVENT_EXTRA,
)

with pytest.raises(MultipleInvalid) as ctx:
schema(
{
"name": "one",
"domain": "two",
"additional_key": "extra",
}
)

assert len(ctx.value.errors) == 1
assert str(ctx.value.errors[0]) == "not a valid value @ data['additional_key']"


def test_any_with_extra_none():
schema = Schema(
{
Any("name", "area"): str,
"domain": str,
},
)

with pytest.raises(MultipleInvalid) as ctx:
schema(
{
"name": "one",
"domain": "two",
"additional_key": "extra",
}
)

assert len(ctx.value.errors) == 1
assert str(ctx.value.errors[0]) == "not a valid value @ data['additional_key']"


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