Skip to content

Commit

Permalink
take into account falsy values in presence validation resolves #12
Browse files Browse the repository at this point in the history
  • Loading branch information
bjrmatos committed Mar 10, 2016
1 parent 8e431b5 commit efb89dc
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 10 deletions.
17 changes: 9 additions & 8 deletions lib/validations.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var get = require('lodash/get');
var isString = require('lodash/isString');
var isPlainObject = require('lodash/isPlainObject');
var isArrayValidator = require('lodash/isArray');
var isPlainObjectValidator = require('lodash/isPlainObject');
var includes = require('lodash/includes');
var validator = require('validator');

Expand All @@ -16,7 +17,7 @@ validations.presence = function presence(paramName, customMessage) {
return {
field: paramName,
message: customMessage || message(paramName, "required"),
result: !!get(params, paramName)
result: get(params, paramName) != null
}
}
}
Expand Down Expand Up @@ -89,13 +90,13 @@ validations.isAlphanumeric = function isAlphanumeric(paramName, customMessage) {
);
}

validations.isArray = function isPlainObject(paramName, customMessage) {
validations.isArray = function isArray(paramName, customMessage) {
return function(params) {

return {
field: paramName,
message: customMessage || message(paramName, "must be an array"),
result: Array.isArray(get(params, paramName))
message: customMessage || message(paramName, 'should be an array'),
result: isArrayValidator(get(params, paramName))
}
}
}
Expand Down Expand Up @@ -220,13 +221,13 @@ validations.isURL = function isURL(paramName, options, customMessage) {
);
}

validations.isPlainObject = function isPlainObjectValidator(paramName, customMessage) {
validations.isPlainObject = function isPlainObject(paramName, customMessage) {
return function(params) {

return {
field: paramName,
message: customMessage || message(paramName, "must be a plain object"),
result: isPlainObject(get(params, paramName))
message: customMessage || message(paramName, 'should be a plain object'),
result: isPlainObjectValidator(get(params, paramName))
}
}
}
Expand Down
45 changes: 43 additions & 2 deletions test/validation_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,47 @@ describe('Validation', function() {
});
});

describe('presence validation', function() {
var params;
var presence = validator.presence;

before(function() {
params = {}
});

it('allows falsy values that are not null or undefined', function() {
params = {
foo: 0,
bar: false,
baz: ''
};

var validation = validate(params, [
presence('foo'),
presence('bar'),
presence('baz')
]);

assert(validation.valid);
assert(validation.errors.length == 0);
});

it('fails when fields are null or undefined', function() {
params = {
foo: null,
bar: undefined
};

var validation = validate(params, [
presence('foo'),
presence('bar')
]);

assert(!validation.valid);
assert(validation.errors.length == 2);
});
});

describe('optional validation', function() {
var params;
var optional = validator.optional;
Expand Down Expand Up @@ -256,7 +297,7 @@ describe('Validation Helpers', function() {
t(v.isArray('i')({ i: [1, 2, 3] }));
f(v.isArray('i')({ i: 'bla' }));

m(v.isArray('i')({ i: 'test' }), '"i" must be an array');
m(v.isArray('i')({ i: 'test' }), '"i" should be an array');
});

it('isCreditCard', function() {
Expand Down Expand Up @@ -294,6 +335,6 @@ describe('Validation Helpers', function() {
it('isPlainObject', function() {
t(v.isPlainObject('i')({ i: { foo: true } }));
f(v.isPlainObject('i')({ i: 'bla' }));
m(v.isPlainObject('i')({ i: 'test' }), '"i" must be a plain object');
m(v.isPlainObject('i')({ i: 'test' }), '"i" should be a plain object');
});
});

0 comments on commit efb89dc

Please sign in to comment.