-
Notifications
You must be signed in to change notification settings - Fork 20
/
schemas.py
440 lines (364 loc) · 13 KB
/
schemas.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
"""
Work with the MDS Provider JSON Schemas.
"""
import os
import jsonschema
import requests
import mds.geometry
import mds.github
from .versions import Version
STATUS_CHANGES = "status_changes"
TRIPS = "trips"
EVENTS = "events"
VEHICLES = "vehicles"
SCHEMA_TYPES = [ STATUS_CHANGES, TRIPS, EVENTS, VEHICLES ]
class Schema():
"""
Represents a MDS Provider JSON Schema.
"""
def __init__(self, schema_type, ref=None, **kwargs):
"""
Initialize a new Schema instance.
Parameters:
schema_type: str
The type of MDS Provider schema.
ref: str, Version, optional
Reference the schema at the version specified, which could be any of:
* git branch name
* git commit hash (long or short)
* version str or Version instance
acquire: bool, optional
Whether to immediately acquire the schema document from GitHub. The default is False.
"""
if schema_type not in SCHEMA_TYPES:
valid_types = ", ".join(SCHEMA_TYPES)
raise ValueError(f"Invalid schema_type '{schema_type}'. Valid schema_types: {valid_types}")
# the underlying schema document is not acquired until necessary
self._schema = None
# configuration
self.schema_type = schema_type
self.data_key = STATUS_CHANGES if schema_type == EVENTS else schema_type
self.ref = ref or mds.github.MDS_DEFAULT_REF
try:
self.ref = Version(self.ref)
except:
pass
finally:
if isinstance(self.ref, Version):
self.ref.raise_if_unsupported()
self.schema_url = mds.github.schema_url(schema_type, self.ref)
if kwargs.get("acquire"):
self._acquire()
def __repr__(self):
return f"<mds.schemas.Schema ('{self.schema_type}', '{self.ref}', '{self.schema_url}')>"
def _acquire(self):
"""
On-demand, one-time acquisition of the schema document from GitHub.
"""
if not self._schema:
try:
self._schema = requests.get(self.schema_url).json()
except:
raise ValueError(f"Problem requesting schema from: {self.schema_url}")
finally:
# override the $id for a non-standard ref
if self._schema and self.ref != mds.github.MDS_DEFAULT_REF:
self._schema["$id"] = self.schema_url
def validate(self, instance_source):
"""
Validate an instance against this schema.
Shortcut method for DataValidator(self).validate(instance_source).
Parameters:
instance_source: dict
An instance (e.g. parsed JSON object) to validate.
Return:
iterator
An iterator that yields validation errors.
"""
self._acquire()
validator = DataValidator(self)
for error in validator.validate(instance_source):
yield error
@property
def schema(self):
"""
Get the underlying schema document.
"""
self._acquire()
return self._schema
@property
def event_types(self):
"""
Get the list of valid event_type values for this schema.
"""
self._acquire()
return list(self.event_type_reasons.keys())
@property
def event_type_reasons(self):
"""
Get a dict(event_type=list(event_type_reason)) for this schema.
"""
etr = {}
if self.data_key == STATUS_CHANGES:
event_key, reason_key = "event_type", "event_type_reason"
elif self.data_key == VEHICLES:
event_key, reason_key = "last_event_type", "last_event_type_reason"
else:
return etr
self._acquire()
if "allOf" in self.item_schema:
for allOf in self.item_schema["allOf"]:
sub_check = ["properties" in sub and event_key in sub["properties"] for sub in allOf["oneOf"]]
if all(sub_check):
item_schema = allOf["oneOf"]
break
else:
item_schema = self.item_schema["oneOf"]
for oneOf in item_schema:
props = oneOf["properties"]
if event_key in props and reason_key in props:
event_type = props[event_key]["enum"][0]
event_type_reasons = props[reason_key]["enum"]
etr[event_type] = event_type_reasons
return etr
@property
def item_schema(self):
"""
Get the schema for items in this schema's data array (e.g. the status_change or trip records).
"""
self._acquire()
return self.schema["properties"]["data"]["properties"][self.data_key]["items"]
@property
def optional_item_fields(self):
"""
Returns the list of optional field names for items in the data array of this schema.
"""
self._acquire()
item_props = self.item_schema["properties"].keys()
return [ip for ip in item_props if ip not in self.required_item_fields]
@property
def required_item_fields(self):
"""
Returns the list of required field names for items in the data array of this schema.
"""
self._acquire()
return self.item_schema["required"]
@property
def propulsion_types(self):
"""
Get the list of valid propulsion_type values for this schema.
"""
self._acquire()
definition = self.schema["definitions"]["propulsion_type"]
return definition["items"]["enum"]
@property
def vehicle_types(self):
"""
Get the list of valid vehicle_type values for this schema.
"""
self._acquire()
definition = self.schema["definitions"]["vehicle_type"]
return definition["enum"]
@classmethod
def status_changes(cls, ref=None):
"""
Get the Status Changes schema.
"""
return Schema(STATUS_CHANGES, ref)
@classmethod
def trips(cls, ref=None):
"""
Get the Trips schema.
"""
return Schema(TRIPS, ref)
@classmethod
def events(cls, ref=None):
"""
Get the Events schema.
"""
return Schema(EVENTS, ref)
@classmethod
def vehicles(cls, ref=None):
"""
Get the Vehicles schema.
"""
return Schema(VEHICLES, ref)
class DataValidationError():
"""
Represents a failed MDS Provider data validation.
"""
def __init__(self, validation_error, instance, provider_schema):
"""
Initialize a new validation error instance.
Parameters:
validation_error: jsonschema.exceptions.ValidationError
The error raised by validation.
instance: dict
The MDS Provider data object under validation.
provider_schema: Schema
The schema instance used as the basis for validation.
"""
self.instance = validation_error.instance
self.message = validation_error.message
self.original_instance = instance
self.version = Version(instance["version"])
self.path = list(validation_error.path)
self.provider_schema = provider_schema
self.schema_type = provider_schema.schema_type
self.data_key = provider_schema.data_key
self.validation_error = validation_error
self.validator = validation_error.validator
def __repr__(self):
return os.linesep.join(self.describe())
def describe(self):
"""
Describe this error.
Return:
list
A list of error messages describing the error.
"""
if len(self.path) >= 3:
return self._describe_item()
elif len(self.path) == 2:
return self._describe_payload()
else:
return self._describe_page()
def _describe_page(self):
"""
Describe a page-level error.
"""
messages = [
"Page error"
]
if len(self.path) > 0:
for key in self.path:
messages.append(f"Field '{key}': value {self.message}")
else:
messages.append(self.message)
return messages
def _describe_payload(self):
"""
Describe a payload-level error.
"""
path = ".".join(self.path)
return [
f"Payload error in {path}",
self.message
]
def _describe_item(self):
"""
Describe an item-level error.
"""
index = list(filter(lambda i: isinstance(i, int), self.path))[0]
path = f"{self.data_key}[{index}]"
message = self.message.lower()
if "is valid under each of" in message:
message = "instance " + self.message[message.index("is valid under each of"):]
if "is not of type" in message:
message = "value " + self.message[message.index("is not of type"):]
# this is an error about a specific attribute on this item
if len(self.path) > 3:
path = ".".join([path, self.path[-1]])
return [
f"Item error in {path}",
message
]
class DataValidator():
"""
Validate MDS Provider data against JSON Schemas.
"""
def __init__(self, schema=None, ref=None):
"""
Initialize a new DataValidator.
Parameters:
schema: str, Schema, optional
The type of schema to validate; or
A Schema instance to use for validation.
ref: str, Version, optional
The reference (git commit, branch, tag, or version) at which to reference the schema.
"""
self.schema = self._get_schema_instance_or_raise(schema, ref)
self.ref = self.schema.ref
self.schema_type = self.schema.schema_type
self.data_key = self.schema.data_key
def __repr__(self):
return f"<mds.schemas.DataValidator ('{self.ref}', '{self.schema_type}')>"
def _get_schema_instance_or_raise(self, schema, ref):
"""
Helper to return a Schema instance from the possible arguments.
"""
# determine the Schema instance to use
if isinstance(schema, Schema):
return schema
elif schema in SCHEMA_TYPES:
return Schema(schema, ref=ref)
elif isinstance(getattr(self, "schema", None), Schema):
return self.schema
else:
raise ValueError("Could not obtain a schema for validation.")
def validate(self, instance_source, schema=None, ref=None):
"""
Validate MDS Provider data against a schema.
Parameters:
instance_source: str, dict, Path
The source of data to validate, any of:
* JSON text str
* JSON object dict
* path to a local file of JSON text
* URL to a remote file of JSON text
schema: str, Schema, optional
The type of schema to validate; or
A Schema instance to use for validation.
ref: str, Version, optional
The reference (git commit, branch, tag, or version) at which to reference the schema.
Return:
iterator
Zero or more DataValidationError instances.
"""
schema = self._get_schema_instance_or_raise(schema, ref)
if isinstance(instance_source, dict):
instances = [instance_source]
else:
try:
from .files import DataFile
instances = DataFile(schema, instance_source).load_payloads()
except:
raise TypeError(f"Unrecognized instance_source type: {type(instance_source)}.")
# schema is a Schema instance
# schema.schema is the JSON Schema (dict) associated with it
v = self._get_validator(schema.schema)
# handles case when instance_source pointed to a list of payloads
for instance in instances:
# do validation, converting and yielding errors
for error in v.iter_errors(instance):
yield DataValidationError(error, instance, schema)
@classmethod
def _get_validator(cls, schema):
"""
Helper to return a jsonschema.IValidator instance for the given JSON schema object.
"""
return jsonschema.Draft6Validator(schema)
@classmethod
def status_changes(cls, ref=None):
"""
Create a Status Changes validator.
"""
return DataValidator(STATUS_CHANGES, ref)
@classmethod
def trips(cls, ref=None):
"""
Create a Trips validator.
"""
return DataValidator(TRIPS, ref)
@classmethod
def events(cls, ref=None):
"""
Create an Events validator.
"""
return DataValidator(EVENTS, ref)
@classmethod
def vehicles(cls, ref=None):
"""
Create a Vehicles validator.
"""
return DataValidator(VEHICLES, ref)