Env Validator
Validates environment variable configurations by cross-referencing .env files against
project requirements. Catches missing variables, type errors, insecure defaults, and
orphaned entries before they cause runtime failures.
Reference Files
| File |
Contents |
Load When |
references/validation-rules.md |
Built-in validation rules and severity definitions |
Always |
Prerequisites
- A
.env file (or equivalent) in the project
- Optionally:
.env.example, docker-compose.yml, or deployment manifests for cross-referencing
Workflow
Phase 1: Discovery
Locate environment configuration sources in the project:
- Primary file: Find
.env in the project root. If absent, check for .env.local, .env.development, .env.production
- Schema file: Find
.env.example or .env.template — this defines the expected variables
- Code references: Grep for
os.environ, process.env, env::var, os.Getenv patterns to find variables referenced in code
- Deployment manifests: Check
docker-compose.yml, Dockerfile, k8s/ manifests for ${VAR} or ENV VAR patterns
Report what was found before proceeding.
Phase 2: Schema Extraction
Build the expected variable schema from discovered sources:
For each variable found across all sources, record:
| Field |
Source |
| Name |
Variable name (e.g., DATABASE_URL) |
| Required |
Present in code references or marked required in example |
| Type hint |
Inferred from usage (URL, integer, boolean, string, path) |
| Default |
Value in .env.example if present |
| Used in |
List of files that reference this variable |
Phase 3: Validation
Run these checks against the primary .env file:
Missing required variables (CRITICAL)
- Variable referenced in code but absent from
.env
- Variable in
.env.example without a default but absent from .env
Type mismatches (HIGH)
PORT=abc when code does int(os.environ["PORT"])
DEBUG=yes when code expects boolean (true/false)
- URL variables without valid URL format
Insecure defaults (HIGH)
SECRET_KEY=changeme, PASSWORD=password, API_KEY=xxx
DEBUG=true or DEBUG=1 in production-targeted files
- Empty values for security-critical variables
Unreferenced variables (MEDIUM)
- Variables in
.env not referenced anywhere in code or manifests
- May indicate stale configuration
Format issues (LOW)
- Lines without
KEY=VALUE format
- Trailing whitespace in values
- Inconsistent quoting (mixing single/double/no quotes)
- Duplicate variable definitions (last wins, but likely a mistake)
See references/validation-rules.md for the complete rule catalog.
Phase 4: Report
Produce a structured validation report:
# Environment Validation Report
**File:** `.env`
**Schema:** `.env.example` + code references
**Verdict:** PASS | FAIL
## Summary
| Severity | Count |
|----------|-------|
| CRITICAL | N |
| HIGH | N |
| MEDIUM | N |
| LOW | N |
## CRITICAL
### [ENV-001] Missing required variable: DATABASE_URL
- **Referenced in:** `src/db.py:12`, `docker-compose.yml:8`
- **Expected type:** URL (postgresql://...)
- **Fix:** Add `DATABASE_URL=postgresql://user:pass@localhost:5432/dbname` to `.env`
## HIGH
...
## Unreferenced Variables
| Variable | In .env | In Code | In Manifests | Status |
|-----------------|---------|---------|--------------|--------------|
| LEGACY_API_KEY | Yes | No | No | Unreferenced |
## Recommendations
1. [Highest priority fix]
2. [Second fix]
Error Handling
| Error |
Resolution |
| No .env file found |
Report absence; check for alternative env sources |
| No .env.example or schema |
Validate based on code references only |
| Binary or very large .env |
Skip; report as unsupported format |
| No code references found |
Validate format and security only; skip completeness |
Limitations
- Cannot validate runtime-injected variables (from vault, AWS SSM, etc.)
- Type inference is heuristic — may misclassify complex values
- Does not check variable values against external services (e.g., valid API key format)
- Production vs. development distinction requires file naming conventions
1---2name: env-validator3description: Validates .env files against code references and manifests for missing vars, type mismatches, insecure defaults, and unused entries. Triggers on: "validate env file", "check environment variables", "missing env vars", "check .env", "dotenv validation". NOT for secret scanning, use repo-sentinel.4---56# Env Validator78Validates environment variable configurations by cross-referencing `.env` files against9project requirements. Catches missing variables, type errors, insecure defaults, and10orphaned entries before they cause runtime failures.1112## Reference Files1314| File | Contents | Load When |15| ------------------------------------ | ------------------------------------------------- | ---------------------- |16| `references/validation-rules.md` | Built-in validation rules and severity definitions | Always |1718## Prerequisites1920- A `.env` file (or equivalent) in the project21- Optionally: `.env.example`, `docker-compose.yml`, or deployment manifests for cross-referencing2223## Workflow2425### Phase 1: Discovery2627Locate environment configuration sources in the project:28291. **Primary file:** Find `.env` in the project root. If absent, check for `.env.local`, `.env.development`, `.env.production`302. **Schema file:** Find `.env.example` or `.env.template` — this defines the expected variables313. **Code references:** Grep for `os.environ`, `process.env`, `env::var`, `os.Getenv` patterns to find variables referenced in code324. **Deployment manifests:** Check `docker-compose.yml`, `Dockerfile`, `k8s/` manifests for `${VAR}` or `ENV VAR` patterns3334Report what was found before proceeding.3536### Phase 2: Schema Extraction3738Build the expected variable schema from discovered sources:3940For each variable found across all sources, record:4142| Field | Source |43| ----------- | --------------------------------------------------------- |44| Name | Variable name (e.g., `DATABASE_URL`) |45| Required | Present in code references or marked required in example |46| Type hint | Inferred from usage (URL, integer, boolean, string, path) |47| Default | Value in `.env.example` if present |48| Used in | List of files that reference this variable |4950### Phase 3: Validation5152Run these checks against the primary `.env` file:53541. **Missing required variables** (CRITICAL)55 - Variable referenced in code but absent from `.env`56 - Variable in `.env.example` without a default but absent from `.env`57582. **Type mismatches** (HIGH)59 - `PORT=abc` when code does `int(os.environ["PORT"])`60 - `DEBUG=yes` when code expects boolean (`true`/`false`)61 - URL variables without valid URL format62633. **Insecure defaults** (HIGH)64 - `SECRET_KEY=changeme`, `PASSWORD=password`, `API_KEY=xxx`65 - `DEBUG=true` or `DEBUG=1` in production-targeted files66 - Empty values for security-critical variables67684. **Unreferenced variables** (MEDIUM)69 - Variables in `.env` not referenced anywhere in code or manifests70 - May indicate stale configuration71725. **Format issues** (LOW)73 - Lines without `KEY=VALUE` format74 - Trailing whitespace in values75 - Inconsistent quoting (mixing single/double/no quotes)76 - Duplicate variable definitions (last wins, but likely a mistake)7778See `references/validation-rules.md` for the complete rule catalog.7980### Phase 4: Report8182Produce a structured validation report:8384```markdown85# Environment Validation Report8687**File:** `.env`88**Schema:** `.env.example` + code references89**Verdict:** PASS | FAIL9091## Summary9293| Severity | Count |94|----------|-------|95| CRITICAL | N |96| HIGH | N |97| MEDIUM | N |98| LOW | N |99100## CRITICAL101102### [ENV-001] Missing required variable: DATABASE_URL103104- **Referenced in:** `src/db.py:12`, `docker-compose.yml:8`105- **Expected type:** URL (postgresql://...)106- **Fix:** Add `DATABASE_URL=postgresql://user:pass@localhost:5432/dbname` to `.env`107108## HIGH109110...111112## Unreferenced Variables113114| Variable | In .env | In Code | In Manifests | Status |115|-----------------|---------|---------|--------------|--------------|116| LEGACY_API_KEY | Yes | No | No | Unreferenced |117118## Recommendations1191201. [Highest priority fix]1212. [Second fix]122```123124## Error Handling125126| Error | Resolution |127| --------------------------------- | ----------------------------------------------------- |128| No .env file found | Report absence; check for alternative env sources |129| No .env.example or schema | Validate based on code references only |130| Binary or very large .env | Skip; report as unsupported format |131| No code references found | Validate format and security only; skip completeness |132133## Limitations134135- Cannot validate runtime-injected variables (from vault, AWS SSM, etc.)136- Type inference is heuristic — may misclassify complex values137- Does not check variable values against external services (e.g., valid API key format)138- Production vs. development distinction requires file naming conventions