forked from DataShades/ckanext-scheming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.py
319 lines (257 loc) · 9.06 KB
/
validation.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
import json
import datetime
import pytz
import re
import ckan.lib.helpers as h
import ckanext.scheming.helpers as sh
from ckantoolkit import get_validator, UnknownValidator, missing, Invalid, _
from ckanext.scheming.errors import SchemingException
OneOf = get_validator('OneOf')
ignore_missing = get_validator('ignore_missing')
not_empty = get_validator('not_empty')
def scheming_validator(fn):
"""
Decorate a validator that needs to have the scheming fields
passed with this function. When generating navl validator lists
the function decorated will be called passing the field
and complete schema to produce the actual validator for each field.
"""
fn.is_a_scheming_validator = True
return fn
@scheming_validator
def scheming_choices(field, schema):
"""
Require that one of the field choices values is passed.
"""
if 'choices' in field:
return OneOf([c['value'] for c in field['choices']])
def validator(value):
if value is missing or not value:
return value
choices = sh.scheming_field_choices(field)
for c in choices:
if value == c['value']:
return value
raise Invalid(_('unexpected choice "%s"') % value)
return validator
@scheming_validator
def scheming_required(field, schema):
"""
not_empty if field['required'] else ignore_missing
"""
if field.get('required'):
return not_empty
return ignore_missing
@scheming_validator
def scheming_multiple_choice(field, schema):
"""
Accept zero or more values from a list of choices and convert
to a json list for storage:
1. a list of strings, eg.:
["choice-a", "choice-b"]
2. a single string for single item selection in form submissions:
"choice-a"
"""
static_choice_values = None
if 'choices' in field:
static_choice_order = [c['value'] for c in field['choices']]
static_choice_values = set(static_choice_order)
def validator(key, data, errors, context):
# if there was an error before calling our validator
# don't bother with our validation
if errors[key]:
return
value = data[key]
if value is not missing:
if isinstance(value, basestring):
value = [value]
elif not isinstance(value, list):
errors[key].append(_('expecting list of strings'))
return
else:
value = []
choice_values = static_choice_values
if not choice_values:
choice_order = [c['value'] for c in sh.scheming_field_choices(field)]
choice_values = set(choice_order)
selected = set()
for element in value:
if element in choice_values:
selected.add(element)
continue
errors[key].append(_('unexpected choice "%s"') % element)
if not errors[key]:
data[key] = json.dumps([v for v in
(static_choice_order if static_choice_values else choice_order)
if v in selected])
if field.get('required') and not selected:
errors[key].append(_('Select at least one'))
return validator
def validate_date_inputs(field, key, data, extras, errors, context):
date_error = _('Date format incorrect')
time_error = _('Time format incorrect')
date = None
def get_input(suffix):
inpt = key[0] + '_' + suffix
new_key = (inpt,) + tuple(x for x in key if x != key[0])
value = extras.get(inpt)
data[new_key] = value
errors[new_key] = []
if value:
del extras[inpt]
if field.get('required'):
not_empty(new_key, data, errors, context)
return (new_key, value)
date_key, value = get_input('date')
value_full = ''
if value:
try:
value_full = value
date = h.date_str_to_datetime(value)
except (TypeError, ValueError), e:
errors[date_key].append(date_error)
time_key, value = get_input('time')
if value:
if not value_full:
errors[date_key].append(
_('Date is required when a time is provided'))
else:
try:
value_full += ' ' + value
date = h.date_str_to_datetime(value_full)
except (TypeError, ValueError), e:
errors[time_key].append(time_error)
tz_key, value = get_input('tz')
if value:
if value not in pytz.all_timezones:
errors[tz_key].append('Invalid timezone')
else:
if isinstance(date, datetime.datetime):
date = pytz.timezone(value).localize(date)
return date
@scheming_validator
def scheming_isodatetime(field, schema):
def validator(key, data, errors, context):
value = data[key]
date = None
if value:
if isinstance(value, datetime.datetime):
return value
else:
try:
date = h.date_str_to_datetime(value)
except (TypeError, ValueError), e:
raise Invalid(_('Date format incorrect'))
else:
extras = data.get(('__extras',))
if not extras or (key[0] + '_date' not in extras and
key[0] + '_time' not in extras):
if field.get('required'):
not_empty(key, data, errors, context)
else:
date = validate_date_inputs(
field, key, data, extras, errors, context)
data[key] = date
return validator
@scheming_validator
def scheming_isodatetime_tz(field, schema):
def validator(key, data, errors, context):
value = data[key]
date = None
if value:
if isinstance(value, datetime.datetime):
date = sh.scheming_datetime_to_UTC(value)
else:
try:
date = sh.date_tz_str_to_datetime(value)
except (TypeError, ValueError), e:
raise Invalid(_('Date format incorrect'))
else:
extras = data.get(('__extras',))
if not extras or (key[0] + '_date' not in extras and
key[0] + '_time' not in extras):
if field.get('required'):
not_empty(key, data, errors, context)
else:
date = validate_date_inputs(
field, key, data, extras, errors, context)
if isinstance(date, datetime.datetime):
date = sh.scheming_datetime_to_UTC(date)
data[key] = date
return validator
def scheming_valid_json_object(value, context):
"""Store a JSON object as a serialized JSON string
It accepts two types of inputs:
1. A valid serialized JSON string (it must be an object or a list)
2. An object that can be serialized to JSON
"""
if not value:
return
elif isinstance(value, basestring):
try:
loaded = json.loads(value)
if not isinstance(loaded, dict):
raise Invalid(
_('Unsupported value for JSON field: {}').format(value)
)
return value
except (ValueError, TypeError) as e:
raise Invalid(_('Invalid JSON string: {}').format(e))
elif isinstance(value, dict):
try:
return json.dumps(value)
except (ValueError, TypeError) as e:
raise Invalid(_('Invalid JSON object: {}').format(e))
else:
raise Invalid(
_('Unsupported type for JSON field: {}').format(type(value))
)
return value
def scheming_load_json(value, context):
if isinstance(value, basestring):
try:
return json.loads(value)
except ValueError:
return value
return value
def scheming_multiple_choice_output(value):
"""
return stored json as a proper list
"""
if isinstance(value, list):
return value
try:
return json.loads(value)
except ValueError:
return [value]
def validators_from_string(s, field, schema):
"""
convert a schema validators string to a list of validators
e.g. "if_empty_same_as(name) unicode" becomes:
[if_empty_same_as("name"), unicode]
"""
out = []
parts = s.split()
for p in parts:
if '(' in p and p[-1] == ')':
name, args = p.split('(', 1)
args = args[:-1].split(',') # trim trailing ')', break up
v = get_validator_or_converter(name)(*args)
else:
v = get_validator_or_converter(p)
if getattr(v, 'is_a_scheming_validator', False):
v = v(field, schema)
out.append(v)
return out
def get_validator_or_converter(name):
"""
Get a validator or converter by name
"""
if name == 'unicode':
return unicode
try:
v = get_validator(name)
return v
except UnknownValidator:
pass
raise SchemingException('validator/converter not found: %r' % name)