fixes #258 chainable .required() (#259)

* fixes #258 chainable .required()

* fixes #258 use var instead of let for travis builds
master
Ahmed Ayoub 2017-11-04 05:34:27 +01:00 committed by Matt Hamann
parent bac910a6df
commit 3607767f90
3 changed files with 27 additions and 8 deletions

View File

@ -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

View File

@ -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;
}
};

View File

@ -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'
});
}
}