feat: add Zod validation to predict/batch route (task 4.2)

Add BatchPredictRequestSchema with Zod to validate pair, timeframe,
start_date, and end_date fields. Returns HTTP 400 with flattened error
details on invalid input. Forward only validated data to the inference
service.

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

View file

@ -1,12 +1,30 @@
import { NextRequest, NextResponse } from 'next/server';
import { z } from 'zod';
const INFERENCE_API_URL = process.env.INFERENCE_API_URL || 'http://localhost:8001';
const INFERENCE_BATCH_TIMEOUT = parseInt(process.env.INFERENCE_BATCH_TIMEOUT || '120000', 10);
const BatchPredictRequestSchema = z.object({
pair: z.string().min(1),
timeframe: z.string().min(1),
start_date: z.string().min(1),
end_date: z.string().min(1),
});
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const result = BatchPredictRequestSchema.safeParse(body);
if (!result.success) {
return NextResponse.json(
{ error: 'Invalid request', details: result.error.flatten() },
{ status: 400 }
);
}
const validatedBody = result.data;
// Forward request to Python inference service
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), INFERENCE_BATCH_TIMEOUT);
@ -18,7 +36,7 @@ export async function POST(request: NextRequest) {
'Content-Type': 'application/json',
'X-API-Key': process.env.API_KEY || '',
},
body: JSON.stringify(body),
body: JSON.stringify(validatedBody),
signal: controller.signal,
});