aboutsummaryrefslogtreecommitdiffstats
path: root/backend/models/form.py
blob: 3c4206734406232b9fd7d4146227999ff1a159ed (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import typing as t

from pydantic import BaseModel, Field, validator

from backend.constants import FormFeatures
from backend.models import Question


class Form(BaseModel):
    """Schema model for form."""

    id: str = Field(alias="_id")
    features: t.List[str]
    questions: t.List[Question]

    @validator("features")
    def validate_features(self, value: t.List[str]) -> t.Optional[t.List[str]]:
        """Validates is all features in allowed list."""
        # Uppercase everything to avoid mixed case in DB
        value = [v.upper() for v in value]
        if not all(v in FormFeatures.__members__.values() for v in value):
            raise ValueError("Form features list contains one or more invalid values.")

        return value