Schema Validation¶
The mds.schema.validation
module supports validating MDS Provider data against the published MDS JSON Schema documents.
This module exports the ProviderDataValidator
class, which works hand-in-hand with the ProviderSchema
class.
Examples¶
Create an instance of a validator for a schema type and (git) reference, and then run validation against some input data:
from mds.schema.validation import ProviderDataValidator
validator = ProviderDataValidator.StatusChanges(ref="dev")
data = {}
for error in validator.validate(data):
print(error)
Outputs a Page error
, indicating a problem with the top-level data structure (the page of data):
Page error
'version' is a required property
Page error
'data' is a required property
With slightly more complex data:
data = { "version": "0.2.0", "data": { "status_changes": "data here" } }
for error in validator.validate(data):
print(error)
Outputs a Payload error
:
Payload error in data.status_changes
value is not of type 'array'
Indicating a problem with the data
payload (in this case, "data here"
is not the required type array
)
A more involved Payload error
:
data = { "version": "0.2.0", "data": { "status_changes": [{}] } }
for error in validator.validate(data):
print(error)
Outputs:
Payload error in data.status_changes[0]
'provider_name' is a required property
Payload error in data.status_changes[0]
'provider_id' is a required property
Payload error in data.status_changes[0]
'device_id' is a required property
Payload error in data.status_changes[0]
'vehicle_id' is a required property
Payload error in data.status_changes[0]
'vehicle_type' is a required property
Payload error in data.status_changes[0]
'propulsion_type' is a required property
Payload error in data.status_changes[0]
'event_type' is a required property
Payload error in data.status_changes[0]
'event_type_reason' is a required property
Payload error in data.status_changes[0]
'event_time' is a required property
Payload error in data.status_changes[0]
'event_location' is a required property
If you already have an instance of ProviderSchema
, this can be used to initialize a validator:
from mds.schema import ProviderSchema
from mds.schema.validation import ProviderDataValidator
schema = ProviderSchema.StatusChanges(ref="dev")
validator = ProviderDataValidator(schema)
data = {}
for error in validator.validate(data):
print(error)
A shortcut for the above is to simply call validate()
on the ProviderSchema
instance itself:
from mds.schema import ProviderSchema
schema = ProviderSchema.StatusChanges(ref="dev")
data = {}
for error in schema.validate(data):
print(error)