Laravel Validated DTO stops data type bugs before they reach production

Laravel Daily////2 min read

The Problem with Request Validation

Standard Laravel form requests validate incoming HTTP data, but they leave payload values as loose strings or integers. When you need to cast boolean flags, instantiate Carbon instances, or resolve Enums, you typically write manual transformation logic in your controllers. If you reuse that request data across console commands, queued jobs, or background services, you must replicate those transformations everywhere. This fractured approach scatters your data schemas and exposes your code to runtime type errors.

Combining Validation and DTOs

Laravel Validated DTO, developed by Wendell Adriel, resolves this issue. By merging validation rules with strict data transfer objects, it creates a single, reusable class that acts as the absolute source of truth for your data structures.

use WendellAdriel\ValidatedDTO\ValidatedDTO;
Laravel Validated DTO stops data type bugs before they reach production
Laravel Validated DTO Package: Why/When You Would Need It?

class CreateEventDTO extends ValidatedDTO { public string $title; public bool $is_public; public CarbonImmutable $starts_at; public EventFormat $format; public VenueDTO $venue;

protected function rules(): array
{ 
    return [
        'title' => ['required', 'string'],
        'is_public' => ['boolean'],
        'starts_at' => ['required', 'date'],
        'format' => ['required', 'string'],
    ];
}

protected function casts(): array
{
    return [
        'is_public' => 'boolean',
        'starts_at' => 'datetime',
        'format' => EventFormat::class,
        'venue' => VenueDTO::class,
    ];
}

}


## Structural Type Safety and Nested Objects

This package handles nested objects natively. You can cast nested structures directly into child DTOs, enforcing type safety down through deep array payloads (e.g., nesting a `VenueDTO` inside your parent `EventDTO`).

```php
protected function casts(): array
{
    return [
        'venue' => VenueDTO::class,
    ];
}

Resolving Key Mapping Inconsistencies

API client payloads often mismatch database schemas. The package addresses this with a dedicated mapping method, allowing you to transform request parameters seamlessly into your internal database column names.

protected function mapData(): array
{
    return [
        'promo_code' => 'promotion_code',
    ];
}
Topic DensityMention share of the most discussed topics · 2 mentions across 2 distinct topics
Laravel Validated DTO
50%· products
Wendell Adriel
50%· people
End of Article
Source video
Laravel Validated DTO stops data type bugs before they reach production

Laravel Validated DTO Package: Why/When You Would Need It?

Watch

Laravel Daily // 11:21

Tutorials, and demo projects with Laravel framework. Host: Povilas Korop

Who and what they mention most
Laravel
50.0%17
Composer
14.7%5
PHP
11.8%4
Filament
11.8%4
2 min read0%
2 min read