forked from python-validators/validators
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbetween.py
More file actions
61 lines (47 loc) · 1.54 KB
/
between.py
File metadata and controls
61 lines (47 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
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
from .extremes import Min, Max
from .utils import validator
@validator
def between(value, min=None, max=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`_.
.. _WTForms NumberRange validator:
https://github.com/wtforms/wtforms/blob/master/wtforms/validators.py
Examples::
>>> from datetime import datetime
>>> between(5, min=2)
True
>>> between(13.2, min=13, max=14)
True
>>> between(500, max=400)
ValidationFailure(func=between, args=...)
>>> between(
... datetime(2000, 11, 11),
... min=datetime(1999, 11, 11)
... )
True
:param min:
The minimum required value of the number. If not provided, minimum
value will not be checked.
:param max:
The maximum value of the number. If not provided, maximum value
will not be checked.
.. versionadded:: 0.2
"""
if min is None and max is None:
raise AssertionError(
'At least one of `min` or `max` must be specified.'
)
if min is None:
min = Min
if max is None:
max = Max
try:
min_gt_max = min > max
except TypeError:
min_gt_max = max < min
if min_gt_max:
raise AssertionError('`min` cannot be more than `max`.')
return min <= value and max >= value