feat: add Zod schema validation to model/load route

Validate run_id in POST /api/model/load using Zod:
- run_id must be a non-empty string matching /^[a-zA-Z0-9_-]+$/
- Returns HTTP 400 with error details if validation fails
- Validated data is forwarded to the inference service

Marks task 4.3 as complete in tasks.md.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Marko Djordjevic 2026-02-18 11:13:13 +01:00
parent 5c399037c3
commit 4cffc223b3
2 changed files with 18 additions and 2 deletions

View file

@ -28,7 +28,7 @@
- [x] 4.1 `[sonnet]` Add Zod schema validation to `src/app/api/predict/route.ts` (validate pair, timeframe, candles array)
- [x] 4.2 `[sonnet]` Add Zod schema validation to `src/app/api/predict/batch/route.ts` (validate pair, timeframe, start_date, end_date)
- [ ] 4.3 `[sonnet]` Add Zod schema validation to `src/app/api/model/load/route.ts` (validate run_id)
- [x] 4.3 `[sonnet]` Add Zod schema validation to `src/app/api/model/load/route.ts` (validate run_id)
- [ ] 4.4 `[sonnet]` Add Zod schema validation to `src/app/api/training/start/route.ts` (validate model_type)
- [ ] 4.5 `[sonnet]` Add Zod schema validation to `src/app/api/patterns/detect/route.ts` (validate candles, patterns array)
- [ ] 4.6 `[sonnet]` Replace `error.message` with generic `"Internal server error"` in all catch blocks across 7+ route files: `health/route.ts`, `candles/route.ts`, `annotations/route.ts`, `annotations/[id]/route.ts`, `upload/route.ts`, `export/route.ts`, `span-annotations/export/route.ts`

View file

@ -1,8 +1,13 @@
import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';
const INFERENCE_API_URL = process.env.INFERENCE_API_URL || 'http://localhost:8001';
const INFERENCE_API_TIMEOUT = parseInt(process.env.INFERENCE_API_TIMEOUT || '30000', 10);
const ModelLoadRequestSchema = z.object({
run_id: z.string().min(1).regex(/^[a-zA-Z0-9_-]+$/, 'run_id must contain only alphanumeric characters, underscores, and hyphens'),
});
export async function POST(request: NextRequest) {
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), INFERENCE_API_TIMEOUT);
@ -10,10 +15,21 @@ export async function POST(request: NextRequest) {
try {
const body = await request.json();
const result = ModelLoadRequestSchema.safeParse(body);
if (!result.success) {
clearTimeout(timeoutId);
return NextResponse.json(
{ error: 'Invalid request', details: result.error.flatten() },
{ status: 400 }
);
}
const validatedBody = result.data;
const response = await fetch(`${INFERENCE_API_URL}/model/load`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'X-API-Key': process.env.API_KEY || '' },
body: JSON.stringify(body),
body: JSON.stringify(validatedBody),
signal: controller.signal,
});
clearTimeout(timeoutId);