Environment Validation
Environment Validation
Relevant source files
The following files were used as context for generating this wiki page:
The env module provides a centralized mechanism for validating process environment variables. Its primary purpose is to replace scattered, manual checks across microservices with a structured schema definition and uniform error reporting. Unlike the Config module, which interacts with external data sources, this module is pure and only operates on process.env src/env/env-validator.ts:15-21.
Core Components
The validation logic is built around three primary structures:
EnvSchema: A dictionary defining the requirements for each environment variable, including its necessity, fallback value, and a human-readable description src/env/env-validator.ts:1-7.validateEnv: A non-throwing function that evaluates the currentprocess.envagainst a schema and returns a detailed result src/env/env-validator.ts:22-35.requireEnv: A wrapper that invokesvalidateEnvbut throws anErrorcontaining all validation failures if the environment is invalid src/env/env-validator.ts:40-46.
Data Structures
| Interface | Property | Type | Description |
|---|---|---|---|
EnvSchema | required | boolean | If true, validation fails if the variable is undefined or "". |
default | string | (Optional) Value to use if the variable is missing from process.env. | |
description | string | (Optional) Human-readable text appended to error messages. | |
EnvValidationResult | valid | boolean | true if all requirements are met. |
errors | string[] | List of error messages for missing required variables. | |
env | Record<string, string | undefined> | The resolved environment (including defaults). |
Sources: src/env/env-validator.ts:1-13
Validation Flow
The validation logic iterates through the provided EnvSchema, resolves values from process.env, and applies defaults before checking the required constraint.
Validation Sequence
This diagram illustrates how validateEnv processes a schema against process.env.
Code
Sources: src/env/env-validator.ts:22-35
Usage Patterns
Soft Validation (validateEnv)
Use validateEnv when the application needs to handle missing variables gracefully or report errors through a specific logging channel without crashing.
Code
Sources: src/env/env-validator.ts:22-35, src/env/tests/env-validator.test.ts:15-21
Strict Validation (requireEnv)
Use requireEnv at the entry point of a microservice to ensure the process fails fast if the environment is misconfigured. It aggregates all errors into a single exception src/env/env-validator.ts:40-46.
Code
Sources: src/env/env-validator.ts:40-46, src/env/tests/env-validator.test.ts:75-83
Behavior Rules
- Empty Strings: If a variable is set to an empty string (
"") and is marked asrequired: true, validation will fail src/env/env-validator.ts:29-31, src/env/tests/env-validator.test.ts:31-35. - Default Priority: The value in
process.envalways takes precedence over thedefaultfield in the schema src/env/env-validator.ts:27-28, src/env/tests/env-validator.test.ts:44-48. - Error Formatting: If a
descriptionis provided, it is appended in parentheses to the error message (e.g.,VARIABLE is required (needed for X)) src/env/env-validator.ts:30, src/env/tests/env-validator.test.ts:23-29.
Sources: src/env/env-validator.ts:22-46, src/env/tests/env-validator.test.ts:1-83