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
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.

if (!fieldError) {
fieldError = errors[key] = new FieldError(err.message)
fieldError.key = key
Expand Down
5 changes: 4 additions & 1 deletion dist/checkit.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ return /******/ (function(modules) { // webpackBootstrap
function addError(errors, key, validation) {
return function(err) {
var fieldError = errors[key];
err.message = validation.message || err.message;
if (!fieldError) {
fieldError = errors[key] = new FieldError(err.message)
fieldError.key = key
Expand Down Expand Up @@ -714,6 +715,7 @@ return /******/ (function(modules) { // webpackBootstrap
alphaUnderscore: 'The {{label}} must only contain alpha-numeric characters, underscores, and dashes',
natural: 'The {{label}} must be a positive number',
naturalNonZero: 'The {{label}} must be a number greater than zero',
integer: 'The {{label}} must be a valid integer',
ipv4: 'The {{label}} must be a valid IPv4 string',
ipv6: 'The {{label}} must be a valid IPv6 address',
base64: 'The {{label}} must be a base64 string',
Expand Down Expand Up @@ -892,6 +894,7 @@ return /******/ (function(modules) { // webpackBootstrap
alphaUnderscore: 'Le champ {{label}} ne doit contenir que des caractères alpha-numériques, des underscores, ou des tirets',
natural: 'Le champ {{label}} doit être un nombre positif',
naturalNonZero: 'Le champ {{label}} doit être un nombre supérieur à zéro',
integer: 'Le champ {{label}} doit être un entier',
ipv4: 'Le champ {{label}} doit être une chaîne IPv4 valide',
ipv6: 'Le champ {{label}} doit être une adresse IPv6 valide',
base64: 'Le champ {{label}} doit être une chaîne en base64',
Expand Down Expand Up @@ -13600,7 +13603,7 @@ return /******/ (function(modules) { // webpackBootstrap
/* 11 */
/***/ function(module, exports, __webpack_require__) {

var require;var __WEBPACK_AMD_DEFINE_RESULT__;/* WEBPACK VAR INJECTION */(function(process) {/** @license MIT License (c) copyright 2010-2014 original author or authors */
var __WEBPACK_AMD_DEFINE_RESULT__;var require;/* WEBPACK VAR INJECTION */(function(process) {/** @license MIT License (c) copyright 2010-2014 original author or authors */
/** @author Brian Cavalier */
/** @author John Hann */

Expand Down
6 changes: 3 additions & 3 deletions dist/checkit.min.js

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
"main": "server.js",
"browser": "browser.js",
"scripts": {
"test": "mocha -R spec test/index.js",
"test": "./node_modules/mocha/bin/mocha -R spec test/index.js",
"build": "npm run build:main && npm run build:dist",
"build:main": "webpack --output-library Checkit --output-library-target umd browser.js dist/checkit.js",
"build:dist": "webpack --output-library Checkit --output-library-target umd --optimize-minimize browser.js dist/checkit.min.js"
"build:main": "./node_modules/webpack/bin/webpack.js --output-library Checkit --output-library-target umd browser.js dist/checkit.js",
"build:dist": "./node_modules/webpack/bin/webpack.js --output-library Checkit --output-library-target umd --optimize-minimize browser.js dist/checkit.min.js"
},
"directories": {
"test": "test"
Expand All @@ -21,7 +21,8 @@
},
"devDependencies": {
"mocha": "~1.15.0",
"node-uuid": "~1.4.1"
"node-uuid": "~1.4.1",
"webpack": "^1.12.6"
},
"repository": {
"type": "git",
Expand Down
18 changes: 16 additions & 2 deletions test/spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ describe('Checkit', function() {
return Checkit(rulesTest).run({valueTest: value})
})

it('should fail when the validation rule throws an error', function(){
it('should fail when the validation rule throws an error', function(done){
var rulesTest = {
failedRuleTest: {
rule: function(val){
Expand All @@ -355,9 +355,23 @@ describe('Checkit', function() {
};
return Checkit(rulesTest).run({failedRuleTest: "value"}).then(null, function(err){
equal(err.get('failedRuleTest').message, 'thrown from rule function');
});
}).then(done, done);
})

it('allow the use of custom messages on failed validation', function(done){
var rulesTest = {
failedRuleTest: {
rule: function(val){
throw new Error('thrown from rule function');
},
message: 'This is a custom message!'
}
};
return Checkit(rulesTest).run({failedRuleTest: "value"}).then(null, function(err){
equal(err.get('failedRuleTest').message, 'This is a custom message!');
}).then(done, done);
})

it('should pass the supplied parameter to the validation rule', function(){
var parameter = 'parameter';
var rulesTest = {
Expand Down