spiral/validation
Spiral Validation is a lightweight PHP validation component for defining rules, validating arrays/DTOs, and collecting readable error messages. Integrates with Spiral Framework but can be used standalone for input validation in APIs and forms.
Start by installing via Composer (composer require spiral/validation) and examine the Validator class and rule definitions in src/Rules. The core workflow is:
Rules::required(Rules::string()))Validator (optionally pass custom message formatters or processors)$validator->validate($data, $rules) to get structured errors.First use case: validating an incoming API request payload (e.g., ['email' => 'bad@example', 'age' => 'twenty']) against a simple schema—this immediately demonstrates structured error output and composability.
class UserRegistrationRules { public function get(): array { return [...] } }).Rules::nested([...]) to validate complex objects or JSON payloads (e.g., ['address' => ['city' => 'NYC', 'zip' => 12345]]).Rules::all(Rules::email(), Rules::max(255)) and combine with Rules::optional() or Rules::nullable().ValidationMiddleware to auto-validate controller inputs or register ValidatorInterface in services.Rules::integer() accept numeric strings (e.g., '42'), but Rules::int() enforces strict int type—check rule names carefully.'address.city')—ensure your frontend or API client expects this format.Rules::required() fails on empty arrays ([]) but passes on associative arrays (['key' => 'val'])—combine with Rules::minCount(1) if needed.Rules::callback().Rule or implementing RuleInterface—the package is designed for this (e.g., domain-specific constraints like UniqueEmail).Rules::date() or Rules::timestamp() with care—they use PHP’s DateTime, so timezone context matters. Test edge cases like leap years or DST transitions.How can I help you explore Laravel packages today?