From 28f3f5d9f8eaa99fce73f12debb897d5e45f5287 Mon Sep 17 00:00:00 2001 From: James Billingham Date: Tue, 27 Jan 2015 09:02:07 +0000 Subject: [PATCH] Fixed nullCheck in validations to correct behavior when dealing with undefined attributes --- lib/validations.js | 3 +-- test/validations.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/validations.js b/lib/validations.js index fafcf5c2..b66c99c5 100644 --- a/lib/validations.js +++ b/lib/validations.js @@ -589,8 +589,7 @@ var defaultMessages = { }; function nullCheck(attr, conf, err) { - var isNull = this[attr] === null || !(attr in this); - if (isNull) { + if (this[attr] == null) { if (!conf.allowNull) { err('null'); } diff --git a/test/validations.test.js b/test/validations.test.js index 85c469b0..9df68d46 100644 --- a/test/validations.test.js +++ b/test/validations.test.js @@ -387,6 +387,30 @@ describe('validations', function () { describe('format', function () { it('should validate format'); it('should overwrite default blank message with custom format message'); + + it('should skip missing values when allowing null', function () { + User.validatesFormatOf('email', { with: /^\S+@\S+\.\S+$/, allowNull: true }); + var u = new User({}); + u.isValid().should.be.true; + }); + + it('should skip null values when allowing null', function () { + User.validatesFormatOf('email', { with: /^\S+@\S+\.\S+$/, allowNull: true }); + var u = new User({ email: null }); + u.isValid().should.be.true; + }); + + it('should not skip missing values', function () { + User.validatesFormatOf('email', { with: /^\S+@\S+\.\S+$/ }); + var u = new User({}); + u.isValid().should.be.false; + }); + + it('should not skip null values', function () { + User.validatesFormatOf('email', { with: /^\S+@\S+\.\S+$/ }); + var u = new User({ email: null }); + u.isValid().should.be.false; + }); }); describe('numericality', function () {