Skip to content

Commit

Permalink
feat: If a default value is given, readConfigValue ignores required
Browse files Browse the repository at this point in the history
This allows the `required` setting to be conditional upon whether a default value has already been supplied by preceding code
  • Loading branch information
williamthorsen committed Aug 23, 2020
1 parent 0ba3007 commit 66f5f5f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/config/__tests__/readConfigValue.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ describe('readConfigValue(', () => {
}).toThrow();
});

it('if a default value is provided, the `required` option should be ignored', () => {
expect.assertions(2);
const options = { filepaths: tmpFilepaths };

expect(() => {
const valueOptions = { defaultValue: 0, required: true };

const value = readConfigValue(options, 'nonexistent-objectPath', valueOptions);

expect(value).toBe(0);
}).not.toThrow();
});

it("if a config file isn't found, should skip it", () => {
const defaultValue = 0;
const options = {
Expand Down
2 changes: 1 addition & 1 deletion src/config/readConfigValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function readConfigValue<T>(
}

const { defaultValue, exitOnError, quiet, required } = options;
if (required) {
if (required && defaultValue === undefined) {
const errorMsg = 'Error!';
if (exitOnError) {
if (!quiet) {
Expand Down

0 comments on commit 66f5f5f

Please sign in to comment.