โœ…
Laravel Validated DTO
View on GitHub
  • โœ…Validated DTO for Laravel
  • ๐Ÿš€Getting Started
    • Installation
    • Configuration
    • Upgrade Guide
    • Changelog
  • ๐Ÿ˜ŽThe Basics
    • Generating DTOs
    • Defining DTO Properties
    • Defining Validation Rules
    • Creating DTO Instances
    • Accessing DTO Data
    • Defining Default Values
    • Transforming DTO Data
    • Mapping DTO properties
    • Simple DTOs
    • Resource DTOs
    • Wireable DTOs
    • Lazy Validation
    • Generating TypeScript Definitions
  • ๐ŸŽจCustomize
    • Custom Error Messages and Attributes
    • Custom Exceptions
  • ๐ŸงชType Casting
    • Introduction
    • Available Types
    • Create Your Own Type Cast
    • Casting Eloquent Model properties to DTOs
Powered by GitBook
On this page

Was this helpful?

  1. The Basics

Defining Validation Rules

You can validate data in the same way you validate Request data:

class UserDTO extends ValidatedDTO
{
    protected function rules(): array
    {
        return [
            'name'     => ['required', 'string'],
            'email'    => ['required', 'email'],
            'password' => [
                'required',
                Password::min(8)
                    ->mixedCase()
                    ->letters()
                    ->numbers()
                    ->symbols()
                    ->uncompromised(),
            ],
        ];
    }
}

Alternatively, for simpler cases, you can use the Rules attribute:

use WendellAdriel\ValidatedDTO\Attributes\Rules;
use WendellAdriel\ValidatedDTO\Concerns\EmptyRules;

class UserDTO extends ValidatedDTO
{
    use EmptyRules;

    #[Rules(['required', 'string', 'min:3', 'max:255'])]
    public string $name;

    #[Rules(rules: ['required', 'email', 'max:255'], messages: ['email.email' => 'The given email is not a valid email address.'])]
    public string $email;

    #[Rules(['sometimes', 'boolean'])]
    public bool $active;
}

If you're using attributes to validate your data, you can use the EmptyRules trait to avoid having to define the rules() method.

Additional Validation

Like the Form Requests, you can easily add additional validation for your DTOs by implementing their after method.

The ValidatedDTO has this method already, but it's not doing anything by default:

protected function after(\Illuminate\Validation\Validator $validator): void
{
    // Do nothing
}

You can override this method in your DTO to add any additional validations that are needed:

protected function after(\Illuminate\Validation\Validator $validator): void
{
    if ($this->invalidData) {
        $validator->errors()->add('test', 'Data is invalid!');
    }
}
PreviousDefining DTO PropertiesNextCreating DTO Instances

Last updated 7 months ago

Was this helpful?

๐Ÿ˜Ž