gpt4 book ai didi

javascript - Joi: "tel"不允许为空

转载 作者:数据小太阳 更新时间:2023-10-29 06:14:35 27 4
gpt4 key购买 nike

即使 tel 设置为可选,Joi 仍返回以下错误。我们如何解决这个问题?

谢谢。

Error: Joi Failed: ValidationError: child "tel" fails because ["tel" is not allowed to be empty]


//Define Joi schema
const schema = {
email: Joi.string().required().email({
errorLevel: 64,
minDomainAtoms: 2
}).min(6),
tel: Joi.string().optional().min(10).max(10),
password: Joi.string().required().min(8).max(64)
}

//Check inputs
const { error, value } = Joi.validate({
email: args.email,
tel: tel,
password: args.password
}, schema)

最佳答案

...empty strings are not allowed by default and must be enabled with allow(''). However, if you want to specify a default value in case of empty string you have to use a different pattern: Joi.string().empty('').default('default value'). This tells Joi that the empty string should be considered as an empty value (instead of invalid) and which value to use as default.

引用:Joi v10.6.0 Documetations

在你的情况下:

tel: Joi.string().optional().allow('').min(10).max(10)

关于javascript - Joi: "tel"不允许为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45515112/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com