- How do I create a validated DTO in Laravel using this package?
- Run `php artisan make:dto YourDtoName` to generate a DTO stub. Define validation rules in the `rules()` method using Laravel’s validation syntax. Instantiate the DTO with `YourDtoName::fromArray($data)` or `YourDtoName::fromRequest($request)` to trigger validation automatically.
- Can I use validated DTOs with Laravel API resources?
- Yes, the package integrates seamlessly with API resources. Use `YourDtoName::fromRequest($request)` in your resource methods to validate incoming data before processing. The DTO’s validation rules will apply consistently across all contexts.
- Does this package support nested DTOs for complex validation?
- Absolutely. You can nest DTOs by defining them as properties in your parent DTO. For example, a `UserCreateDTO` can include a `AddressDTO` property. Validation will cascade automatically, ensuring nested data adheres to its rules.
- What Laravel versions does wendelladriel/laravel-validated-dto support?
- The package officially supports Laravel 11 through 13 as of version 4.6.0. It requires PHP 8.1+ due to its use of attributes for type safety and custom logic. Check the [GitHub releases](https://github.com/WendellAdriel/laravel-validated-dto/releases) for version-specific details.
- How does lazy validation work, and when should I use it?
- Lazy validation defers validation until the data is accessed (e.g., via a getter method). Use it for performance-critical paths where validation isn’t needed immediately, like background jobs or CLI commands where input data is trusted. Enable it with the `[Lazy]` attribute on properties.
- Can I reuse validation rules from FormRequest classes in my DTOs?
- Yes, you can manually copy validation rules from your existing `FormRequest` classes into the `rules()` method of your DTO. This ensures consistency while decoupling validation logic from HTTP-specific concerns. Consider refactoring reusable rules into a shared trait or base DTO class.
- How do I test validated DTOs in Laravel?
- Test DTOs in isolation by instantiating them with valid/invalid data and asserting validation outcomes. For example, use `YourDtoName::fromArray($invalidData)` and check for validation exceptions. Mock DTOs in service tests by injecting them directly or using factories to generate test data.
- Will this package work with Laravel Excel or other third-party packages that rely on FormRequest?
- The package won’t break existing `FormRequest` usage, but you’ll need to refactor those classes to use DTOs if you want to leverage validation reuse. For Laravel Excel, replace `FormRequest` validation with DTO validation in your import/export logic. Most third-party packages support this transition.
- How do I handle custom validation logic beyond Laravel’s built-in rules?
- Use the `rules()` method to define custom validation logic with Laravel’s `Validator` facade or closure-based rules. For complex scenarios, override the `validate()` method in your DTO to add custom logic, such as database checks or cross-field validation.
- What’s the performance impact of using validated DTOs compared to FormRequest?
- Validated DTOs introduce minimal overhead, especially with lazy validation. They avoid redundant validation logic (e.g., same rules in API and CLI contexts) and reduce memory usage by reusing validation rules. Benchmark your specific use case, but expect negligible differences in most scenarios.