Skip to content

Commit 17b33f6

Browse files
committed
fix: marks $env as "lazy" in validateOptions, to allow "enc:" in a non-active environment path
1 parent 97c81a5 commit 17b33f6

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

app-config-extension-utils/src/index.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,14 +46,21 @@ export function validateOptions<T>(
4646
key: ParsingExtensionKey,
4747
context: ParsingExtensionKey[],
4848
) => ParsingExtensionTransform | false,
49+
{ lazy = false }: { lazy?: boolean } = {},
4950
): ParsingExtension {
5051
const schema = builder(SchemaBuilder);
5152

5253
schema.cacheValidationFunction();
5354

5455
return (value, ctxKey, ctx) => {
5556
return async (parse, ...args) => {
56-
const valid = ((await parse(value)).toJSON() as unknown) as T;
57+
let valid: T;
58+
59+
if (lazy) {
60+
valid = (value as unknown) as T;
61+
} else {
62+
valid = ((await parse(value)).toJSON() as unknown) as T;
63+
}
5764

5865
try {
5966
schema.validate(valid);
@@ -74,6 +81,7 @@ export function validateOptions<T>(
7481
if (call) {
7582
return call(parse, ...args);
7683
}
84+
7785
throw new AppConfigError(
7886
`A parsing extension returned as non-applicable, when using validateOptions. This isn't supported.`,
7987
);

app-config-extensions/src/index.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { withTempFiles } from '@app-config/test-utils';
22
import { LiteralSource, NotFoundError } from '@app-config/core';
33
import { FileSource } from '@app-config/node';
4+
import { forKey } from '@app-config/extension-utils';
45
import {
56
envDirective,
67
extendsDirective,
@@ -508,6 +509,20 @@ describe('$env directive', () => {
508509
expect(parsed.toJSON()).toEqual(null);
509510
});
510511

512+
it('doesnt evaluate non-current environment', async () => {
513+
const failDirective = forKey('$fail', () => () => {
514+
throw new Error();
515+
});
516+
517+
process.env.NODE_ENV = 'test';
518+
const source = new LiteralSource({
519+
$env: { test: null, dev: { $fail: true } },
520+
});
521+
522+
const parsed = await source.read([envDirective(), failDirective]);
523+
expect(parsed.toJSON()).toEqual(null);
524+
});
525+
511526
it('merges selection with sibling keys', async () => {
512527
const source = new LiteralSource({
513528
sibling: true,

app-config-extensions/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export function envDirective(
108108
`An $env directive was used, but none matched the current environment (wanted ${environment}, saw [${found}])`,
109109
);
110110
},
111+
{ lazy: true },
111112
),
112113
);
113114
}

0 commit comments

Comments
 (0)