diff --git a/README.md b/README.md index bf48f5c..e7f4e03 100644 --- a/README.md +++ b/README.md @@ -180,6 +180,24 @@ Declares a set of string keys to be mandatory, and throw an error if any are mis nconf.required(['keya', 'keyb']); // Error: Missing required keys: keyb ``` +You can also chain `.required()` calls when needed. for example when a configuration depends on another configuration store + +```js +config + .argv() + .env() + .required([ 'STAGE']) //here you should have STAGE otherwise throw an error + .file( 'stage', path.resolve( 'configs', 'stages', config.get( 'STAGE' ) + '.json' ) ) + .required([ 'OAUTH:redirectURL']) // here you should have OAUTH:redirectURL, otherwise throw an error + .file( 'oauth', path.resolve( 'configs', 'oauth', config.get( 'OAUTH:MODE' ) + '.json' ) ) + .file( 'app', path.resolve( 'configs', 'app.json' ) ) + .required([ 'LOGS_MODE']) // here you should haveLOGS_MODE, otherwise throw an error + .add( 'logs', { + type: 'literal', + store: require( path.resolve( 'configs', 'logs', config.get( 'LOGS_MODE' ) + '.js') ) + } ) + .defaults( defaults ); +``` ## Storage Engines diff --git a/lib/nconf/provider.js b/lib/nconf/provider.js index bb8d049..d094cba 100644 --- a/lib/nconf/provider.js +++ b/lib/nconf/provider.js @@ -363,7 +363,7 @@ Provider.prototype.required = function (keys) { if (missing.length) { throw new Error('Missing required keys: ' + missing.join(', ')); } else { - return true; + return this; } }; diff --git a/test/nconf-test.js b/test/nconf-test.js index 7244bbc..dddbabf 100644 --- a/test/nconf-test.js +++ b/test/nconf-test.js @@ -48,9 +48,10 @@ vows.describe('nconf').addBatch({ nconf.set('foo:bar:bazz', 'buzz'); assert.throws(nconf.required.bind(nconf, ['missing', 'foo:bar:bazz']), Error); }, - "should return true if all required keys exist": function() { + "should return the provider if all required keys exist": function() { + var Provider = nconf.Provider; nconf.set('foo:bar:bazz', 'buzz'); - assert.isTrue(nconf.required(['foo:bar:bazz'])); + assert.isTrue(nconf.required(['foo:bar:bazz']) instanceof Provider); } } } @@ -118,22 +119,22 @@ vows.describe('nconf').addBatch({ "without a callback": { "should respond with the merged store": function () { assert.deepEqual(nconf.load(), { - title: 'My specific title', + title: 'My specific title', color: 'green', - movie: 'Kill Bill' + movie: 'Kill Bill' }); } }, "with a callback": { topic: function () { - nconf.load(this.callback.bind(null, null)); + nconf.load(this.callback.bind(null, null)); }, "should respond with the merged store": function (ign, err, store) { assert.isNull(err); assert.deepEqual(store, { - title: 'My specific title', + title: 'My specific title', color: 'green', - movie: 'Kill Bill' + movie: 'Kill Bill' }); } }