-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.ts
More file actions
31 lines (27 loc) · 696 Bytes
/
index.ts
File metadata and controls
31 lines (27 loc) · 696 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { z } from "zod";
export const formatZodErrors = (
errors: z.ZodFormattedError<Map<string, string>, string>
) =>
Object.entries(errors)
.map(([name, value]) => {
if (value && "_errors" in value)
return `${name}: ${value._errors.join(", ")}\n`;
return;
})
.filter(Boolean);
export const validateEnvironmentVariables = <
T extends ReturnType<typeof z.object>,
K
>(
schema: T,
unparsedEnv: K
) => {
const env = schema.safeParse(unparsedEnv);
if (!env.success) {
console.error(
"❌ Invalid environment variables:\n",
...formatZodErrors(env.error.format())
);
throw new Error("Invalid environment variables");
}
};