Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 

README.md

Validator Example

This example demonstrates all validation tests that zenv can perform.

Structure

validator/
├── .env.example           # Documented environment variables
├── test-env.config.json   # Validator configuration
├── src/
│   ├── env.ts             # Zod schema (correct pattern)
│   └── config.ts          # Contains intentional violations
└── README.md

Run the Validator

# From the repository root
./target/release/zenv validate examples/validator

# With custom config
./target/release/zenv validate -c examples/validator/test-env.config.json examples/validator

# Or from this directory
cd examples/validator
../../target/release/zenv

Expected Output

The validator will find several issues:

Errors (process-env test)

src/config.ts contains intentional process.env usage outside of env.ts:

  • Line with process.env.DEBUG
  • Line with process.env.NODE_ENV
  • Line with process.env.LEGACY_API_KEY

Warnings

  1. Orphaned variable: LEGACY_API_KEY is in .env.example but not in the Zod schema
  2. Undocumented variable: INTERNAL_API_KEY is in the Zod schema but not in .env.example

Fixing the Issues

Fix process.env usage

Change src/config.ts to use the validated env:

// Before (bad)
const debugMode = process.env.DEBUG === 'true';

// After (good)
import { env } from './env';
const debugMode = env.DEBUG;

Fix orphaned variable

Either:

  • Add LEGACY_API_KEY to the Zod schema in src/env.ts
  • Or remove it from .env.example if no longer needed

Fix undocumented variable

Add INTERNAL_API_KEY to .env.example:

# Internal
INTERNAL_API_KEY=

Configuration

See test-env.config.json for how to:

  • Enable/disable specific tests
  • Exclude certain directories or files
  • Allow specific generic variable names
  • Exclude variables from specific checks