Skip to content

Commit 21e2db8

Browse files
committed
feat(#107): uses validateOptions for built in extensions
1 parent 93eb97e commit 21e2db8

File tree

2 files changed

+224
-198
lines changed

2 files changed

+224
-198
lines changed

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

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,22 +52,32 @@ export function validateOptions<T>(
5252
schema.cacheValidationFunction();
5353

5454
return (value, ctxKey, ctx) => {
55-
const valid = (value as unknown) as T;
55+
return async (parse, ...args) => {
56+
const valid = ((await parse(value)).toJSON() as unknown) as T;
5657

57-
try {
58-
schema.validate(valid);
59-
} catch (error) {
60-
const message = error instanceof Error ? error.message : 'unknown';
58+
try {
59+
schema.validate(valid);
60+
} catch (error) {
61+
const message = error instanceof Error ? error.message : 'unknown';
6162

62-
const parents =
63-
[...ctx, ctxKey]
64-
.map(([, k]) => k)
65-
.filter((v) => !!v)
66-
.join('.') || 'root';
63+
const parents =
64+
[...ctx, ctxKey]
65+
.map(([, k]) => k)
66+
.filter((v) => !!v)
67+
.join('.') || 'root';
6768

68-
throw new ParsingExtensionInvalidOptions(`Validation failed in "${parents}": ${message}`);
69-
}
69+
throw new ParsingExtensionInvalidOptions(`Validation failed in "${parents}": ${message}`);
70+
}
71+
72+
const call = extension(valid, ctxKey, ctx);
7073

71-
return extension(valid, ctxKey, ctx);
74+
if (call) {
75+
return call(parse, ...args);
76+
} else {
77+
throw new AppConfigError(
78+
`A parsing extension returned as non-applicable, when using validateOptions. This isn't supported.`,
79+
);
80+
}
81+
};
7282
};
7383
}

0 commit comments

Comments
 (0)