Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config get optional one of #60

Merged
merged 4 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ Once the class is instantiated, you can leverage the following `ConfigScope` met
* `getOptionalBoolean()`, returns the value of an optional configuration parameter and validates that it is a boolean. It the value is missing, it is assigned the `defaultValue`. If it is not a boolean, an `InternalError` is thrown. Parameters are:
* `param`, the configuration parameter name;
* `defaultValue`.
* `getOptionalOneOf()`, returns the value of an optional configuration parameter, if the value is missing, it falls back to the specified default value, and validates that it is one of the supported values. If the value is not supported, an `InternalError` is thrown. Parameters are:
* `param`, the configuration parameter name;
* `defaultValue`
* `supportedValues`;

### Environment Configuration Parameter

Expand Down
38 changes: 38 additions & 0 deletions src/config/ConfigScope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,44 @@ describe('ConfigScope', () => {
})
})

describe('getOptionalOneOf', () => {
it('returns env value if it exists on the list', () => {
process.env.value = 'g'
const configScope = new ConfigScope()

const resolvedValue = configScope.getOptionalOneOf('value', 'default', ['a', 'g', 'b'])

expect(resolvedValue).toBe('g')
})

it('returns default if env value not exists and the default one exists on the list', () => {
delete process.env.value
const configScope = new ConfigScope()

const resolvedValue = configScope.getOptionalOneOf('value', 'g', ['a', 'g', 'b'])

expect(resolvedValue).toBe('g')
})

it('throws an error on env value item not from list', () => {
process.env.value = 'c'
const configScope = new ConfigScope()

expect(() => configScope.getOptionalOneOf('value', 'default', ['a', 'g', 'b'])).toThrow(
/Unsupported value/,
)
})

it('throws an error if env value not exists and the default one not exists on the list', () => {
delete process.env.value
const configScope = new ConfigScope()

expect(() => configScope.getOptionalOneOf('value', 'default', ['a'])).toThrow(
/Unsupported value/,
)
})
})

describe('getOptionalInteger', () => {
it('accepts value', () => {
process.env.value = '3'
Expand Down
9 changes: 9 additions & 0 deletions src/config/ConfigScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@ export class ConfigScope {
)
}

getOptionalOneOf(param: string, defaultValue: string, supportedValues: string[]): string {
kibertoad marked this conversation as resolved.
Show resolved Hide resolved
const result = this.getOptional(param, defaultValue)
return validateOneOf(
result,
supportedValues,
`Unsupported ${param}: ${result}. Supported values: ${supportedValues.toString()}`,
)
}

getOptionalValidated(
param: string,
defaultValue: string,
Expand Down