92 lines
2.6 KiB
Python
92 lines
2.6 KiB
Python
"""User schemas for request and response validation.
|
|
|
|
This module defines Pydantic schemas for user-related operations including
|
|
registration, authentication, and user information retrieval.
|
|
"""
|
|
|
|
from datetime import datetime
|
|
from typing import Optional
|
|
from uuid import UUID
|
|
|
|
from pydantic import BaseModel, EmailStr, Field
|
|
|
|
|
|
class UserBase(BaseModel):
|
|
"""Base user schema with common attributes."""
|
|
|
|
email: EmailStr = Field(description="User's email address")
|
|
is_active: bool = Field(default=True, description="Whether account is active")
|
|
is_superuser: bool = Field(default=False, description="Whether user is admin")
|
|
|
|
|
|
class UserCreate(BaseModel):
|
|
"""Schema for user registration.
|
|
|
|
Attributes:
|
|
email: User's email address (must be unique)
|
|
password: Plain text password (will be hashed)
|
|
"""
|
|
|
|
email: EmailStr = Field(description="User's email address")
|
|
password: str = Field(
|
|
min_length=8,
|
|
description="User's password (minimum 8 characters)",
|
|
)
|
|
|
|
|
|
class UserUpdate(BaseModel):
|
|
"""Schema for user updates.
|
|
|
|
All fields are optional to allow partial updates.
|
|
"""
|
|
|
|
email: Optional[EmailStr] = Field(default=None, description="New email address")
|
|
password: Optional[str] = Field(
|
|
default=None,
|
|
min_length=8,
|
|
description="New password (minimum 8 characters)",
|
|
)
|
|
is_active: Optional[bool] = Field(default=None, description="Account status")
|
|
|
|
|
|
class UserInDB(UserBase):
|
|
"""Schema representing user as stored in database.
|
|
|
|
Includes the hashed password - should never be returned in API responses.
|
|
"""
|
|
|
|
id: UUID
|
|
hashed_password: str = Field(description="Bcrypt hashed password")
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
class UserPublic(UserBase):
|
|
"""Schema for user information returned in API responses.
|
|
|
|
Excludes sensitive fields like hashed_password.
|
|
"""
|
|
|
|
id: UUID
|
|
created_at: datetime
|
|
|
|
class Config:
|
|
from_attributes = True
|
|
|
|
|
|
class Token(BaseModel):
|
|
"""Schema for authentication token response."""
|
|
|
|
access_token: str = Field(description="JWT access token")
|
|
token_type: str = Field(default="bearer", description="Token type")
|
|
|
|
|
|
class TokenPayload(BaseModel):
|
|
"""Schema for JWT token payload."""
|
|
|
|
sub: Optional[str] = Field(default=None, description="Subject (user ID)")
|
|
exp: Optional[datetime] = Field(default=None, description="Expiration time")
|
|
type: Optional[str] = Field(default=None, description="Token type") |