Files
notifications-api/app/schema_validation/__init__.py
Rebecca Law 23a4f00e56 New package structure for the version 2 of the public api.
Start building up the validators required for post notificaiton.
The app/v2/errors.py is a rough sketch, will be passed a code, the error can look up the message and link for the error message.
2016-10-25 18:04:03 +01:00

27 lines
835 B
Python

import json
from jsonschema import Draft4Validator, ValidationError
def validate(json_to_validate, schema):
validator = Draft4Validator(schema)
errors = list(validator.iter_errors(json_to_validate))
if errors.__len__() > 0:
raise ValidationError(build_error_message(errors, schema))
return json_to_validate
def build_error_message(errors, schema):
fields = []
for e in errors:
field = "'{}' {}".format(e.path[0], e.schema.get('validationMessage')) if e.schema.get(
'validationMessage') else e.message
fields.append(field)
message = {
"code": "1001",
"message": "Validation error occurred - {}".format(schema['title']),
"link": "link to error documentation (not yet implemented)",
"fields": fields
}
return json.dumps(message)