34 lines
1009 B
TypeScript
34 lines
1009 B
TypeScript
import { z } from "zod";
|
|
|
|
// Server-side environment schema (t3.gg style)
|
|
const serverSchema = z.object({
|
|
// Full MySQL connection URL, e.g. mysql://user:pass@host:3306/zxdb
|
|
ZXDB_URL: z
|
|
.string()
|
|
.url()
|
|
.refine((s) => s.startsWith("mysql://"), {
|
|
message: "ZXDB_URL must be a valid mysql:// URL",
|
|
}),
|
|
});
|
|
|
|
function formatErrors(errors: z.ZodFormattedError<Map<string, string>, string>) {
|
|
return Object.entries(errors)
|
|
.map(([name, value]) => {
|
|
if (value && "_errors" in value) {
|
|
const errs = (value as z.ZodFormattedError<string>)._errors;
|
|
return `${name}: ${errs.join(", ")}`;
|
|
}
|
|
return `${name}: invalid`;
|
|
})
|
|
.join("\n");
|
|
}
|
|
|
|
const parsed = serverSchema.safeParse(process.env);
|
|
if (!parsed.success) {
|
|
// Fail fast with helpful output in server context
|
|
console.error("❌ Invalid environment variables:\n" + formatErrors(parsed.error.format()));
|
|
throw new Error("Invalid environment variables");
|
|
}
|
|
|
|
export const env = parsed.data;
|