Skip to content

Instantly share code, notes, and snippets.

@w3cj
Created March 18, 2025 20:56
Show Gist options
  • Save w3cj/0cdce90054cbba33294f61471f9f2f54 to your computer and use it in GitHub Desktop.
Save w3cj/0cdce90054cbba33294f61471f9f2f54 to your computer and use it in GitHub Desktop.
/* 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