V2 schemas for post sms notifications, post_sms_request and post_sms_response

This commit is contained in:
Rebecca Law
2016-10-25 14:53:31 +01:00
parent e5bedbd789
commit a5e07d8aff
6 changed files with 193 additions and 0 deletions

View File

@@ -0,0 +1,26 @@
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)

View File

@@ -0,0 +1,20 @@
"""
Definitions are intended for schema definitions that are not likely to change from version to version.
If the definition is specific to a version put it in a definition file in the version package
"""
uuid = {
"type": "string",
"pattern": "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$",
"validationMessage": "not a valid UUID",
"code": "1001", # yet to be implemented
"link": "link to our error documentation not yet implemented"
}
personalisation = {
"type": "object",
"validationMessage": "should contain key value pairs",
"code": "1001", # yet to be implemented
"link": "link to our error documentation not yet implemented"
}

View File

@@ -0,0 +1,56 @@
from app.schema_validation.definitions import (uuid, personalisation)
post_sms_request = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST sms notification schema",
"type": "object",
"title": "POST v2/notifications/sms",
"properties": {
"reference": {"type": "string"},
"phone_number": {"type": "string", "format": "sms"},
"template_id": uuid,
"personalisation": personalisation
},
"required": ["phone_number", "template_id"]
}
content = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST sms notification response schema",
"type": "object",
"title": "notification content",
"properties": {
"body": {"type": "string"},
"from_number": {"type": "string"}
},
"required": ["body"]
}
# this may belong in a templates module
template = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST sms notification response schema",
"type": "object",
"title": "notification content",
"properties": {
"id": uuid,
"version": {"type": "integer"},
"uri": {"type": "string"}
},
"required": ["id", "version", "uri"]
}
post_sms_response = {
"$schema": "http://json-schema.org/draft-04/schema#",
"description": "POST sms notification response schema",
"type": "object",
"title": "response v2/notifications/sms",
"properties": {
"id": uuid,
"reference": {"type": "string"},
"content": content,
"uri": {"type": "string"},
"template": template
},
"required": ["id", "content", "uri", "template"]
}