问题描述
我希望 Joi 允许 JSON 对象(在 POST 请求中接收)具有电子邮件或电话或两者。 如您所知,JSON 对象发送键值对,因此空值必须作为空字符串(或 null)发送。
如果这样做,Joi 不会允许的。 因此逻辑永远不会到达
.or("email","tel")
因为 Joi 甚至不能接受空字符串。
如果我这样做:
email: joi.string().allow('')
Joi 'or' 运算符认为空字符串是有效的。 所以我绝对不能 .allow(' ')
const Joi = require("Joi");
const fakejsonObject = {
name: "Bob",email: "",tel: 567567575
};
function createCustomer(jsonObject) {
const customer = {
name: jsonObject.name,email: jsonObject.email,tel: jsonObject.tel,};
console.log(customer);
return customer;
}
function validateCustomer(customer) {
const schema = Joi.object({
name: Joi.string().required(),email: Joi.string().email(),tel: Joi.number(),}).or("email","tel");
console.log(schema.validate(customer));
return schema.validate(customer);
}
validateCustomer(createCustomer(fakejsonObject));
那么“或”运算符是如何工作的?!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)