import { ZodError } from 'zod'; /** * Validate request body against a Zod schema * @param {import('zod').ZodSchema} schema */ export const validateBody = (schema) => (req, res, next) => { try { req.body = schema.parse(req.body); next(); } catch (err) { if (err instanceof ZodError) { const errors = err.errors.map((e) => ({ field: e.path.join('.'), message: e.message, })); return res.status(400).json({ success: false, error: 'Validation failed', details: errors, }); } next(err); } }; /** * Validate request params against a Zod schema * @param {import('zod').ZodSchema} schema */ export const validateParams = (schema) => (req, res, next) => { try { req.params = schema.parse(req.params); next(); } catch (err) { if (err instanceof ZodError) { return res.status(400).json({ success: false, error: 'Invalid parameters', details: err.errors.map((e) => ({ field: e.path.join('.'), message: e.message })), }); } next(err); } };