tianji/src/server/middleware/validate.ts

22 lines
596 B
TypeScript
Raw Normal View History

2023-09-02 14:53:57 +00:00
import { Handler } from 'express';
import { validationResult, ValidationChain } from 'express-validator';
import { compose } from 'compose-middleware';
export function validate(...validator: ValidationChain[]): Handler {
const handler: Handler = (req, res, next) => {
const result = validationResult(req);
if (!result.isEmpty()) {
res
.status(422)
.json({ errors: result.array(), message: 'Invalid Parameters' });
} else {
next();
}
};
return compose([...validator, handler as any]);
}
export { body, query, param } from 'express-validator';