UNPKG

@overture-stack/lyric

Version:
36 lines (35 loc) 1.28 kB
import { ZodError } from 'zod'; import { BadRequest, InternalServerError } from './errors.js'; /** * Validate the body using Zod parse * @param schema Zod objects used to validate request * @returns Throws a Bad Request when validation fails */ export function validateRequest(schema, handler) { const LOG_MODULE = 'REQUEST_VALIDATION'; return async (req, res, next) => { try { if (schema.body) { schema.body.parse(req.body); } if (schema.query) { schema.query.parse(req.query); } if (schema.pathParams) { schema.pathParams.parse(req.params); } return handler(req, res, next); } catch (error) { if (error instanceof ZodError) { const errorMessages = error.errors.map((issue) => `${issue.path.join('.')} is ${issue.message}`).join(' | '); console.log(LOG_MODULE, req.method, req.url, JSON.stringify(errorMessages)); next(new BadRequest(errorMessages)); } else { console.error(LOG_MODULE, req.method, req.url, 'Internal Server Error'); next(new InternalServerError()); } } }; }