Download the PHP package philiprehberger/env-validator without Composer
On this page you can find all versions of the php package philiprehberger/env-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download philiprehberger/env-validator
More information about philiprehberger/env-validator
Files in philiprehberger/env-validator
Package env-validator
Short Description Validate required environment variables with type checking and defaults
License MIT
Homepage https://github.com/philiprehberger/env-validator
Informations about the package env-validator
PHP Env Validator
Validate required environment variables with type checking and defaults.
Requirements
- PHP 8.2+
Installation
Usage
Required Variables
Check that environment variables are present:
Schema with Type Rules
Define variables with type validation in a single call:
Default Values
Provide fallback values for variables that may not be set:
Optional Variables
Optional variables generate warnings but do not cause validation failure:
Type Validation
Add type rules to individual variables:
Validate or Fail
Throw an exception if validation fails:
IP Validation
Validate IP addresses with specific version constraints:
Custom Validation Rules
Define your own validation logic with a callable:
Enum Validation
Validate that a value matches one of a backed enum's cases:
Dependency Rules
Make variables conditionally required based on other variables:
.env File Parsing
Validate a .env file without loading it into the environment:
The parser handles comments, quoted values, multiline values, export prefix, and ${VAR} interpolation.
Environment Profiles
Register named validation profiles for different environments:
Supported Types
| Type | Description |
|---|---|
string |
Always passes (any string value) |
int, integer |
Numeric digits, optionally prefixed with - |
float, number |
Any numeric value (uses is_numeric) |
bool, boolean |
true, false, 1, 0, yes, no (case-insensitive) |
url |
Valid URL (uses FILTER_VALIDATE_URL) |
email |
Valid email (uses FILTER_VALIDATE_EMAIL) |
ip |
Valid IP address (uses FILTER_VALIDATE_IP) |
ipv4 |
Valid IPv4 address |
ipv6 |
Valid IPv6 address |
json |
Valid JSON string |
API
EnvValidator
| Method | Description |
|---|---|
required(array $vars): PendingValidation |
Define required environment variables |
schema(array $rules): PendingValidation |
Define variables with type rules |
fromFile(string $path): PendingFileValidation |
Validate a .env file without loading it |
profile(string $name, array $schema): void |
Register a named validation profile |
validateProfile(string $name): ValidationResult |
Validate against a named profile |
PendingValidation
| Method | Description |
|---|---|
optional(array $vars): self |
Add optional variables (warnings only) |
defaults(array $defaults): self |
Set default values for missing variables |
type(string $var, string $type): self |
Add a type rule for a variable |
custom(string $var, callable $validator, string $message = ''): self |
Add a custom validation rule |
enum(string $var, string $enumClass): self |
Validate against a backed enum |
dependsOn(string $var, string $dependency): self |
Require variable only when dependency is set |
requiredIf(string $var, string $conditionVar, mixed $value): self |
Require variable when condition equals value |
requiredUnless(string $var, string $conditionVar, mixed $value): self |
Require variable unless condition equals value |
validate(): ValidationResult |
Run validation and return result |
validateOrFail(): void |
Run validation, throw on failure |
ValidationResult
| Property | Type | Description |
|---|---|---|
passed |
bool |
Whether validation passed |
missing |
array<string> |
List of missing required variables |
invalid |
array<string, string> |
Map of variable names to error messages |
warnings |
array<string> |
List of warning messages |
toArray() |
array |
Convert result to array |
Development
Support
If you find this project useful:
License
MIT