"""Validation helpers for incoming request data.""" from typing import List from fastapi import HTTPException def validate_history(history: List[float], min_length: int = 2) -> None: """Raise HTTP 422 if the time-series history is unusable. Checks performed: * Minimum length constraint * No NaN or infinite values * At least some non-zero variance (all-identical series cannot be meaningfully extrapolated by a trend model, but we allow it and simply emit a warning via the response) """ if len(history) < min_length: raise HTTPException( status_code=422, detail=( f"history must contain at least {min_length} data points, " f"got {len(history)}" ), ) for i, v in enumerate(history): if v != v: # NaN check (works without math.isnan) raise HTTPException( status_code=422, detail=f"history[{i}] is NaN", ) if v == float("inf") or v == float("-inf"): raise HTTPException( status_code=422, detail=f"history[{i}] is infinite", )