Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
190
rated 0 times [  193] [ 3]  / answers: 1 / hits: 18616  / 7 Years ago, fri, august 4, 2017, 12:00:00

Joi is returning the following error even though tel is set to be optional. How do we fix this?



Thanks.




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)

More From » node.js

 Answers
69

...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.




Reference: Joi v10.6.0 Documetations



In your case:



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

[#56864] Wednesday, August 2, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
gregorio

Total Points: 362
Total Questions: 95
Total Answers: 93

Location: Puerto Rico
Member since Sun, Jun 27, 2021
3 Years ago
;