Skip to content

Commit

Permalink
fix: flag required params as missing when explicitly undefined, null,…
Browse files Browse the repository at this point in the history
… or an empty string (#90)

before, we were checking for "truthyness". when required params were set to `false`, they were incorrectly being flagged as missing.
  • Loading branch information
dpopp07 committed May 4, 2020
1 parent b8b35f8 commit 414b674
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ export function getMissingParams(
missing = requires;
} else {
missing = [];
requires.forEach((require) => {
if (!params[require]) {
requires.forEach(require => {
if (isMissing(params[require])) {
missing.push(require);
}
});
Expand All @@ -124,6 +124,17 @@ export function getMissingParams(
: null;
}

/**
* Returns true if value is determined to be "missing". Currently defining "missing"
* as `undefined`, `null`, or the empty string.
*
* @param value - the parameter value
* @returns boolean
*/
function isMissing(value: any): boolean {
return value === undefined || value === null || value === '';
}

/**
* Return true if 'text' is html
* @param {string} text - The 'text' to analyze
Expand Down
4 changes: 4 additions & 0 deletions test/unit/get-missing-params.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,8 @@ describe('getMissingParams', function() {
'Missing required parameters: a, b'
);
});

it('should not throw an error if a required parameter is given and set to false', function() {
expect(getMissingParams({ a: 'a', b: false }, ['a', 'b'])).toBeNull();
});
});

0 comments on commit 414b674

Please sign in to comment.