Laravel 9: implementing Custom Validation Rules for Nested JSON Structures in API Requests
I'm having trouble with After trying multiple solutions online, I still can't figure this out... I'm working with an scenario when trying to validate a nested JSON structure in an API request using Laravel 9. Specifically, I have a request body structured like this: ```json { "user": { "name": "John Doe", "email": "john@example.com", "roles": [ { "name": "admin" }, { "name": "editor" } ] } } ``` I need to ensure that the `name` is required, the `email` is a valid email, and that each `role` has a `name` that is also required. I created a custom request class and defined my rules like this: ```php public function rules() { return [ 'user.name' => 'required|string', 'user.email' => 'required|email', 'user.roles.*.name' => 'required|string', ]; } ``` However, when I send a request that is missing the `user.name` field, I get a validation message that reads: 'The user.roles.0.name field is required.' instead of the more expected response for the missing `user.name`. I've tried using the `array` validation rule for `roles`, but that didn't seem to help either. I've also looked into using the `validate()` method directly in my controller, but I prefer keeping validation logic within the form request. Am I missing something in how Laravel processes nested validation rules, or is there a better way to structure my rules for this case? Any insights would be appreciated! What am I doing wrong? My development environment is Windows. Thanks in advance! Hoping someone can shed some light on this.