/
schema.py
391 lines (345 loc) · 12.2 KB
/
schema.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
from collections import defaultdict
from marshmallow import (
Schema,
fields,
validate,
validates_schema,
ValidationError as MarshmallowValidationError,
)
from paramtools.contrib import (
validate as contrib_validate,
fields as contrib_fields,
)
class RangeSchema(Schema):
"""
Schema for range object
{
"range": {"min": field, "max": field}
}
"""
_min = fields.Field(attribute="min", data_key="min")
_max = fields.Field(attribute="max", data_key="max")
class ChoiceSchema(Schema):
choices = fields.List(fields.Field)
class ValueValidatorSchema(Schema):
"""
Schema for validation specification for each parameter value
"""
_range = fields.Nested(
RangeSchema(), attribute="range", data_key="range", required=False
)
date_range = fields.Nested(RangeSchema(), required=False)
choice = fields.Nested(ChoiceSchema(), required=False)
class BaseParamSchema(Schema):
"""
Defines a base parameter schema. This specifies the required fields and
their types.
{
"title": str,
"description": str,
"notes": str,
"type": str (limited to 'int', 'float', 'bool', 'str'),
"value": `BaseValidatorSchema`, "value" type depends on "type" key,
"range": range schema ({"min": ..., "max": ..., "other ops": ...}),
}
This class is defined further by a JSON file indicating extra fields that
are required by the implementer of the schema.
"""
title = fields.Str(required=True)
description = fields.Str(required=True)
notes = fields.Str(required=False)
_type = fields.Str(
required=True,
validate=validate.OneOf(
choices=["str", "float", "int", "bool", "date"]
),
attribute="type",
data_key="type",
)
number_dims = fields.Integer(required=False, missing=0)
value = fields.Field(required=True) # will be specified later
validators = fields.Nested(
ValueValidatorSchema(), required=False, missing={}
)
class EmptySchema(Schema):
"""
An empty schema that is used as a base class for creating other classes via
the `type` function
"""
pass
class OrderedSchema(Schema):
"""
Same as `EmptySchema`, but preserves the order of its fields.
"""
class Meta:
ordered = True
class ValueObject(fields.Nested):
"""
Schema for value objects
"""
def _deserialize(self, value, attr, data, partial=None, **kwargs):
if not isinstance(value, list) or (
isinstance(value, list) and not isinstance(value[0], dict)
):
value = [{"value": value}]
return super()._deserialize(
value, attr, data, partial=partial, **kwargs
)
class BaseValidatorSchema(Schema):
"""
Schema that validates parameter adjustments such as:
```
{
"STD": [{
"year": 2017,
"MARS": "single",
"value": "3000"
}]
}
```
Information defined for each variable on the `BaseParamSchema` is utilized
to define this class and how it should validate its data. See
`build_schema.SchemaBuilder` for how parameters are defined onto this
class.
"""
WRAPPER_MAP = {
"range": "_get_range_validator",
"date_range": "_get_range_validator",
"choice": "_get_choice_validator",
}
@validates_schema
def validate_params(self, data):
"""
Loop over all parameters defined on this class. Validate them using
the `self.validate_param`. Errors are stored until all
parameters have been validated. Note that all data has been
type-validated. These methods only do range validation.
"""
errors = defaultdict(dict)
errors_exist = False
for name, specs in data.items():
for i, spec in enumerate(specs):
iserrors = self.validate_param(name, spec, data)
if iserrors:
errors_exist = True
errors[name][i] = {"value": iserrors}
if errors_exist:
raise MarshmallowValidationError(dict(errors))
def validate_param(self, param_name, param_spec, raw_data):
"""
Do range validation for a parameter.
"""
param_info = self.context["spec"]._data[param_name]
# sort keys to guarantee order.
labels = " , ".join(
[
f"{k}={param_spec[k]}"
for k in sorted(param_spec)
if k != "value"
]
)
validator_spec = param_info["validators"]
validators = []
for validator_name, method_name in self.WRAPPER_MAP.items():
if validator_name in validator_spec:
validator = getattr(self, method_name)(
validator_name,
validator_spec[validator_name],
param_name,
labels,
param_spec,
raw_data,
)
validators.append(validator)
value = param_spec["value"]
errors = []
for validator in validators:
try:
validator(value)
except MarshmallowValidationError as ve:
errors.append(str(ve))
return errors
def _get_range_validator(
self, vname, range_dict, param_name, labels, param_spec, raw_data
):
if vname == "range":
range_class = contrib_validate.Range
elif vname == "date_range":
range_class = contrib_validate.DateRange
else:
raise MarshmallowValidationError(
f"{vname} is not an allowed validator."
)
min_value = range_dict.get("min", None)
if min_value is not None:
min_value = self._resolve_op_value(
min_value, param_name, param_spec, raw_data
)
max_value = range_dict.get("max", None)
if max_value is not None:
max_value = self._resolve_op_value(
max_value, param_name, param_spec, raw_data
)
label_suffix = f" for labels {labels}" if labels else ""
min_error = (
"{param_name} {input} must be greater than " "{min}{label_suffix}."
).format(
param_name=param_name,
labels=labels,
input="{input}",
min="{min}",
label_suffix=label_suffix,
)
max_error = (
"{param_name} {input} must be less than " "{max}{label_suffix}."
).format(
param_name=param_name,
labels=labels,
input="{input}",
max="{max}",
label_suffix=label_suffix,
)
return range_class(min_value, max_value, min_error, max_error)
def _get_choice_validator(
self, vname, choice_dict, param_name, labels, param_spec, raw_data
):
choices = choice_dict["choices"]
label_suffix = f" for labels {labels}" if labels else ""
if len(choices) < 20:
error_template = (
'{param_name} "{input}" must be in list of choices '
"{choices}{label_suffix}."
)
else:
error_template = '{param_name} "{input}" must be in list of choices{label_suffix}.'
error = error_template.format(
param_name=param_name,
labels=labels,
input="{input}",
choices="{choices}",
label_suffix=label_suffix,
)
return contrib_validate.OneOf(choices, error=error)
def _resolve_op_value(self, op_value, param_name, param_spec, raw_data):
"""
Operator values (`op_value`) are the values pointed to by the "min"
and "max" keys. These can be values to compare against, another
variable to compare against, or the default value of the adjusted
variable.
"""
if op_value in self.fields or op_value == "default":
return self._get_comparable_value(
op_value, param_name, param_spec, raw_data
)
return op_value
def _get_comparable_value(
self, oth_param_name, param_name, param_spec, raw_data
):
"""
Get the value that the adjusted variable will be compared against.
Candidates are:
- the parameter's own default value if "default" is specified
- a reference variable's value
- first, look in the raw adjustment data
- second, look in the defaults data
"""
if oth_param_name in raw_data:
vals = raw_data[oth_param_name]
else:
# If comparing against the "default" value then get the current
# value of the parameter being updated.
if oth_param_name == "default":
oth_param_name = param_name
oth_param = self.context["spec"]._data[oth_param_name]
vals = oth_param["value"]
labels_to_check = tuple(k for k in param_spec if k != "value")
res = [
val
for val in vals
if all(val[k] == param_spec[k] for k in labels_to_check)
]
assert len(res) == 1
return res[0]["value"]
# A few fields that have not been instantiated yet
CLASS_FIELD_MAP = {
"str": contrib_fields.Str,
"int": contrib_fields.Integer,
"float": contrib_fields.Float,
"bool": contrib_fields.Boolean,
"date": contrib_fields.Date,
}
INVALID_NUMBER = {"invalid": "Not a valid number: {input}."}
INVALID_BOOLEAN = {"invalid": "Not a valid boolean: {input}."}
INVALID_DATE = {"invalid": "Not a valid date: {input}."}
# A few fields that have been instantiated
FIELD_MAP = {
"str": contrib_fields.Str(allow_none=True),
"int": contrib_fields.Integer(
allow_none=True, error_messages=INVALID_NUMBER
),
"float": contrib_fields.Float(
allow_none=True, error_messages=INVALID_NUMBER
),
"bool": contrib_fields.Boolean(
allow_none=True, error_messages=INVALID_BOOLEAN
),
"date": contrib_fields.Date(allow_none=True, error_messages=INVALID_DATE),
}
VALIDATOR_MAP = {
"range": contrib_validate.Range,
"date_range": contrib_validate.DateRange,
"choice": contrib_validate.OneOf,
}
def get_type(data):
numeric_types = {
"int": contrib_fields.Int64(
allow_none=True, error_messages=INVALID_NUMBER
),
"bool": contrib_fields.Bool_(
allow_none=True, error_messages=INVALID_BOOLEAN
),
"float": contrib_fields.Float64(
allow_none=True, error_messages=INVALID_NUMBER
),
}
types = dict(FIELD_MAP, **numeric_types)
fieldtype = types[data["type"]]
dim = data.get("number_dims", 0)
while dim > 0:
fieldtype = fields.List(fieldtype, allow_none=True)
dim -= 1
return fieldtype
def get_param_schema(base_spec, field_map=None):
"""
Read in data from the initializing schema. This will be used to fill in the
optional properties on classes derived from the `BaseParamSchema` class.
This data is also used to build validators for schema for each parameter
that will be set on the `BaseValidatorSchema` class
"""
if field_map is not None:
field_map = dict(FIELD_MAP, **field_map)
else:
field_map = FIELD_MAP.copy()
optional_fields = {}
for k, v in base_spec.get("additional_members", {}).items():
fieldtype = field_map[v["type"]]
if v.get("number_dims", 0) > 0:
d = v["number_dims"]
while d > 0:
fieldtype = fields.List(fieldtype)
d -= 1
optional_fields[k] = fieldtype
ParamSchema = type(
"ParamSchema",
(BaseParamSchema,),
{k: v for k, v in optional_fields.items()},
)
label_validators = {}
for name, label in base_spec.get("labels", {}).items():
validators = []
for vname, kwargs in label["validators"].items():
validator_class = VALIDATOR_MAP[vname]
validators.append(validator_class(**kwargs))
fieldtype = CLASS_FIELD_MAP[label["type"]]
label_validators[name] = fieldtype(validate=validators)
return ParamSchema, label_validators