laravel-data icon indicating copy to clipboard operation
laravel-data copied to clipboard

De-serialization exception

Open kminek opened this issue 4 months ago • 0 comments

✏️ Describe the bug I am using version 4.10.1 and also i have validation_strategy set to always.

↪️ To Reproduce

class Event extends \Spatie\LaravelData\Data
{
    public function __construct(
        public string $name,
        public array $params = [],
    ) {}
}

class Post extends \Spatie\LaravelData\Data
{
    public function __construct(
        public string $title,
        /**
         * @var Collection<int, Event>
         */
        public Collection $history = new Collection,
    ) {}
}

$post = new Post(
    title: 'Lorem Ipsum',
);

$post->history->push(new Event(
    name: 'post.created',
));

$postArray = $post->toArray();

$postDeserialized = Post::from($postArray); // throws Exception
dump($postDeserialized);

I'm getting following exception:

 Illuminate\Validation\ValidationException  This field is required.

✅ Expected behavior De-serialization should happen also when params are not set as the default value (empty array) is set in data object definition.

🖥️ Versions

Laravel: 11 Laravel Data: 4.10.1 PHP: 8.3

kminek avatar Oct 16 '24 15:10 kminek