Skip to content

Commit a5b2dd3

Browse files
committed
Moved files
1 parent f72183c commit a5b2dd3

5 files changed

Lines changed: 133 additions & 115 deletions

File tree

docs/index.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,16 +63,20 @@ ValidationFailure object also holds all the arguments passed to original functio
6363

6464

6565

66-
.. module:: validators
66+
6767

6868
email
6969
-----
7070

71+
.. module:: validators.email
72+
7173
.. autofunction:: email
7274

7375
ipv4
7476
----
7577

78+
.. module:: validators
79+
7680
.. autofunction:: ipv4
7781

7882
ipv6

validators/__init__.py

Lines changed: 6 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import six
21
from .email import email
32
from .extremes import Min, Max
43
from .ip_address import ipv4, ipv6
4+
from .length import length
55
from .mac_address import mac_address
6+
from .range import range
7+
from .truthy import truthy
68
from .utils import ValidationFailure, validator
79
from .url import url
810
from .uuid import uuid
@@ -12,7 +14,10 @@
1214
ipv4,
1315
ipv6,
1416
email,
17+
length,
1518
mac_address,
19+
range,
20+
truthy,
1621
url,
1722
uuid,
1823
validator,
@@ -23,116 +28,3 @@
2328

2429

2530
__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)

validators/length.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
@validator
2+
def length(value, min=None, max=None):
3+
"""
4+
Returns whether or not the length of given string is within a specified
5+
range.
6+
7+
Examples::
8+
9+
10+
>>> assert length('something', min=2)
11+
12+
>>> assert length('something', min=9, max=9)
13+
14+
>>> assert not length('something', max=5)
15+
16+
17+
:param value:
18+
The string to validate.
19+
:param min:
20+
The minimum required length of the string. If not provided, minimum
21+
length will not be checked.
22+
:param max:
23+
The maximum length of the string. If not provided, maximum length
24+
will not be checked.
25+
26+
.. versionadded:: 0.2
27+
"""
28+
if (min is not None and min < 0) or (max is not None and max < 0):
29+
raise AssertionError(
30+
'`min` and `max` need to be greater than zero.'
31+
)
32+
return range(len(value), min=min, max=max)

validators/range.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from .extremes import Min, Max
2+
from .utils import validator
3+
4+
5+
@validator
6+
def range(value, min=None, max=None):
7+
"""
8+
Validates that a number is of a minimum and/or maximum value, inclusive.
9+
This will work with any comparable type, such as floats, decimals and dates
10+
not just integers.
11+
12+
This validator is based on `WTForms NumberRange validator`_.
13+
14+
.. _WTForms NumberRange validator:
15+
https://github.com/wtforms/wtforms/blob/master/wtforms/validators.py
16+
17+
Examples::
18+
19+
>>> import validators
20+
21+
>>> assert validators.range(5, min=2)
22+
23+
>>> assert validators.range(13.2, min=13, max=14)
24+
25+
>>> assert not validators.range(500, max=400)
26+
27+
>>> from datetime import datetime
28+
29+
>>> assert validator.range(
30+
... datetime(2000, 11, 11),
31+
... min=datetime(1999, 11, 11)
32+
... )
33+
34+
35+
:param min:
36+
The minimum required value of the number. If not provided, minimum
37+
value will not be checked.
38+
:param max:
39+
The maximum value of the number. If not provided, maximum value
40+
will not be checked.
41+
42+
.. versionadded:: 0.2
43+
"""
44+
if min is None and max is None:
45+
raise AssertionError(
46+
'At least one of `min` or `max` must be specified.'
47+
)
48+
if min is None:
49+
min = Min
50+
if max is None:
51+
max = Max
52+
if min > max:
53+
raise AssertionError('`min` cannot be more than `max`.')
54+
55+
return min <= value <= max

validators/truthy.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import six
2+
from .utils import validator
3+
4+
5+
@validator
6+
def truthy(value):
7+
"""
8+
Validates that given value is not a falsey value.
9+
10+
This validator is based on `WTForms DataRequired validator`_.
11+
12+
.. _WTForms DataRequired validator:
13+
https://github.com/wtforms/wtforms/blob/master/wtforms/validators.py
14+
15+
Examples::
16+
17+
18+
>>> assert truthy(1)
19+
20+
>>> assert truthy('someone')
21+
22+
>>> assert not truthy(0)
23+
24+
>>> assert not truthy(' ')
25+
26+
>>> assert not truthy(False)
27+
28+
>>> assert not truthy(None)
29+
30+
.. versionadded:: 0.2
31+
"""
32+
return (
33+
not value or
34+
isinstance(value, six.string_types) and not value.strip()
35+
)

0 commit comments

Comments
 (0)