17 lines
450 B
TypeScript
17 lines
450 B
TypeScript
import { Request, Response, NextFunction } from 'express';
|
|
import { Schema } from 'zod';
|
|
|
|
export function validateRequest(schema: Schema) {
|
|
return async (req: Request, res: Response, next: NextFunction) => {
|
|
try {
|
|
await schema.parseAsync({
|
|
body: req.body,
|
|
query: req.query,
|
|
params: req.params
|
|
});
|
|
next();
|
|
} catch (error) {
|
|
res.status(400).json({ error: 'Invalid request data' });
|
|
}
|
|
};
|
|
} |