diff --git a/src/validators/between.py b/src/validators/between.py index 2fc3a0c9..0cd79d46 100644 --- a/src/validators/between.py +++ b/src/validators/between.py @@ -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 diff --git a/src/validators/length.py b/src/validators/length.py index 92a274d3..be60d06e 100644 --- a/src/validators/length.py +++ b/src/validators/length.py @@ -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: @@ -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)) diff --git a/tests/test_between.py b/tests/test_between.py index 6b1ccd72..bdb71551 100644 --- a/tests/test_between.py +++ b/tests/test_between.py @@ -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"), diff --git a/tests/test_length.py b/tests/test_length.py index 65737254..c9cd1dc1 100644 --- a/tests/test_length.py +++ b/tests/test_length.py @@ -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."""