Skip to content

Commit

Permalink
feat(envconfig): fix optional number
Browse files Browse the repository at this point in the history
  • Loading branch information
fernandolguevara committed Jan 17, 2023
1 parent 0bb6179 commit 5e839b9
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/envconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,11 @@ export const parse = <
env[key] = (val?.split(",") || "").map(Number);
} else {
env[key] = Number(val);
if (
!required && typeof original === "undefined" && isNaN(env[key])
) {
env[key] = undefined;
}
}
break;
case type === "bool":
Expand Down Expand Up @@ -812,7 +817,11 @@ export const parse = <
type === "number" &&
(!split_values && isNaN(val) || split_values && val?.some(isNaN))
) {
throw invalidNumberError(key, original, val);
if (!required && typeof original === "undefined") {
val = env[key] = undefined;
} else {
throw invalidNumberError(key, original, val);
}
}

if (
Expand Down Expand Up @@ -1610,6 +1619,18 @@ describe("complex marks", () => {
assertEquals(parsed.myapp.super.nest.port, 3003);
});

it("should parse undefined on `number", () => {
const env = {};

const config = {
port: "`number",
} as const;

const parsed = parse(env, config);

assertEquals(parsed.port, undefined);
});

it("should parse super nested `number with prefix override", () => {
const env = {
MYAPP_SUB_P1: "1",
Expand Down

0 comments on commit 5e839b9

Please sign in to comment.