-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschemas.py
56 lines (41 loc) · 1.58 KB
/
schemas.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from typing import Optional, Union
from fastapi import Form
from pydantic import BaseModel, EmailStr, root_validator, validator
class ForceUnset:
pass
class SignupForm(BaseModel):
email: EmailStr
password: str
password_confirmation: str
def __init__(self, email: EmailStr = Form(...), password: str = Form(...), password_confirmation: str = Form(...)):
super().__init__(email=email, password=password, password_confirmation=password_confirmation)
# self.email = email
# self.password = password
# self.password_confirmation = password_confirmation
@validator("password")
def password_is_not_empty(cls, value):
if len(value) == 0:
raise ValueError("Password can't be an empty string.")
return value
@root_validator
def passwords_match(cls, values):
pw1, pw2 = values.get("password"), values.get("password_confirmation")
if pw1 != pw2:
raise ValueError(f"Passwords do not match ({pw1} vs. {pw2}).")
return values
class LoginForm(BaseModel):
email: EmailStr
password: str
def __init__(self, email: EmailStr = Form(...), password: str = Form(...)):
super().__init__(email=email, password=password)
@validator("password")
def password_is_not_empty(cls, value):
if len(value) == 0:
raise ValueError("Password can't be an empty string.")
return value
class UpdateUserModel(BaseModel):
id: str
password: Optional[str]
username: Union[str, ForceUnset, None]
class Config:
arbitrary_types_allowed = True