Skip to content

Add environment variable validation on app startup #24

@pragmaticAweds

Description

@pragmaticAweds

Context

The app relies on several environment variables for contract IDs, network configuration, and RPC endpoints (lib/constants.ts). When these are missing or malformed, the app fails silently at runtime — contract calls return cryptic errors, balances don't load, and transactions fail. There is no startup validation that checks whether required env vars are present and valid before the app renders.

What Success Looks Like

  • A validation step runs at app initialization that checks all required env vars
  • Missing or malformed env vars produce a clear developer-facing error message listing exactly what's wrong
  • The app shows a meaningful error page (not a white screen) if critical config is missing
  • The .env.example file is updated with all required variables and their expected formats
  • Optional env vars have documented defaults

Implementation Guidance

  • apps/web/src/lib/constants.ts — currently uses process.env.NEXT_PUBLIC_* with fallback values (lines 1-2), some fallbacks are placeholder contract IDs like "CB..."
  • Create a validation module (e.g., apps/web/src/lib/env.ts) using Zod to define and validate the env schema
  • Call the validation in the root layout (apps/web/src/app/layout.tsx) or in a provider
  • Use z.string().startsWith('C') for contract ID validation, z.enum(['testnet', 'public']) for network
  • Update .env.example at the project root with all required variables, descriptions, and example values
  • Consider using the t3-env pattern (server vs client env separation) or a simpler Zod-based approach

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions