spatie/laravel-data
Create rich, typed data objects for Laravel that replace form requests and API transformers. Automatically map from requests, validate with inferred rules, transform to resources (with lazy/partial fields), and generate TypeScript definitions from the same source.
A round of bug fixes for validation rules, install resolution, and rule overrides.
rules() overrides regardless of property defaults (#1187)Properties with a default value were silently dropping rules declared in the rules() method when the property was missing from the payload, so conditional rules like required, required_if, and prohibited_if never fired. The skip in DataValidationRulesResolver was originally there to stop auto-generated rules from rejecting empty payloads on all-default data classes (#441), but it also short-circuited explicit overrides.
Overrides are now resolved upfront, and the default-value skip only applies when no override exists for that property. #[WithoutValidation] no longer suppresses an override for the same key. If you wrote rules(), you own them. Thanks @rubenvanassche.
composer require spatie/laravel-data failed when Pest was already installed, because Pest's chain pulls phpdocumentor/reflection-docblock 6.0.3 while the phpdocumentor/reflection ^6.0 meta package hard-pinned reflection-docblock ^5. The meta package is now dropped in favour of direct dependencies on the three sub-packages we use, with widened constraints:
- "phpdocumentor/reflection" : "^6.0",
+ "phpdocumentor/type-resolver" : "^1.7 || ^2.0",
+ "phpdocumentor/reflection-common" : "^2.2",
+ "phpdocumentor/reflection-docblock" : "^5.3 || ^6.0",
The phpDocumentor APIs we call are identical across both majors, so no code changes are required. Thanks @rubenvanassche.
Min, Max and Size validation attributes (#1185)The constructors typed their value as int|ExternalReference, and because those classes don't declare strict types, PHP silently coerced floats to int. #[Min(0.01)] became min:0, letting 0 pass validation, even though Laravel's validator accepts floats for these rules. Types are now widened to int|float|ExternalReference, mirroring MultipleOf:
#[Min(0.01), Max(99.99)]
public float $price;
Digit-count rules (Digits, MinDigits, MaxDigits, DigitsBetween) and Password length stay int. Thanks @rubenvanassche.
Full Changelog: https://github.com/spatie/laravel-data/compare/4.22.1...4.23.0
Full Changelog: https://github.com/spatie/laravel-data/compare/4.22.0...4.22.1
Full Changelog: https://github.com/spatie/laravel-data/compare/4.21.0...4.22.0
Full Changelog: https://github.com/spatie/laravel-data/compare/4.20.1...4.21.0
Full Changelog: https://github.com/spatie/laravel-data/compare/4.19.0...4.19.1
Full Changelog: https://github.com/spatie/laravel-data/compare/4.18.0...4.19.0
Full Changelog: https://github.com/spatie/laravel-data/compare/4.17.1...4.18.0
Full Changelog: https://github.com/spatie/laravel-data/compare/4.17.0...4.17.1
Full Changelog: https://github.com/spatie/laravel-data/compare/4.16.1...4.17.0
Full Changelog: https://github.com/spatie/laravel-data/compare/4.16.0...4.16.1
Full Changelog: https://github.com/spatie/laravel-data/compare/4.15.3...4.16.0
Full Changelog: https://github.com/spatie/laravel-data/compare/4.15.2...4.15.3
Full Changelog: https://github.com/spatie/laravel-data/compare/4.15.1...4.15.2
Full Changelog: https://github.com/spatie/laravel-data/compare/4.15.0...4.15.1
Full Changelog: https://github.com/spatie/laravel-data/compare/4.14.1...4.15.0
Full Changelog: https://github.com/spatie/laravel-data/compare/4.14.0...4.14.1
If you're using cached versions of your data objects and don't clear this cache on deploy, now is the time since we've updated some internal structures.
Some internals structures (attributes) in the DataProperty and DataClass have changed, if you're using these classes, update them accordingly.
Full Changelog: https://github.com/spatie/laravel-data/compare/4.13.1...4.13.2
Allow Laravel 12
Full Changelog: https://github.com/spatie/laravel-data/compare/4.13.0...4.13.1
Full Changelog: https://github.com/spatie/laravel-data/compare/4.12.0...4.13.0
What a release! Probably to biggest minor release we've ever done!
Some cool highlights:
Optional values are great, but sometimes a null value is desirable from now on you can do the following:
class SongData extends Data {
public function __construct(
public string $title,
public string $artist,
public Optional|null|string $album,
) {
}
}
SongData::factory()
->withoutOptionalValues()
->from(['title' => 'Never gonna give you up', 'artist' => 'Rick Astley']); // album will `null` instead of `Optional`
It was already possible to inject a Laravel route parameter when creating a data object, we've now extended this functionality quite a bit and also allow injecting dependencies from the container and the authenticated user.
class SongData extends Data {
#[FromAuthenticatedUser]
public UserData $user;
}
In the past when the validation rules of a property were manually defined, the automatic validation rules for that property were omitted. From now on, you can define manual validation rules and merge them with the automatically generated validation rules:
```php
#[MergeValidationRules]
class SongData extends Data
{
public function __construct(
public string $title,
public string $artist,
) {
}
public static function rules(): array
{
return [
'title' => ['max:20'],
'artist' => ['max:20'],
];
}
}
We now ship by default a Uppercase and Lowercase mapper for mapping property names.
Data::authorize() to allow for dependencies by @cosmastech in https://github.com/spatie/laravel-data/pull/910LowerCaseMapper and UpperCaseMapper by @andrey-helldar in https://github.com/spatie/laravel-data/pull/927Full Changelog: https://github.com/spatie/laravel-data/compare/4.11.1...4.12.0
Full Changelog: https://github.com/spatie/laravel-data/compare/4.10.1...4.11.0
It has been a fews weeks, a mostly bugfix release with one new feature, enjoy!
Full Changelog: https://github.com/spatie/laravel-data/compare/4.7.2...4.8.0
Full Changelog: https://github.com/spatie/laravel-data/compare/4.7.0...4.7.1
Full Changelog: https://github.com/spatie/laravel-data/compare/4.6.0...4.7.0
Full Changelog: https://github.com/spatie/laravel-data/compare/4.5.1...4.6.0
Full Changelog: https://github.com/spatie/laravel-data/compare/4.5.1...3.12.0
How can I help you explore Laravel packages today?