From efb89dcc64a1a14f3ba85967de5cfa64ada57294 Mon Sep 17 00:00:00 2001 From: BJR Matos Date: Tue, 8 Mar 2016 23:40:01 -0500 Subject: [PATCH] take into account falsy values in presence validation resolves #12 --- lib/validations.js | 17 ++++++++-------- test/validation_test.js | 45 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 52 insertions(+), 10 deletions(-) diff --git a/lib/validations.js b/lib/validations.js index 730506a..7806886 100644 --- a/lib/validations.js +++ b/lib/validations.js @@ -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'); @@ -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 } } } @@ -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)) } } } @@ -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)) } } } diff --git a/test/validation_test.js b/test/validation_test.js index bbfad21..4417d79 100644 --- a/test/validation_test.js +++ b/test/validation_test.js @@ -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; @@ -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() { @@ -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'); }); });