spiral/validator
Spiral Validator is a lightweight PHP validation component for the Spiral Framework. Define rules, validate arrays and DTOs, collect detailed error messages, and integrate cleanly with requests, forms, and domain services for consistent input validation.
Start by installing the package via Composer:
composer require spiral/validator
The core entry point is the Validator class. For a first use case, define a simple validation schema using rule objects (e.g., Required, Email, Length) and validate an associative array:
use Spiral\Validator\Validator;
use Spiral\Validator\Rule as R;
$validator = new Validator();
$input = ['email' => 'invalid'];
$rules = [
'email' => [new R\Required(), new R\Email()],
];
$errors = $validator->validate($input, $rules);
// $errors contains structured error data if validation fails
Check the src/Rule/ directory and README.md for available built-in rules.
UserRequestValidator) with a getRules() method, promoting testability and reuse across handlers/controllers.Nested or Each rules to validate arrays of structs (e.g., validating a list of order items with their own rules).Spiral\Validator\Rule\AbstractRule to implement domain-specific constraints (e.g., UniqueEmail). Override validate() to return false on failure and provide descriptive error messages.ValidationFilter (if available in the framework bundle) to auto-validate HTTP requests.ErrorBag (returned from validate()) to group errors by field and render them in views or JSON responses using getMessages() or first().Validator expects array-like input; if passing objects, use ArrayObject or extract to arrays first. No built-in object hydration—validate flattened arrays.Required rule is ignored, not treated as invalid. Always add Required() explicitly for mandatory fields.new R\Required(), new R\Email()), early failures short-circuit—subsequent rules won’t run. Use separate arrays if all rules must fire (e.g., [new R\Required()], [new R\Email()]).Validator if used as a dependency in higher-level tests.ErrorBag for front-end compatibility.How can I help you explore Laravel packages today?