Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/waterline/query/dql/update.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ module.exports = function(criteria, values, cb) {
createBelongsTo.call(this, valuesObject, function(err) {
if(err) return cb(err);

beforeCallbacks.call(self, valuesObject.values, function(err) {
beforeCallbacks.call(self, valuesObject, function(err) {
if(err) return cb(err);
updateRecords.call(self, valuesObject, cb);
});
Expand Down Expand Up @@ -150,23 +150,23 @@ function createBelongsTo(valuesObject, cb) {
/**
* Run Before* Lifecycle Callbacks
*
* @param {Object} values
* @param {Object} valuesObject
* @param {Function} cb
*/

function beforeCallbacks(values, cb) {
function beforeCallbacks(valuesObject, cb) {
var self = this;

async.series([

// Run Validation with Validation LifeCycle Callbacks
function(cb) {
callbacks.validate(self, values, true, cb);
callbacks.validate(self, valuesObject.values, valuesObject.criteria, true, cb);
},

// Before Update Lifecycle Callback
function(cb) {
callbacks.beforeUpdate(self, values, cb);
callbacks.beforeUpdate(self, valuesObject.values, valuesObject.criteria, cb);
}

], cb);
Expand Down
44 changes: 36 additions & 8 deletions lib/waterline/query/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,52 @@ var _ = require('lodash'),

module.exports = {

validate: function(values, presentOnly, cb) {
// validate(values, [criteria], [presentOnly], cb)
validate: function(values, criteria, presentOnly, cb) {
var self = this;

//Handle optional second arg
if (typeof presentOnly === 'function') {
cb = presentOnly;
var args = [];
for (var i = 0; i < arguments.length; ++i) {
if (arguments[i] !== undefined) args.push(arguments[i]);
}

if (args.length === 2) {
// validate(values, cb)
values = args[0];
criteria = null;
presentOnly = false;
cb = args[1];
} else if (args.length === 3 && typeof args[1] === 'boolean') {
// validate(values, presentOnly, cb)
values = args[0];
criteria = null;
presentOnly = args[1];
cb = args[2];
} else if (args.length === 3 && typeof args[1] === 'object') {
// validate(values, criteria, cb)
values = args[0];
criteria = args[1];
presentOnly = false;
cb = args[2];
}

async.series([

// Run Before Validate Lifecycle Callbacks
function(cb) {
var runner = function(item, callback) {
item.call(self, values, function(err) {
if(err) return callback(err);
callback();
});
if (item.length === 2) {
// Pass only 2 parameters for backwards compatibility
item.call(self, values, function(err) {
if(err) return callback(err);
callback();
});
} else {
item.call(self, values, criteria, function(err) {
if(err) return callback(err);
callback();
});
}
};

async.eachSeries(self._callbacks.beforeValidate, runner, function(err) {
Expand Down
22 changes: 17 additions & 5 deletions lib/waterline/utils/callbacksRunner.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ var runner = module.exports = {};
*
* @param {Object} context
* @param {Object} values
* @param {Object} criteria
* @param {Boolean} presentOnly
* @param {Function} cb
* @api public
*/

runner.validate = function(context, values, presentOnly, cb) {
context.validate(values, presentOnly, cb);
runner.validate = function(context, values, criteria, presentOnly, cb) {
context.validate(values, criteria, presentOnly, cb);
};


Expand Down Expand Up @@ -69,14 +70,20 @@ runner.afterCreate = function(context, values, cb) {
*
* @param {Object} context
* @param {Object} values
* @param {Object} criteria
* @param {Function} cb
* @api public
*/

runner.beforeUpdate = function(context, values, cb) {
runner.beforeUpdate = function(context, values, criteria, cb) {

var fn = function(item, next) {
item.call(context, values, next);
if (item.length === 2) {
// Pass only 2 parameters for backwards compatibility
item.call(context, values, next);
} else {
item.call(context, values, criteria, next);
}
};

async.eachSeries(context._callbacks.beforeUpdate, fn, cb);
Expand Down Expand Up @@ -114,7 +121,12 @@ runner.afterUpdate = function(context, values, cb) {
runner.beforeDestroy = function(context, criteria, cb) {

var fn = function(item, next) {
item.call(context, criteria, next);
if (item.length === 1) {
// Pass only 1 parameter for backwards compatibility
item.call(context, next);
} else {
item.call(context, criteria, next);
}
};

async.eachSeries(context._callbacks.beforeDestroy, fn, cb);
Expand Down
1 change: 1 addition & 0 deletions lib/waterline/utils/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ schema.normalizeCallbacks = function(context) {
var i, _i, len, _len, fn, fns = {};

function defaultFn(fn) {
if (fn === 'beforeUpdate' || fn === 'beforeValidate') return function(values, criteria, next) { return next(); };
return function(values, next) { return next(); };
}

Expand Down
15 changes: 12 additions & 3 deletions test/unit/callbacks/beforeValidation.create.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ describe('.beforeValidate()', function() {

describe('basic function', function() {
var person;
var passedCriteria;

before(function(done) {
var waterline = new Waterline();
Expand All @@ -15,8 +16,9 @@ describe('.beforeValidate()', function() {
name: 'string'
},

beforeValidate: function(values, cb) {
beforeValidate: function(values, criteria, cb) {
values.name = values.name + ' updated';
passedCriteria = criteria;
cb();
}
});
Expand Down Expand Up @@ -49,6 +51,7 @@ describe('.beforeValidate()', function() {
person.create({ name: 'test' }, function(err, user) {
assert(!err);
assert(user.name === 'test updated');
assert(passedCriteria === null);
done();
});
});
Expand All @@ -62,6 +65,7 @@ describe('.beforeValidate()', function() {

describe('array of functions', function() {
var person, status;
var passedCriteria = [];

before(function(done) {
var waterline = new Waterline();
Expand All @@ -74,14 +78,16 @@ describe('.beforeValidate()', function() {

beforeValidate: [
// Function 1
function(values, cb) {
function(values, criteria, cb) {
values.name = values.name + ' fn1';
passedCriteria.push(criteria);
cb();
},

// Function 2
function(values, cb) {
function(values, criteria, cb) {
values.name = values.name + ' fn2';
passedCriteria.push(criteria);
cb();
}
]
Expand Down Expand Up @@ -109,6 +115,9 @@ describe('.beforeValidate()', function() {
person.create({ name: 'test' }, function(err, user) {
assert(!err);
assert(user.name === 'test fn1 fn2');
assert(passedCriteria.length === 2);
assert(passedCriteria[0] === null);
assert(passedCriteria[1] === null);
done();
});
});
Expand Down
6 changes: 3 additions & 3 deletions test/unit/callbacks/beforeValidation.createEach.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('.beforeValidate()', function() {
name: 'string'
},

beforeValidate: function(values, cb) {
beforeValidate: function(values, criteria, cb) {
values.name = values.name + ' updated';
cb();
}
Expand Down Expand Up @@ -75,13 +75,13 @@ describe('.beforeValidate()', function() {

beforeValidate: [
// Function 1
function(values, cb) {
function(values, criteria, cb) {
values.name = values.name + ' fn1';
cb();
},

// Function 2
function(values, cb) {
function(values, criteria, cb) {
values.name = values.name + ' fn2';
cb();
}
Expand Down
6 changes: 3 additions & 3 deletions test/unit/callbacks/beforeValidation.findOrCreate.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('.beforeValidate()', function() {
name: 'string'
},

beforeValidate: function(values, cb) {
beforeValidate: function(values, criteria, cb) {
values.name = values.name + ' updated';
cb();
}
Expand Down Expand Up @@ -135,13 +135,13 @@ describe('.beforeValidate()', function() {

beforeValidate: [
// Function 1
function(values, cb) {
function(values, criteria, cb) {
values.name = values.name + ' fn1';
cb();
},

// Function 1
function(values, cb) {
function(values, criteria, cb) {
values.name = values.name + ' fn2';
cb();
}
Expand Down
6 changes: 3 additions & 3 deletions test/unit/callbacks/beforeValidation.findOrCreateEach.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('.beforeValidate()', function() {
name: 'string'
},

beforeValidate: function(values, cb) {
beforeValidate: function(values, criteria, cb) {
values.name = values.name + ' updated';
cb();
}
Expand Down Expand Up @@ -77,13 +77,13 @@ describe('.beforeValidate()', function() {

beforeValidate: [
// Function 1
function(values, cb) {
function(values, criteria, cb) {
values.name = values.name + ' fn1';
cb();
},

// Function 2
function(values, cb) {
function(values, criteria, cb) {
values.name = values.name + ' fn2';
cb();
}
Expand Down
15 changes: 12 additions & 3 deletions test/unit/callbacks/beforeValidation.update.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ describe('.beforeValidate()', function() {

describe('basic function', function() {
var person;
var passedCriteria;

before(function(done) {
var waterline = new Waterline();
Expand All @@ -15,8 +16,9 @@ describe('.beforeValidate()', function() {
name: 'string'
},

beforeValidate: function(values, cb) {
beforeValidate: function(values, criteria, cb) {
values.name = values.name + ' updated';
passedCriteria = criteria;
cb();
}
});
Expand Down Expand Up @@ -49,6 +51,7 @@ describe('.beforeValidate()', function() {
person.update({ name: 'criteria' }, { name: 'test' }, function(err, users) {
assert(!err);
assert(users[0].name === 'test updated');
assert(passedCriteria && passedCriteria.where && passedCriteria.where.name === 'criteria');
done();
});
});
Expand All @@ -62,6 +65,7 @@ describe('.beforeValidate()', function() {

describe('array of functions', function() {
var person, status;
var passedCriteria = [];

before(function(done) {
var waterline = new Waterline();
Expand All @@ -74,14 +78,16 @@ describe('.beforeValidate()', function() {

beforeValidate: [
// Function 1
function(values, cb) {
function(values, criteria, cb) {
values.name = values.name + ' fn1';
passedCriteria.push(criteria);
cb();
},

// Function 2
function(values, cb) {
function(values, criteria, cb) {
values.name = values.name + ' fn2';
passedCriteria.push(criteria);
cb();
}
]
Expand Down Expand Up @@ -109,6 +115,9 @@ describe('.beforeValidate()', function() {
person.update({ name: 'criteria' }, { name: 'test' }, function(err, users) {
assert(!err);
assert(users[0].name === 'test fn1 fn2');
assert(passedCriteria.length === 2);
assert(passedCriteria[0].where && passedCriteria[0].where.name === 'criteria');
assert(passedCriteria[1].where && passedCriteria[1].where.name === 'criteria');
done();
});
});
Expand Down