php – 是否可以在laravel请求类中添加验证规则的名称和描述

我有要求上课:

class UpdateProductRequest extends ApiRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name'        => 'required',
            'description' => 'required',
        ];
    }
}

但我想要类似的东西:

class UpdateProductRequest extends ApiRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'name'=> [
                'name' => 'Product name',
                'description' => 'Product name description',
                'validation' => 'required',
                'type' => 'string',
            ],
            'description'=> [
                'name' => 'Product description',
                'description' => 'Product description description',
                'validation' => 'required',
                'type' => 'string',
            ]
        ];
    }
}

因为我想将这些附加数据用于Rest API文档.我不想复制像这样的名字:

class UpdateProductRequest extends ApiRequest
{
    public function authorize()
    {
        return true;
    }

    public function apiData(){
        return [
            'name'=> [
                'name' => 'Product name',
                'description' => 'Product name description',
                'type' => 'string',
            ],
            'description'=> [
                'name' => 'Product description',
                'description' => 'Product description description',
                'type' => 'string',
            ]
        ];
    }

    public function rules()
    {
        return [
            'name'        => 'required',
            'description' => 'required',
        ];
    }
}

最佳答案 您可以制定规则来使用您的apiData,如下所示:

class UpdateProductRequest extends ApiRequest
{
    public function authorize()
    {
        return true;
    }

    public function apiData()
    {
        return [
            'name'=> [
                'name' => 'Product name',
                'description' => 'Product name description',
                'validation' => 'required',
                'type' => 'string',
            ],
            'description'=> [
                'name' => 'Product description',
                'description' => 'Product description description',
                'validation' => 'required',
                'type' => 'string',
            ]
        ];
    }

    public function rules()
    {
        return array_map(function($item){
            return $item['validation'];
        }, $this->apiData());
    }
}

但是,查看Swagger文档可能是一个好主意. larave的绑定很少:https://packagist.org/search/?q=swagger%20laravel

编辑:

rules是apiData的一种适配器,它保存数据.因此,对于未提及的问题,您需要将您的逻辑放在apiData中而不更改规则中的逻辑:

public function apiData()
{
    return array_diff_key(
        (new Requests\Backend\UpdateProductRequest)->apiData(),
        ['something' => false]
    );
}

public function rules()
{
    return array_map(function($item){
        return $item['validation'];
    }, $this->apiData());
}
点赞