scribe icon indicating copy to clipboard operation
scribe copied to clipboard

FormRequest with switch statement

Open natinho68 opened this issue 3 years ago • 2 comments

I want to use my form requests to implement examples & descriptions for my documentation.

I think the architecture of my form requests doesn't work with Scribe.

My API controllers are "cruddy by design" so I have one form request by controller (don't know if it's against best practices, but it's working).

It's designed like:

<?php

namespace App\Http\Requests\Api\Admin\Hub\Auth;

use Illuminate\Foundation\Http\FormRequest;

class UserRequest extends FormRequest
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        switch ($this->method()) {
            case 'GET':
                return [];
            case 'POST':
                return [
                    'firstname' => ['required', 'string'],
                    'lastname'  => ['required', 'string'],
                    'email'     => ['required', 'email', 'max:100', 'unique:App\Models\User,email'],
                    'phone'     => ['sometimes', 'string'],
                    'address'   => ['required', 'array'],
                ];
            case 'PUT':
                return [];
            case 'PATCH':
                return [];
            case 'DELETE':
                return [];
            default:
                return [];
        }
    }

    /**
     * For scribe api documentation.
     **/
    public function bodyParameters(): array
    {
        switch ($this->method()) {
            case 'GET':
                return [];
            case 'POST':
                return [
                    'firstname' => [
                        'description' => 'User firstname',
                        'example' => 'Michel',
                    ],
                    'lastname' => [
                        'description' => 'User lastname',
                        'example' => 'Drucker',
                    ],
                    'email' => [
                        'description' => 'User email',
                        'example' => '[email protected]',
                    ],
                    'phone' => [
                        'description' => 'User phone',
                        'example' => '060606060606',
                    ],
                    'address'   => [
                        'description' => 'User phone',
                        'example' => [
                            [
                                'state' => 'IDF',
                                'address' => '7 Espl. Henri de France, Paris, France',
                                'country' => 'France',
                                'locality' => 'Paris',
                                'postcode' => '75015',
                                'countrycode' => 'FR',
                            ],
                        ],
                    ],
                ];
            case 'PUT':
                return [];
            case 'PATCH':
                return [];
            case 'DELETE':
                return [];
            default:
                return [];
        }
    }

    public function messages()
    {
        return [
            'email.unique' => 'An user already exists with this email, please reset your password.',
        ];
    }
}

I think Scribe is not able to access my rules & descriptions because all of my bodyparams are empty in the documentation.

Is that correct? There is a way to figure it out?

I thought that Scribe will "run" this endpoint like Laravel does.

When I dump($this->method()) at the top of bodyParameters(); it only returns "GET", so it never goes in the right case, while the same dump at the top of rules() prompts "GET" and "POST" (don't know why the GET is prompted 😄)

natinho68 avatar Dec 12 '22 14:12 natinho68

Hmm, that's weird. Are there any warnings in the console output?

shalvah avatar Dec 13 '22 22:12 shalvah

Hmm, that's weird. Are there any warnings in the console output?

No warnings, it pretends that everything works well

natinho68 avatar Dec 13 '22 22:12 natinho68

Fixed (4.10.1)

shalvah avatar Dec 14 '22 18:12 shalvah