From 97df2a506546a04f5f3b46cf0518d314dd9e616f Mon Sep 17 00:00:00 2001 From: Max Gooding Date: Sat, 17 Oct 2020 16:54:48 -0600 Subject: [PATCH] Warn undefined configs (#13) --- CHANGELOG.md | 4 ++++ README.md | 7 +++++++ package.json | 2 +- src/index.ts | 5 +++-- src/validate-config.ts | 16 ++++++++++++++++ test/fixtures/validate/config.default.ts | 7 +++++++ test/index.test.ts | 11 +++++++++++ 7 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 src/validate-config.ts create mode 100644 test/fixtures/validate/config.default.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 5998663..55127b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.4.2 (October 17, 2020) + +- Warn if a config is undefined, null, 'undefined' or an empty string + ## 1.4.1 (October 17, 2020) - Better error logging when fetching from Secrets Manager diff --git a/README.md b/README.md index 63b0cc0..8bbf5eb 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,13 @@ export default { The default region is `us-east-1` and the default connection timeout is `5000`ms. +Config Dug will warn if it detects invalid config values. Invalid values include: + +- undefined +- null +- the string 'undefined' +- an empty string + This package uses the [aws-sdk](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/) internally. Refer to their documentation for information about [authentication](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-credentials-node.html), configuring a default [region](https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/setting-region.html) and configuring [access control for AWS Secrets Manager](https://docs.aws.amazon.com/secretsmanager/latest/userguide/auth-and-access.html). ## Advanced diff --git a/package.json b/package.json index edb0961..f4e7123 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "config-dug", - "version": "1.4.1", + "version": "1.4.2", "description": "Config loader with support for AWS Secrets Manager", "author": "Neo Financial Engineering ", "main": "build/index.js", diff --git a/src/index.ts b/src/index.ts index 056c1dd..1af19ac 100644 --- a/src/index.ts +++ b/src/index.ts @@ -6,10 +6,11 @@ import path from 'path'; import createDebug from 'debug'; import getSecret from './get-secret'; +import validateConfig from './validate-config'; const debug = createDebug('config-dug'); -interface ConfigObject { +export interface ConfigObject { [key: string]: string | boolean | number; } @@ -128,7 +129,7 @@ const loadConfig = (configPath = ''): ConfigObject => { ); const config = Object.assign({}, fileConfig, loadSecrets(fileConfig), loadEnvironment()); - return config; + return validateConfig(config); }; const init = (): ConfigObject => { diff --git a/src/validate-config.ts b/src/validate-config.ts new file mode 100644 index 0000000..7912d61 --- /dev/null +++ b/src/validate-config.ts @@ -0,0 +1,16 @@ +/* eslint-disable no-console */ +import { ConfigObject } from '.'; + +const validateConfig = (config: ConfigObject): ConfigObject => { + Object.keys(config).forEach(key => { + const value = config[key]; + + if (value === undefined || value === null || value === 'undefined' || value === '') { + console.warn(`WARNING: Found undefined config value for ${key}`); + } + }); + + return config; +}; + +export default validateConfig; diff --git a/test/fixtures/validate/config.default.ts b/test/fixtures/validate/config.default.ts new file mode 100644 index 0000000..2be1082 --- /dev/null +++ b/test/fixtures/validate/config.default.ts @@ -0,0 +1,7 @@ +export default { + KEY_1: 'value 1', + KEY_2: 0, + KEY_3: undefined, + KEY_4: 'undefined', + KEY_5: '' +}; diff --git a/test/index.test.ts b/test/index.test.ts index 650a5a0..6b2f7d9 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,3 +1,4 @@ +/* eslint-disable no-console */ import { loadConfig } from '../src'; test('loading TypeScript config works', (): void => { @@ -77,3 +78,13 @@ test('loading invalid config does nothing', (): void => { expect(typeof invalidConfig).toBe('object'); }); + +test('config value that is undefined causes a warning', (): void => { + jest.spyOn(global.console, 'warn'); + + loadConfig('test/fixtures/validate'); + + expect(console.warn).toHaveBeenCalledWith('WARNING: Found undefined config value for KEY_3'); + expect(console.warn).toHaveBeenCalledWith('WARNING: Found undefined config value for KEY_4'); + expect(console.warn).toHaveBeenCalledWith('WARNING: Found undefined config value for KEY_5'); +});