Skip to content

Commit

Permalink
feat: 🧑‍💻 Allow single-argument call of `initVariable(envVariable: st…
Browse files Browse the repository at this point in the history
…ring)`, defaulting to the `OPTIONAL` built-in validator
  • Loading branch information
pklaschka committed Oct 29, 2024
1 parent c64120f commit 6fe664d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export class EnvNotSetError extends Error {
* 5. Validate the environment variable's value using `validator`.
*
* @param envVariable name of the environment variable
* @param validator Zod schema used to validate the environment variable's value
* @param validator Zod schema used to validate the environment variable's value. Defaults to {@link OPTIONAL}.
* @param defaultValue default value for the environment variable
*
* @throws {ConfigParseError} If the final environment variable's value cannot be parsed using `validator`,
Expand All @@ -66,7 +66,7 @@ export class EnvNotSetError extends Error {
*/
export async function initVariable(
envVariable: string,
validator: ZodSchemaCompat,
validator: ZodSchemaCompat = OPTIONAL,
defaultValue?: string,
): Promise<void> {
logger().debug(`(${envVariable}) Setting up environment variable.`, {
Expand Down
19 changes: 18 additions & 1 deletion mod_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,23 @@ Deno.test("initVariable", async (t) => {
"Expected the value to be undefined",
);
});

await t.step("Single Argument defaults to OPTIONAL", async (t) => {
prepare("XXX");
await initVariable(ENV_VAR);
await assertEquals(
Deno.env.get(ENV_VAR),
"XXX",
"Expected the value to be undefined",
);
prepare();
await initVariable(ENV_VAR);
await assertEquals(
Deno.env.get(ENV_VAR),
undefined,
"Expected the value to be undefined",
);
});
});

Deno.test("Built-in ZodSchemaCompat Validators", async (t) => {
Expand Down Expand Up @@ -332,7 +349,7 @@ Deno.test("Built-in ZodSchemaCompat Validators", async (t) => {
[
"",
],
)
);

function testZodSchemaCompatValidator(
ctx: Deno.TestContext,
Expand Down

0 comments on commit 6fe664d

Please sign in to comment.