refactor: using Zod to validate .env schema

This commit is contained in:
2025-11-10 21:33:25 +00:00
parent 2a563bbd7a
commit d4fc41c6bf
2 changed files with 20 additions and 9 deletions

18
packages/backend/src/env/index.ts vendored Normal file
View File

@ -0,0 +1,18 @@
import { z } from 'zod';
import dotenv from 'dotenv';
dotenv.config();
const envSchema = z.object({
PORT: z
.string()
.refine(
(port) => parseInt(port) > 0 && parseInt(port) < 65536,
"Invalid port number"
),
DATABASE_URL: z.string().min(10)
});
type Env = z.infer<typeof envSchema>;
export const ENV: Env = envSchema.parse(process.env);

View File

@ -1,11 +1,4 @@
import 'dotenv/config';
import { drizzle } from 'drizzle-orm/node-postgres';
import { pgTable, text, uuid } from 'drizzle-orm/pg-core';
const DATABASE_URL = process.env.DATABASE_URL;
if (!DATABASE_URL) {
throw new Error("DATABASE_URL is missing")
}
export const db = drizzle(DATABASE_URL);
import { ENV } from '../env';
export const db = drizzle(ENV.DATABASE_URL);