PHPackages                             selfphp/env-schema - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. selfphp/env-schema

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

selfphp/env-schema
==================

Validate .env environment variables in PHP using a declarative schema.

v1.0.0(1y ago)01MITPHPPHP &gt;=8.1

Since May 28Pushed 1y agoCompare

[ Source](https://github.com/selfphp/env-schema)[ Packagist](https://packagist.org/packages/selfphp/env-schema)[ RSS](/packages/selfphp-env-schema/feed)WikiDiscussions main Synced today

READMEChangelogDependencies (2)Versions (2)Used By (0)

env-schema
==========

[](#env-schema)

**env-schema** is a lightweight PHP library for validating `.env` files using a declarative schema.

✅ Features
----------

[](#-features)

- Validate environment variables at runtime
- Supports types: `string`, `int`, `bool`, `float`
- Optional fields with `default`
- Required fields with `required`
- Allowed values (`allowed`) and regex (`pattern`) checks
- Fully tested (PHPUnit 12)
- No dependencies

📦 Installation
--------------

[](#-installation)

```
composer require selfphp/env-schema
```

🧪 Usage
-------

[](#-usage)

### .env

[](#env)

```
APP_ENV=production
DEBUG=true
PORT=8080
APP_SECRET=abcd1234efgh5678ijkl9012mnop3456

```

### schema.php

[](#schemaphp)

```
use Selfphp\EnvSchema\EnvSchema;

$schema = [
    'APP_ENV' => [
        'type' => 'string',
        'default' => 'production',
        'allowed' => ['dev', 'test', 'production'],
    ],
    'DEBUG' => [
        'type' => 'bool',
        'default' => false,
    ],
    'PORT' => [
        'type' => 'int',
        'required' => true,
    ],
    'APP_SECRET' => [
        'type' => 'string',
        'pattern' => '/^[A-Za-z0-9]{32}$/',
        'required' => true,
    ],
];

$validatedEnv = EnvSchema::validate($schema, __DIR__ . '/.env');

echo $validatedEnv['PORT']; // 8080
```

📁 Example Files
---------------

[](#-example-files)

- `.env` – runtime environment
- `.env.example` – template for others
- `.gitignore` – excludes secrets

📄 License
---------

[](#-license)

MIT License ©2025 SELFPHP - Damir Enseleit
------------------------------------------

[](#mit-license-2025-selfphp---damir-enseleit)

🖥️ Run CLI Example
------------------

[](#️-run-cli-example)

You can quickly test your `.env` file and schema using the provided example script:

```
php examples/validate-env.php
```

This script:

- Loads a predefined schema
- Parses your `.env` file
- Outputs all validated values or detailed error messages

### Example Output

[](#example-output)

```
✅ .env file is valid.
APP_ENV = 'production'
DEBUG = true
PORT = 8080
APP_SECRET = 'abcd1234efgh5678ijkl9012mnop3456'

```

If validation fails, you'll see helpful errors like:

```
❌ Validation error: Missing required variable: PORT

```

> ℹ️ Make sure to install dependencies first via `composer install`.

###  Health Score

27

—

LowBetter than 47% of packages

Maintenance46

Moderate activity, may be stable

Popularity1

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity46

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 75% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

402d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/d9da560cb003f45736b988e005afce3c17647553a1c1cea771b3cde3b7890ad1?d=identicon)[SELFPHP](/maintainers/SELFPHP)

---

Top Contributors

[![DamirEnseleit](https://avatars.githubusercontent.com/u/206047444?v=4)](https://github.com/DamirEnseleit "DamirEnseleit (3 commits)")[![selfphp](https://avatars.githubusercontent.com/u/12782362?v=4)](https://github.com/selfphp "selfphp (1 commits)")

---

Tags

schemavalidationutilityconfigenvdotenvenvironment variablestypecheckenv-schema

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Type Coverage Yes

### Embed Badge

![Health badge](/badges/selfphp-env-schema/health.svg)

```
[![Health](https://phpackages.com/badges/selfphp-env-schema/health.svg)](https://phpackages.com/packages/selfphp-env-schema)
```

###  Alternatives

[vlucas/phpdotenv

Loads environment variables from `.env` to `getenv()`, `$\_ENV` and `$\_SERVER` automagically.

13.5k640.2M6.3k](/packages/vlucas-phpdotenv)[nette/utils

🛠 Nette Utils: lightweight utilities for string &amp; array manipulation, image handling, safe JSON encoding/decoding, validation, slug or strong password generating etc.

2.1k430.4M1.7k](/packages/nette-utils)[league/config

Define configuration arrays with strict schemas and access values with dot notation

565335.0M36](/packages/league-config)[m1/env

Env is a lightweight library bringing .env file parser compatibility to PHP. In short - it enables you to read .env files with PHP.

6412.8M24](/packages/m1-env)[dotenv-org/phpdotenv-vault

Load environment variables from encrypted .env.vault files

1019.5k2](/packages/dotenv-org-phpdotenv-vault)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
