-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschemas.py
114 lines (73 loc) · 2.12 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from pydantic import BaseModel, EmailStr, Field
from typing import Optional, List
from enum import Enum
import re
class PhoneNumberStr(str):
@classmethod
def __get_pydantic_json_schema__(cls, model, context):
return {'type': 'string', 'format': 'phonenumber', 'example': '+98-9151234567'}
@classmethod
def __get_validators__(cls):
yield cls.validate
@classmethod
def validate(cls, v, x):
if not re.match(r"^\+\d{1,3}-\d{6,12}$", v):
raise ValueError("Not a valid phone number")
return v
# ========== Base ==========
class BaseResponse(BaseModel):
message: str
code: int
class HTTPError(BaseModel):
message: str
code: str
class Config:
json_schema_extra = {
"example": {"detail": "HTTPException raised.", 'code':1001},
}
# ========== User ==========
class UserRole(str ,Enum):
ADMIN = 'ADMIN'
USER = 'USER'
class UserInfoResponse(BaseModel):
username: str
name: str
email: Optional[EmailStr] = Field(default=None)
phone_number: PhoneNumberStr
role: UserRole
class Config:
from_attributes = True
class UserRegister(BaseModel):
username : str
password: str
name : str
email: Optional[EmailStr] = Field(default=None)
phone_number: PhoneNumberStr
role: UserRole
class UserUpdatePassword(BaseModel):
new_password: str
class UserUpdateRole(BaseModel):
username : str
new_role: UserRole
class UserUpdateInfo(BaseModel):
new_email: Optional[EmailStr] = Field(default=None)
new_phone_number: Optional[PhoneNumberStr] = Field(default=None)
new_name: Optional[str] = Field(default=None)
class UserDelete(BaseModel):
username : str
# ========== Auth ==========
class UserAuthResponse(BaseModel):
access_token: str
type_token: str
class Token(BaseModel):
access_token: str
token_type: str
class TokenData(BaseModel):
user_id: int
username: str
role: UserRole
scopes: List[str] = []
class TokenUser(BaseModel):
user_id: int
username: str
role: UserRole