我想验证driverId不能在passengerId中,所以我尝试了此操作:
driverId: Joi
.string()
.min(24)
.max(24)
.alphanum()
.required(),
passengersId: Joi
.array()
.items(Joi.string().min(24).max(24).alphanum(), Joi.string().label('driverId').forbidden())
.min(1)
.required(),
但是,在乘客ID中不允许有任何与driverId格式匹配的ID。
请求
{
"date": "2020-05-06",
"driverId": "5eef835636187301529b312c",
"passengersId": ["5eef835636187301529b312c","5eef847088632e0178c2e85b"],
"destinyId": "5eed7c3b5149460a45b85091"
}
验证:
{
"statusCode": 400,
"error": "Bad Request",
"message": "\"passengersId[0]\" contains an excluded value. \"passengersId[1]\" contains an excluded value",
"validation": {
"source": "body",
"keys": [
"passengersId.0",
"passengersId.1"
]
}
}
EDIT1:
如果我这样验证,它可以工作,但是当尝试在Joi.ref('driverId')
内使用valid()
时,它将不起作用。
passengersId: Joi
.array()
.items(Joi.string().min(24).max(24).alphanum(), Joi.string().valid('5eef835636187301529b312c').forbidden())
.required(),
答案 0 :(得分:1)
已解决:在密钥名称前使用Joi.in
和'/'来引用先前的密钥。
来源:LINK
passengersId: Joi
.array()
.items(Joi.string().min(24).max(24).alphanum(), Joi.string().valid(Joi.in('/driverId')).forbidden())
.required(),