feat: validate required environment variables at startup (closes #7) - #31
Merged
Conversation
Add server/utils/env.ts, which validates required env vars (DATABASE_URL, BETTER_AUTH_SECRET, BETTER_AUTH_URL, EMAIL_*) with Zod on import and throws a clear, human-readable error listing what's missing. db.ts and auth.ts consume the validated env, so a missing value fails the app fast at startup instead of surfacing as a confusing crash deep inside the database/auth later. Validation lives in a shared util (not a Nitro plugin) because db.ts reads DATABASE_URL at module load, earlier than any plugin runs. Skipped under Vitest. Closes #7. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem (#7)
If a required env var is missing (e.g. a fresh clone with no
.env), the app crashed with a confusing error deep inside the code — literallyTypeError: Cannot read properties of undefined (reading 'replace')fromserver/utils/db.ts.Fix
New
server/utils/env.tsvalidates the required variables with Zod once, on import, and throws a clear message listing exactly what's missing:db.tsandauth.tsconsume the validatedenv, so validation runs before anything touches the database or auth.Why a util and not a Nitro plugin
The issue suggested a server plugin, but
db.tsreadsDATABASE_URLat module load — earlier than any Nitro plugin runs — so a plugin would never get to report the problem (verified: the plugin approach still crashed with the oldTypeError). Putting validation in a module thatdb.tsimports guarantees it runs first. Validation is skipped under Vitest (which runs without a real env).Validated:
DATABASE_URL,BETTER_AUTH_SECRET,BETTER_AUTH_URL,EMAIL_HOST,EMAIL_USER,EMAIL_PASS,EMAIL_FROM.UPLOAD_STORAGE_PATHis omitted (it has a safe code default).Verification
pnpm buildsucceeds;pnpm testpasses (validation skipped under Vitest).GET /api/health→200.Closes #7.
🤖 Generated with Claude Code