Skip to main content

Overview

The env_vars section checks for the presence of environment variables and optionally validates their values against regex patterns. This ensures critical configuration values are set before running your application.

Configuration

Each environment variable check is defined by an EnvVarCheck struct (src/config.rs:31) with the following fields:
name
string
required
The name of the environment variable to check.
required
boolean
default:"true"
Whether the environment variable must be set. If true, validation fails when the variable is not defined.
pattern
string
A regex pattern to validate the variable’s value. If specified, the value must match this pattern.

Examples

Basic Presence Check

Check that an environment variable is set:
env_vars:
  - name: DATABASE_URL
    required: true

Optional Variables

Mark variables as optional when they have sensible defaults:
env_vars:
  - name: NODE_ENV
    required: false
  - name: LOG_LEVEL
    required: false

Pattern Validation

Validate that environment variable values match expected formats:
env_vars:
  - name: RAILS_ENV
    required: false
    pattern: "^(development|test|production)$"
  
  - name: DEBUG
    required: false
    pattern: "^(True|False|1|0)$"
  
  - name: PORT
    required: true
    pattern: "^[0-9]{1,5}$"

Real-World Examples

version: "1"

env_vars:
  - name: NODE_ENV
    required: false
  - name: DATABASE_URL
    required: true

Pattern Syntax

The pattern field uses standard regex syntax. Here are common patterns:

Exact Values

# Must be one of: development, test, production
pattern: "^(development|test|production)$"

Boolean Values

# Common boolean representations
pattern: "^(true|false|1|0|yes|no)$"

# Python-style booleans
pattern: "^(True|False|1|0)$"

Numeric Ranges

# Port numbers (1-65535)
pattern: "^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$"

# Simple 1-5 digit number
pattern: "^[0-9]{1,5}$"

URL Validation

# Simple URL pattern
pattern: "^https?://.*"

# Database URL
pattern: "^postgres(ql)?://.*"

Custom Formats

# Alphanumeric only
pattern: "^[a-zA-Z0-9]+$"

# Semantic version
pattern: "^[0-9]+\\.[0-9]+\\.[0-9]+$"
Remember to escape backslashes in YAML strings. Use \\ for a single backslash in regex patterns.

Default Behavior

From the struct definition in src/config.rs:31:
  • name: Required field, no default
  • required: Defaults to true
  • pattern: Defaults to None (no pattern validation)
When required: true, validation fails if:
  • The environment variable is not set
  • The variable is set but its value doesn’t match the pattern (if specified)
Use required: false with a pattern to validate optional variables only when they’re set. This allows developers to omit the variable entirely while ensuring correct values when provided.