mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-02 09:15:19 -05:00
Validate broadcast against schema
This commit adds a JSONSchema which can validate the fields in an API call to create a broadcast. It takes the CAP XML schema as a starting point.
This commit is contained in:
97
app/v2/broadcast/broadcast_schemas.py
Normal file
97
app/v2/broadcast/broadcast_schemas.py
Normal file
@@ -0,0 +1,97 @@
|
||||
post_broadcast_schema = {
|
||||
"$schema": "http://json-schema.org/draft-07/schema",
|
||||
"type": "object",
|
||||
"required": [
|
||||
"reference",
|
||||
"category",
|
||||
"content",
|
||||
"areas",
|
||||
],
|
||||
"additionalProperties": False,
|
||||
"properties": {
|
||||
"reference": {
|
||||
"type": [
|
||||
"string",
|
||||
"null",
|
||||
],
|
||||
},
|
||||
"category": {
|
||||
"type": "string",
|
||||
"enum": [
|
||||
"Geo",
|
||||
"Met",
|
||||
"Safety",
|
||||
"Security",
|
||||
"Rescue",
|
||||
"Fire",
|
||||
"Health",
|
||||
"Env",
|
||||
"Transport",
|
||||
"Infra",
|
||||
"CBRNE",
|
||||
"Other",
|
||||
],
|
||||
},
|
||||
"expires": {
|
||||
"type": "string",
|
||||
"format": "date-time",
|
||||
},
|
||||
"content": {
|
||||
"type": "string",
|
||||
"minLength": 1,
|
||||
"maxLength": 1395,
|
||||
},
|
||||
"web": {
|
||||
"type": "string",
|
||||
"format": "uri",
|
||||
},
|
||||
"areas": {
|
||||
"type": "array",
|
||||
"minItems": 1,
|
||||
"items": {
|
||||
"$ref": "#/definitions/area",
|
||||
},
|
||||
},
|
||||
},
|
||||
"definitions": {
|
||||
"area": {
|
||||
"type": "object",
|
||||
"required": [
|
||||
"name",
|
||||
"polygons",
|
||||
],
|
||||
"additionalProperties": False,
|
||||
"properties": {
|
||||
"name": {
|
||||
"type": "string",
|
||||
},
|
||||
"polygons": {
|
||||
"type": "array",
|
||||
"minItems": 1,
|
||||
"items": {
|
||||
"oneOf": [
|
||||
{
|
||||
"$ref": "#/definitions/polygon",
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
"polygon": {
|
||||
"type": "array",
|
||||
"minItems": 4,
|
||||
"items": {
|
||||
"$ref": "#/definitions/coordinatePair",
|
||||
},
|
||||
},
|
||||
"coordinatePair": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "number"
|
||||
},
|
||||
"minItems": 2,
|
||||
"maxItems": 2,
|
||||
},
|
||||
},
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
from itertools import chain
|
||||
from flask import jsonify, request
|
||||
from app import authenticated_service, api_user
|
||||
from app.dao.dao_utils import dao_save_object
|
||||
from app.notifications.validators import check_service_has_permission
|
||||
from app.models import BROADCAST_TYPE, BroadcastMessage, BroadcastStatusType
|
||||
from app.schema_validation import validate
|
||||
from app.v2.broadcast import v2_broadcast_blueprint
|
||||
from app.v2.broadcast.broadcast_schemas import post_broadcast_schema
|
||||
|
||||
|
||||
@v2_broadcast_blueprint.route("", methods=['POST'])
|
||||
@@ -14,15 +17,19 @@ def create_broadcast():
|
||||
authenticated_service.permissions,
|
||||
)
|
||||
|
||||
request_json = request.get_json()
|
||||
broadcast_json = validate(request.get_json(), post_broadcast_schema)
|
||||
|
||||
broadcast_message = BroadcastMessage(
|
||||
service_id=authenticated_service.id,
|
||||
content=request_json['content'],
|
||||
reference=request_json['reference'],
|
||||
content=broadcast_json['content'],
|
||||
reference=broadcast_json['reference'],
|
||||
areas={
|
||||
"areas": [],
|
||||
"simple_polygons": request_json['polygons'],
|
||||
'areas': [
|
||||
area['name'] for area in broadcast_json['areas']
|
||||
],
|
||||
'simple_polygons': list(chain.from_iterable((
|
||||
area['polygons'] for area in broadcast_json['areas']
|
||||
)))
|
||||
},
|
||||
status=BroadcastStatusType.PENDING_APPROVAL,
|
||||
api_key_id=api_user.id,
|
||||
|
||||
Reference in New Issue
Block a user