diff --git a/lib/dao.js b/lib/dao.js index 58ca673f..d54b952a 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -8,8 +8,7 @@ module.exports = DataAccessObject; * Module dependencies */ var jutil = require('./jutil'); -var validations = require('./validations.js'); -var ValidationError = validations.ValidationError; +var ValidationError = require('./validations').ValidationError; var Relation = require('./relations.js'); var Inclusion = require('./include.js'); var List = require('./list.js'); diff --git a/lib/model.js b/lib/model.js index 069d39c1..5d29a5d8 100644 --- a/lib/model.js +++ b/lib/model.js @@ -11,7 +11,7 @@ var util = require('util'); var jutil = require('./jutil'); var List = require('./list'); var Hookable = require('./hooks'); -var validations = require('./validations.js'); +var validations = require('./validations'); var _extend = util._extend; // Set up an object for quick lookup diff --git a/lib/validations.js b/lib/validations.js index 7e1a62e9..2a1925e4 100644 --- a/lib/validations.js +++ b/lib/validations.js @@ -25,18 +25,6 @@ exports.Validatable = Validatable; function Validatable() { } -Validatable.validations = function() { - var validations = {}; - (this._validations || []).forEach(function(v) { - var key = v[0], validation = v[1], options = v[3]; - var copy = extend({}, validation); - copy.options = options || {}; - validations[key] = validations[key] || []; - validations[key].push(copy); - }); - return validations; -}; - /** * Validate presence of one or more specified properties. * Requires a model to include a property to be considered valid; fails when validated field is blank. @@ -423,9 +411,10 @@ function getConfigurator(name, opts) { */ Validatable.prototype.isValid = function (callback, data) { var valid = true, inst = this, wait = 0, async = false; + var validations = this.constructor.validations; // exit with success when no errors - if (!this.constructor._validations) { + if (typeof validations !== 'object') { cleanErrors(this); if (callback) { this.trigger('validate', function (validationsDone) { @@ -447,21 +436,25 @@ Validatable.prototype.isValid = function (callback, data) { var inst = this, asyncFail = false; - this.constructor._validations.forEach(function (v) { - if (v[2] && v[2].async) { - async = true; - wait += 1; - process.nextTick(function () { - validationFailed(inst, v, done); - }); - } else { - if (validationFailed(inst, v)) { - valid = false; + var attrs = Object.keys(validations || {}); + + attrs.forEach(function(attr) { + var attrValidations = validations[attr] || []; + attrValidations.forEach(function(v) { + if (v.options && v.options.async) { + async = true; + wait += 1; + process.nextTick(function () { + validationFailed(inst, attr, v, done); + }); + } else { + if (validationFailed(inst, attr, v)) { + valid = false; + } } - } - + }); }); - + if (!async) { validationsDone.call(inst, function () { if (valid) cleanErrors(inst); @@ -503,11 +496,9 @@ function cleanErrors(inst) { }); } -function validationFailed(inst, v, cb) { - var attr = v[0]; - var conf = v[1]; - var opts = v[2] || {}; - +function validationFailed(inst, attr, conf, cb) { + var opts = conf.options || {}; + if (typeof attr !== 'string') return false; // here we should check skip validation conditions (if, unless) @@ -631,12 +622,12 @@ function blank(v) { } function configure(cls, validation, args, opts) { - if (!cls._validations) { - Object.defineProperty(cls, '_validations', { + if (!cls.validations) { + Object.defineProperty(cls, 'validations', { writable: true, configurable: true, enumerable: false, - value: [] + value: {} }); } args = [].slice.call(args); @@ -651,9 +642,12 @@ function configure(cls, validation, args, opts) { } conf.validation = validation; args.forEach(function (attr) { - var validation = opts ? [attr, conf, opts] : [attr, conf]; - cls._validations.push(validation); + var validation = extend({}, conf); + validation.options = opts || {}; + cls.validations[attr] = cls.validations[attr] || []; + cls.validations[attr].push(validation); }); + } function Errors() { diff --git a/test/manipulation.test.js b/test/manipulation.test.js index d31b3b9d..2d609063 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -102,7 +102,7 @@ describe('manipulation', function () { Person.validatesPresenceOf('name'); Person.create(batch,function (errors, persons) { - delete Person._validations; + delete Person.validations; should.exist(errors); errors.should.have.lengthOf(batch.length); should.not.exist(errors[0]); diff --git a/test/validations.test.js b/test/validations.test.js index 338b156f..97ee49fe 100644 --- a/test/validations.test.js +++ b/test/validations.test.js @@ -39,7 +39,7 @@ describe('validations', function () { beforeEach(function (done) { User.destroyAll(function () { - delete User._validations; + delete User.validations; done(); }); }); @@ -67,7 +67,7 @@ describe('validations', function () { describe('lifecycle', function () { it('should work on create', function (done) { - delete User._validations; + delete User.validations; User.validatesPresenceOf('name'); User.create(function (e, u) { should.exist(e); @@ -79,7 +79,7 @@ describe('validations', function () { }); it('should work on update', function (done) { - delete User._validations; + delete User.validations; User.validatesPresenceOf('name'); User.create({name: 'Valid'}, function (e, d) { d.updateAttribute('name', null, function (e) { @@ -95,7 +95,7 @@ describe('validations', function () { }); it('should return error code', function (done) { - delete User._validations; + delete User.validations; User.validatesPresenceOf('name'); User.create(function (e, u) { should.exist(e); @@ -112,7 +112,7 @@ describe('validations', function () { }); it('should include validation messages in err.message', function(done) { - delete User._validations; + delete User.validations; User.validatesPresenceOf('name'); User.create(function (e, u) { should.exist(e); @@ -122,7 +122,7 @@ describe('validations', function () { }); it('should include model name in err.message', function(done) { - delete User._validations; + delete User.validations; User.validatesPresenceOf('name'); User.create(function (e, u) { should.exist(e); @@ -133,9 +133,9 @@ describe('validations', function () { it('should return validation metadata', function() { var expected = {name:[{validation: 'presence', options: {}}]}; - delete User._validations; + delete User.validations; User.validatesPresenceOf('name'); - var validations = User.validations(); + var validations = User.validations; validations.should.eql(expected); }); });