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 vol.Remove not removing keys that do not validate #515

Merged
merged 2 commits into from
Jun 23, 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
6 changes: 3 additions & 3 deletions voluptuous/schema_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -435,11 +435,11 @@ def validate_mapping(path, iterable, out):

break
else:
if error:
errors.append(error)
elif remove_key:
if remove_key:
# remove key
continue
elif error:
errors.append(error)
elif self.extra == ALLOW_EXTRA:
out[key] = value
elif self.extra != REMOVE_EXTRA:
Expand Down
23 changes: 23 additions & 0 deletions voluptuous/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,29 @@ def test_remove():
assert out_ == [1, 2, 1.0, 4]


def test_remove_with_error():
def starts_with_dot(key: str) -> str:
"""Check if key starts with dot."""
if not key.startswith("."):
raise Invalid("Key does not start with .")
return key

def does_not_start_with_dot(key: str) -> str:
"""Check if key does not start with dot."""
if key.startswith("."):
raise Invalid("Key starts with .")
return key

schema = Schema(
{
Remove(All(str, starts_with_dot)): object,
does_not_start_with_dot: Any(None),
}
)
out_ = schema({".remove": None, "ok": None})
assert ".remove" not in out_ and "ok" in out_


def test_extra_empty_errors():
schema = Schema({'a': {Extra: object}}, required=True)
schema({'a': {}})
Expand Down
Loading