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

Commit 2bf4ea51 by Sascha Depold

parameter validation

1 parent 3e92cb5a
...@@ -3,6 +3,7 @@ var util = require("util") ...@@ -3,6 +3,7 @@ var util = require("util")
, SqlString = require("./sql-string") , SqlString = require("./sql-string")
, lodash = require("lodash") , lodash = require("lodash")
, _string = require('underscore.string') , _string = require('underscore.string')
, ParameterValidator = require('./utils/parameter-validator')
var Utils = module.exports = { var Utils = module.exports = {
_: (function() { _: (function() {
...@@ -535,6 +536,10 @@ var Utils = module.exports = { ...@@ -535,6 +536,10 @@ var Utils = module.exports = {
var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8) var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8)
return v.toString(16) return v.toString(16)
}) })
},
validateParameter: function(value, expectation, options) {
return ParameterValidator.check(value, expectation, options)
} }
} }
......
var ParameterValidator = module.exports = {
check: function(value, expectation, options) {
options = Utils._.extend({
throwError: true,
deprecated: false,
onDeprecated: console.log
}, options || {})
if (value === undefined) {
throw new Error('No value has been passed.')
}
if (expectation === undefined) {
throw new Error('No expectation has been passed.')
}
validateDeprication(value, expectation, options)
return validate(value, expectation, options)
}
}
var matchesExpectation = function(value, expectation) {
if (typeof expectation === 'string') {
return (typeof value === expectation.toString())
} else {
return (value instanceof expectation)
}
}
var validateDeprication = function(value, expectation, options) {
if (options.deprecated) {
if (matchesExpectation(value, options.deprecated)) {
options.onDeprecated("Deprecated!")
}
}
}
var validate = function(value, expectation, options) {
var result = matchesExpectation(value, expectation)
if (result) {
return result
} else if (!options.throwError) {
return false
} else {
throw new Error('The parameter (value: ' + value.toString() + ') is no ' + expectation + '.')
}
}
...@@ -58,7 +58,8 @@ ...@@ -58,7 +58,8 @@
"mocha": "~1.13.0", "mocha": "~1.13.0",
"chai-datetime": "~1.1.1", "chai-datetime": "~1.1.1",
"sinon": "~1.7.3", "sinon": "~1.7.3",
"mariasql": "git://github.com/sequelize/node-mariasql.git" "mariasql": "git://github.com/sequelize/node-mariasql.git",
"chai-spies": "~0.5.1"
}, },
"keywords": [ "keywords": [
"mysql", "mysql",
......
var chai = require('chai') var chai = require('chai')
, spies = require('chai-spies')
, expect = chai.expect , expect = chai.expect
, Utils = require(__dirname + '/../lib/utils') , Utils = require(__dirname + '/../lib/utils')
, Support = require(__dirname + '/support') , Support = require(__dirname + '/support')
chai.use(spies)
chai.Assertion.includeStack = true chai.Assertion.includeStack = true
describe(Support.getTestDialectTeaser("Utils"), function() { describe(Support.getTestDialectTeaser("Utils"), function() {
...@@ -158,4 +160,53 @@ describe(Support.getTestDialectTeaser("Utils"), function() { ...@@ -158,4 +160,53 @@ describe(Support.getTestDialectTeaser("Utils"), function() {
done() done()
}) })
}) })
describe('validateParameter', function() {
describe('method signature', function() {
it('throws an error if the value is not defined', function() {
expect(function() {
Utils.validateParameter()
}).to.throw('No value has been passed.')
})
it('throws an error if the expectation is not defined', function() {
expect(function() {
Utils.validateParameter(1)
}).to.throw('No expectation has been passed.')
})
})
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() {
expect(Utils.validateParameter(new Number(1), Number)).to.be.true
})
})
describe('failing expectations', function() {
it('throws an error if the expectation does not match', function() {
expect(function() {
Utils.validateParameter(1, String)
}).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!