23 lines
631 B
TypeScript
23 lines
631 B
TypeScript
import { Request, Response, NextFunction } from 'express';
|
|
import Joi from 'joi';
|
|
|
|
export function Validate<T = any>(schema: Joi.ObjectSchema<T>) {
|
|
return function (target: any, propertyKey: string, descriptor: PropertyDescriptor) {
|
|
const originalMethod = descriptor.value;
|
|
|
|
descriptor.value = async function (req: Request, res: Response, next: NextFunction) {
|
|
try {
|
|
await schema.validateAsync(req.body);
|
|
} catch (error) {
|
|
logging.error(error);
|
|
|
|
return res.status(400).json(error);
|
|
}
|
|
|
|
return originalMethod.call(this, req, res, next);
|
|
};
|
|
|
|
return descriptor;
|
|
};
|
|
}
|