Skip to content

Commit

Permalink
Merge pull request #293 from raveljs/bugfix/290
Browse files Browse the repository at this point in the history
Deliberately registering an undefined default value for a param should throw, as this is likely to be user error.
  • Loading branch information
Ghnuberath authored Jan 30, 2020
2 parents f68159d + f42195f commit 1e6d819
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 9 additions & 0 deletions jest/core/params.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ describe('Ravel', () => {
app.set('log level', app.$log.NONE);
});

describe('#registerParamter()', () => {
it('should throw if a client attempts to register a parameter with a deliberately undefined default value', () => {
expect(() => {
const undefinedEnvironmentVariable = undefined;
app.registerParameter('my param', true, undefinedEnvironmentVariable);
}).toThrow(app.$err.IllegalValue);
});
});

describe('#set()', () => {
it('should allow clients to set the value of a parameter', async () => {
app.registerParameter('test param', false);
Expand Down
10 changes: 7 additions & 3 deletions lib/core/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,21 +125,25 @@ module.exports = function (Ravel) {
}
};

/* eslint-disable jsdoc/check-param-names */
/**
* Register an application parameter.
*
* @param {string} key - The key for the parameter.
* @param {boolean} required - True, iff the parameter is required. False otherwise.
* @param {(any | undefined)} defaultValue - The default value for the parameter.
*/
Ravel.prototype.registerParameter = function (key, required, defaultValue) {
Ravel.prototype.registerParameter = function (key, required) {
this[symbols.knownParameters][key] = {
required: required
};
if (defaultValue !== undefined) {
defaults[key] = JSON.parse(JSON.stringify(defaultValue));
if (arguments.length === 3 && arguments[2] !== undefined) {
defaults[key] = JSON.parse(JSON.stringify(arguments[2]));
} else if (arguments.length === 3 && arguments[2] === undefined) {
throw new this.$err.IllegalValue(`Undefined default value supplied for parameter "${key}"`);
}
};
/* eslint-enable jsdoc/check-param-names */

/**
* Set the value of an application parameter.
Expand Down

0 comments on commit 1e6d819

Please sign in to comment.