Skip to content

Commit

Permalink
fix: between and length validators
Browse files Browse the repository at this point in the history
  • Loading branch information
yozachar committed Mar 19, 2024
1 parent 3ac4baa commit d1a5b22
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 30 deletions.
33 changes: 8 additions & 25 deletions src/validators/between.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,42 +57,25 @@ def between(
If `value` is not in between the given conditions.
Raises:
ValueError: If both `min_val` and `max_val` are `None`,
or if `min_val` is greater than `max_val`.
TypeError: If there's a type mismatch before comparison.
(ValueError): If `min_val` is greater than `max_val`.
(TypeError): If there's a type mismatch during comparison.
Note:
- `PossibleValueTypes` = `TypeVar("PossibleValueTypes", int, float, str, datetime)`
- Either one of `min_val` or `max_val` must be provided.
> *New in version 0.2.0*.
- If neither `min_val` nor `max_val` is provided, result will always be `True`.
"""
if value is None:
return False

if min_val is max_val is None:
raise ValueError("At least one of either `min_val` or `max_val` must be specified")

if max_val is None:
max_val = AbsMax()
if min_val is None:
min_val = AbsMin()

if isinstance(min_val, AbsMin):
if type(value) is type(max_val):
return min_val <= value <= max_val
raise TypeError("`value` and `max_val` must be of same type")

if isinstance(max_val, AbsMax):
if type(value) is type(min_val):
return min_val <= value <= max_val
raise TypeError("`value` and `min_val` must be of same type")

if type(min_val) is type(max_val):
try:
if min_val > max_val:
raise ValueError("`min_val` cannot be more than `max_val`")
if type(value) is type(min_val): # or is type(max_val)
return min_val <= value <= max_val
raise TypeError("`value` and (`min_val` or `max_val`) must be of same type")
raise ValueError("`min_val` cannot be greater than `max_val`")
except TypeError as err:
raise TypeError("Comparison type mismatch") from err

raise TypeError("`value` and `min_val` and `max_val` must be of same type")
return min_val <= value <= max_val
15 changes: 12 additions & 3 deletions src/validators/length.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""Length."""

# standard
from typing import Union

# local
from .between import between
from .utils import validator


@validator
def length(value: str, /, *, min_val: int = 0, max_val: int = 0):
def length(value: str, /, *, min_val: Union[int, None] = None, max_val: Union[int, None] = None):
"""Return whether or not the length of given string is within a specified range.
Examples:
Expand All @@ -33,6 +36,12 @@ def length(value: str, /, *, min_val: int = 0, max_val: int = 0):
(ValidationError):
If `len(value)` is not in between the given conditions.
> *New in version 0.2.0*.
Raises:
(ValueError): If either `min_val` or `max_val` is negative.
"""
return between(len(value), min_val=min_val, max_val=max_val) if value else False
if min_val is not None and min_val < 0:
raise ValueError("Length cannot be negative. `min_val` is less than zero.")
if max_val is not None and max_val < 0:
raise ValueError("Length cannot be negative. `max_val` is less than zero.")

return bool(between(len(value), min_val=min_val, max_val=max_val))
1 change: 0 additions & 1 deletion tests/test_between.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ def test_returns_true_on_valid_range(value: T, min_val: T, max_val: T):
(None, 13, 14),
(12, 13, 14),
(12, None, 11),
(12, None, None),
(12, 13, None),
(12, "13.5", datetime(1970, 1, 1)),
("12", 20.5, "None"),
Expand Down
2 changes: 1 addition & 1 deletion tests/test_length.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@pytest.mark.parametrize(
("value", "min_val", "max_val"),
[("password", 2, 10), ("password", 0, 10), ("password", 8, 8)],
[("password", 2, None), ("password", None, None), ("password", 0, 10), ("password", 8, 8)],
)
def test_returns_true_on_valid_length(value: str, min_val: int, max_val: int):
"""Test returns true on valid length."""
Expand Down

0 comments on commit d1a5b22

Please sign in to comment.