有三个参数:latitude, longitude, zipcode
我需要一个joi validation
这样的东西?
Joi.object().keys({
latitude: Joi.number().when('zipcode', { is: undefined, then: Joi.required() }),
longitude: Joi.number().when('zipcode', { is: undefined, then: Joi.required() }),
zipcode: Joi.number().when(['latitude', 'longitude'], { is: undefined, then: Joi.required() })
});
我认为使用object.and()
可能会有更优雅的解决方案答案 0 :(得分:0)
此解决方案可能很有用:
schema = Joi.object().keys({
location: Joi.object().keys({
lat: Joi.number(),
long: Joi.number()
}).and('lat', 'long'),
timezone: Joi.alternatives()
.when('location', {
is: null,
then: Joi.number().required(),
otherwise: Joi.number()
})
});
答案 1 :(得分:0)
您可以在以下架构中验证多个条件。
const schema = Joi.object().keys({ searchby: Joi.string().valid('phone', '_id', 'cno').required(), // field name searchvalue: Joi .when('searchby', { is: "phone", then: Joi.string().regex(/^(923)\d{9}$/, 'numbers').max(12).min(12).required() }) .when('searchby', { is: "_id", then: Joi.objectId().required() }) .when('searchby', { is: "nic", then: Joi.number().required() }) });