mirror of
https://github.com/GSA/notifications-api.git
synced 2026-01-30 14:31:57 -05:00
V2 schemas for post sms notifications, post_sms_request and post_sms_response
This commit is contained in:
26
app/schema_validation/__init__.py
Normal file
26
app/schema_validation/__init__.py
Normal 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)
|
||||
20
app/schema_validation/definitions.py
Normal file
20
app/schema_validation/definitions.py
Normal 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"
|
||||
}
|
||||
56
app/v2/notifications/notification_schemas.py
Normal file
56
app/v2/notifications/notification_schemas.py
Normal 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"]
|
||||
}
|
||||
Reference in New Issue
Block a user