ZORL

zorath-env Documentation

Package: zorath-env | Binary: zenv

Everything you need to get started with zorath-env.

1. Installation

Install zorath-env using Cargo (Rust package manager). The package installs as the zenv binary:

From crates.io

cargo install zorath-env

From source

git clone https://github.com/zorl-engine/zorath-env.git cd zorath-env cargo install --path .

After installation, verify it works by running:

zenv version

2. Quick Start

Option A: Start from scratch

Create an env.schema.json file in your project root:

{
  "DATABASE_URL": {
    "type": "url",
    "required": true,
    "description": "Database connection string"
  },
  "PORT": {
    "type": "int",
    "required": false,
    "default": 3000,
    "description": "Server port number"
  }
}

Option B: Generate from existing .env.example

If you already have a .env.example file, generate a schema from it:

zenv init

This creates an env.schema.json with intelligent type inference (detects bool, int, float, url). Edit the file to add descriptions and refine types.

Validate your .env

Once you have both .env and env.schema.json, run:

$ zenv check

zenv: OK

3. Schema Reference

Schema Structure

The schema file is a JSON object where each key is a variable name:

{
  "VARIABLE_NAME": {
    "type": "string",
    "required": true,
    "default": "value",
    "description": "What this variable does",
    "values": ["option1", "option2"],
    "validate": { "min_length": 1 }
  }
}

Variable Properties

PropertyTypeRequiredDescription
typestringYesThe data type of the variable
requiredbooleanNoWhether the variable must be present (default: false)
defaultstringNoDefault value if not provided
descriptionstringNoHuman-readable description
valuesstring[]For enumList of allowed values (required for enum type)
validateobjectNoValidation rules (min/max, min_length/max_length, pattern)

Supported Types

string

Any string value. This is the default type.

"API_KEY": { "type": "string", "required": true }
int

Integer numbers only. Validates that the value can be parsed as an integer.

"PORT": { "type": "int", "default": "3000" }
float

Floating-point numbers. Validates that the value can be parsed as a float.

"RATE_LIMIT": { "type": "float", "default": "1.5" }
bool

Boolean values. Accepts true, false, 1, 0, yes, no (case-insensitive).

"DEBUG": { "type": "bool", "default": false }
url

Valid URL format. Validates URL structure.

"DATABASE_URL": { "type": "url", "required": true }
enum

One of the specified values. Requires the values property.

"LOG_LEVEL": { "type": "enum", "values": ["debug", "info", "warn", "error"] }

4. CLI Commands

zenv check

Validates your .env file against the schema. Returns exit code 0 on success, 1 on failure.

Options:

  • --env <path> - Path to .env file (default: .env)
  • --schema <path> - Path to schema file (default: env.schema.json)
  • --allow-missing-env - Allow missing .env if schema validates (default: true)

Example:

$ zenv check
zenv: OK

$ zenv check --env .env.production --schema prod.schema.json

zenv init

Generates a schema file from an existing .env.example file with type inference.

Options:

  • --example <path> - Path to .env.example (default: .env.example)
  • --schema <path> - Output path (default: env.schema.json)

Example:

$ zenv init
Created env.schema.json from .env.example

zenv docs

Generates documentation from your schema. Outputs to stdout.

Options:

  • --schema <path> - Path to schema file (default: env.schema.json)
  • --format <format> - Output format: markdown or json (default: markdown)

Example:

$ zenv docs
# prints markdown to stdout

$ zenv docs --format json > schema.json

zenv version

Displays the current version of zenv.

Options:

  • --check-update - Check crates.io for newer version

Example:

$ zenv version
zenv v0.2.2

$ zenv version --check-update

zenv --help

Shows help information and available commands.

Example:

$ zenv --help

5. Examples

Web Application

{
  "NODE_ENV": {
    "type": "enum",
    "values": ["development", "production", "test"],
    "required": true,
    "description": "Application environment"
  },
  "PORT": {
    "type": "int",
    "required": false,
    "default": 3000,
    "description": "HTTP server port",
    "validate": { "min": 1024, "max": 65535 }
  },
  "DATABASE_URL": {
    "type": "url",
    "required": true,
    "description": "PostgreSQL connection string"
  },
  "JWT_SECRET": {
    "type": "string",
    "required": true,
    "description": "Secret key for JWT signing",
    "validate": { "min_length": 32 }
  },
  "DEBUG": {
    "type": "bool",
    "required": false,
    "default": false,
    "description": "Enable debug logging"
  }
}

