mirror of
https://github.com/directus/directus.git
synced 2026-04-25 03:00:53 -04:00
Correct treatment of empty string envs (#21371)
* Correct treatment of empty string envs * Add changeset * Update .changeset/swift-squids-invite.md
This commit is contained in:
5
.changeset/swift-squids-invite.md
Normal file
5
.changeset/swift-squids-invite.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@directus/env": patch
|
||||
---
|
||||
|
||||
Fixed the treatment of environment variables when set to an empty string
|
||||
23
packages/env/src/lib/cast.test.ts
vendored
23
packages/env/src/lib/cast.test.ts
vendored
@@ -77,26 +77,26 @@ describe('Casting', () => {
|
||||
vi.mocked(getCastFlag).mockReturnValue('string');
|
||||
|
||||
vi.mocked(toString).mockReturnValue('cast-value');
|
||||
expect(cast('value')).toBe('cast-value', 'key');
|
||||
expect(cast('value')).toBe('cast-value');
|
||||
});
|
||||
|
||||
test('Uses toNumber for number types', () => {
|
||||
vi.mocked(getCastFlag).mockReturnValue('number');
|
||||
|
||||
vi.mocked(toNumber).mockReturnValue(123);
|
||||
expect(cast('value')).toBe(123, 'key');
|
||||
expect(cast('value')).toBe(123);
|
||||
});
|
||||
|
||||
test('Uses toBoolean for number types', () => {
|
||||
vi.mocked(getCastFlag).mockReturnValue('boolean');
|
||||
|
||||
vi.mocked(toBoolean).mockReturnValue(false);
|
||||
expect(cast('value')).toBe(false, 'key');
|
||||
expect(cast('value')).toBe(false);
|
||||
});
|
||||
|
||||
test('Uses RegExp for regex types', () => {
|
||||
vi.mocked(getCastFlag).mockReturnValue('regex');
|
||||
expect(cast('value')).toBeInstanceOf(RegExp, 'key');
|
||||
expect(cast('value')).toBeInstanceOf(RegExp);
|
||||
});
|
||||
|
||||
test('Uses toArray for array types', () => {
|
||||
@@ -109,13 +109,24 @@ describe('Casting', () => {
|
||||
vi.mocked(toNumber).mockImplementation((v) => v);
|
||||
vi.mocked(toArray).mockReturnValue([1, 2, 3]);
|
||||
|
||||
expect(cast('array:value')).toEqual([1, 2, 3], 'key');
|
||||
expect(cast('array:value')).toEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
test('Filters empty strings values out of the array', () => {
|
||||
vi.mocked(getCastFlag).mockImplementation((v) => {
|
||||
if (String(v).startsWith('array')) return 'array';
|
||||
return null;
|
||||
});
|
||||
|
||||
vi.mocked(toArray).mockReturnValue(['', '']);
|
||||
|
||||
expect(cast('array:,')).toEqual([]);
|
||||
});
|
||||
|
||||
test('Uses tryJson for json types', () => {
|
||||
vi.mocked(getCastFlag).mockReturnValue('json');
|
||||
|
||||
vi.mocked(tryJson).mockReturnValue('cast-value');
|
||||
expect(cast('value')).toBe('cast-value', 'key');
|
||||
expect(cast('value')).toBe('cast-value');
|
||||
});
|
||||
});
|
||||
|
||||
4
packages/env/src/lib/cast.ts
vendored
4
packages/env/src/lib/cast.ts
vendored
@@ -23,7 +23,9 @@ export const cast = (value: unknown, key?: string): unknown => {
|
||||
case 'regex':
|
||||
return new RegExp(String(value));
|
||||
case 'array':
|
||||
return toArray(value).map((v) => cast(v));
|
||||
return toArray(value)
|
||||
.map((v) => cast(v))
|
||||
.filter((v) => v !== '');
|
||||
case 'json':
|
||||
return tryJson(value);
|
||||
}
|
||||
|
||||
4
packages/env/src/utils/guess-type.test.ts
vendored
4
packages/env/src/utils/guess-type.test.ts
vendored
@@ -43,4 +43,8 @@ describe('json', () => {
|
||||
test('Defaults to json for object type values', () => {
|
||||
expect(guessType({ hello: 'world' })).toBe('json');
|
||||
});
|
||||
|
||||
test('Defaults to json for empty string', () => {
|
||||
expect(guessType('')).toBe('json');
|
||||
});
|
||||
});
|
||||
|
||||
1
packages/env/src/utils/guess-type.ts
vendored
1
packages/env/src/utils/guess-type.ts
vendored
@@ -8,6 +8,7 @@ export const guessType = (value: unknown): EnvType => {
|
||||
if (
|
||||
String(value).startsWith('0') === false &&
|
||||
isNaN(Number(value)) === false &&
|
||||
String(value).length > 0 &&
|
||||
Number(value) <= Number.MAX_SAFE_INTEGER
|
||||
) {
|
||||
return 'number';
|
||||
|
||||
Reference in New Issue
Block a user