Alternatively, for simpler cases, you can use the Rules attribute:
useWendellAdriel\ValidatedDTO\Attributes\Rules;useWendellAdriel\ValidatedDTO\Concerns\EmptyRules;classUserDTOextendsValidatedDTO{useEmptyRules; #[Rules(['required','string','min:3','max:255'])]publicstring $name; #[Rules(rules: ['required','email','max:255'], messages: ['email.email'=>'The given email is not a valid email address.'])]publicstring $email; #[Rules(['sometimes','boolean'])]publicbool $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:
protectedfunctionafter(\Illuminate\Validation\Validator $validator):void{// Do nothing}
You can override this method in your DTO to add any additional validations that are needed:
protectedfunctionafter(\Illuminate\Validation\Validator $validator):void{if ($this->invalidData) { $validator->errors()->add('test','Data is invalid!'); }}