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': {}})