Skip to content

Commit 9b82fac

Browse files
committed
feat: adds 'allowMissing' option on $envVar
1 parent 1912747 commit 9b82fac

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

app-config-extensions/src/env-var-directive.test.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('$envVar directive', () => {
2222
expect(parsed.toJSON()).toEqual({ foo: 'foo', bar: 'bar' });
2323
});
2424

25-
it('reads object with $name', async () => {
25+
it('reads object with name', async () => {
2626
process.env.FOO = 'foo';
2727

2828
const source = new LiteralSource({
@@ -34,15 +34,15 @@ describe('$envVar directive', () => {
3434
expect(parsed.toJSON()).toEqual({ foo: 'foo' });
3535
});
3636

37-
it('fails with $name when not defined', async () => {
37+
it('fails with name when not defined', async () => {
3838
const source = new LiteralSource({
3939
foo: { $envVar: { name: 'FOO' } },
4040
});
4141

4242
await expect(source.read([envVarDirective()])).rejects.toThrow();
4343
});
4444

45-
it('uses $name when $fallback is defined', async () => {
45+
it('uses name when fallback is defined', async () => {
4646
process.env.FOO = 'foo';
4747

4848
const source = new LiteralSource({
@@ -54,7 +54,7 @@ describe('$envVar directive', () => {
5454
expect(parsed.toJSON()).toEqual({ foo: 'foo' });
5555
});
5656

57-
it('uses $fallback when $name was not found', async () => {
57+
it('uses fallback when name was not found', async () => {
5858
const source = new LiteralSource({
5959
foo: { $envVar: { name: 'FOO', fallback: 'bar' } },
6060
});
@@ -64,7 +64,7 @@ describe('$envVar directive', () => {
6464
expect(parsed.toJSON()).toEqual({ foo: 'bar' });
6565
});
6666

67-
it('allows null value when $allowNull', async () => {
67+
it('allows null value when allowNull', async () => {
6868
const source = new LiteralSource({
6969
foo: { $envVar: { name: 'FOO', fallback: null, allowNull: true } },
7070
});
@@ -74,14 +74,27 @@ describe('$envVar directive', () => {
7474
expect(parsed.toJSON()).toEqual({ foo: null });
7575
});
7676

77-
it('does not allow number even when $allowNull', async () => {
77+
it('does not allow number even when allowNull', async () => {
7878
const source = new LiteralSource({
7979
foo: { $envVar: { name: 'FOO', fallback: 42, allowNull: true } },
8080
});
8181

8282
await expect(source.read([envVarDirective()])).rejects.toThrow();
8383
});
8484

85+
it('returns null when allowMissing', async () => {
86+
process.env.BAR = 'foo';
87+
88+
const source = new LiteralSource({
89+
foo: { $envVar: { name: 'FOO', allowMissing: true } },
90+
bar: { $envVar: { name: 'BAR', allowMissing: true } },
91+
});
92+
93+
const parsed = await source.read([envVarDirective()]);
94+
95+
expect(parsed.toJSON()).toEqual({ foo: null, bar: 'foo' });
96+
});
97+
8598
it('parses ints', async () => {
8699
process.env.FOO = '11';
87100

app-config-extensions/src/env-var-directive.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,24 @@ export function envVarDirective(
8686
return parseValue(resolvedValue);
8787
}
8888

89-
if (typeof value === 'object' && value.fallback !== undefined) {
90-
const fallback = (await parse(value.fallback)).toJSON();
91-
const allowNull = (await parse(value.allowNull)).toJSON();
92-
93-
if (allowNull) {
94-
validateStringOrNull(fallback, [...ctx, key, [InObject, 'fallback']]);
95-
} else {
96-
validateString(fallback, [...ctx, key, [InObject, 'fallback']]);
89+
if (typeof value === 'object') {
90+
if (value.fallback !== undefined) {
91+
const fallback = (await parse(value.fallback)).toJSON();
92+
const allowNull = (await parse(value.allowNull)).toJSON();
93+
94+
if (allowNull) {
95+
validateStringOrNull(fallback, [...ctx, key, [InObject, 'fallback']]);
96+
} else {
97+
validateString(fallback, [...ctx, key, [InObject, 'fallback']]);
98+
}
99+
100+
return parseValue(fallback);
97101
}
102+
const allowMissing = (await parse(value.allowMissing)).toJSON();
98103

99-
return parseValue(fallback);
104+
if (allowMissing) {
105+
return parseValue(null);
106+
}
100107
}
101108

102109
throw new AppConfigError(`$envVar could not find ${name} environment variable`);

0 commit comments

Comments
 (0)