Skip to content

Add schema-validated environment config for the backend - #62

Merged
Muhammadcodes112 merged 1 commit into
MiHashport:mainfrom
chillwur:issue-2-env-schema-validation
Jul 10, 2026
Merged

Muhammadcodes112 merged 1 commit into
MiHashport:mainfrom
chillwur:issue-2-env-schema-validation

Conversation

@chillwur

Copy link
Copy Markdown
Contributor

Problem

apps/backend/src/config/env.ts read process.env with hand-rolled required()/optional() helpers. Missing or malformed variables (e.g. a non-numeric PORT, or an unrecognized STELLAR_NETWORK) weren't caught in a structured way, so bad configuration could surface later as a confusing runtime crash instead of a clear failure at boot.

Solution

  • Replaced the ad-hoc env reads with a zod schema (envSchema) covering every variable documented in apps/backend/.env.example: NODE_ENV, HOST, PORT, DATABASE_URL, REDIS_URL, the WHATSAPP_* vars, the STELLAR_* vars, HASHPORT_CONTRACT_ID, SIGNER_SECRET_KEY, and WALLET_ENCRYPTION_KEY.
  • The schema distinguishes required-in-production from optional-in-dev vars via a superRefine check keyed on NODE_ENV: storage/WhatsApp/Stellar/secret vars stay optional for local development (preserving the existing behavior where the backend boots without WhatsApp/Stellar credentials), but become required once NODE_ENV=production.
  • On validation failure, the backend now logs a readable, one-per-line list of exactly which keys are missing or invalid and exits non-zero (loadEnv() in env.ts), instead of failing later inside request handling.
  • parseEnv() and formatEnvIssues() are exported as pure functions (no process side effects) so they're directly unit-testable.
  • Added apps/backend/test/env.test.ts covering: default values with no vars set, a malformed PORT, an invalid STELLAR_NETWORK, an invalid NODE_ENV, missing production-required vars, a fully-populated production config, and the readable-error-list formatting.
  • The public shape of env and secrets (including the lazy secret getters) is unchanged, so no call sites elsewhere in the backend needed to change.

closes #2

@Muhammadcodes112
Muhammadcodes112 merged commit 220b117 into MiHashport:main Jul 10, 2026
1 of 2 checks passed
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.

Add schema-validated environment config for the backend

2 participants