Validate that a number is between minimum and/or maximum value.
This will work with any comparable type, such as floats, decimals and dates
not just integers. This validator is originally based on WTForms-NumberRange-Validator.
Examples:
>>> from datetime import datetime
>>> between(5, min_val=2)
# Output: True
>>> between(13.2, min_val=13, max_val=14)
# Output: True
>>> between(500, max_val=400)
# Output: ValidationError(func=between, args=...)
>>> between(
... datetime(2000, 11, 11),
... min_val=datetime(1999, 11, 11)
... )
# Output: True
Parameters:
| Name |
Type |
Description |
Default |
value |
PossibleValueTypes
|
Value which is to be compared.
|
required
|
min_val |
Union[PossibleValueTypes, AbsMin, None]
|
The minimum required value of the number.
If not provided, minimum value will not be checked.
|
None
|
max_val |
Union[PossibleValueTypes, AbsMax, None]
|
The maximum value of the number.
If not provided, maximum value will not be checked.
|
None
|
Returns:
| Type |
Description |
Literal[True]
|
If value is in between the given conditions.
|
ValidationError
|
If value is not in between the given conditions.
|
Raises:
| Type |
Description |
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.
|
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.
Source code in /opt/hostedtoolcache/Python/3.11.7/x64/lib/python3.11/site-packages/validators/between.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98 | @validator
def between(
value: PossibleValueTypes,
/,
*,
min_val: Union[PossibleValueTypes, AbsMin, None] = None,
max_val: Union[PossibleValueTypes, AbsMax, None] = None,
):
"""Validate that a number is between minimum and/or maximum value.
This will work with any comparable type, such as floats, decimals and dates
not just integers. This validator is originally based on [WTForms-NumberRange-Validator][1].
[1]: https://github.com/wtforms/wtforms/blob/master/src/wtforms/validators.py#L166-L220
Examples:
>>> from datetime import datetime
>>> between(5, min_val=2)
# Output: True
>>> between(13.2, min_val=13, max_val=14)
# Output: True
>>> between(500, max_val=400)
# Output: ValidationError(func=between, args=...)
>>> between(
... datetime(2000, 11, 11),
... min_val=datetime(1999, 11, 11)
... )
# Output: True
Args:
value:
Value which is to be compared.
min_val:
The minimum required value of the number.
If not provided, minimum value will not be checked.
max_val:
The maximum value of the number.
If not provided, maximum value will not be checked.
Returns:
(Literal[True]):
If `value` is in between the given conditions.
(ValidationError):
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.
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 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):
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 TypeError("`value` and `min_val` and `max_val` must be of same type")
|