knuckleswtf / scribe

Generate API documentation for humans from your Laravel codebase.✍
https://scribe.knuckles.wtf/laravel/
MIT License
1.59k stars 280 forks source link

Fixed parsing of nested fields in validation rules #749

Closed raphaelflash closed 7 months ago

raphaelflash commented 8 months ago

Description

If there are nested fields in the request validation rules, the parsing is incorrect. This PR fixes this, so that it also works when an array is expected, see examples below.

Screenshots

Before the change

Bildschirmfoto 2023-10-16 um 03 12 22

After the change

Bildschirmfoto 2023-10-16 um 03 11 48

Example request class

class ExampleRequest extends FormRequest
{
    public function rules()
    {
        return [
            '*.foo' => 'required|array',
            '*.foo.*' => 'required|array',
            '*.foo.*.bar' => 'required',
        ];
    }

    public function bodyParameters()
    {
        return [
            '*.foo' => [
                'description' => 'This is not shown (intended?).',
            ],
            '*.foo.*' => [
                'description' => 'A nested field.',
            ],
            '*.foo.*.bar' => [
                'description' => 'A double-nested field.',
            ],
        ];
    }
}

Note: The first description (of *.foo is ignored and will not be shown by scribe. I'm not sure if this is intended?

Example request body

Single object example

[
  {
    "foo": [
      {
        "bar": "hello world"
      }
    ]
  }
]

Multi object example

[
  {
    "foo": [
      {
        "bar": "hello world"
      }
    ]
  },
  {
    "foo": [
      {
        "bar": "hallo welt"
      },
      {
        "bar": "hej världen"
      }
    ]
  }
]