fix: validate required environment variables before startup - #25
Conversation
|
@alok-kumar0421 is attempting to deploy a commit to the Yash Pouranik's projects Team on Vercel. A member of the Team first needs to authorize it. |
📝 WalkthroughWalkthroughA new environment variable validation utility was added to enforce required configuration at server startup. The validation checks for MONGO_URL, PORT, and REDIS_URL, logging descriptive errors and exiting the process if any are missing. Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~5 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Fix all issues with AI agents
In `@backend/utils/validateEnv.js`:
- Around line 1-11: The requiredEnvVars array in validateEnv (requiredEnvVars /
validateEnv) declares "PORT" mandatory while app.js still uses a fallback const
PORT = process.env.PORT || 1234, causing a contradiction; choose one approach
and make them consistent: either remove "PORT" from requiredEnvVars so
validateEnv won't exit when PORT is missing and keep the fallback in app.js, or
remove the || 1234 fallback in app.js and keep "PORT" in requiredEnvVars so the
app fails fast; update whichever file you change (requiredEnvVars list in
validateEnv or the PORT initialization in app.js) accordingly.
- Around line 3-11: The validateEnv function currently checks requiredEnvVars
unconditionally and exits the process, breaking tests; update validateEnv (or
its caller) to skip or relax validation when NODE_ENV === 'test' (or accept a
context flag) so tests that intentionally omit MONGO_URL/REDIS_URL won't call
process.exit(1). Locate validateEnv and the requiredEnvVars list and either add
a guard at the top of validateEnv (return early if process.env.NODE_ENV ===
'test') or allow validateEnv to accept an options param (e.g., {
allowMissingInTest: true }) and use that in app.js where NODE_ENV !== 'test' is
already checked; ensure any change references validateEnv and requiredEnvVars so
the test environment isn't forced to provide DB URLs.
🧹 Nitpick comments (1)
backend/utils/validateEnv.js (1)
4-4: Empty-string values pass silently.
!process.env[key]will catchundefined, but an env var set to an empty string ("") is also falsy and will be caught — which is good. However, a whitespace-only value like" "will slip through. If you want stricter validation, use.trim():const missing = requiredEnvVars.filter((key) => !process.env[key]?.trim());
|
Thank you for the PR @alok-kumar0421 that solves #18 |
|
Thank you! |
Added a centralized validation step for required environment variables.
The app now checks for MONGO_URL, PORT, and REDIS_URL during initialization.
If any variable is missing, it logs a clear error and exits early, preventing unexpected runtime crashes in dependent modules.
Summary by CodeRabbit