簡體   English   中英

Joi:“ tel”不允許為空

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

即使tel設置為optional, Joi也會返回以下錯誤。 我們該如何解決?

謝謝。

錯誤: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)   

...默認情況下不允許使用空字符串,並且必須使用allow('')啟用。 但是,如果要在字符串為空的情況下指定默認值,則必須使用其他模式: Joi.string().empty('').default('default value') 這告訴Joi空字符串應被視為一個空值(而不是無效值),並將哪個值用作默認值。

參考: Joi v10.6.0文檔

在您的情況下:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM