Validation

Validate your environment variables against a schema to prevent misconfigurations and ensure reliability.

Validation with Env-Sentinel

Env-Sentinel allows you to validate your .env files against a schema to ensure type safety, presence of required variables, and correct formatting. Using schema-based validation reduces runtime errors and enforces consistency across environments.


Core Validators

ValidatorDescriptionExample
stringEnsures the value is a stringAPP_NAME=required|string
numberEnsures the value is numericAPP_PORT=required|number|min:1|max:65535
minMinimum value or lengthAPI_KEY=required|min:32
maxMaximum value or lengthMAX_CONNECTIONS=optional|number|max:100
booleanMust be true or falseDEBUG=optional|boolean
secureSensitive value that should not be exposedDB_PASSWORD=required|secure
enumMust match one of the allowed valuesAPP_ENV=required|enum:development,staging,production

Example Schema

# Application Configuration
APP_NAME=required|desc:"Application name displayed in logs and UI"|example:"MyAwesomeApp"
APP_ENV=required|enum:development,staging,production|desc:"Application environment mode"|example:"production"
APP_DEBUG=boolean|desc:"Enable debug mode with verbose logging"|default:"false"
APP_URL=required|desc:"Base URL of the application"|example:"https://myapp.com"
APP_PORT=number|min:1|max:65535|desc:"Port number for the application server"|default:"3000"

# Database Configuration
DB_CONNECTION=required|enum:mysql,postgresql,sqlite|desc:"Database driver to use"|example:"mysql"
DB_HOST=required|desc:"Database server hostname or IP address"|example:"localhost"
DB_PORT=required|number|min:1|max:65535|desc:"Database server port number"|example:"3306"
DB_PASSWORD=required|secure|desc:"Database password (keep secure!)"

How It Works

  1. Generate a Schema (optional) – If you don't have a schema yet, run:
    npx env-sentinel init
    
  2. Define a Schema – Specify the expected environment variables and rules.
  3. Run Validation – Compare your .env against the schema:
    npx env-sentinel validate
    
  4. Review Results – Env-Sentinel reports errors, warnings, and notices.

See also

  • Explore the Linting rules and best practices.
  • Learn how to Document your configuration for better team workflows.