File size: 804 Bytes
e16db8b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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)}"
        )