Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

mattiloh
Copy link

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.

I also added webpack to package.json and changed the npm-scripts build and test to use local versions of mocha and webpack.

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;
Copy link
Collaborator

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?

Copy link
Author

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).

Copy link
Author

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.

@mattiloh
Copy link
Author

Hi Rhys! Are there any suggestions on this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants