Validation
Env Core validates your environment variables at application startup, ensuring that all required variables are present and have the correct types. This prevents runtime errors caused by misconfiguration.How validation works
When you callvalidateEnv, Env Core performs the following steps:
Load environment variables
Env Core loads variables from your .env file (or custom file) and merges them with
process.env.Validate each variable
For each variable in your schema, Env Core:
- Checks if the variable exists
- Validates the type matches the schema
- Applies default values if specified
Validation rules
Required variables
By default, all variables defined in your schema are required. If a required variable is missing, validation fails.PORT or NODE_ENV are not set, you’ll see:
Optional variables
Variables become optional when:- A
defaultvalue is provided required: falseis explicitly set
Type validation
Env Core validates that environment variables match their declared types:String validation
Strings are always valid - the environment variable value is used as-is.Number validation
The value must be convertible to a valid number. Non-numeric values cause validation errors.Env Core uses JavaScript’s
Number() function for conversion, so values like "3000", "3.14", and "1e3" are all valid.Boolean validation
Boolean values must be exactly"true" or "false" (case-sensitive). Other values cause validation errors.
Error handling
When validation fails, Env Core provides detailed error messages to help you quickly identify and fix issues.Error message format
Errors are displayed in a clear, actionable format:- Missing required field - A required variable is not set
- Should be a [type] - A variable has the wrong type
- Has an unsupported type - The schema uses an unsupported type
Process termination
By default, validation failures cause the process to exit with code1. This prevents your application from starting with invalid configuration.
For NestJS integration, errors are thrown instead of exiting the process, allowing NestJS’s error handling to take over.
Error recovery
There is no error recovery - validation failures are fatal. This is by design:- Fail fast - Catch configuration errors immediately at startup
- Prevent runtime errors - Don’t let the application run with invalid config
- Clear feedback - Developers see exactly what needs to be fixed
Validation in different environments
Development
In development, validation ensures your .env file has all required variables:.env
Production
In production, validation works with environment variables set by your hosting platform:Testing
For tests, you can use a custom .env file:Custom validation logic
Env Core focuses on type validation. For complex validation logic (e.g., regex patterns, custom formats), you can add additional checks after validation:Best practices
Validate early
Validate early
Call
validateEnv at the very start of your application, before any other initialization. This ensures configuration errors are caught before anything else runs.Use sensible defaults for non-critical config
Use sensible defaults for non-critical config
Provide default values for non-critical configuration to make local development easier. Reserve required variables for critical settings.
Keep .env.example updated
Keep .env.example updated
Maintain a
.env.example file showing all required variables with example values. This helps other developers set up their environment.Document expected values
Document expected values
Use comments in your schema to document what values are acceptable:
Next steps
Type safety
Learn about TypeScript type inference
Framework guides
See validation in action with Express.js and NestJS