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

Commit 0e0cbf19 by Sascha Depold

parameter validator supports optional parameters

1 parent 2bf4ea51
...@@ -3,9 +3,14 @@ var ParameterValidator = module.exports = { ...@@ -3,9 +3,14 @@ var ParameterValidator = module.exports = {
options = Utils._.extend({ options = Utils._.extend({
throwError: true, throwError: true,
deprecated: false, deprecated: false,
onDeprecated: console.log onDeprecated: console.log,
optional: false
}, options || {}) }, options || {})
if (options.optional && (value === undefined)) {
return true
}
if (value === undefined) { if (value === undefined) {
throw new Error('No value has been passed.') throw new Error('No value has been passed.')
} }
...@@ -14,9 +19,9 @@ var ParameterValidator = module.exports = { ...@@ -14,9 +19,9 @@ var ParameterValidator = module.exports = {
throw new Error('No expectation has been passed.') throw new Error('No expectation has been passed.')
} }
validateDeprication(value, expectation, options) return false
|| validateDeprication(value, expectation, options)
return validate(value, expectation, options) || validate(value, expectation, options)
} }
} }
...@@ -32,6 +37,7 @@ var validateDeprication = function(value, expectation, options) { ...@@ -32,6 +37,7 @@ var validateDeprication = function(value, expectation, options) {
if (options.deprecated) { if (options.deprecated) {
if (matchesExpectation(value, options.deprecated)) { if (matchesExpectation(value, options.deprecated)) {
options.onDeprecated("Deprecated!") options.onDeprecated("Deprecated!")
return true
} }
} }
} }
......
...@@ -169,6 +169,12 @@ describe(Support.getTestDialectTeaser("Utils"), function() { ...@@ -169,6 +169,12 @@ describe(Support.getTestDialectTeaser("Utils"), function() {
}).to.throw('No value has been passed.') }).to.throw('No value has been passed.')
}) })
it('does not throw an error if the value is not defined and the parameter is optional', function() {
expect(function() {
Utils.validateParameter(undefined, Object, { optional: true })
}).to.not.throw()
})
it('throws an error if the expectation is not defined', function() { it('throws an error if the expectation is not defined', function() {
expect(function() { expect(function() {
Utils.validateParameter(1) Utils.validateParameter(1)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!