Validation with custom
followed by withMessage
always uses thrown message.
#1047
Labels
custom
followed by withMessage
always uses thrown message.
#1047
Describe the bug
There seems to be a regression of #203 from the v2 days or the documentation for
withMessage
is incorrect.If you try to override the default message of a custom validator using
withMessage
, it ignores the provided message.According to the documentation of
withMessage
:And, according to the Dynamic Messages guide:
...which a colleague initially read as it will always use the message from the error. So, we're not sure which it is supposed to be and it's been so long since #203 that it's hard to say if the intended behavior had been altered since then.
But having
withMessage
override a default error message from a custom validator to provide better context is a handy tool to have in a validation library.To Reproduce
A partial reproduction:
Expected behavior
I would like it to use the custom error message provided by
withMessage
...or the documentation updated to be more clear about how custom validator error messages are intended to be presented.
Current behavior
Always uses the thrown error message; checked in code for
src/context-items/custom-validation.js
and it indeed preferentially useserr.message
orerr
overthis.message
when catching an exception from the custom validator.Express-validator version:
The text was updated successfully, but these errors were encountered: