Skip to content

Commit d19c32a

Browse files
committed
Respect false values for the validation error raise flag
1 parent 6ffd862 commit d19c32a

2 files changed

Lines changed: 40 additions & 5 deletions

File tree

‎src/validators/utils.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ def validator(func: Callable[..., Any]):
7272

7373
@wraps(func)
7474
def wrapper(*args: Any, **kwargs: Any):
75-
raise_validation_error = False
76-
if "r_ve" in kwargs:
77-
raise_validation_error = True
78-
del kwargs["r_ve"]
75+
raise_validation_error = bool(kwargs.pop("r_ve", False))
7976
if environ.get("RAISE_VALIDATION_ERROR", "False") == "True":
8077
raise_validation_error = True
8178

‎tests/test_validation_failure.py‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,45 @@
11
"""Test validation Failure."""
22

3+
# standard
4+
from typing import Any, Callable
5+
6+
# third-party
7+
import pytest
8+
39
# local
4-
from validators import between
10+
from validators import ValidationError, between, slug, uuid
11+
12+
13+
@pytest.mark.parametrize("function,value", [(slug, "invalid_"), (uuid, "not-a-uuid")])
14+
def test_false_raise_validation_error_flag(
15+
function: Callable[..., Any], value: str, monkeypatch: pytest.MonkeyPatch
16+
) -> None:
17+
"""A false flag returns validation errors instead of raising them."""
18+
monkeypatch.delenv("RAISE_VALIDATION_ERROR", raising=False)
19+
result = function(value, r_ve=False)
20+
assert isinstance(result, ValidationError)
21+
assert not result
22+
23+
24+
@pytest.mark.parametrize("function,value", [(slug, "invalid_"), (uuid, "not-a-uuid")])
25+
def test_environment_still_enables_raise_validation_error(
26+
function: Callable[..., Any], value: str, monkeypatch: pytest.MonkeyPatch
27+
) -> None:
28+
"""The environment option enables raising even when the call flag is false."""
29+
monkeypatch.setenv("RAISE_VALIDATION_ERROR", "True")
30+
with pytest.raises(ValidationError):
31+
function(value, r_ve=False)
32+
33+
34+
@pytest.mark.parametrize("function,value", [(slug, "invalid_"), (uuid, "not-a-uuid")])
35+
def test_true_raise_validation_error_flag(
36+
function: Callable[..., Any], value: str, monkeypatch: pytest.MonkeyPatch
37+
) -> None:
38+
"""A true call flag continues to raise validation errors."""
39+
monkeypatch.delenv("RAISE_VALIDATION_ERROR", raising=False)
40+
with pytest.raises(ValidationError):
41+
function(value, r_ve=True)
42+
543

644
failed_obj_repr = "ValidationError(func=between"
745

0 commit comments

Comments
 (0)