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

Conditional validation rules are not present in the documentation #724

Closed oskobri closed 9 months ago

oskobri commented 9 months ago

Scribe version

4.23.1

PHP version

8.2

Framework

Laravel

Framework version

v10.22.0

Scribe config

title => "API"
base_url => "api.com"
try_it_out.enabled => false
auth.enabled => true
auth.default => true
auth.name => "token"
logo => "../img/nova-logo.svg"
strategies.bodyParameters => added App\Docs\Strategies\GetFromFormRequest: removed Knuckles\Scribe\Extracting\Strategies\BodyParameters\GetFromFormRequest

What happened?

Hello,

I use Form request rules and the rules inside an if are not read by scribe. Ex:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class IndexRequest extends FormRequest
{
    public function rules(): array
    {
        $rules = [
            'type' => ['required', 'string'], // present in the doc
        ];

        if ($this->get('type') === 'something') {
            $rules['name'] = ['required', 'string']; // not present in the doc 
        }

        return $rules;
    }
}

In my example, body param type is sent as "something".

Docs

shalvah commented 9 months ago

Hm, I'll need you to debug what actually happens when the FormRequest is called. Maybe try dumping all the incoming request parameters and running php artisan scribe:generate.

In my example, body param type is sent as "something".

I find this unlikely. I don't think Scribe can, on its own, send one parameter when making a response call to get other parameters, but tou can achieve this by using beforeResponseCall (see the docs).

Anyway, what about using required_if instead? Seems like it would work fine for your use case, and I think Scribe supports that better.