From 295ebc88b6afaed9245d9115dc56044bcffbbae5 Mon Sep 17 00:00:00 2001 From: Andreas Kristiansen Date: Fri, 4 Aug 2023 13:10:33 +0200 Subject: [PATCH] Empty string should could as null --- src/config/ConfigScope.spec.ts | 31 +++++++++++++++++++++++++++++++ src/config/ConfigScope.ts | 6 +++--- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/config/ConfigScope.spec.ts b/src/config/ConfigScope.spec.ts index e8818bd..f4728e0 100644 --- a/src/config/ConfigScope.spec.ts +++ b/src/config/ConfigScope.spec.ts @@ -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', () => { @@ -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() @@ -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', () => { diff --git a/src/config/ConfigScope.ts b/src/config/ConfigScope.ts index 9bbed54..c348d83 100644 --- a/src/config/ConfigScope.ts +++ b/src/config/ConfigScope.ts @@ -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 { @@ -118,7 +118,7 @@ export class ConfigScope { defaultValue: string, validator: EnvValueValidator, ): 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}`, @@ -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) }