Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Validation Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing via Composer (composer require spiral/validation) and examine the Validator class and rule definitions in src/Rules. The core workflow is:

  1. Define validation rules (e.g., Rules::required(Rules::string()))
  2. Instantiate Validator (optionally pass custom message formatters or processors)
  3. Call $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.

Implementation Patterns

  • Schema Per Use Case: Define validation schemas as reusable arrays or dedicated classes (e.g., class UserRegistrationRules { public function get(): array { return [...] } }).
  • Nested Validation: Use Rules::nested([...]) to validate complex objects or JSON payloads (e.g., ['address' => ['city' => 'NYC', 'zip' => 12345]]).
  • Delegated Validation: Chain rules like Rules::all(Rules::email(), Rules::max(255)) and combine with Rules::optional() or Rules::nullable().
  • DTO Integration: Inject validation rules into DTO constructors or static factory methods to ensure immutability and type safety post-validation.
  • Framework Integration: In Spiral apps, use the provided ValidationMiddleware to auto-validate controller inputs or register ValidatorInterface in services.

Gotchas and Tips

  • Non-strict comparison: Rules like Rules::integer() accept numeric strings (e.g., '42'), but Rules::int() enforces strict int type—check rule names carefully.
  • Error path structure: Nested errors use dot-notation ('address.city')—ensure your frontend or API client expects this format.
  • Empty array handling: Rules::required() fails on empty arrays ([]) but passes on associative arrays (['key' => 'val'])—combine with Rules::minCount(1) if needed.
  • No built-in async: Validation is synchronous; avoid heavy I/O rules in Rules::callback().
  • Extensibility: Create custom rules by extending Rule or implementing RuleInterface—the package is designed for this (e.g., domain-specific constraints like UniqueEmail).
  • Date/time validation: Use 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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport