Skip to content

Commit 6958769

Browse files
authored
NODE-935, NODE-931 Introduce validateOptions option to MongoClient.co… (#1478)
* NODE-935, NODE-931 Introduce validateOptions option to MongoClient.connect options to allow for strict validation of MongoClient options * fixed test case
1 parent 1a69e80 commit 6958769

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

lib/mongo_client.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var validOptionNames = ['poolSize', 'ssl', 'sslValidate', 'sslCA', 'sslCert',
3434
'serializeFunctions', 'ignoreUndefined', 'raw', 'promoteLongs', 'bufferMaxEntries',
3535
'readPreference', 'pkFactory', 'promiseLibrary', 'readConcern', 'maxStalenessSeconds',
3636
'loggerLevel', 'logger', 'promoteValues', 'promoteBuffers', 'promoteLongs',
37-
'domainsEnabled', 'keepAliveInitialDelay', 'checkServerIdentity'];
37+
'domainsEnabled', 'keepAliveInitialDelay', 'checkServerIdentity', 'validateOptions'];
3838
var ignoreOptionNames = ['native_parser'];
3939
var legacyOptionNames = ['server', 'replset', 'replSet', 'mongos', 'db'];
4040

@@ -46,11 +46,13 @@ function validOptions(options) {
4646
continue;
4747
}
4848

49-
if(_validOptions.indexOf(name) == -1) {
49+
if(_validOptions.indexOf(name) == -1 && options.validateOptions) {
5050
return new MongoError(f('option %s is not supported', name));
51+
} else if(_validOptions.indexOf(name) == -1) {
52+
console.warn(f('the options [%s] is not supported', name));
5153
}
5254

53-
if(legacyOptionNames.indexOf(name) == -1) {
55+
if(legacyOptionNames.indexOf(name) != -1) {
5456
console.warn(f('the server/replset/mongos options are deprecated, '
5557
+ 'all their options are supported at the top level of the options object [%s]', validOptionNames));
5658
}
@@ -121,6 +123,7 @@ function MongoClient() {
121123
* @param {number} [options.maxStalenessSeconds=undefined] The max staleness to secondary reads (values under 10 seconds cannot be guaranteed);
122124
* @param {string} [options.loggerLevel=undefined] The logging level (error/warn/info/debug)
123125
* @param {object} [options.logger=undefined] Custom logger object
126+
* @param {object} [options.validateOptions=false] Validate MongoClient passed in options for correctness.
124127
* @param {MongoClient~connectCallback} [callback] The command result callback
125128
* @return {Promise} returns Promise if no callback passed
126129
*/
@@ -181,6 +184,7 @@ var define = MongoClient.define = new Define('MongoClient', MongoClient, false);
181184
* @param {number} [options.maxStalenessSeconds=undefined] The max staleness to secondary reads (values under 10 seconds cannot be guaranteed);
182185
* @param {string} [options.loggerLevel=undefined] The logging level (error/warn/info/debug)
183186
* @param {object} [options.logger=undefined] Custom logger object
187+
* @param {object} [options.validateOptions=false] Validate MongoClient passed in options for correctness.
184188
* @param {MongoClient~connectCallback} [callback] The command result callback
185189
* @return {Promise} returns Promise if no callback passed
186190
*/

test/functional/mongo_client_options_tests.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ exports['should error on unexpected options'] = {
5353
var connect = configuration.require;
5454

5555
connect(configuration.url(), {
56-
autoReconnect: true, poolSize: 4, notlegal: {}
56+
autoReconnect: true, poolSize: 4, notlegal: {}, validateOptions:true
5757
}, function(err, db) {
5858
test.ok(err.message.indexOf('option notlegal is not supported') != -1);
5959
test.done();

0 commit comments

Comments
 (0)