I have the below Joi validation for a field called store_type
store_type: Joi.string().valid(...Object.values(storeTypes))
.error(() => ({
message: `Store Type must be one of: ${Object.values(storeTypes).join(', ')}`,
})).default(storeTypes.aaa),
wherein storeTypes is defined as
storeTypes: {
aaa: 'AAA',
bbb: 'BBB'
}
As per the above validation, what I expect is
- When I don’t pass a value for store_type it should by default set the value as
AAA
- In case if I pass an empty string or non-empty string then it should check if thats either
AAA
orBBB
, if not then it should fail with the mentioned error
What changes can be done to the above code so that it works as per my requirement.
Thanks in advance
2
Answers
Thanks for the answer @cmgchess Actually the below snippet worked for me
error() must return an Error object
now you can access the error when you validate
Error
objectand access the error like