From 66f5f5f3e27e505786148054cd9411bcdac63a14 Mon Sep 17 00:00:00 2001 From: William Thorsen Date: Sun, 23 Aug 2020 02:13:48 -0600 Subject: [PATCH] feat: If a default value is given, `readConfigValue` ignores `required` This allows the `required` setting to be conditional upon whether a default value has already been supplied by preceding code --- src/config/__tests__/readConfigValue.unit.test.ts | 13 +++++++++++++ src/config/readConfigValue.ts | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/config/__tests__/readConfigValue.unit.test.ts b/src/config/__tests__/readConfigValue.unit.test.ts index ba0cf7b..44e80d8 100644 --- a/src/config/__tests__/readConfigValue.unit.test.ts +++ b/src/config/__tests__/readConfigValue.unit.test.ts @@ -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 = { diff --git a/src/config/readConfigValue.ts b/src/config/readConfigValue.ts index f615096..1e494ef 100644 --- a/src/config/readConfigValue.ts +++ b/src/config/readConfigValue.ts @@ -46,7 +46,7 @@ export function readConfigValue( } const { defaultValue, exitOnError, quiet, required } = options; - if (required) { + if (required && defaultValue === undefined) { const errorMsg = 'Error!'; if (exitOnError) { if (!quiet) {