Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Validated Dto Laravel Package

wendelladriel/laravel-validated-dto

Build typed Data Transfer Objects for Laravel that validate incoming data using familiar validation rules, defaults, and casting. Create DTOs by extending ValidatedDTO, define rules(), and get safe, validated, ready-to-use properties for your app.

View on GitHub
Deep Wiki
Context7

Installation

You can install the package via composer:

composer require wendelladriel/laravel-validated-dto

You can publish the config file with:

php artisan vendor:publish --tag="validated-dto"

Usage

Create a DTO by extending ValidatedDTO, declaring typed properties, and defining the validation rules for incoming data:

use Illuminate\Validation\Rules\Password;
use WendellAdriel\ValidatedDTO\Casting\BooleanCast;
use WendellAdriel\ValidatedDTO\Casting\StringCast;
use WendellAdriel\ValidatedDTO\ValidatedDTO;

final class UserDTO extends ValidatedDTO
{
    public string $name;

    public string $email;

    public string $password;

    public bool $active;

    protected function rules(): array
    {
        return [
            'name' => ['required', 'string'],
            'email' => ['required', 'email'],
            'password' => ['required', Password::min(8)],
            'active' => ['sometimes', 'boolean'],
        ];
    }

    protected function defaults(): array
    {
        return [
            'active' => true,
        ];
    }

    protected function casts(): array
    {
        return [
            'name' => new StringCast(),
            'email' => new StringCast(),
            'password' => new StringCast(),
            'active' => new BooleanCast(),
        ];
    }
}

Create an instance from an array, request, JSON string, Eloquent model, or artisan command:

$dto = UserDTO::fromArray([
    'name' => 'John Doe',
    'email' => 'john.doe@example.com',
    'password' => 's3CreT!@1a2B',
]);

$dto->name; // John Doe
$dto->toArray();

You can also inject a DTO into a controller action and let the service container resolve it from the current request:

use Illuminate\Http\JsonResponse;

final class StoreUserController
{
    public function __invoke(UserDTO $dto): JsonResponse
    {
        return response()->json($dto->toArray());
    }
}

Access the full documentation here.

Changelog

Please see the changelog for more information on what has changed recently.

Contributing

Thank you for considering contributing to Validated DTO for Laravel! You can read the contribution guide here.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

Validated DTO for Laravel is open-sourced software licensed under the MIT license.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai