我正在使用快速验证器来验证我的API的输入,但是我在理解匹配函数时遇到了一些问题。基本上,我需要能够找出一个字符串是否与一个可接受的值数组中的任何一个值相匹配,如下所示,但它似乎不起作用。有什么建议吗?
var schema = {
"role": {
in: 'body',
matches: {
options: ["administrator", "editor", "contributor", "user"],
errorMessage: "Invalid role"
}
}
}
req.check(schema)发布于 2016-12-22 20:02:01
matches.options构造一个正则表达式。可以将regex作为数组的第一个元素传递。试试这个:
var schema = {
"role": {
in: 'body',
matches: {
options: [/\b(?:administrator|editor|contributor|user)\b/],
errorMessage: "Invalid role"
}
}
}发布于 2019-07-15 13:42:35
作为一种选择,您可以使用此模式:
var schema = {
"role": {
in: 'body',
isIn: {
options: [["administrator", "editor", "contributor", "user"]],
errorMessage: "Invalid role"
}
}
}更多关于本期的报道。
发布于 2021-03-24 21:17:18
我的方法是使用isIn()
check('action').isIn(['like', 'dislike']).run(req)https://stackoverflow.com/questions/41289980
复制相似问题