forked from python-validators/validators
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtruthy.py
More file actions
40 lines (28 loc) · 900 Bytes
/
truthy.py
File metadata and controls
40 lines (28 loc) · 900 Bytes
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
import six
from .utils import validator
@validator
def truthy(value):
"""
Validate that given value is not a falsey value.
This validator is based on `WTForms DataRequired validator`_.
.. _WTForms DataRequired validator:
https://github.com/wtforms/wtforms/blob/master/wtforms/validators.py
Examples::
>>> truthy(1)
True
>>> truthy('someone')
True
>>> truthy(0)
ValidationFailure(func=truthy, args={'value': 0})
>>> truthy(' ')
ValidationFailure(func=truthy, args={'value': ' '})
>>> truthy(False)
ValidationFailure(func=truthy, args={'value': False})
>>> truthy(None)
ValidationFailure(func=truthy, args={'value': None})
.. versionadded:: 0.2
"""
return (
value and
(not isinstance(value, six.string_types) or value.strip())
)