| 11 | }) |
| 12 | |
| 13 | export function validateEnvironmentVariables() { |
| 14 | try { |
| 15 | const env = envSchema.parse(process.env) |
| 16 | logger.info('Environment variables validated successfully') |
| 17 | return env |
| 18 | } catch (error) { |
| 19 | if (error instanceof z.ZodError) { |
| 20 | const missingVars = error.errors.map(err => `${err.path.join('.')}: ${err.message}`).join(', ') |
| 21 | logger.error(`Environment validation failed: ${missingVars}`) |
| 22 | |
| 23 | if (process.env.NODE_ENV === 'production') { |
| 24 | throw new Error(`Required environment variables are missing: ${missingVars}`) |
| 25 | } else { |
| 26 | logger.warn('Some environment variables are missing, but continuing in development mode') |
| 27 | } |
| 28 | } else { |
| 29 | logger.error('Unknown error during environment validation', error) |
| 30 | throw error |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | export type ValidatedEnv = z.infer<typeof envSchema> |