easy-express-cwa
Version:
CLI tool to setup a common Express.js backend developed by codewithashim
21 lines (18 loc) • 531 B
text/typescript
import { NextFunction, Request, Response } from 'express';
import { AnyZodObject, ZodEffects } from 'zod';
const validateRequest =
(schema: AnyZodObject | ZodEffects<AnyZodObject>) =>
async (req: Request, res: Response, next: NextFunction): Promise<void> => {
try {
await schema.parseAsync({
body: req.body,
query: req.query,
params: req.params,
cookies: req.cookies,
});
return next();
} catch (error) {
next(error);
}
};
export default validateRequest;