From 82a8e3d9a152f4327031e2c70f17801babfcaeec Mon Sep 17 00:00:00 2001 From: Simo Moujami Date: Wed, 19 Jul 2017 19:26:13 -0400 Subject: [PATCH] #1386 Allow empty values when allowBlank is true --- lib/validations.js | 2 +- test/validations.test.js | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/lib/validations.js b/lib/validations.js index ad4b29f7..8024a516 100644 --- a/lib/validations.js +++ b/lib/validations.js @@ -709,7 +709,7 @@ var defaultMessages = { */ function nullCheck(attr, conf, err) { // First determine if attribute is defined - if (typeof this[attr] === 'undefined') { + if (typeof this[attr] === 'undefined' || this[attr] === '') { if (!conf.allowBlank) { err('blank'); } diff --git a/test/validations.test.js b/test/validations.test.js index e54bb49c..6f3d6ac2 100644 --- a/test/validations.test.js +++ b/test/validations.test.js @@ -755,6 +755,25 @@ describe('validations', function() { done(); }); }); + + it('passes with an empty value when allowBlank option is true', function(done) { + User.validatesInclusionOf('gender', {in: ['male', 'female'], allowBlank: true}); + User.create({gender: ''}, done); + }); + + it('fails with an empty value when allowBlank option is false', function(done) { + User.validatesInclusionOf('gender', {in: ['male', 'female'], allowBlank: false}); + User.create({gender: ''}, function(err) { + err.should.be.instanceOf(ValidationError); + getErrorDetails(err) + .should.equal('`gender` is blank (value: "").'); + done(); + }); + }); + + function getErrorDetails(err) { + return err.message.replace(/^.*Details: /, ''); + } }); describe('exclusion', function() {