在快速验证器中链接自定义验证器?
chaining custom validators in express-validator?
我认为这很简单,但我觉得将自定义验证器与现有验证器链接起来会导致 req 对象发生一些奇怪的事情,这似乎是未定义的:
req.checkBody('Game18awayScore', 'must be between 0 and 30').isInt({min:0, max:30}).custom((value,{ req }) => {
console.log(something);
if (Math.abs(value - req.body.Game18homeScore) < 2){
if (value < 30 && req.body.Game18homeScore < 30){
throw new Error("winning score isn't 2 greater than losing score");
}
}
});
req.checkBody('homeMan1', 'Please choose a player.').notEmpty().custom((value,{req}) => {
if (value != 0){
if (value == req.body.homeMan2 || value == req.body.homeMan3 || value == req.body.awayMan1 || value == req.body.awayMan2 || value == req.body.awayMan3){
throw new Error("can't use the same player more than once")
}
}
});
但我不断得到:
TypeError: Cannot destructure property
要求of 'undefined' or 'null'.
第一个习惯是检查两个值之间是否至少相差两倍,除非其中一个值是 30。
第二个习惯是检查其他 5 个选项中没有使用一个值。
我应该补充一点,这段代码在验证器函数中:
function validateScorecard (req,res,next){ [all my validations for the form including the ones above] }
然后包含在路线中:
app.post('/scorecard-beta',validateScorecard, fixture_controller.full_fixture_post);
有什么想法吗?
当您使用旧版 API(例如 req.checkBody(...).custom()
)时,使用 .custom()
指定这样的内联验证器不起作用。
遗留 API 多年来一直以完全不同的方式支持自定义验证器:
您将它们指定为 express-validator 中间件中的选项,并在您使用 req.checkBody(...)
.
时提供它们
然后这些可以接收字段值以外的其他参数。
示例:
app.use(expressValidator({
customValidators: {
isMagicNumber(value, additionalNumbers) {
return value === 42 || additionalNumbers.includes(value);
}
}
}));
app.post('/captcha', (req, res, next) => {
// world's safest captcha implementation
req.checkBody('answer').isMagicNumber();
req.checkBody('answer').isMagicNumber([req.user.age]);
});
.custom()
调用有点有效,因为该方法在那里,但在遗留 API 内部,express-validator 不知道您是如何定义它的。
你的解决方案?
- 继续这样使用它,但不要使用额外的参数,因为那样会违反 what is documented for
.custom()
。
- 停止使用旧版 API,因为它已被弃用,最终将被删除。
我认为这很简单,但我觉得将自定义验证器与现有验证器链接起来会导致 req 对象发生一些奇怪的事情,这似乎是未定义的:
req.checkBody('Game18awayScore', 'must be between 0 and 30').isInt({min:0, max:30}).custom((value,{ req }) => {
console.log(something);
if (Math.abs(value - req.body.Game18homeScore) < 2){
if (value < 30 && req.body.Game18homeScore < 30){
throw new Error("winning score isn't 2 greater than losing score");
}
}
});
req.checkBody('homeMan1', 'Please choose a player.').notEmpty().custom((value,{req}) => {
if (value != 0){
if (value == req.body.homeMan2 || value == req.body.homeMan3 || value == req.body.awayMan1 || value == req.body.awayMan2 || value == req.body.awayMan3){
throw new Error("can't use the same player more than once")
}
}
});
但我不断得到:
TypeError: Cannot destructure property
要求of 'undefined' or 'null'.
第一个习惯是检查两个值之间是否至少相差两倍,除非其中一个值是 30。
第二个习惯是检查其他 5 个选项中没有使用一个值。
我应该补充一点,这段代码在验证器函数中:
function validateScorecard (req,res,next){ [all my validations for the form including the ones above] }
然后包含在路线中:
app.post('/scorecard-beta',validateScorecard, fixture_controller.full_fixture_post);
有什么想法吗?
当您使用旧版 API(例如 req.checkBody(...).custom()
)时,使用 .custom()
指定这样的内联验证器不起作用。
遗留 API 多年来一直以完全不同的方式支持自定义验证器:
您将它们指定为 express-validator 中间件中的选项,并在您使用 req.checkBody(...)
.
时提供它们
然后这些可以接收字段值以外的其他参数。
示例:
app.use(expressValidator({
customValidators: {
isMagicNumber(value, additionalNumbers) {
return value === 42 || additionalNumbers.includes(value);
}
}
}));
app.post('/captcha', (req, res, next) => {
// world's safest captcha implementation
req.checkBody('answer').isMagicNumber();
req.checkBody('answer').isMagicNumber([req.user.age]);
});
.custom()
调用有点有效,因为该方法在那里,但在遗留 API 内部,express-validator 不知道您是如何定义它的。
你的解决方案?
- 继续这样使用它,但不要使用额外的参数,因为那样会违反 what is documented for
.custom()
。 - 停止使用旧版 API,因为它已被弃用,最终将被删除。