Skip to content

Commit

Permalink
Fix vol.Remove not removing keys that do not validate (#515)
Browse files Browse the repository at this point in the history
* Fix vol.Remove not removing keys

fixes a regression from #479
blocks #479

* add test
  • Loading branch information
bdraco authored Jun 23, 2024
1 parent 2232c0e commit ca4006a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
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

0 comments on commit ca4006a

Please sign in to comment.