This repository was archived by the owner on Jul 17, 2025. It is now read-only.
forked from ckan/ckanext-scheming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidation.py
More file actions
131 lines (105 loc) · 3.48 KB
/
validation.py
File metadata and controls
131 lines (105 loc) · 3.48 KB
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
import json
from ckan.plugins.toolkit import get_validator, UnknownValidator, missing, _
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.
"""
return OneOf([c['value'] for c in field['choices']])
@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"
"""
choice_values = set(c['value'] for c in field['choices'])
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 = []
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([
c['value'] for c in field['choices'] if c['value'] in selected])
return validator
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)