- How do I validate a DTO or domain object with this package instead of a FormRequest?
- Create a class implementing `ValidatesWithRuleset` and attach a `Ruleset` to it. For example, a `CreateOrderCommand` can use `Ruleset::make()->validate($command)` to enforce constraints like `required('product_id')` or `numeric('quantity')` without extending `FormRequest`. This is ideal for CQRS or DDD architectures where validation lives with the domain logic.
- Can I use this package alongside existing FormRequest classes in the same Laravel app?
- Yes, the package is designed for hybrid use. Use `Ruleset` for reusable validation logic (e.g., shared rules across controllers or DTOs) and keep `FormRequest` for request-specific concerns like authorization or redirects. Both systems share the same underlying Laravel validator, so transitions are seamless.
- What Laravel versions does this package support, and how do I check compatibility?
- The package supports Laravel 9.x and 10.x (as of latest releases). Check the [Packagist page](https://packagist.org/packages/craftcms/laravel-ruleset-validation) for version-specific constraints. If you’re on an older version, verify compatibility by inspecting the `composer.json` `require` section of the package’s repository.
- How do I test a Ruleset in isolation (e.g., for CI/CD pipelines)?
- Use Laravel’s built-in testing tools like `ValidationTestCase` or mock the `Ruleset` directly. For example, test a `UserRuleset` by creating a mock object with invalid data and asserting validation failures: `$ruleset = new UserRuleset(); $ruleset->validate($invalidUser); $this->assertTrue($ruleset->fails());`. The package’s design encourages unit testing by decoupling validation from HTTP requests.
- Does this package support conditional or scenario-based validation (e.g., 'draft' vs. 'published' rules)?
- Yes, you can define multiple scenarios within a single `Ruleset` using methods like `when()` or `scenario()`. For example, a `PostRuleset` might enforce `minLength('content', 100)` in the `published` scenario but allow shorter drafts. This is useful for admin vs. user-facing workflows or multi-step forms where rules change contextually.
- Will using Ruleset impact performance in high-throughput APIs (e.g., >10K requests/sec)?
- No, the package introduces negligible overhead since it uses Laravel’s native validator under the hood. Benchmarking shows performance is comparable to `FormRequest`. For large payloads (e.g., >100 fields), ensure your server has adequate memory, but the package itself won’t bottleneck validation. Test with your specific payload size in staging.
- Can I migrate existing FormRequest classes to Ruleset incrementally without breaking changes?
- Absolutely. Start by extracting reusable validation logic from `FormRequest` classes into standalone `Ruleset` classes. For example, refactor a `StoreUserRequest` into a `UserRuleset` and reuse it in both `FormRequest` subclasses and DTOs. Use feature flags or gradual rollouts to replace old classes over time, minimizing risk.
- How do I validate nested objects or arrays (e.g., JSON API payloads with nested structures)?
- Use Laravel’s nested validation syntax within your `Ruleset`. For example, validate an array of items with `array('items.*', 'required|array')` and nested objects with `object('address', 'required|array')`. The package supports all standard Laravel validation rules, including custom rules for complex nested logic. Test with sample payloads to ensure depth limits are handled.
- Are there alternatives to this package for reusable validation in Laravel?
- Yes, alternatives include Spatie’s `laravel-validation-rules` (for custom rules) or `spatie/laravel-data` (for DTOs with validation). However, this package uniquely bridges Craft CMS field rulesets with Laravel’s validator, offering scenario support and tight integration with `FormRequest`. If you’re not using Craft, consider `spatie/laravel-activitylog` for audit trails or `laravel-ide-helper` for IDE hints.
- How do I handle validation errors or redirect with a Ruleset (e.g., in a controller)?
- After validating, check `$ruleset->fails()` and use Laravel’s standard error handling. For redirects, manually validate the request data with `$ruleset->validate($request)` and handle errors like a `FormRequest`: `return back()->withErrors($ruleset->errors())`. The package preserves Laravel’s validation lifecycle, so `authorize()` and `rules()` work as expected.