-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpydantic_API.py
More file actions
51 lines (33 loc) · 926 Bytes
/
pydantic_API.py
File metadata and controls
51 lines (33 loc) · 926 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from fastapi import FastAPI
from pydantic import BaseModel, Field, EmailStr, ConfigDict
app = FastAPI()
data = {
"email": "abc@mail.ru",
"bio": "Я пирожок",
"age": 12,
}
data_wo_age = {
"email": "abc@mail.ru",
"bio": "Я пирожок",
# "gender": "male",
# "birthday": "2022"
}
class UserSchema(BaseModel):
email: EmailStr
bio: str = Field(max_length=10)
model_config = ConfigDict(extra='forbid') # убираем доп инфу кроме имаила и био
users = []
@app.get("/")
def root():
return "hello"
@app.post("/users")
def add_user(user: UserSchema):
users.append(user)
return {"ok": True, "msg": "Юзер добавлен"}
@app.get("/users")
def get_users():
return users
# class UserAgeSchema(UserSchema):
# age: int = Field(ge=0, le=130)
# print(repr(UserSchema(**data_wo_age)))
# print(repr(UserAgeSchema(**data)))