-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
30 lines (25 loc) · 896 Bytes
/
models.py
File metadata and controls
30 lines (25 loc) · 896 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from datetime import datetime
from typing import Optional
from pydantic import BaseModel, Field, EmailStr, validator
class SurveySubmission(BaseModel):
name: str = Field(..., min_length=1, max_length=100)
email: str
age: str
consent: bool = Field(..., description="Must be true to accept")
rating: int = Field(..., ge=1, le=5)
comments: Optional[str] = Field(None, max_length=1000)
user_agent: Optional[str] = None
submission_id: Optional[str] = None
@validator("comments")
def _strip_comments(cls, v):
return v.strip() if isinstance(v, str) else v
@validator("consent")
def _must_consent(cls, v):
if v is not True:
raise ValueError("consent must be true")
return v
#Good example of inheritance
class StoredSurveyRecord(SurveySubmission):
received_at: datetime
ip: str
user_agent: str