Add input validation schema for SignupFormViewSet create

This commit is contained in:
Aarni Halinen
2022-07-28 21:10:21 +03:00
parent dd0254a08e
commit 8bb6e9e9a7
2 changed files with 297 additions and 3 deletions
+291
View File
@@ -0,0 +1,291 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"CheckboxQuestion": {
"properties": {
"id": {
"type": "string"
},
"options": {
"properties": {
"enum": {
"items": {
"type": "string"
},
"type": "array"
},
"enumNames_en": {
"items": {
"type": "string"
},
"type": "array"
},
"enumNames_fi": {
"items": {
"type": "string"
},
"type": "array"
}
},
"type": "object"
},
"required": {
"type": "boolean"
},
"title_en": {
"type": "string"
},
"title_fi": {
"type": "string"
},
"type": {
"enum": [
"checkbox"
],
"type": "string"
}
},
"type": "object"
},
"EmailQuestion": {
"properties": {
"id": {
"type": "string"
},
"required": {
"type": "boolean"
},
"title_en": {
"type": "string"
},
"title_fi": {
"type": "string"
},
"type": {
"enum": [
"email"
],
"type": "string"
}
},
"type": "object"
},
"InfoQuestion": {
"properties": {
"description_en": {
"type": "string"
},
"description_fi": {
"type": "string"
},
"id": {
"type": "string"
},
"required": {
"enum": [
false
],
"type": "boolean"
},
"title_en": {
"type": "string"
},
"title_fi": {
"type": "string"
},
"type": {
"enum": [
"info"
],
"type": "string"
}
},
"type": "object"
},
"IntegerQuestion": {
"properties": {
"id": {
"type": "string"
},
"options": {
"properties": {
"enum": {
"anyOf": [
{
"items": [
{
"items": {
"type": "string"
},
"type": "array"
}
],
"maxItems": 1,
"minItems": 1,
"type": "array"
},
{
"items": [
{
"items": {
"type": "string"
},
"type": "array"
},
{
"items": {
"type": "string"
},
"type": "array"
}
],
"maxItems": 2,
"minItems": 2,
"type": "array"
}
]
}
},
"type": "object"
},
"required": {
"type": "boolean"
},
"title_en": {
"type": "string"
},
"title_fi": {
"type": "string"
},
"type": {
"enum": [
"integer"
],
"type": "string"
}
},
"type": "object"
},
"NameQuestion": {
"properties": {
"id": {
"type": "string"
},
"required": {
"type": "boolean"
},
"title_en": {
"type": "string"
},
"title_fi": {
"type": "string"
},
"type": {
"enum": [
"name"
],
"type": "string"
}
},
"type": "object"
},
"RadioQuestion": {
"properties": {
"id": {
"type": "string"
},
"options": {
"properties": {
"enum": {
"items": {
"type": "string"
},
"type": "array"
},
"enumNames_en": {
"items": {
"type": "string"
},
"type": "array"
},
"enumNames_fi": {
"items": {
"type": "string"
},
"type": "array"
}
},
"type": "object"
},
"required": {
"enum": [
true
],
"type": "boolean"
},
"title_en": {
"type": "string"
},
"title_fi": {
"type": "string"
},
"type": {
"enum": [
"radiobutton"
],
"type": "string"
}
},
"type": "object"
},
"TextQuestion": {
"properties": {
"id": {
"type": "string"
},
"required": {
"type": "boolean"
},
"title_en": {
"type": "string"
},
"title_fi": {
"type": "string"
},
"type": {
"enum": [
"text"
],
"type": "string"
}
},
"type": "object"
}
},
"items": {
"anyOf": [
{
"$ref": "#/definitions/TextQuestion"
},
{
"$ref": "#/definitions/InfoQuestion"
},
{
"$ref": "#/definitions/IntegerQuestion"
},
{
"$ref": "#/definitions/RadioQuestion"
},
{
"$ref": "#/definitions/CheckboxQuestion"
},
{
"$ref": "#/definitions/EmailQuestion"
},
{
"$ref": "#/definitions/NameQuestion"
}
]
},
"type": "array"
}
+6 -3
View File
@@ -1,5 +1,6 @@
"""Webapp views."""
import json
from jwt import decode
from jwt.exceptions import InvalidTokenError
from django.utils import timezone
@@ -29,6 +30,10 @@ from webapp.serializers import *
from webapp.utils import admin_send_email_signupees, decode_base64_file
with open("./webapp/questionSchema.json", "r") as file:
QUESTION_SCHEMA = json.load(file)
class SignupPermission(BasePermission):
def has_permission(self, request, view):
if request.method == "POST":
@@ -125,9 +130,7 @@ class SignupFormViewSet(ModelViewSet):
def create(self, request, *args, **kwargs):
try:
schema = {
"type": "array",
}
schema = QUESTION_SCHEMA
validate(instance=request.data["questions"], schema=schema)
return super().create(request, *args, **kwargs)
except ValidationError as err: