refactor: made pydantic check the input.

no longer by the code itself.

ref: N25B-198
This commit is contained in:
JobvAlewijk
2025-11-18 12:35:44 +01:00
parent 2ed2a84f13
commit 39c07dd3cf
2 changed files with 21 additions and 37 deletions

View File

@@ -1,9 +1,7 @@
import logging
from fastapi import APIRouter, HTTPException, Request
from pydantic import ValidationError
from fastapi import APIRouter, Request
from control_backend.schemas.message import Message
from control_backend.schemas.program import Program
logger = logging.getLogger(__name__)
@@ -11,20 +9,12 @@ router = APIRouter()
@router.post("/program", status_code=202)
async def receive_message(program: Message, request: Request):
async def receive_message(program: Program, request: Request):
"""
Receives a BehaviorProgram as a stringified JSON list inside `message`.
Receives a BehaviorProgram, pydantic checks it.
Converts it into real Phase objects.
"""
logger.debug("Received raw program: %s", program)
raw_str = program.message # This is the JSON string
# Validate program
try:
program = Program.model_validate_json(raw_str)
except ValidationError as e:
logger.error("Failed to validate program JSON: %s", e)
raise HTTPException(status_code=400, detail="Not a valid program") from None
# send away
topic = b"program"