Skip to content

NODE-935, NODE-931 Introduce validateOptions option to MongoClient.co… #1478

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/mongo_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var validOptionNames = ['poolSize', 'ssl', 'sslValidate', 'sslCA', 'sslCert',
'serializeFunctions', 'ignoreUndefined', 'raw', 'promoteLongs', 'bufferMaxEntries',
'readPreference', 'pkFactory', 'promiseLibrary', 'readConcern', 'maxStalenessSeconds',
'loggerLevel', 'logger', 'promoteValues', 'promoteBuffers', 'promoteLongs',
'domainsEnabled', 'keepAliveInitialDelay', 'checkServerIdentity'];
'domainsEnabled', 'keepAliveInitialDelay', 'checkServerIdentity', 'validateOptions'];
var ignoreOptionNames = ['native_parser'];
var legacyOptionNames = ['server', 'replset', 'replSet', 'mongos', 'db'];

Expand All @@ -46,11 +46,13 @@ function validOptions(options) {
continue;
}

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

if(legacyOptionNames.indexOf(name) == -1) {
if(legacyOptionNames.indexOf(name) != -1) {
console.warn(f('the server/replset/mongos options are deprecated, '
+ 'all their options are supported at the top level of the options object [%s]', validOptionNames));
}
Expand Down Expand Up @@ -121,6 +123,7 @@ function MongoClient() {
* @param {number} [options.maxStalenessSeconds=undefined] The max staleness to secondary reads (values under 10 seconds cannot be guaranteed);
* @param {string} [options.loggerLevel=undefined] The logging level (error/warn/info/debug)
* @param {object} [options.logger=undefined] Custom logger object
* @param {object} [options.validateOptions=false] Validate MongoClient passed in options for correctness.
* @param {MongoClient~connectCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Expand Down Expand Up @@ -181,6 +184,7 @@ var define = MongoClient.define = new Define('MongoClient', MongoClient, false);
* @param {number} [options.maxStalenessSeconds=undefined] The max staleness to secondary reads (values under 10 seconds cannot be guaranteed);
* @param {string} [options.loggerLevel=undefined] The logging level (error/warn/info/debug)
* @param {object} [options.logger=undefined] Custom logger object
* @param {object} [options.validateOptions=false] Validate MongoClient passed in options for correctness.
* @param {MongoClient~connectCallback} [callback] The command result callback
* @return {Promise} returns Promise if no callback passed
*/
Expand Down
2 changes: 1 addition & 1 deletion test/functional/mongo_client_options_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ exports['should error on unexpected options'] = {
var connect = configuration.require;

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