generated from UK-Export-Finance/nestjs-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(APIM-468): change how integer config values are parsed (#357)
### Introduction Current way to parse environment variables and set default value doesn't allow using 0. 0 can be useful for APIM_INFORMATICA_MAX_REDIRECTS, APIM_INFORMATICA_TIMEOUT and similar integer environment variables. ### Resolution This is copy of similar implementation in TFS API. Created helper getIntConfig. It parses input string and returns integer from input or optional default value. Helper throws exception for these edge cases: - input is set, but not string - input and default values are undefined - input is not integer in string format. For example it is float or has some non digital characters - input number is not using decimal base ### Misc Moved boolean environment variable test to shared test `withEnvironmentVariableParsingUnitTests`
- Loading branch information
Showing
7 changed files
with
246 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { InvalidConfigException } from '@ukef/config/invalid-config.exception'; | ||
|
||
import { getIntConfig } from './get-int-config'; | ||
|
||
describe('GetIntConfig helper', () => { | ||
describe('getIntConfig returns value', () => { | ||
it.each([ | ||
{ value: undefined, defaultValue: 60, expectedResult: 60 }, | ||
{ value: '123', defaultValue: 60, expectedResult: 123 }, | ||
{ value: '123', defaultValue: undefined, expectedResult: 123 }, | ||
{ value: '-123', defaultValue: 60, expectedResult: -123 }, | ||
{ value: '0', defaultValue: 60, expectedResult: 0 }, | ||
{ value: `${Number.MAX_SAFE_INTEGER}`, defaultValue: 60, expectedResult: Number.MAX_SAFE_INTEGER }, | ||
{ value: `-${Number.MAX_SAFE_INTEGER}`, defaultValue: 60, expectedResult: -Number.MAX_SAFE_INTEGER }, | ||
])('if input is "$value", returns $expectedResult', ({ value, defaultValue, expectedResult }) => { | ||
expect(getIntConfig(value, defaultValue)).toBe(expectedResult); | ||
}); | ||
}); | ||
|
||
describe('getIntConfig throws invalid integer exception', () => { | ||
it.each(['abc', '12.5', '20th', '0xFF', '0b101'])(`throws InvalidConfigException for "%s" because it is not valid integer`, (value) => { | ||
const gettingTheConfig = () => getIntConfig(value as unknown as string); | ||
|
||
expect(gettingTheConfig).toThrow(InvalidConfigException); | ||
expect(gettingTheConfig).toThrow(`Invalid integer value "${value}" for configuration property.`); | ||
}); | ||
}); | ||
|
||
describe('getIntConfig throws InvalidConfigException because environment variable type is not string', () => { | ||
it.each([12, true, null, false, /.*/, {}, [], 0xff, 0b101])( | ||
'throws InvalidConfigException for "%s" because environment variable type is not string', | ||
(value) => { | ||
const gettingTheConfig = () => getIntConfig(value as unknown as string); | ||
|
||
expect(gettingTheConfig).toThrow(InvalidConfigException); | ||
expect(gettingTheConfig).toThrow(`Input environment variable type for ${value} should be string.`); | ||
}, | ||
); | ||
}); | ||
|
||
it('throws InvalidConfigException if environment variable and default value is missing', () => { | ||
const gettingTheConfig = () => getIntConfig(undefined); | ||
|
||
expect(gettingTheConfig).toThrow(InvalidConfigException); | ||
expect(gettingTheConfig).toThrow("Environment variable is missing and doesn't have default value."); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { InvalidConfigException } from '@ukef/config/invalid-config.exception'; | ||
|
||
// This helper function is used to get integer from configuration. | ||
export const getIntConfig = (environmentVariable: string, defaultValue?: number): number => { | ||
if (typeof environmentVariable === 'undefined') { | ||
if (typeof defaultValue === 'undefined') { | ||
throw new InvalidConfigException(`Environment variable is missing and doesn't have default value.`); | ||
} | ||
return defaultValue; | ||
} | ||
if (typeof environmentVariable !== 'string') { | ||
throw new InvalidConfigException(`Input environment variable type for ${environmentVariable} should be string.`); | ||
} | ||
|
||
const integer = parseInt(environmentVariable, 10); | ||
// Check if parseInt is number, decimal base integer and input didn't have anything non-numeric. | ||
if (isNaN(integer) || integer.toString() !== environmentVariable) { | ||
throw new InvalidConfigException(`Invalid integer value "${environmentVariable}" for configuration property.`); | ||
} | ||
return integer; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.