chatbridge/server/middleware/validate.ts
ansible user/allowed to read system logs 22340d8a96 working group controller
2023-07-24 18:40:22 -07:00

24 lines
No EOL
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);
}
};