import re from typing import Optional from pydantic import BaseModel, validator # Try to import EmailStr, but fallback to str if email-validator is not installed try: from pydantic import EmailStr email_type = EmailStr except ImportError: email_type = str # Shared properties class UserBase(BaseModel): email: Optional[email_type] = None is_active: Optional[bool] = True is_superuser: bool = False full_name: Optional[str] = None # Add email validation if using fallback str type @validator('email') def validate_email(cls, v): if v is None: return v if not isinstance(v, str): return v # Basic email validation pattern pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$' if not re.match(pattern, v): raise ValueError('Invalid email format') return v # Properties to receive via API on creation class UserCreate(UserBase): email: email_type password: str # Properties to receive via API on update class UserUpdate(UserBase): password: Optional[str] = None class UserInDBBase(UserBase): id: Optional[int] = None class Config: from_attributes = True # Additional properties to return via API class User(UserInDBBase): pass # Additional properties stored in DB class UserInDB(UserInDBBase): hashed_password: str