Skip to content

Commit

Permalink
Empty string should could as null (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
lokankr committed Aug 4, 2023
1 parent bf318dd commit 75377bd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
31 changes: 31 additions & 0 deletions src/config/ConfigScope.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ describe('ConfigScope', () => {

expect(resolvedValue).toBeNull()
})

it('uses default on empty string', () => {
process.env.value = ''
const configScope = new ConfigScope()

const resolvedValue = configScope.getOptionalNullable('value', 'def')

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

describe('getOptional', () => {
Expand Down Expand Up @@ -397,6 +406,15 @@ describe('ConfigScope', () => {
expect(resolvedValue).toBe('def')
})

it('uses default value on empty string', () => {
process.env.value = ''
const configScope = new ConfigScope()

const resolvedValue = configScope.getOptionalValidated('value', 'def', validator)

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

it('throws an error if failing validation', () => {
process.env.value = '12345678900'
const configScope = new ConfigScope()
Expand Down Expand Up @@ -433,6 +451,19 @@ describe('ConfigScope', () => {

expect(resolvedValue).toBe('def/')
})

it('uses default value on empty string', () => {
process.env.value = ''
const configScope = new ConfigScope()

const resolvedValue = configScope.getOptionalTransformed(
'value',
'def',
ensureClosingSlashTransformer,
)

expect(resolvedValue).toBe('def/')
})
})

describe('getMandatoryTransformed', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/config/ConfigScope.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class ConfigScope {
param: string,
defaultValue: T,
): T | string {
return this.env[param] ?? defaultValue
return this.env[param] || defaultValue
}

getOptional(param: string, defaultValue: string): string {
Expand Down Expand Up @@ -118,7 +118,7 @@ export class ConfigScope {
defaultValue: string,
validator: EnvValueValidator<string>,
): string {
const value = this.env[param] ?? defaultValue
const value = this.env[param] || defaultValue
if (!validator(value)) {
throw new InternalError({
message: `Value ${value} is invalid for parameter ${param}`,
Expand Down Expand Up @@ -150,7 +150,7 @@ export class ConfigScope {
defaultValue: string,
transformer: (value: string) => string,
): string {
const value = this.env[param] ?? defaultValue
const value = this.env[param] || defaultValue
return transformer(value)
}

Expand Down

0 comments on commit 75377bd

Please sign in to comment.