lemmon / validator
A lightweight, fluent validation library for PHP.
Requires
- php: ^8.3
Requires (Dev)
- carthage-software/mago: ^1
- ergebnis/composer-normalize: ^2.48
- pestphp/pest: ^4.3
- phpstan/phpstan: ^2.1
- symfony/error-handler: ^7.0
- symfony/var-dumper: ^7.0
README
Lemmon Validator is a PHP library with no third-party Composer dependencies for validating and
transforming scalar values, indexed arrays, and nested schemas for associative arrays or stdClass
objects through a fluent API. It combines form-safe coercion, ordered pipelines, and structured errors
for exception and non-exception workflows.
Note
Lemmon Validator is pre-1.0 and under active development. Minor releases may contain breaking changes; review the changelog before upgrading.
Installation
composer require lemmon/validator
Requirements: PHP 8.3 or higher with the mbstring extension
Quick Start
Validators accept null by default. Use required() when a value must be present.
use Lemmon\Validator\ValidationException; use Lemmon\Validator\Validator; $input = [ 'name' => 'Ada Lovelace', 'age' => '36', 'email' => 'ada@example.com', ]; $userSchema = Validator::isAssociative([ 'name' => Validator::isString() ->pipe('trim') ->notEmpty() ->required(), 'age' => Validator::isInt() ->coerce() ->min(18), // Optional; "36" becomes 36 'email' => Validator::isString() ->pipe('trim') ->email() ->required(), ]); // Non-exception workflow [$valid, $user, $errors] = $userSchema->tryValidate($input); if (!$valid) { foreach ($errors as $error) { echo $error->getPath() . ': ' . $error->getMessage() . PHP_EOL; } } // Exception workflow try { $user = $userSchema->validate($input); } catch (ValidationException $exception) { $allErrors = $exception->getErrors(); $emailErrors = $exception->getErrors('email'); }
Features
- No third-party Composer dependencies — the runtime depends only on PHP and
mbstring - Runtime type validation for strings, integers, floats, booleans, indexed arrays, associative
arrays, and
stdClassobjects - Form-safe optional fields — validators accept
nullunlessrequired(); numeric, boolean, and container coercion turns an empty string intonull, while strings can opt in withnullifyEmpty() - Predictable processing — pipeline steps execute in chain order, then
default()andrequired()resolve the final value - Composable schemas — validate nested data, omit undeclared fields by default, or retain them
explicitly with
passthrough() - Structured errors — stable codes, dotted paths, messages, and parameters support programmatic handling, i18n, path filtering, and JSON serialization
- Extensible rules — use PHP callables with
transform(),pipe(), andsatisfies(), or compose validators with logical combinators
Philosophy
Lemmon Validator focuses on core validation and predictable schema behavior rather than reimplementing every specialized rule. Its transformation and custom-validation methods accept ordinary PHP callables, so application-specific behavior can be composed without framework integrations or custom validator subclasses.
Documentation
Getting Started
Validation Guides
- String Validation — Email, URL, patterns, length constraints
- Numeric Validation — Integers, floats, ranges, constraints
- Array Validation — Indexed arrays and item validation
- Object & Schema Validation — Complex nested structures
- Custom Validation — User-defined functions and business logic
- Error Handling — Working with validation errors
Factory Reference
Examples
For AI Agents
llms.txt— Technical specification with API signatures, parameters, and core behavior
Security
Please report suspected vulnerabilities privately by following the Security Policy. Do not disclose security issues in the public issue tracker.
Contributing
Contributions are welcome. Read the Contributing Guide, or open an issue to report a bug or propose an improvement.
License
Lemmon Validator is licensed under the MIT License.