Schema definition
The environment schema is the core of Env Core. It defines which environment variables your application needs, their types, and whether they’re required or optional.Schema basics
An environment schema is a plain JavaScript object where:- Keys represent environment variable names
- Values define the type and validation rules
Three ways to define schemas
Env Core supports three different approaches to defining your schema, giving you flexibility based on your needs.Use case 1: Simple types
The simplest approach uses constructor functions directly. All variables defined this way are required.Use simple types when all your environment variables are required and have no default values.
Use case 2: Configuration options
For more control, use configuration objects withtype, default, and required properties.
Use case 3: Mixed configuration
Combine both approaches for maximum flexibility - use simple types for required variables and configuration objects for optional ones.This is the recommended approach for most applications - it’s clear which variables are required and which have sensible defaults.
Schema format reference
Here’s a complete reference of how to define environment variables in your schema: | Format | Description | Example | |--------|-------------|---------|| |KEY: Type | Simple type definition (required by default) | PORT: Number |
| KEY: { type: Type, default: value } | Type with default value (optional) | DEBUG: { type: Boolean, default: false } |
| KEY: { type: Type, required: boolean } | Explicitly set if required | NODE_ENV: { type: String, required: false } |
Supported types
Env Core supports three primitive types:String- For text values (e.g.,NODE_ENV,DATABASE_URL)Number- For numeric values (e.g.,PORT,MAX_CONNECTIONS)Boolean- For true/false values (e.g.,DEBUG,ENABLE_LOGGING)
Configuration object properties
When using the configuration object format, you can specify:type (required)
The expected type of the environment variable. Must be String, Number, or Boolean.
default (optional)
A default value to use if the environment variable is not set. When a default is provided, the variable becomes optional automatically.
required (optional)
Explicitly control whether a variable is required. Defaults to true for simple types, but defaults to false when a default value is provided.
Best practices
Organize schemas in a separate file
Organize schemas in a separate file
Keep your environment schema in a dedicated file (e.g.,
envSchema.js or env.config.js) to make it easy to maintain and reuse across your application.Use descriptive variable names
Use descriptive variable names
Choose clear, uppercase names for environment variables following the CONSTANT_CASE convention (e.g.,
DATABASE_URL, MAX_RETRY_ATTEMPTS).Provide sensible defaults
Provide sensible defaults
For non-critical configuration, provide sensible defaults to make local development easier. Reserve required variables for critical configuration like database URLs or API keys.
Document your schema
Document your schema
Add comments to your schema file explaining what each variable is used for and what values are acceptable.
Next steps
Validation
Learn how Env Core validates your environment variables
Type safety
Understand TypeScript type inference