UNPKG

mcp-product-manager

Version:

MCP Orchestrator for task and project management with web interface

120 lines 3.58 kB
import { ZodError } from 'zod'; /** * Create validation middleware for request body */ export function validateBody(schema) { return async (req, res, next) => { try { // Validate and transform the body req.body = await schema.parseAsync(req.body); next(); } catch (error) { if (error instanceof ZodError) { const validationErrors = error.errors.map(err => ({ field: err.path.join('.'), message: err.message })); res.status(400).json({ success: false, error: { code: 'VALIDATION_ERROR', message: 'Request validation failed', details: validationErrors } }); } else { next(error); } } }; } /** * Create validation middleware for query parameters */ export function validateQuery(schema) { return async (req, res, next) => { try { // Validate and transform the query req.query = await schema.parseAsync(req.query); next(); } catch (error) { if (error instanceof ZodError) { const validationErrors = error.errors.map(err => ({ field: err.path.join('.'), message: err.message })); res.status(400).json({ success: false, error: { code: 'VALIDATION_ERROR', message: 'Query parameter validation failed', details: validationErrors } }); } else { next(error); } } }; } /** * Create validation middleware for route params */ export function validateParams(schema) { return async (req, res, next) => { try { // Validate and transform the params req.params = await schema.parseAsync(req.params); next(); } catch (error) { if (error instanceof ZodError) { const validationErrors = error.errors.map(err => ({ field: err.path.join('.'), message: err.message })); res.status(400).json({ success: false, error: { code: 'VALIDATION_ERROR', message: 'Route parameter validation failed', details: validationErrors } }); } else { next(error); } } }; } /** * Validate data against a schema (for internal use) */ export async function validate(schema, data) { return schema.parseAsync(data); } /** * Safe validation that returns result or errors */ export async function safeValidate(schema, data) { try { const validated = await schema.parseAsync(data); return { success: true, data: validated }; } catch (error) { if (error instanceof ZodError) { const errors = error.errors.map(err => ({ field: err.path.join('.'), message: err.message })); return { success: false, errors }; } throw error; } } //# sourceMappingURL=validation.js.map