diff options
author | 2020-11-30 20:26:01 +0200 | |
---|---|---|
committer | 2020-11-30 20:26:01 +0200 | |
commit | 53b3bebeca248e115f2d0e73e913aa34d224c2d3 (patch) | |
tree | 048f4c06d490ea0e34dca275d4231f374ce19dce /backend/models/form.py | |
parent | Add Enum for form features (diff) |
Create form object model
Diffstat (limited to 'backend/models/form.py')
-rw-r--r-- | backend/models/form.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/backend/models/form.py b/backend/models/form.py new file mode 100644 index 0000000..6e1a9ab --- /dev/null +++ b/backend/models/form.py @@ -0,0 +1,25 @@ +import typing as t + +from pydantic import BaseModel, Field, validator + +from backend.constants import FormFeatures +from backend.models import Question +from backend.models.types import ObjectId + + +class Form(BaseModel): + """Schema model for form.""" + + id: ObjectId = 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 |