I am using joi in a node project, and I would like to put the following rule : my field could be of length 6 or 8 (nothing else), I've tried to do the following but its not working :
Joi.object({
iban: Joi.string().alphanum().length(6).length(8)
})
The last written rules override the first one, so here I only accept value with length 8 and no more value with length 6
Thanks in advance
Try writing custom validator like this. you can read more about custom validators here
Joi.object({
iban: Joi.string().alphanum().custom((value, helper) => {
if(value.length === 6 || value.length === 8){
return value;
} else {
return helper.message("iban must be 6 or 8 characters long")
}
});
})
``
这个运行良好:
Joi.alternatives().try(Joi.string().alphanum().length(6), Joi.string().alphanum().length(8))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.