Created
March 18, 2025 20:56
-
-
Save w3cj/0cdce90054cbba33294f61471f9f2f54 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* eslint-disable node/no-process-env */ | |
import type { ZodObject, ZodRawShape } from "zod"; | |
import { ZodError } from "zod"; | |
export default function tryParseEnv<T extends ZodRawShape>( | |
EnvSchema: ZodObject<T>, | |
buildEnv: Record<string, string | undefined> = process.env, | |
) { | |
try { | |
EnvSchema.parse(buildEnv); | |
} | |
catch (error) { | |
if (error instanceof ZodError) { | |
let message = "Missing required values in .env:\n"; | |
error.issues.forEach((issue) => { | |
message += `${issue.path[0]}\n`; | |
}); | |
const e = new Error(message); | |
e.stack = ""; | |
throw e; | |
} | |
else { | |
console.error(error); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment