from pydantic import BaseModel, Field, ValidationError class User(BaseModel): name: str = Field(..., title="使用者名稱", min_length=2) age: int = Field(..., gt=0, le=150, description="年齡必須介於 1 到 150 之間") email: str # 測試資料 try: user = User(name="Tim", age=30, email="tim@example.com") print(user.dict()) # 轉換為字典 except ValidationError as e: print(e)