在Joi验证中,这些带()和with()函数做了什么?
const schema = Joi.object().keys({
username: Joi.string().alphanum().min(3).max(30).required(),
password: Joi.string().regex(/^[a-zA-Z0-9]{3,30}$/),
access_token: [Joi.string(), Joi.number()],
birthyear: Joi.number().integer().min(1900).max(2013),
email: Joi.string().email()
}).with('username', 'birthyear').without('password', 'access_token');发布于 2018-04-13 22:31:54
object.with(key, peers)当指定的密钥存在时,需要存在其他密钥,其中:key-引用键。peers-必须与密钥一起出现的所需对等密钥名称。peers可以是单个字符串值,也可以是字符串值数组。
翻译为您的示例,意思是“当键username存在时,键birthyear也必须存在”。
object.without(key, peers)当指定的密钥存在时,禁止在下列情况下出现其他密钥:key-引用键。peers-禁止的对等密钥名称不能与密钥一起出现。peers可以是单个字符串值,也可以是字符串值数组。
翻译为您的示例,意思是“当键password存在时,密钥access_token也不允许出现”。
发布于 2018-04-13 22:32:58
.with(keyA, keyB)意味着keyB必须在keyA出现时出现。
您的模式示例没有很好地使用.with(),因为“用户名”是必需的键。然后,你也可以要求“生日”。
.without(keyA, keyB)意味着keyB必须在keyA存在时不在场,而不是。
https://stackoverflow.com/questions/49825500
复制相似问题