-
Notifications
You must be signed in to change notification settings - Fork 2k
/
validators.py
113 lines (75 loc) · 2.87 KB
/
validators.py
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
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import ckan.lib.navl.dictization_functions as df
from ckan.common import _
missing = df.missing
StopOnError = df.StopOnError
Invalid = df.Invalid
def identity_converter(key, data, errors, context):
return
def keep_extras(key, data, errors, context):
extras = data.pop(key, {})
for extras_key, value in extras.iteritems():
data[key[:-1] + (extras_key,)] = value
def not_missing(key, data, errors, context):
value = data.get(key)
if value is missing:
errors[key].append(_('Missing value'))
raise StopOnError
def not_empty(key, data, errors, context):
value = data.get(key)
if not value or value is missing:
errors[key].append(_('Missing value'))
raise StopOnError
def if_empty_same_as(other_key):
def callable(key, data, errors, context):
value = data.get(key)
if not value or value is missing:
data[key] = data[key[:-1] + (other_key,)]
return callable
def both_not_empty(other_key):
def callable(key, data, errors, context):
value = data.get(key)
other_value = data.get(key[:-1] + (other_key,))
if (not value or value is missing and
not other_value or other_value is missing):
errors[key].append(_('Missing value'))
raise StopOnError
return callable
def empty(key, data, errors, context):
value = data.pop(key, None)
if value and value is not missing:
errors[key].append(_(
'The input field %(name)s was not expected.') % {"name": key[-1]})
def ignore(key, data, errors, context):
value = data.pop(key, None)
raise StopOnError
def default(defalult_value):
def callable(key, data, errors, context):
value = data.get(key)
if not value or value is missing:
data[key] = defalult_value
return callable
def ignore_missing(key, data, errors, context):
'''If the key is missing from the data, ignore the rest of the key's
schema.
By putting ignore_missing at the start of the schema list for a key,
you can allow users to post a dict without the key and the dict will pass
validation. But if they post a dict that does contain the key, then any
validators after ignore_missing in the key's schema list will be applied.
:raises ckan.lib.navl.dictization_functions.StopOnError: if ``data[key]``
is :py:data:`ckan.lib.navl.dictization_functions.missing` or ``None``
:returns: ``None``
'''
value = data.get(key)
if value is missing or value is None:
data.pop(key, None)
raise StopOnError
def ignore_empty(key, data, errors, context):
value = data.get(key)
if value is missing or not value:
data.pop(key, None)
raise StopOnError
def convert_int(value, context):
try:
return int(value)
except ValueError:
raise Invalid(_('Please enter an integer value'))