Enforce strict validation of environment variables using Zod schemas with explicit environment-specific rules. Required configurations should be mandatory in production while allowing flexibility in development/test environments.
Enforce strict validation of environment variables using Zod schemas with explicit environment-specific rules. Required configurations should be mandatory in production while allowing flexibility in development/test environments.
Key principles:
Example:
export const env = createEnv({
server: {
STRIPE_SECRET_KEY: z.string().refine(
(val) => process.env.NODE_ENV === 'test' || !!val,
'Missing STRIPE_SECRET_KEY in production',
),
REDIS_URL: z.string().refine(
(val) => process.env.NODE_ENV === 'test' || !!val,
'Missing REDIS_URL in production',
),
// Optional in all environments
LOG_LEVEL: z.string().optional(),
},
client: {
// Required in all environments
NEXT_PUBLIC_API_URL: z.string().min(1),
},
// Validate on app startup
runtimeEnv: process.env,
});
This approach prevents silent misconfigurations in production while maintaining development flexibility and providing clear error messages when required variables are missing.
Enter the URL of a public GitHub repository