validation
This commit is contained in:
1 parent
a1858e8302
commit
09534757a2
5 files changed
+98
-23
No files matched your search
@@ -0,0 +1,47 @@
|
||||
import { z } from "zod";
|
||||
|
||||
/**
|
||||
* Environment variable schema with validation
|
||||
*/
|
||||
const envSchema = z.object({
|
||||
// Server Configuration
|
||||
NODE_ENV: z.enum(["development", "production", "test"]).default("development"),
|
||||
LOG_LEVEL: z.enum(["error", "warn", "info", "debug"]).default("debug"),
|
||||
|
||||
// Database
|
||||
DB_CONNECTION_STRING: z.string().min(1, "Database connection string is required"),
|
||||
|
||||
// JWT Authentication
|
||||
JWT_SECRET: z.string().min(1, "JWT_SECRET is required"),
|
||||
TOKEN_TTL: z.string().default("99d"),
|
||||
|
||||
// Client Configuration
|
||||
CLIENT_HOST: z.string().url("CLIENT_HOST must be a valid URL"),
|
||||
|
||||
// Optional
|
||||
ORIGIN: z.string().optional(),
|
||||
});
|
||||
|
||||
export type ValidatedEnv = z.infer<typeof envSchema>;
|
||||
|
||||
export const validateEnv = (): ValidatedEnv => {
|
||||
const result = envSchema.safeParse(process.env);
|
||||
|
||||
if (!result.success) {
|
||||
console.error("Environment validation failed:");
|
||||
console.error("");
|
||||
|
||||
const errors = result.error.format();
|
||||
for (const [key, value] of Object.entries(errors)) {
|
||||
if (key !== "_errors" && value && typeof value === "object" && "_errors" in value) {
|
||||
console.error(` ${key}: ${value._errors.join(", ")}`);
|
||||
}
|
||||
}
|
||||
|
||||
console.error("Please check your .env file and ensure all required variables are set.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.log("Environment variables validated successfully");
|
||||
return result.data;
|
||||
};
|
||||
@@ -8,6 +8,9 @@
|
||||
// Shared utilities
|
||||
export * from "./shared.js";
|
||||
|
||||
// Environment validation
|
||||
export * from "./envValidation.js";
|
||||
|
||||
// Domain-specific validations
|
||||
export * from "./authValidation.js";
|
||||
export * from "./monitorValidation.js";
|
||||
|
||||
Reference in new issue
Block a user