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:

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:

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

Last updated

Was this helpful?