简体   繁体   English

Joi:“ tel”不允许为空

[英]Joi: “tel” is not allowed to be empty

Joi is returning the following error even though tel is set to be optional . 即使tel设置为optional, Joi也会返回以下错误。 How do we fix this? 我们该如何解决?

Thanks. 谢谢。

Error: Joi Failed: ValidationError: child "tel" fails because ["tel" is not allowed to be empty] 错误:Joi失败:ValidationError:子级“ tel”失败,因为[“ tel”不允许为空]


//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('') . ...默认情况下不允许使用空字符串,并且必须使用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') . 但是,如果要在字符串为空的情况下指定默认值,则必须使用其他模式: 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空字符串应被视为一个空值(而不是无效值),并将哪个值用作默认值。

Reference: Joi v10.6.0 Documetations 参考: Joi v10.6.0文档

In your case: 在您的情况下:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM