knuckleswtf / scribe

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

Form request body parameters for arrays #674

Closed laurenwilliams92 closed 1 year ago

laurenwilliams92 commented 1 year ago

Scribe version

4.19.0

Your question

I can't seem to see any documentation or work out how I can write description and examples for properties inside an array in a form request?

Here's an example of my rules

'test_questions' => 'array',
'test_questions.*.sync_id' => 'nullable|string',
'test_questions.*.score' => 'required|int',
'test_questions.*.answers' => 'array',
'test_questions.*.answers.*.value' => 'nullable|int',
'test_questions.*.answers.*.correct' => 'required|boolean'

Screenshot of output Screenshot 2023-05-28 at 18 50 32

Docs

shalvah commented 1 year ago

Hmm. Scribe should be able to handle this as is, but try adding a validation for the second level:

  'test_questions' => 'array',
+ 'test_questions.*' => 'array',
  'test_questions.*.sync_id' => 'nullable|string',
  'test_questions.*.score' => 'required|int',
  'test_questions.*.answers' => 'array',
  'test_questions.*.answers.*.value' => 'nullable|int',
  'test_questions.*.answers.*.correct' => 'required|boolean'
laurenwilliams92 commented 1 year ago

Thanks @shalvah that seemed to do the trick.

I'd also like to know how I can write a description and example for the fields within the array from the bodyParameters function in the form request.

I've tried this but no success, and also setting key to include the '*'

'test_questions' => [
      'description'=> 'Questions taken in the test',
      'example' => [
          [
              'id' => 832,
              'user_test_id' => '42',
              'question_id' => '1026',
              'question_number' => '1',
              'score' => '1',
              'correct' => true,
              'accessed' => '2023-05-18 15:45:37',
              'answered' => '2023-05-18 15:45:40',
              'viewed_explanation' => '2023-05-18 15:46:02',
              'sync_id' => NULL,
              'created' => '2023-05-18 15:45:36',
              'modified' => '2023-05-18 15:46:02',
              'deleted' => NULL,
              'answers' => [
                  [
                      'id' => 410,
                      'user_test_question_id' => '832',
                      'question_answer_id' => '526',
                      'question_cloze_group' => NULL,
                      'value' => NULL,
                      'correct' => true,
                  ],
              ],
          ]
      ],
      'sync_id' => [
          'description' => 'Unique sync ID',
          'example' => 'b88ae59a-2df1-3369-8c9e-64efb374abac'
      ]
  ]
laurenwilliams92 commented 1 year ago

For anyone else having trouble working this out the solution was: