|
1 | | -import six |
2 | 1 | from .email import email |
3 | 2 | from .extremes import Min, Max |
4 | 3 | from .ip_address import ipv4, ipv6 |
| 4 | +from .length import length |
5 | 5 | from .mac_address import mac_address |
| 6 | +from .range import range |
| 7 | +from .truthy import truthy |
6 | 8 | from .utils import ValidationFailure, validator |
7 | 9 | from .url import url |
8 | 10 | from .uuid import uuid |
|
12 | 14 | ipv4, |
13 | 15 | ipv6, |
14 | 16 | email, |
| 17 | + length, |
15 | 18 | mac_address, |
| 19 | + range, |
| 20 | + truthy, |
16 | 21 | url, |
17 | 22 | uuid, |
18 | 23 | validator, |
|
23 | 28 |
|
24 | 29 |
|
25 | 30 | __version__ = '0.2' |
26 | | - |
27 | | - |
28 | | -@validator |
29 | | -def truthy(value): |
30 | | - """ |
31 | | - Validates that given value is not a falsey value. |
32 | | -
|
33 | | - This validator is based on `WTForms DataRequired validator`_. |
34 | | -
|
35 | | - .. _WTForms DataRequired validator: |
36 | | - https://github.com/wtforms/wtforms/blob/master/wtforms/validators.py |
37 | | -
|
38 | | - Examples:: |
39 | | -
|
40 | | -
|
41 | | - >>> assert truthy(1) |
42 | | -
|
43 | | - >>> assert truthy('someone') |
44 | | -
|
45 | | - >>> assert not truthy(0) |
46 | | -
|
47 | | - >>> assert not truthy(' ') |
48 | | -
|
49 | | - >>> assert not truthy(False) |
50 | | -
|
51 | | - >>> assert not truthy(None) |
52 | | -
|
53 | | - .. versionadded:: 0.2 |
54 | | - """ |
55 | | - return ( |
56 | | - not value or |
57 | | - isinstance(value, six.string_types) and not value.strip() |
58 | | - ) |
59 | | - |
60 | | - |
61 | | -@validator |
62 | | -def range(value, min=None, max=None): |
63 | | - """ |
64 | | - Validates that a number is of a minimum and/or maximum value, inclusive. |
65 | | - This will work with any comparable type, such as floats, decimals and dates |
66 | | - not just integers. |
67 | | -
|
68 | | - This validator is based on `WTForms NumberRange validator`_. |
69 | | -
|
70 | | - .. _WTForms NumberRange validator: |
71 | | - https://github.com/wtforms/wtforms/blob/master/wtforms/validators.py |
72 | | -
|
73 | | - Examples:: |
74 | | -
|
75 | | - >>> import validators |
76 | | -
|
77 | | - >>> assert validators.range(5, min=2) |
78 | | -
|
79 | | - >>> assert validators.range(13.2, min=13, max=14) |
80 | | -
|
81 | | - >>> assert not validators.range(500, max=400) |
82 | | -
|
83 | | -
|
84 | | - :param min: |
85 | | - The minimum required value of the number. If not provided, minimum |
86 | | - value will not be checked. |
87 | | - :param max: |
88 | | - The maximum value of the number. If not provided, maximum value |
89 | | - will not be checked. |
90 | | -
|
91 | | - .. versionadded:: 0.2 |
92 | | - """ |
93 | | - if min is None and max is None: |
94 | | - raise AssertionError( |
95 | | - 'At least one of `min` or `max` must be specified.' |
96 | | - ) |
97 | | - if min is None: |
98 | | - min = Min |
99 | | - if max is None: |
100 | | - max = Max |
101 | | - if min > max: |
102 | | - raise AssertionError('`min` cannot be more than `max`.') |
103 | | - |
104 | | - return min <= value <= max |
105 | | - |
106 | | - |
107 | | -@validator |
108 | | -def length(value, min=None, max=None): |
109 | | - """ |
110 | | - Returns whether or not the length of given string is within a specified |
111 | | - range. |
112 | | -
|
113 | | - Examples:: |
114 | | -
|
115 | | -
|
116 | | - >>> assert length('something', min=2) |
117 | | -
|
118 | | - >>> assert length('something', min=9, max=9) |
119 | | -
|
120 | | - >>> assert not length('something', max=5) |
121 | | -
|
122 | | -
|
123 | | - :param value: |
124 | | - The string to validate. |
125 | | - :param min: |
126 | | - The minimum required length of the string. If not provided, minimum |
127 | | - length will not be checked. |
128 | | - :param max: |
129 | | - The maximum length of the string. If not provided, maximum length |
130 | | - will not be checked. |
131 | | -
|
132 | | - .. versionadded:: 0.2 |
133 | | - """ |
134 | | - if (min is not None and min < 0) or (max is not None and max < 0): |
135 | | - raise AssertionError( |
136 | | - '`min` and `max` need to be greater than zero.' |
137 | | - ) |
138 | | - return range(len(value), min=min, max=max) |
0 commit comments