Skip to content

fix: validate required environment variables before startup - #25

Merged
yash-pouranik merged 1 commit into
geturbackend:mainfrom
alok-kumar0421:env-validation
Feb 10, 2026
Merged

fix: validate required environment variables before startup#25
yash-pouranik merged 1 commit into
geturbackend:mainfrom
alok-kumar0421:env-validation

Conversation

@alok-kumar0421

@alok-kumar0421 alok-kumar0421 commented Feb 10, 2026

Copy link
Copy Markdown
Contributor

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

  • Chore
    • Application now validates required environment variables at startup and exits with an error if any are missing.

@vercel

vercel Bot commented Feb 10, 2026

Copy link
Copy Markdown
Contributor

@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.

@coderabbitai

coderabbitai Bot commented Feb 10, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

A 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

Cohort / File(s) Summary
Environment Validation
backend/utils/validateEnv.js
New utility function that defines required environment variables (MONGO_URL, PORT, REDIS_URL), identifies missing variables, logs detailed errors for each missing variable, and exits the process with code 1 if validation fails.
Server Initialization
backend/app.js
Integrated environment validation by importing and immediately invoking validateEnv after loading dotenv, ensuring environment checks occur before further server initialization.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Poem

🐰 A check at the start, before code takes flight,
Required variables gathered, all shining bright,
MONGO, PORT, REDIS—the trinity complete,
Or exit with haste when needs go unmet! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: adding environment variable validation before startup. It is concise, specific, and directly relates to the core functionality introduced in the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 catch undefined, 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());

Comment thread backend/utils/validateEnv.js
Comment thread backend/utils/validateEnv.js
@yash-pouranik

Copy link
Copy Markdown
Member

Thank you for the PR @alok-kumar0421 that solves #18

@yash-pouranik
yash-pouranik merged commit 2c3d3fc into geturbackend:main Feb 10, 2026
4 of 6 checks passed
@alok-kumar0421

Copy link
Copy Markdown
Contributor Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants