| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- # app/schemas/user.py
- from pydantic import BaseModel, EmailStr, Field, validator
- from typing import Optional
- from datetime import datetime, timezone, timedelta
-
- class UserBase(BaseModel):
- """用户基础模型"""
- username: str = Field(..., min_length=3, max_length=50, example="john_doe")
- email: EmailStr = Field(..., example="user@example.com")
- full_name: Optional[str] = Field(None, example="John Doe")
-
- class UserCreate(UserBase):
- """创建用户模型"""
- password: str = Field(..., min_length=6, example="password123")
- password_confirm: str = Field(..., example="password123")
-
- @validator('password_confirm')
- def passwords_match(cls, v, values, **kwargs):
- if 'password' in values and v != values['password']:
- raise ValueError('密码不匹配')
- return v
-
- class UserLogin(BaseModel):
- """用户登录模型"""
- username: str = Field(..., example="john_doe")
- password: str = Field(..., example="password123")
-
- class UserUpdate(BaseModel):
- """更新用户模型"""
- full_name: Optional[str] = None
- email: Optional[EmailStr] = None
-
- class UserResponse(UserBase):
- """用户响应模型"""
- id: int
- is_active: bool = True
- is_verified: bool = False
- created_at: datetime
-
- class Config:
- from_attributes = True
-
- class UserProfile(UserResponse):
- """用户详情模型"""
- last_login: Optional[datetime] = None
- avatar: Optional[str] = None
-
- class PasswordChange(BaseModel):
- """修改密码模型"""
- current_password: str
- new_password: str = Field(..., min_length=6)
- confirm_password: str
-
- @validator('confirm_password')
- def passwords_match(cls, v, values, **kwargs):
- if 'new_password' in values and v != values['new_password']:
- raise ValueError('新密码不匹配')
- return v
-
- __all__ = [
- "UserBase",
- "UserCreate",
- "UserLogin",
- "UserUpdate",
- "UserResponse",
- "UserProfile",
- "PasswordChange"
- ]
|