Spaces:
Sleeping
Sleeping
| #!/usr/bin/env python3 | |
| """Request validation helpers.""" | |
| from pydantic import ValidationError | |
| from fastapi import HTTPException, status | |
| from logger import setup_logger | |
| logger = setup_logger("security.validation") | |
| async def validate_json(model_class, data: dict): | |
| """Validate and parse JSON against Pydantic model.""" | |
| try: | |
| return model_class(**data) | |
| except ValidationError as e: | |
| logger.warning(f"Validation error: {e}") | |
| raise HTTPException( | |
| status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, | |
| detail=f"Invalid request: {str(e)}" | |
| ) | |
| except Exception as e: | |
| logger.error(f"Parse error: {e}") | |
| raise HTTPException( | |
| status_code=status.HTTP_400_BAD_REQUEST, | |
| detail=f"Bad request: {str(e)}" | |
| ) | |