From 82ba2cd22688a57cd7b71f8a0bfde3ab3a9bdac6 Mon Sep 17 00:00:00 2001 From: Paul Craig Date: Mon, 21 Nov 2016 15:42:17 +0000 Subject: [PATCH] Create new notification schema This is the schema that individual notifications will conform to when they are returned from this API. JSON logic enforces that the right keys are set depending on the `"type"`. (eg a schema with `"type": "sms"` must have a `"phone_number"` value and it cannot have an `"email_address"`) --- app/schema_validation/__init__.py | 6 +- app/v2/notifications/notification_schemas.py | 88 ++++++++++++++++---- 2 files changed, 78 insertions(+), 16 deletions(-) diff --git a/app/schema_validation/__init__.py b/app/schema_validation/__init__.py index e3f267999..ee8f27943 100644 --- a/app/schema_validation/__init__.py +++ b/app/schema_validation/__init__.py @@ -10,12 +10,14 @@ def validate(json_to_validate, schema): @format_checker.checks('phone_number', raises=InvalidPhoneError) def validate_schema_phone_number(instance): - validate_phone_number(instance) + if instance is not None: + validate_phone_number(instance) return True @format_checker.checks('email_address', raises=InvalidEmailError) def validate_schema_email_address(instance): - validate_email_address(instance) + if instance is not None: + validate_email_address(instance) return True validator = Draft4Validator(schema, format_checker=format_checker) diff --git a/app/v2/notifications/notification_schemas.py b/app/v2/notifications/notification_schemas.py index d77a4d21a..ee1b11af7 100644 --- a/app/v2/notifications/notification_schemas.py +++ b/app/v2/notifications/notification_schemas.py @@ -1,5 +1,79 @@ from app.schema_validation.definitions import (uuid, personalisation) +# this may belong in a templates module +template = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "template schema", + "type": "object", + "title": "notification content", + "properties": { + "id": uuid, + "version": {"type": "integer"}, + "uri": {"type": "string"} + }, + "required": ["id", "version", "uri"] +} + +get_notification_response = { + "$schema": "http://json-schema.org/draft-04/schema#", + "description": "GET notification response schema", + "type": "object", + "title": "response v2/notification", + "oneOf": [ + {"properties": { + "email_address": {"type": "string", "format": "email_address"}, + "type": {"enum": ["email"]}, + + "phone_number": {"type": "null"}, + "line_1": {"type": "null"}, + "postcode": {"type": "null"} + }}, + {"properties": { + "phone_number": {"type": "string", "format": "phone_number"}, + "type": {"enum": ["sms"]}, + + "email_address": {"type": "null"}, + "line_1": {"type": "null"}, + "postcode": {"type": "null"} + }}, + {"properties": { + "line_1": {"type": "string", "minLength": 1}, + "postcode": {"type": "string", "minLength": 1}, + "type": {"enum": ["letter"]}, + + "email_address": {"type": "null"}, + "phone_number": {"type": "null"} + }} + ], + "properties": { + "id": uuid, + "reference": {"type": ["string", "null"]}, + "email_address": {"type": ["string", "null"]}, + "phone_number": {"type": ["string", "null"]}, + "line_1": {"type": ["string", "null"]}, + "line_2": {"type": ["string", "null"]}, + "line_3": {"type": ["string", "null"]}, + "line_4": {"type": ["string", "null"]}, + "line_5": {"type": ["string", "null"]}, + "line_6": {"type": ["string", "null"]}, + "postcode": {"type": ["string", "null"]}, + "cost": {"type": "number"}, + "type": {"enum": ["sms", "letter", "email"]}, + "status": {"type": "string"}, + "template": template, + "created_at": {"type": "string"}, + "sent_at": {"type": ["string", "null"]}, + "completed_at": {"type": ["string", "null"]} + }, + "required": [ + # technically, all keys are required since we always have all of them + "id", "reference", "email_address", "phone_number", + "line_1", "line_2", "line_3", "line_4", "line_5", "line_6", "postcode", + "cost", "type", "status", "template", + "created_at", "sent_at", "completed_at" + ] +} + post_sms_request = { "$schema": "http://json-schema.org/draft-04/schema#", "description": "POST sms notification schema", @@ -26,20 +100,6 @@ sms_content = { "required": ["body"] } -# this may belong in a templates module -template = { - "$schema": "http://json-schema.org/draft-04/schema#", - "description": "template 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",