-
Notifications
You must be signed in to change notification settings - Fork 53
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
Fixed #45: custom messages for custom validators #49
base: master
Are you sure you want to change the base?
Conversation
I added a test for the 'allow the use of custom messages on failed validation', too. Unfortunately the test-setup seems not to work well with the promise-based interface of Checkit. Even failing tests are counted as passed and you only see the errors by scanning the test-list for thrown errors. I added a `done`-callback in a final `then(done, done)` to the Checkit-Promise of my test to make it work. All other tests should be updated, too. But I'll open an issue for that. There seemed to be some un-built changes of former commits. So lines 718 + 897 of `dist/checkit.js` are unrelated to this fix and contain new messages for the field-type *integer*.
@@ -217,6 +217,7 @@ function processItemAsync(runner, currentValidation, key, context) { | |||
function addError(errors, key, validation) { | |||
return function(err) { | |||
var fieldError = errors[key]; | |||
err.message = validation.message || err.message; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems strange that this modifies err
. Is there another way?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're right. I tried another way like this:
var errorMessage = validation.message || err.message;
...
fieldError = errors[key] = new FieldError(errorMessage)
But unfortunately that only works for one validation-rule per field, as it will be only applied if fieldError
is still undefined. If there are several errors for one field, the error-objects will just be pushed to the array fieldError.errors
(in line 226).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just refined the tests to check for validation-fields with one rule and with several rules. If I try the version without modifying err
, it's not passing.
Hi Rhys! Are there any suggestions on this? |
I added a test for the 'allow the use of custom messages on failed validation', too.
Unfortunately the test-setup seems not to work well with the promise-based interface of Checkit. Even failing tests are counted as passed and you only see the errors by scanning the test-list for thrown errors. I added a
done
-callback in a finalthen(done, done)
to the Checkit-Promise of my test to make it work. All other tests should be updated, too. But I'll open an issue for that.There seemed to be some un-built changes of former commits. So lines 718 + 897 of
dist/checkit.js
are unrelated to this fix and contain new messages for the field-type integer.I also added webpack to
package.json
and changed the npm-scriptsbuild
andtest
to use local versions of mocha and webpack.