- How do I integrate Respect/Validation into a Laravel project?
- Install via Composer: `composer require respect/validation`. Use it alongside Laravel’s Validator by injecting the `Validation` class into Form Requests or services. Example: `$validator = new Validation(); $validator->numeric()->positive()->validate($input)`. No Laravel-specific setup is required beyond Composer.
- Can I use Respect/Validation to replace Laravel’s built-in Validator?
- Yes, it’s fully compatible. Replace `Validator::make()` with Respect’s chainable syntax (e.g., `v::email()->validate($request->input())`) in Form Requests, API resources, or anywhere. The package doesn’t conflict with Laravel’s core, so you can mix and match.
- Does Respect/Validation support Laravel’s Form Request validation rules?
- Absolutely. Use it in `FormRequest::rules()` by returning an array of Respect’s validator methods (e.g., `['email' => v::email()->noWhitespace()]`). It integrates seamlessly with Laravel’s validation pipeline, including error message formatting and failed validation responses.
- What Laravel versions does Respect/Validation support?
- It works with Laravel 5.5+ (PHP 8+). While it’s framework-agnostic, the package’s lightweight design ensures no conflicts with Laravel’s service container or dependency injection. Tested thoroughly with Laravel 9/10, but older versions (5.5+) should work without issues.
- How do I handle nested validation (e.g., arrays or objects) in Laravel?
- Use Respect’s path-based validation: `v::arrayVal()->key('user')->email()->validate($data)`. For Laravel API Resources, nest rules under `resources:array` or use `->path('user.address')` for deep validation. The package handles nested structures like Laravel’s `Validator::extend()`.
- Are there performance concerns when using Respect/Validation in high-traffic Laravel APIs?
- No, it’s optimized for performance (~100KB) with no reported overhead. Benchmarks show it handles high-throughput validation (e.g., 10K+ requests/sec) without issues. For Laravel APIs, pair it with caching (e.g., `Validator::extend()` caching) for further optimization.
- How do I create custom validation rules for Laravel with Respect/Validation?
- Extend Respect’s `Rule` class or use closures: `v::custom(function($input) { return strlen($input) > 10; })`. In Laravel, register custom rules via `Validator::extend()` or use Respect’s `RuleSet` for reusable logic. Example: `v::rule(new CustomRule())->validate($input)`.
- Does Respect/Validation work with Livewire or Inertia.js for client-side validation?
- Yes, it’s ideal for server-side validation in Livewire/Inertia. Use the same rules in your backend (e.g., Form Requests) and mirror them in client-side libraries like VeeValidate. Respect’s chainable syntax maps cleanly to frontend validation frameworks, reducing duplication.
- What are the alternatives to Respect/Validation for Laravel?
- Laravel’s built-in `Validator` is the primary alternative, but Respect offers more composable rules (e.g., `numeric()->between()` vs. `between:1,255`). Other options include `laravel-validator` (Laravel-specific) or `symfony/validator` (heavier). Respect stands out for its lightweight, chainable API and PHP-first design.
- How do I test validation logic in Laravel using Respect/Validation?
- Use PHPUnit with Respect’s built-in testing utilities. Example: `$validator = new Validation(); $this->assertTrue($validator->email()->isValid('test@example.com'))`. For Laravel, test Form Requests with `assertValidationException()` or mock the `Validation` class in unit tests. The package’s TDD-friendly design accelerates validation testing.