Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form request body parameters for arrays #674

Closed
1 task done
laurenwilliams92 opened this issue May 28, 2023 · 3 comments
Closed
1 task done

Form request body parameters for arrays #674

laurenwilliams92 opened this issue May 28, 2023 · 3 comments
Labels
question Further information is requested triage

Comments

@laurenwilliams92
Copy link

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

@laurenwilliams92 laurenwilliams92 added question Further information is requested triage labels May 28, 2023
@shalvah
Copy link
Contributor

shalvah commented May 28, 2023

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
Copy link
Author

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
Copy link
Author

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

  • Define '{field}.*' => 'array' in rules i.e.
'test_questions.*.answers.*' => 'array',
'test_questions.*.answers.*.value' => 'nullable|int'
  • Use exact key as set in rules in bodyParameters i.e
'test_questions.*.answers.*.value' => [
    'description' => 'Value of answer given, applies to text type questions only',
    'example' => 6548748
],

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested triage
Projects
None yet
Development

No branches or pull requests

2 participants