New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Customizing global error messages #1179
Comments
Another option I had thought of was creating my own ValidationChain and using it repeatedly everywhere with the message included, but I wasn't sure how to do that. Something like....
Where |
My temporary fix feels ugly and dirty but is as follows...
I want to do this but chain it instead. Chaining feels much cleaner with express-validator |
Hey @ChrisGambrell, unfortunately what you want is not really possible at the moment. In case you're keen on doing the work, I want to offer a different outlook, as I'm not sure what the API/developer experience for a default message should look like. I think there's an issue or PR somewhere here asking for the validator name to be included in the error object. So you could do something like this, leveraging const customValidationResult = validationResult.withDefaults({
formatter: error => ({
...error,
msg: customErrorMessages[error.validatorName],
})
}); |
I love express-validator, but the one thing I miss from Joi is global error messages. In Joi, I could do something like...
... and get nice error messages that I could handle globally. When I create my schemas in express-validator and want to use
isString()
, I have a custom error message for this ValidationChain, but I have to chain the.withMessage({ ... })
with every instance ofisString()
to use my custom message.Is there a way to achieve this behavior with express-validator that I am just not aware of/familiar with? Thanks so much in advance.
The text was updated successfully, but these errors were encountered: