Skip to content

feat(config): type-safe config module with Zod validation#33

Merged
oomokaro1 merged 2 commits into
OrbitStream:mainfrom
OpenSourceCOntr:feat/zod-config-module
Jun 24, 2026
Merged

feat(config): type-safe config module with Zod validation#33
oomokaro1 merged 2 commits into
OrbitStream:mainfrom
OpenSourceCOntr:feat/zod-config-module

Conversation

@Uchechukwu-Ekezie

Copy link
Copy Markdown
Contributor

Closes #11

What changed

Zod schema (src/config/config.schema.ts)

  • Full schema covering every env var with coercion (z.coerce), URL validation, string minimums, enum checks, and defaults
  • CORS_ALLOWED_ORIGINS transforms a comma-separated string into string[]
  • STELLAR_NETWORK_PASSPHRASE is optional (derived from the network enum at runtime)
  • validate() export: crashes with a descriptive, field-level error on invalid input; in production all defaultable keys must be explicitly set (no silent fallbacks); in development missing defaults emit a console.warn

Config module (src/config/config.module.ts)

  • Wraps @nestjs/config's ConfigModule.forRoot with isGlobal: true, cache: true, and the Zod validate hook
  • AppModule imports it first so validation runs at bootstrap — misconfiguration surfaces immediately, not mid-request

All process.env.* reads migrated to ConfigService

  • CheckoutService, StellarService, RedisService, AuthService, PaymentDetectorService, WebhookQueueService, DynamicCorsMiddleware — all now inject ConfigService<Config> with { infer: true } for full TypeScript type inference

config:check script

  • scripts/config-check.ts — calls validate(process.env) and exits 0/1
  • "config:check": "ts-node scripts/config-check.ts" added to package.json — useful for CI/CD and Docker health checks

.env.example

  • Fully rewritten; every variable marked REQUIRED or OPTIONAL with a one-line description and example value

Tests (src/config/config.schema.spec.ts — 14 tests)

  • Valid config passes; defaults applied; PORT coerced from string; CORS split and trimmed
  • Missing DATABASE_URL, short JWT_SECRET, invalid URL, non-G receiving account, unknown STELLAR_NETWORK — all rejected
  • validate() throws a descriptive field-level message on invalid input
  • Production rejects defaultable keys; production passes when all keys explicitly provided

How to test

npm test               # 264 tests, all passing
npm run config:check   # validate env vars without starting the app

Uchechukwu-Ekezie and others added 2 commits June 24, 2026 15:41
Introduces a Zod-validated ConfigModule that replaces scattered
process.env reads across the codebase with a single typed ConfigService.

- src/config/config.schema.ts: full Zod schema with coercions, transforms,
  and environment-specific validation (production rejects defaults)
- src/config/config.module.ts: NestJS ConfigModule wrapper (isGlobal: true)
- scripts/config-check.ts + npm run config:check: validates .env without
  starting the app, useful in CI / Docker health checks
- Injects ConfigService into: CheckoutService, StellarService, RedisService,
  AuthService, PaymentDetectorService, WebhookQueueService,
  DynamicCorsMiddleware
- Updates all specs to provide ConfigService mocks; adds 14 new config tests
- jest-setup.ts: adds test env vars required by Zod schema
- .env.example: rewritten with all vars, required vs optional annotations

Closes OrbitStream#11

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

LGTM — type-safe config module with Zod validation. Merge conflicts resolved, only minor lint issues remaining (extra blank lines in checkout.service.spec.ts). Approving to unblock merge.

@oomokaro1 oomokaro1 merged commit 4fe6833 into OrbitStream:main Jun 24, 2026
1 check failed
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.

[INFRA] Type-Safe Config Module with Zod Validation

2 participants