当存在或缺少两个值中的任何一个时进行Joi验证
Joi validation when either of two values are present or missing
有三个参数:latitude, longitude, zipcode
我需要一个joi验证
- 需要纬度和经度,当其中一个存在或邮编缺失 当纬度或经度缺失时,
- 要求邮政编码。
像这样?
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()
您可以在以下模式中验证多个条件。
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() }) });
这个解决方案可能有用:
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()
})
});
相关文章:
- 创建一个数组数组,没有任何重复的Javascript
- 使用Booklet Jquery插件,我如何验证最后一页是否显示
- 任何一种简单的方法都可以将带有onload的元素作为目标
- 在scala或scalajs Diode中,现有类型中的任何一种都符合“;更新一个没有'还不存在”;
- Parsley.js,多步骤表单,在移动到上一步时跳过验证
- 是否有一种方法可以使用公共代码库在Java和JavaScript中进行输入验证
- 有没有一种方法可以使用任何浏览器扩展整个dom树'的开发工具
- javascript regexp,用于验证一开始没有http的url
- 有没有一种优雅的方式来告诉esint,以确保我们're没有使用任何ES6语法/函数
- 使用JavaScript验证用户交互/输入-这是一种很好的方法
- 是否有任何不需要后端的验证码角度指令
- CSS 类在验证时只工作一次
- 在不使用任何http、https或ftp的情况下验证URL
- 如果发现任何数据,如何停止每7秒运行一次的函数
- 表单验证转到下一页
- Regex匹配除以7开头的10位数字以外的任何一组数字
- JavaScript,如何检查字符是否为以下任何一种
- Javascript检查字符串是否在两个数组中的任何一个数组中
- 验证VueJS中模型中的数据?任何Vue验证器都不会'不起作用
- 如何在没有任何身份验证的情况下调用WCF服务