# Environment Variable Validation with Zod

Stop your app from crashing 10 minutes into production because `DATABASE_URL` is undefined.

```typescript
import { z } from 'zod';

const envSchema = z.object({
  DATABASE_URL: z.string().url(),
  BETTER_AUTH_SECRET: z.string().min(32),
  BETTER_AUTH_URL: z.string().url(),
  GOOGLE_CLIENT_ID: z.string().optional(),
  GOOGLE_CLIENT_SECRET: z.string().optional(),
  NODE_ENV: z.enum(['development', 'production', 'test']).default('development'),
  PORT: z.coerce.number().default(3000),
  LOG_LEVEL: z.enum(['debug', 'info', 'warn', 'error']).default('info'),
});

export const env = envSchema.parse(process.env);
```

## Why This Matters

Without validation, `process.env.PORT` is `string | undefined`. With Zod:
- **Fail at startup**, not at the first request that uses the variable
- **Type coercion** -- PORT becomes a number automatically
- **Default values** -- no more `process.env.PORT || 3000` scattered everywhere
- **Documentation** -- the schema IS the documentation

Put this in `src/lib/env.ts` and import `env` instead of using `process.env` directly.