[英]Joi validation - how to require or optional field based on another key exists in array?
我有这个 Joi 架构:
let schema = {};
let stations = {
contact: {
first_name: Joi.string().min(2).max(10).regex(Regex.alphabeta, 'alphabeta').allow("").error(JoiCustomErrors),
last_name: Joi.string().min(2).max(10).regex(Regex.alphabeta, 'alphabeta').allow("").error(JoiCustomErrors),
phone: Joi.string().min(10).max(10).regex(Regex.num, 'num').allow("").error(JoiCustomErrors),
},
address: {
place: Joi.string().min(2).max(10).regex(Regex.alphanum, 'alphanum').required().error(JoiCustomErrors),
city: Joi.string().min(2).max(30).required().error(JoiCustomErrors),
street: Joi.string().min(2).max(30).regex(Regex.alphabeta, 'alphabeta').required().error(JoiCustomErrors),
house_number: Joi.string().min(1).max(6).regex(Regex.alphanum, 'alphanum').allow("").error(JoiCustomErrors)
},
passengers_amount: Joi.number().min(0).max(4).required().error(JoiCustomErrors),
notes: Joi.string().min(2).max(100).regex(Regex.alphanum, 'alphanum').allow("").error(JoiCustomErrors)
};
schema.stations = Joi.array().items(stations).min(1).max(5).required().error(JoiCustomErrors);
如您所见, schema.stations
是一个包含最少 1 和最多 5 个元素的数组。 我希望每个元素只有在"contact.first_name" AND "contact.last_name" AND "contact.phone"
存在(或根据架构正确填充)时才具有“address.place”字段。
我怎样才能做到这一点?
您可以使用Joi.when()
方法并创建这样的架构:
Joi.object().keys({
contact: Joi.object().keys({
first_name: Joi.string(),
last_name: Joi.string(),
phone: Joi.string(),
}),
address: Joi.object().keys({
place: Joi.string(),
city: Joi.string().min(2).max(30),
street: Joi.string(),
house_number: Joi.string()
}).when('contact', {
is: Joi.object().keys({
first_name: Joi.exist(),
last_name: Joi.exist(),
phone: Joi.exist(),
}),
then: Joi.object({ place: Joi.required() }).required(),
otherwise: Joi.object({ place: Joi.forbidden() })
}),
passengers_amount: Joi.number(),
notes: Joi.string()
});
我只是简化了你的shema,所以很容易理解。
基本上,我们在这里说的是,如果contact.first_name 、 contact.last_name和contact.phone存在,那么address和address.place是必需的,否则address.place被禁止。
例如,这个对象将失败,因为地址不存在:
{
address: {
first_name: 'a',
last_name: 'b',
phone: 'c'
}
}
这将失败,因为address.place不存在:
{
address: {
first_name: 'a',
last_name: 'b',
phone: 'c'
},
contact: {
}
}
最后,根据定义的模式,这个对象将通过:
{
address: {
first_name: 'a',
last_name: 'b',
phone: 'c'
},
contact: {
place: 'd'
}
};
感谢 Soltex,这是应该使用的正确模式(但请参考我所做的更改):
Joi.object().keys({
contact: {
first_name: Joi.string().min(2).max(10).regex(Regex.alphabeta, 'alphabeta').allow("").error(JoiCustomErrors),
last_name: Joi.string().min(2).max(10).regex(Regex.alphabeta, 'alphabeta').allow("").error(JoiCustomErrors),
phone: Joi.string().min(10).max(10).regex(Regex.num, 'num').allow("").error(JoiCustomErrors),
},
address: Joi.object().keys({
place: Joi.string().min(2).max(10).regex(Regex.alphanum, 'alphanum').error(JoiCustomErrors),
city: Joi.string().min(2).max(30).required().error(JoiCustomErrors),
street: Joi.string().min(2).max(30).regex(Regex.alphabeta, 'alphabeta').required().error(JoiCustomErrors),
house_number: Joi.string().min(1).max(6).regex(Regex.alphanum, 'alphanum').allow("").error(JoiCustomErrors)
}).when('contact', {
is: Joi.object().keys({
first_name: Joi.string().min(1),
last_name: Joi.string().min(1),
phone: Joi.string().min(1),
}),
then: Joi.object({ place: Joi.required() }).required(),
otherwise: Joi.object({
place: Joi.optional().allow("")
})
}),
passengers_amount: Joi.number().min(0).max(4).required().error(JoiCustomErrors),
notes: Joi.string().min(2).max(100).regex(Regex.alphanum, 'alphanum').allow("").error(JoiCustomErrors)
})
请注意,我对 Soltex 的回答的回答发生了变化:他将 when "contact.first_name" "contact.last_name" "contact.phone"改为: Joi.exists() 。 这不好,因为以这种方式,即使是空对象也是“存在”的,然后要求用户提供“address.place”。 我们不想要这样的东西,我们需要在每个字段中至少有一个字符。
另外,Soltex 答案中的其他声明是使用Joi.forbidden()而这不是此处所需的行为 - 我们仍然需要允许用户提供位置,即使没有联系,但这不应该是强制性的 -所以我使用了: Joi.optional()代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.