From ca4006a6838be83f682c594bc15f7ea3d79cc80f Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 23 Jun 2024 16:55:31 -0500 Subject: [PATCH] Fix vol.Remove not removing keys that do not validate (#515) * Fix vol.Remove not removing keys fixes a regression from #479 blocks https://github.com/alecthomas/voluptuous/pull/479 * add test --- voluptuous/schema_builder.py | 6 +++--- voluptuous/tests/tests.py | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/voluptuous/schema_builder.py b/voluptuous/schema_builder.py index ca134ca..2bf6954 100644 --- a/voluptuous/schema_builder.py +++ b/voluptuous/schema_builder.py @@ -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: diff --git a/voluptuous/tests/tests.py b/voluptuous/tests/tests.py index f6d6784..8fa1883 100644 --- a/voluptuous/tests/tests.py +++ b/voluptuous/tests/tests.py @@ -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': {}})