不要怂,就是干,撸起袖子干!

Commit 35a6cbed by Nuno Sousa

Simplify parameter validation and deprecation

1 parent 6b257be0
...@@ -744,7 +744,7 @@ module.exports = (function() { ...@@ -744,7 +744,7 @@ module.exports = (function() {
Instance.prototype.increment = function(fields, countOrOptions) { Instance.prototype.increment = function(fields, countOrOptions) {
Utils.validateParameter(countOrOptions, Object, { Utils.validateParameter(countOrOptions, Object, {
optional: true, optional: true,
deprecated: 'number', deprecated: Number,
deprecationWarning: 'Increment expects an object as second parameter. Please pass the incrementor as option! ~> instance.increment(' + JSON.stringify(fields) + ', { by: ' + countOrOptions + ' })' deprecationWarning: 'Increment expects an object as second parameter. Please pass the incrementor as option! ~> instance.increment(' + JSON.stringify(fields) + ', { by: ' + countOrOptions + ' })'
}); });
...@@ -809,7 +809,7 @@ module.exports = (function() { ...@@ -809,7 +809,7 @@ module.exports = (function() {
Instance.prototype.decrement = function(fields, countOrOptions) { Instance.prototype.decrement = function(fields, countOrOptions) {
Utils.validateParameter(countOrOptions, Object, { Utils.validateParameter(countOrOptions, Object, {
optional: true, optional: true,
deprecated: 'number', deprecated: Number,
deprecationWarning: 'Decrement expects an object as second parameter. Please pass the decrementor as option! ~> instance.decrement(' + JSON.stringify(fields) + ', { by: ' + countOrOptions + ' })' deprecationWarning: 'Decrement expects an object as second parameter. Please pass the decrementor as option! ~> instance.decrement(' + JSON.stringify(fields) + ', { by: ' + countOrOptions + ' })'
}); });
......
...@@ -1143,7 +1143,7 @@ module.exports = (function() { ...@@ -1143,7 +1143,7 @@ module.exports = (function() {
*/ */
Model.prototype.bulkCreate = function(records, fieldsOrOptions, options) { Model.prototype.bulkCreate = function(records, fieldsOrOptions, options) {
Utils.validateParameter(fieldsOrOptions, Object, { deprecated: Array, optional: true, index: 2, method: 'Model#bulkCreate' }); Utils.validateParameter(fieldsOrOptions, Object, { deprecated: Array, optional: true, index: 2, method: 'Model#bulkCreate' });
Utils.validateParameter(options, 'undefined', { deprecated: Object, optional: true, index: 3, method: 'Model#bulkCreate' }); Utils.validateParameter(options, undefined, { deprecated: Object, optional: true, index: 3, method: 'Model#bulkCreate' });
if (!records.length) { if (!records.length) {
return Promise.resolve([]); return Promise.resolve([]);
......
'use strict'; 'use strict';
var cJSON = require('circular-json') var _ = require('lodash');
, _ = require('lodash'); // Don't require Utils here, as it creates a circular dependency var util = require('util');
var extractClassName = function(o) { var validateDeprecation = function(value, expectation, options) {
if (typeof o === 'string') { if (!options.deprecated) {
return o; return;
} else if (!!o) {
return o.toString().match(/function ([^\(]+)/)[1];
} else {
return 'undefined';
} }
};
var generateDeprecationWarning = function(value, expectation, options) { var valid = value instanceof options.deprecated;
options = options || {};
if (options.method && options.index) { if (valid) {
return [ var message = util.format('%s should not be of type "%s"', util.inspect(value), options.deprecated.name);
'The',
{1: 'first', 2: 'second', 3: 'third', 4: 'fourth', 5: 'fifth'}[options.index],
'parameter of',
options.method,
'should be a',
extractClassName(expectation) + '!'
].join(' ');
} else {
return ['Expected', cJSON.stringify(value), 'to be', extractClassName(expectation) + '!'].join(' ');
}
};
var matchesExpectation = function(value, expectation) { console.log('DEPRECATION WARNING:', options.deprecationWarning || message);
if (typeof expectation === 'string') {
return (typeof value === expectation.toString());
} else {
return (value instanceof expectation);
} }
};
var validateDeprication = function(value, expectation, options) { return valid;
if (options.deprecated) {
if (matchesExpectation(value, options.deprecated)) {
options.onDeprecated(options.deprecationWarning);
return true;
}
}
}; };
var validate = function(value, expectation, options) { var validate = function(value, expectation, options) {
var result = matchesExpectation(value, expectation); if (value instanceof expectation) {
return true;
if (result) {
return result;
} else if (!options.throwError) {
return false;
} else {
var _value = (value === null) ? 'null' : value.toString()
, _expectation = extractClassName(expectation);
throw new Error('The parameter (value: ' + _value + ') is no ' + _expectation + '.');
} }
throw new Error(util.format('The parameter (value: %s) is no %s.', value, expectation.name));
}; };
var ParameterValidator = module.exports = { var ParameterValidator = module.exports = {
check: function(value, expectation, options) { check: function(value, expectation, options) {
options = _.extend({ options = _.extend({
throwError: true,
deprecated: false, deprecated: false,
deprecationWarning: generateDeprecationWarning(value, expectation, options),
onDeprecated: function(s) { console.log('DEPRECATION WARNING:', s); },
index: null, index: null,
method: null, method: null,
optional: false optional: false
}, options || {}); }, options || {});
if (options.optional && ((value === undefined) || (value === null))) { if (!value && options.optional) {
return true; return true;
} }
...@@ -87,7 +49,7 @@ var ParameterValidator = module.exports = { ...@@ -87,7 +49,7 @@ var ParameterValidator = module.exports = {
} }
return false return false
|| validateDeprication(value, expectation, options) || validateDeprecation(value, expectation, options)
|| validate(value, expectation, options); || validate(value, expectation, options);
} }
}; };
...@@ -49,7 +49,6 @@ ...@@ -49,7 +49,6 @@
"toposort-class": "~0.3.0", "toposort-class": "~0.3.0",
"generic-pool": "2.0.4", "generic-pool": "2.0.4",
"sql": "~0.35.0", "sql": "~0.35.0",
"circular-json": "~0.1.5",
"sequelize-bluebird": "git://github.com/sequelize/bluebird.git#9df139af53c5d346ffd38df30fc9dc60c62a9c98", "sequelize-bluebird": "git://github.com/sequelize/bluebird.git#9df139af53c5d346ffd38df30fc9dc60c62a9c98",
"node-uuid": "~1.4.1" "node-uuid": "~1.4.1"
}, },
......
...@@ -146,10 +146,6 @@ describe(Support.getTestDialectTeaser("Utils"), function() { ...@@ -146,10 +146,6 @@ describe(Support.getTestDialectTeaser("Utils"), function() {
}) })
describe('expectation', function() { describe('expectation', function() {
it('uses the typeof method if the expectation is a string', function() {
expect(Utils.validateParameter(1, 'number')).to.be.true
})
it('uses the instanceof method if the expectation is a class', function() { it('uses the instanceof method if the expectation is a class', function() {
expect(Utils.validateParameter(new Number(1), Number)).to.be.true expect(Utils.validateParameter(new Number(1), Number)).to.be.true
}) })
...@@ -161,21 +157,6 @@ describe(Support.getTestDialectTeaser("Utils"), function() { ...@@ -161,21 +157,6 @@ describe(Support.getTestDialectTeaser("Utils"), function() {
Utils.validateParameter(1, String) Utils.validateParameter(1, String)
}).to.throw(/The parameter.*is no.*/) }).to.throw(/The parameter.*is no.*/)
}) })
it('does not throw an error if throwError is false', function() {
expect(Utils.validateParameter(1, String, { throwError: false })).to.be.false
})
})
describe('deprecation warning', function() {
it('uses the passed function', function() {
var spy = chai.spy(function(s){})
Utils.validateParameter([], Object, {
deprecated: Array,
onDeprecated: spy
})
expect(spy).to.have.been.called()
})
}) })
}) })
}) })
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!