export function valUPM() {
return (req: Request, _res: Response, next: NextFunction) => {
req
.checkBody(
"paymentType",
`paymentType: ${messages.getFromSession(req, "mustNotBeEmpty")}`
)
.notEmpty();
if (req.body.paymentType === "USP") {
req
.checkBody(
"storeId",
`storeId: ${messages.getFromSession(req, "mustNotBeEmpty")}`
)
.notEmpty();
} else if (req.body.paymentType === "CC") {
if (req.body.register) {
req
.checkBody(
"register",
`register: ${messages.getFromSession(req, "mustBeBoolean")}`
)
.isBoolean();
} else {
req
.checkBody(
"register",
`register: ${messages.getFromSession(req, "mustNotBeEmpty")}`
)
.notEmpty();
}
}
req.getValidationResult().then(errs => {
if (errs.isEmpty()) {
return next();
}
const error = new BFFError(
400,
"BadRequest",
1,
errs.array().map(error => {
return { [error.param]: error.msg };
})
);
return next(Error(JSON.stringify(error)));
});
};
}
更改API后,如何在快速验证器中实现这种类型的逻辑
在if循环中调用req.checkBody或所需的验证函数可以完成上述操作,但是在更改API后如何实现 我尝试了一种变通方法,即将PaymentTYpe作为自定义验证器进行检查,并实施支票并将消息扔到自定义验证器中,但是密钥会更改。
使用当前的APi,执行此操作的正确方法是什么,因为这对于所有希望从3.0.0s更新到最新的express-validator API的人都是有用的
答案 0 :(得分:2)
快速验证器v5.x.x
中还没有提供条件验证支持,但很快就会提供。
如果您想对API提出反馈,请参见以下请求请求:https://github.com/express-validator/express-validator/pull/658
但是,请注意,传统API(使用req.checkBody()
,req.getValidationResult()
等的API尚未从express-validator中删除。
您可以继续在v5.x.x
中使用它,就像在v3.x.x
中一样。只是不建议这样做,因为它已过时,可能会在v6到期时删除(尚未!)。
所有文档均为here,与最新的API文档并列。
免责声明:我是快递验证员的维护者。