# Defining Validation Rules

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

```php
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:

```php
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:

```php
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:

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


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://wendell-adriel.gitbook.io/laravel-validated-dto/basics/defining-validation-rules.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
