-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Custom errors - chaining breaks default functionality #2158
Comments
what version of @hapi/joi are you using ? { |
Sorry forgot to mention that. I'm also using the same 16.1.4. (I've updated the ticket with a little more details) |
You are using the wrong method. |
@hueniverse trying to use
this is my validation code:
|
UPD: found how to tackle it with |
@hueniverse Thanks. @hugebdu I tried this:
with input as:
Expected output: "a should be a type of 'text'. b should be a type of 'text'" Can you please tell me what I'm doing wrong? (I went through the documentation: https://hapi.dev/family/joi/?v=16.1.5#anymessagesmessages, but could not find what is the correct usage) Additional details:
and
But they all seem to fail to respond with custom message. |
Update:
You can have the For not having to hard-code values, like in min/max, you can also use references:
Output: ""a" should have a minimum length of 2." Thanks @hueniverse and @hugebdu. |
The documentation is pretty detailed about which error codes go with which method. |
Yes @hueniverse. I found the list of errors (along with description) here: https://github.com/hapijs/joi/blob/master/API.md#list-of-errors. Thanks again. |
* 'master' of github.com:hapijs/joi: Update API.md Improve compile version conflict error message. Closes hapijs#2173 Closes hapijs#2172 Fix docs malformed code block ending at section `object.pattern.match` End code block Fix docs missing code block ending at section `date.less(date)` Fix function signature. Fixes hapijs#2170. Delete .npmrc Delete .editorconfig Delete feature_request.md Delete bug_report.md Delete CONTRIBUTING.md 16.1.7 Fix date format validation. Closes hapijs#2168 16.1.6 Closes hapijs#2165 16.1.5 Clarify error(). Closes hapijs#2158 Closes hapijs#2161 Fix handling of shadow values. Closes hapijs#2156
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. |
Hi team, really appreciate the effort that has been into this validator to make it so dynamic and easy to use.
Describe the problem you are trying to fix (provide as much context as possible)
I'm using 16.1.4 version of the validator.
The problem I'm facing is with custom error messages. I've gone through #1739, and that did not help (in the sense that it does not accomplish the functionality for multiple keys).
My schema is:
My validation code is:
joiSchema.validate(payload, { abortEarly: false });
and my input payload is:
Expected result:
{ message: "a should be a type of text, b should be a type of text" }
Actual result:
{ message: "a should be a min of 2" }
There are 3 concerns here:
abortEarly
is set to false, the validation should proceed and additionally provide the error for 'b'. And similar to 'a', it should show at least the first encountered error, or all the errors encountered.I did a little test, and found out that if I use the custom error feature (any.error), even once anywhere, then it breaks the functionality.
By functionality I mean that:
abortEarly: false
is ignored. Other schema keys are not validated..required
should trigger (which is not happening) and the error message should be '"a" is required.' (since this is the joi-defined error forany.required
).and by even once I mean:
In this changed schema, the custom error is used only once.
Which API (or modification of the current API) do you suggest to solve that problem ?
I'm not sure yet, but I can look around the code if required.
Are you ready to work on a pull request if your suggestion is accepted ?
Yes (but it might take some time, as I've barely gone through the code yet).
The text was updated successfully, but these errors were encountered: