Spaces:
Sleeping
Sleeping
File size: 1,832 Bytes
94f31ec | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | """
Request Validation Utilities.
This module provides validation helpers for request parameters,
following FastAPI best practices.
"""
import uuid
from pydantic import BaseModel, Field, field_validator
class UUIDParam(BaseModel):
"""Base model for UUID path parameters with validation."""
uuid: str = Field(..., description="UUID parameter")
@field_validator("uuid")
@classmethod
def validate_uuid(cls, v: str) -> str:
"""Validate that the string is a valid UUID."""
try:
uuid.UUID(v)
except ValueError:
raise ValueError(f"Invalid UUID format: {v}")
return v
def validate_uuid(v: str) -> str:
"""Standalone UUID validator function."""
try:
uuid.UUID(v)
except ValueError:
raise ValueError(f"Invalid UUID format: {v}")
return v
class SessionIdParam(BaseModel):
"""Validated session ID parameter."""
session_id: str = Field(..., min_length=36, max_length=36)
@field_validator("session_id")
@classmethod
def validate_session_id(cls, v: str) -> str:
"""Validate session ID is a valid UUID."""
try:
uuid.UUID(v)
except ValueError:
raise ValueError("Invalid session_id format - must be a valid UUID")
return v
class ProjectIdParam(BaseModel):
"""Validated project ID parameter."""
project_id: int = Field(..., gt=0)
@field_validator("project_id")
@classmethod
def validate_project_id(cls, v: int) -> int:
"""Validate project ID is positive."""
if v <= 0:
raise ValueError("project_id must be a positive integer")
return v
# Re-export validation errors for consistent error handling
__all__ = [
"UUIDParam",
"SessionIdParam",
"ProjectIdParam",
"validate_uuid",
]
|