Skip to content

Commit d3c5cf6

Browse files
author
Ruben Bridgewater
committed
Improve options validation
1 parent 200a8cf commit d3c5cf6

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

lib/parser.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,16 @@ function parseType (parser, type) {
224224
}
225225
}
226226

227+
// All allowed options including their typeof value
228+
var optionTypes = {
229+
returnError: 'function',
230+
returnFatalError: 'function',
231+
returnReply: 'function',
232+
returnBuffers: 'boolean',
233+
stringNumbers: 'boolean',
234+
name: 'string'
235+
}
236+
227237
/**
228238
* Javascript Redis Parser
229239
* @param options
@@ -233,18 +243,19 @@ function JavascriptRedisParser (options) {
233243
if (!(this instanceof JavascriptRedisParser)) {
234244
return new JavascriptRedisParser(options)
235245
}
236-
if (
237-
!options ||
238-
typeof options.returnError !== 'function' ||
239-
typeof options.returnReply !== 'function'
240-
) {
246+
if (!options || !options.returnError || !options.returnReply) {
241247
throw new TypeError('Please provide all return functions while initiating the parser')
242248
}
249+
for (var key in options) {
250+
if (typeof options[key] !== optionTypes[key]) {
251+
throw new TypeError('The options argument contains unkown properties or properties of a wrong type')
252+
}
253+
}
243254
if (options.name === 'hiredis') {
244255
/* istanbul ignore next: hiredis is only supported for legacy usage */
245256
try {
246257
var Hiredis = require('../test/hiredis')
247-
console.error(new TypeError('Using the hiredis parser is discouraged. Please remove the name option.').stack.replace('Error', 'Warning'))
258+
console.error(new TypeError('Using hiredis is discouraged. Please use the faster JS parser by removing the name option.').stack.replace('Error', 'Warning'))
248259
return new Hiredis(options)
249260
} catch (e) {
250261
console.error(new TypeError('Hiredis is not installed. Please remove the `name` option. The (faster) JS parser is used instead.').stack.replace('Error', 'Warning'))

test/parsers.spec.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,39 @@ describe('parsers', function () {
2424
assert.strictEqual(parser.name, 'hiredis')
2525
})
2626

27-
it('fail for missing options', function () {
27+
it('fail for missing options argument', function () {
28+
assert.throws(function () {
29+
JavascriptParser()
30+
}, function (err) {
31+
assert.strictEqual(err.message, 'Please provide all return functions while initiating the parser')
32+
assert(err instanceof TypeError)
33+
return true
34+
})
35+
})
36+
37+
it('fail for faulty options properties', function () {
2838
assert.throws(function () {
2939
JavascriptParser({
3040
returnReply: returnReply,
31-
returnBuffers: true,
32-
name: 'hiredis'
41+
returnError: true
3342
})
3443
}, function (err) {
35-
assert.strictEqual(err.message, 'Please provide all return functions while initiating the parser')
44+
assert.strictEqual(err.message, 'The options argument contains unkown properties or properties of a wrong type')
45+
assert(err instanceof TypeError)
46+
return true
47+
})
48+
})
49+
50+
it('fail for faulty options properties #2', function () {
51+
assert.throws(function () {
52+
JavascriptParser({
53+
returnReply: returnReply,
54+
returnError: returnError,
55+
bla: undefined
56+
})
57+
}, function (err) {
58+
assert.strictEqual(err.message, 'The options argument contains unkown properties or properties of a wrong type')
59+
assert(err instanceof TypeError)
3660
return true
3761
})
3862
})

0 commit comments

Comments
 (0)