From b0a1ed3bed3ce87927c1dc428ee7b74a7892f253 Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Mon, 1 Apr 2013 20:08:22 +0400 Subject: [PATCH 1/2] Fix validation issue --- lib/model.js | 1 + lib/validations.js | 6 +----- test/validations.test.js | 17 +++++++++++++++++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/model.js b/lib/model.js index 9c24e12e..988ad6f1 100644 --- a/lib/model.js +++ b/lib/model.js @@ -199,6 +199,7 @@ AbstractClass.create = function (data, callback) { } } + var obj; // if we come from save if (data instanceof Model && !data.id) { diff --git a/lib/validations.js b/lib/validations.js index 78afb65a..0b2bf60d 100644 --- a/lib/validations.js +++ b/lib/validations.js @@ -396,11 +396,7 @@ Validatable.prototype.isValid = function (callback, data) { }, data); - if (!async) { - if (valid) cleanErrors(this); - if (callback) callback(valid); - return valid; - } else { + if (async) { // in case of async validation we should return undefined here, // because not all validations are finished yet return; diff --git a/test/validations.test.js b/test/validations.test.js index 3ec8cbd2..745fa664 100644 --- a/test/validations.test.js +++ b/test/validations.test.js @@ -47,6 +47,7 @@ describe('validations', function() { describe('commons', function() { describe('skipping', function() { + it('should allow to skip using if: attribute', function() { User.validatesPresenceOf('pendingPeriod', {if: 'createdByAdmin'}); var user = new User; @@ -56,6 +57,22 @@ describe('validations', function() { user.pendingPeriod = 1 user.isValid().should.be.true; }); + + }); + + describe.only('lifecycle', function() { + + it('should work on create', function(done) { + User.validatesPresenceOf('name'); + User.create(function(e) { + should.exist(e); + User.create({name: 'Valid'}, function(e, d) { + should.not.exist(e); + done(); + }); + }); + }); + }); }); From c5019ef3548a87ce83aa655212c107a06cb6cba0 Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Mon, 1 Apr 2013 20:10:46 +0400 Subject: [PATCH 2/2] Test update --- test/validations.test.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/validations.test.js b/test/validations.test.js index 745fa664..dc86182d 100644 --- a/test/validations.test.js +++ b/test/validations.test.js @@ -73,6 +73,20 @@ describe('validations', function() { }); }); + it('should work on update', function(done) { + User.validatesPresenceOf('name'); + User.create({name: 'Valid'}, function(e, d) { + d.updateAttribute('name', null, function(e) { + should.exist(e); + e.should.be.instanceOf(Error); + d.updateAttribute('name', 'Vasiliy', function(e) { + should.not.exist(e); + done(); + }); + }) + }); + }); + }); });