实施 express-validator 命令式验证时,正确的验证类型是什么?
What is the right type for validations when implementing express-validator imperative validations?
尝试在 TypeScript 中实现 express-validator’s imperative validations,但找不到 validations
的类型。
// can be reused by many routes
const validate = validations => {
return async (req, res, next) => {
await Promise.all(validations.map(validation => validation.run(req)));
const errors = validationResult(req);
if (errors.isEmpty()) {
return next();
}
res.status(422).json({ errors: errors.array() });
};
};
app.post('/api/create-user', validate([
body('email').isEmail(),
body('password').isLength({ min: 6 })
]), async (req, res, next) => {
// request is guaranteed to not have any validation errors.
const user = await User.create({ ... });
});
找到了!类型是 ValidationChain[]
.
尝试在 TypeScript 中实现 express-validator’s imperative validations,但找不到 validations
的类型。
// can be reused by many routes
const validate = validations => {
return async (req, res, next) => {
await Promise.all(validations.map(validation => validation.run(req)));
const errors = validationResult(req);
if (errors.isEmpty()) {
return next();
}
res.status(422).json({ errors: errors.array() });
};
};
app.post('/api/create-user', validate([
body('email').isEmail(),
body('password').isLength({ min: 6 })
]), async (req, res, next) => {
// request is guaranteed to not have any validation errors.
const user = await User.create({ ... });
});
找到了!类型是 ValidationChain[]
.