Skip to content

Commit

Permalink
Warn undefined configs (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGoo authored Oct 17, 2020
1 parent 5a46560 commit 97df2a5
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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 <engineering@neofinancial.com>",
"main": "build/index.js",
Expand Down
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -128,7 +129,7 @@ const loadConfig = (configPath = ''): ConfigObject => {
);
const config = Object.assign({}, fileConfig, loadSecrets(fileConfig), loadEnvironment());

return config;
return validateConfig(config);
};

const init = (): ConfigObject => {
Expand Down
16 changes: 16 additions & 0 deletions src/validate-config.ts
Original file line number Diff line number Diff line change
@@ -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;
7 changes: 7 additions & 0 deletions test/fixtures/validate/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default {
KEY_1: 'value 1',
KEY_2: 0,
KEY_3: undefined,
KEY_4: 'undefined',
KEY_5: ''
};
11 changes: 11 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import { loadConfig } from '../src';

test('loading TypeScript config works', (): void => {
Expand Down Expand Up @@ -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');
});

0 comments on commit 97df2a5

Please sign in to comment.