11
This commit is contained in:
15
app/schemas/__init__.py
Normal file
15
app/schemas/__init__.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from app.schemas.auth import (
|
||||
LoginRequest,
|
||||
RegisterRequest,
|
||||
TokenResponse,
|
||||
UserPublic,
|
||||
)
|
||||
from app.schemas.common import ApiResponse
|
||||
|
||||
__all__ = [
|
||||
"ApiResponse",
|
||||
"LoginRequest",
|
||||
"RegisterRequest",
|
||||
"TokenResponse",
|
||||
"UserPublic",
|
||||
]
|
||||
45
app/schemas/auth.py
Normal file
45
app/schemas/auth.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
||||
|
||||
|
||||
class RegisterRequest(BaseModel):
|
||||
username: str = Field(min_length=1, max_length=64)
|
||||
password: str = Field(min_length=6, max_length=128)
|
||||
confirm_password: str = Field(min_length=6, max_length=128, alias="confirmPassword")
|
||||
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
@field_validator("username")
|
||||
@classmethod
|
||||
def strip_username(cls, value: str) -> str:
|
||||
return value.strip()
|
||||
|
||||
@model_validator(mode="after")
|
||||
def passwords_match(self) -> "RegisterRequest":
|
||||
if self.password != self.confirm_password:
|
||||
raise ValueError("两次输入的密码不一致")
|
||||
return self
|
||||
|
||||
|
||||
class LoginRequest(BaseModel):
|
||||
username: str = Field(min_length=1, max_length=64)
|
||||
password: str = Field(min_length=1, max_length=128)
|
||||
|
||||
model_config = ConfigDict(populate_by_name=True)
|
||||
|
||||
@field_validator("username")
|
||||
@classmethod
|
||||
def strip_username(cls, value: str) -> str:
|
||||
return value.strip()
|
||||
|
||||
|
||||
class UserPublic(BaseModel):
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
id: int
|
||||
username: str
|
||||
|
||||
|
||||
class TokenResponse(BaseModel):
|
||||
access_token: str
|
||||
token_type: str = "bearer"
|
||||
user: UserPublic
|
||||
15
app/schemas/common.py
Normal file
15
app/schemas/common.py
Normal file
@@ -0,0 +1,15 @@
|
||||
from typing import Generic, TypeVar
|
||||
|
||||
from pydantic import BaseModel, ConfigDict
|
||||
|
||||
T = TypeVar("T")
|
||||
|
||||
|
||||
class ApiResponse(BaseModel, Generic[T]):
|
||||
"""与 aiclient 前端约定的统一响应结构。"""
|
||||
|
||||
model_config = ConfigDict(from_attributes=True)
|
||||
|
||||
ok: bool
|
||||
message: str = ""
|
||||
data: T | None = None
|
||||
Reference in New Issue
Block a user