Skip to content

Commit 6fe664d

Browse files
committed
feat: 🧑‍💻 Allow single-argument call of initVariable(envVariable: string), defaulting to the OPTIONAL built-in validator
1 parent c64120f commit 6fe664d

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

mod.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class EnvNotSetError extends Error {
5656
* 5. Validate the environment variable's value using `validator`.
5757
*
5858
* @param envVariable name of the environment variable
59-
* @param validator Zod schema used to validate the environment variable's value
59+
* @param validator Zod schema used to validate the environment variable's value. Defaults to {@link OPTIONAL}.
6060
* @param defaultValue default value for the environment variable
6161
*
6262
* @throws {ConfigParseError} If the final environment variable's value cannot be parsed using `validator`,
@@ -66,7 +66,7 @@ export class EnvNotSetError extends Error {
6666
*/
6767
export async function initVariable(
6868
envVariable: string,
69-
validator: ZodSchemaCompat,
69+
validator: ZodSchemaCompat = OPTIONAL,
7070
defaultValue?: string,
7171
): Promise<void> {
7272
logger().debug(`(${envVariable}) Setting up environment variable.`, {

mod_test.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,23 @@ Deno.test("initVariable", async (t) => {
296296
"Expected the value to be undefined",
297297
);
298298
});
299+
300+
await t.step("Single Argument defaults to OPTIONAL", async (t) => {
301+
prepare("XXX");
302+
await initVariable(ENV_VAR);
303+
await assertEquals(
304+
Deno.env.get(ENV_VAR),
305+
"XXX",
306+
"Expected the value to be undefined",
307+
);
308+
prepare();
309+
await initVariable(ENV_VAR);
310+
await assertEquals(
311+
Deno.env.get(ENV_VAR),
312+
undefined,
313+
"Expected the value to be undefined",
314+
);
315+
});
299316
});
300317

301318
Deno.test("Built-in ZodSchemaCompat Validators", async (t) => {
@@ -332,7 +349,7 @@ Deno.test("Built-in ZodSchemaCompat Validators", async (t) => {
332349
[
333350
"",
334351
],
335-
)
352+
);
336353

337354
function testZodSchemaCompatValidator(
338355
ctx: Deno.TestContext,

0 commit comments

Comments
 (0)