24 lines
741 B
TypeScript
24 lines
741 B
TypeScript
|
import { Request, Response, NextFunction } from 'express';
|
||
|
import { AnyZodObject, ZodError } from 'zod';
|
||
|
|
||
|
export const validate =
|
||
|
(schema: AnyZodObject) =>
|
||
|
(req: Request, res: Response, next: NextFunction) => {
|
||
|
try {
|
||
|
schema.parse({
|
||
|
params: req.params,
|
||
|
query: req.query,
|
||
|
body: req.body,
|
||
|
});
|
||
|
|
||
|
next();
|
||
|
} catch (error) {
|
||
|
if (error instanceof ZodError) {
|
||
|
return res.status(400).json({
|
||
|
status: 'fail',
|
||
|
errors: error.errors,
|
||
|
});
|
||
|
}
|
||
|
next(error);
|
||
|
}
|
||
|
};
|