revenkroz/validating-param-converter

带有请求验证的参数转换器

v1.2 2022-10-23 21:17 UTC

This package is auto-updated.

Last update: 2024-09-24 01:07:21 UTC


README

该想法是对原始有效负载进行验证,然后将请求映射到对象。它仅在解码和规范化之间添加了验证步骤。

安装

composer require revenkroz/validating-param-converter

将服务添加到您的 services.yaml

Revenkroz\ValidatingParamConverter\Request\ParamConverter\ValidatingParamConverter:
    class: Revenkroz\ValidatingParamConverter\Request\ParamConverter\ValidatingParamConverter
    tags:
        - { name: 'request.param_converter', priority: false }

使用方法

创建一个实现 ValidatableParamInterface 的 DTO

use Revenkroz\ValidatingParamConverter\Request\ValidatableParamInterface;

class YourDto implements ValidatableParamInterface
{
    public static function getRequestConstraint(): Constraint
    {
        // ...
    }
}

在控制器方法中获取您的 DTO

public function customAction(YourDto $dto, Request $request): Response {}

要使用您的验证组验证查询,请使用 CustomGroupsValidatableParamInterface 接口代替

use Revenkroz\ValidatingParamConverter\Request\CustomGroupsValidatableParamInterface;

class YourDto implements CustomGroupsValidatableParamInterface
{
    public static function getRequestConstraint(): Constraint
    {
        // ...
    }

    public static function getRequestValidationGroups(): array
    {
        return ['one_group', 'another_group'];
    }
}