API Service

{
  "API_HOST": {
    "type": "string",
    "required": false,
    "default": "0.0.0.0",
    "description": "Host to bind the API server"
  },
  "API_PORT": {
    "type": "int",
    "required": false,
    "default": 8080,
    "description": "Port for the API server",
    "validate": { "min": 1, "max": 65535 }
  },
  "LOG_LEVEL": {
    "type": "enum",
    "values": ["debug", "info", "warn", "error"],
    "required": false,
    "default": "info",
    "description": "Logging verbosity level"
  },
  "RATE_LIMIT": {
    "type": "float",
    "required": false,
    "default": 100.0,
    "description": "Requests per second limit",
    "validate": { "min_value": 0.0, "max_value": 10000.0 }
  },
  "CORS_ORIGIN": {
    "type": "url",
    "required": false,
    "description": "Allowed CORS origin"
  }
}

6. .env File Features

zenv supports advanced .env file features beyond simple key=value pairs.

Variable Interpolation

Reference other variables using ${VAR} or $VAR syntax:

BASE_URL=https://api.example.com
API_ENDPOINT=${BASE_URL}/v2
PORT=3000
SERVER_URL=http://localhost:${PORT}

Circular references are detected and will cause a validation error.

Multiline Values

Double-quoted strings can span multiple lines:

SSH_KEY="-----BEGIN RSA PRIVATE KEY-----
MIIEowIBAAKCAQEA...
-----END RSA PRIVATE KEY-----"

Escape Sequences

Double-quoted strings support escape sequences:

SequenceResult
\nNewline
\tTab
\rCarriage return
\\Backslash
\"Double quote

Comments and Export Syntax

Full-line comments, inline comments, and shell export syntax are supported:

# This is a full-line comment
DATABASE_URL=postgres://localhost/db  # inline comment

# Shell export syntax works too
export NODE_ENV=production
export DEBUG=false

.env Fallback Chain

If .env is not found, zenv automatically checks these fallbacks in order:

  • .env.local
  • .env.development
  • .env.development.local

7. Schema Inheritance

Extend base schemas for environment-specific configurations using the extends field.

Base Schema (base.schema.json)

{
  "DATABASE_URL": {
    "type": "url",
    "required": true,
    "description": "Database connection string"
  },
  "LOG_LEVEL": {
    "type": "enum",
    "values": ["debug", "info", "warn", "error"],
    "default": "info"
  }
}

Production Schema (prod.schema.json)

{
  "extends": "base.schema.json",
  "SENTRY_DSN": {
    "type": "url",
    "required": true,
    "description": "Sentry error tracking DSN"
  },
  "LOG_LEVEL": {
    "type": "enum",
    "values": ["info", "warn", "error"],
    "default": "warn"
  }
}

The production schema inherits all variables from base.schema.json, adds SENTRY_DSN, and overrides LOG_LEVEL to exclude debug level.

Inheritance supports up to 10 levels of depth. Circular references are detected and will cause an error.

8. CI/CD Integration

zenv is designed for CI/CD pipelines with clear exit codes and easy integration.

Exit Codes

CodeMeaning
0Validation passed
1Validation failed

Pre-commit Hook

Add to .git/hooks/pre-commit or use with pre-commit framework:

#!/usr/bin/env bash
set -e

if [ -f "env.schema.json" ]; then
  if command -v zenv >/dev/null 2>&1; then
    zenv check || exit 1
  else
    echo "Warning: zenv not installed, skipping env validation"
  fi
fi

GitHub Actions

- name: Install zenv
  run: cargo install zorath-env

- name: Validate environment
  run: zenv check --schema env.schema.json

Validation Failure Output

When validation fails, zenv provides clear error messages:

$ zenv check
zenv check failed:

- DATABASE_URL: expected url, got 'not-a-url'
- NODE_ENV: expected one of ["development", "staging", "production"], got 'dev'
- API_KEY: missing (required)
- UNKNOWN_VAR: not in schema (unknown key)

Need Help?

Found a bug, have a feature request, or just want to discuss zorath-env?