import type { NextFunction, Request, Response } from 'express'; import type { ZodTypeAny } from 'zod'; type Target = 'body' | 'query' | 'params'; export function validate(target: Target, schema: ZodTypeAny) { return (req: Request, _res: Response, next: NextFunction) => { const result = schema.safeParse(req[target]); if (!result.success) { next(result.error); return; } req.validated ??= {}; req.validated[target] = result.data; next(); }; }