-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathmodels.py
More file actions
27 lines (22 loc) · 832 Bytes
/
models.py
File metadata and controls
27 lines (22 loc) · 832 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
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: EmailStr
age: int = Field(..., ge=13, le=120)
consent: bool = Field(..., description="Must be true to accept")
rating: int = Field(..., ge=1, le=5)
comments: Optional[str] = Field(None, max_length=1000)
@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