from pydantic import BaseModel, EmailStr, field_validator import re class RegisterRequest(BaseModel): username: str email: EmailStr password: str @field_validator("username") @classmethod def username_alphanumeric(cls, v: str) -> str: if not re.match(r"^[a-zA-Z0-9_]{3,20}$", v): raise ValueError("用户名只能包含字母、数字、下划线,长度3-20位") return v @field_validator("password") @classmethod def password_min_length(cls, v: str) -> str: if len(v) < 6: raise ValueError("密码至少6位") return v class LoginRequest(BaseModel): username: str password: str class TokenResponse(BaseModel): access_token: str refresh_token: str token_type: str = "bearer" class RefreshRequest(BaseModel): refresh_token: str class UserOut(BaseModel): id: int username: str email: str is_active: bool is_admin: bool model_config = {"from_attributes": True}