I am using the language option to override joi error messages.
It does not seem to be working. Am I doing it incorrectly or is this a bug
validate: {
params: {
zipcode: Joi.string().regex(/^\d{5}(?:-\d{4})?$/).required().options({
language: {
any: {
allowOnly: 'not a valid zipcode'
}
}
})
}
The error message is this
"message": "zipcode with value 95120-123 fails to match the required pattern: /^\\d{5}(?:-\\d{4})?$/",
You're not replacing the string.regex.base error.
What do you mean?
There is not much documentation on the language object so its kinda hard to figure out the right way to do this.
Agree on the docs and I'll take a PR for that.
Look at the language file it's obvious. allowOnly has nothing to do with regex validation.
Got it. Thanks!
In case someone ends up here, here's the whole thing:
.when('type', {
is: channelTypes.ITEM,
then: joi.object({
key: joi.string().regex(/^[a-fA-F0-9-]{16,64}$/).options({
language: {
string: {
regex: {
base: 'must be hexadecimal numbers separated with dashes'
}
}
}
})
})
})
Since 8.1 you also can :
.when('type', {
is: channelTypes.ITEM,
then: joi.object({
key: joi.string().regex(/^[a-fA-F0-9-]{16,64}$/).error(new Error('key must be hexadecimal numbers separated with dashes'))
})
})
But that would be replacing all errors whatever their type. There's also no key insertion or error templating yet so the message is up to you.
@szkrd, thank you so much for your response. It worked for me.
This thread has been automatically locked due to inactivity. Please open a new issue for related bugs or questions following the new issue template instructions.
Most helpful comment
In case someone ends up here, here's the whole thing: