sfmok/request-input-bundle

将请求数据转换为带有验证的 DTO 输入对象。

安装次数: 7,399

依赖项: 0

建议者: 0

安全性: 0

星标: 37

关注者: 2

分支: 0

公开问题: 4

类型:symfony-bundle

v1.2.5 2023-02-26 12:59 UTC

This package is auto-updated.

Last update: 2024-09-23 21:14:06 UTC


README

CI codecov Latest Stable Version License

RequestInputBundle 将请求数据转换为带有验证的 DTO 输入对象。

  • 支持的请求数据类型:基于 Content-Type 头的 jsonxmlform
  • 解析控制器动作的输入参数。
  • 在控制器外创建 DTO 输入。
  • 验证 DTO 输入对象。
  • 全局 YAML 配置。
  • 通过每个控制器动作的输入属性进行自定义配置。

安装

使用 composer 需要这个包。

composer require sfmok/request-input-bundle

如何使用

  • 创建 DTO 输入并实现 Sfmok\RequestInput\InputInterface
use Sfmok\RequestInput\InputInterface;

class PostInput implements InputInterface
{
    #[Assert\NotBlank]
    private string $title;

    #[Assert\NotBlank]
    private string $content;

    #[Assert\NotBlank]
    private array $tags;

    #[SerializedName('author')]
    #[Assert\NotBlank]
    private string $name;
    
    # getters and setters or make properties public
}
  • 将 DTO 输入用作控制器动作的参数
class PostController
{
    # Example with global config
    #[Route(path: '/posts', name: 'create')]
    public function create(PostInput $input): Response
    {
        dd($input);
    }
    
    # Example with specific config
    #[Route(path: '/posts', name: 'create')]
    #[Input(format: 'json', groups: ['create'], context: ['groups' => ['create']])]
    public function create(PostInput $input): Response
    {
        dd($input);
    }
}

验证

  • 响应头
Content-Type: application/json; charset=utf-8
  • 响应体
{
  "type": "https://symfony.com.cn/errors/validation",
  "title": "Validation Failed",
  "detail": "title: This value should not be blank.",
  "violations": [
    {
      "propertyPath": "title",
      "title": "This value should not be blank.",
      "parameters": {
        "{{ value }}": "\"\""
      },
      "type": "urn:uuid:c1051bb4-d103-4f74-8988-acbcafc7fdc3"
    }
  ]
}

反序列化

如果请求数据包含无效语法或无效属性类型,将返回清晰的 400 json 响应。

  • 数据错误
{
  "title": 12
}
{
  "title": "Deserialization Failed",
  "detail": "Data error",
  "violations": [
    {
      "propertyPath": "title",
      "message": "This value should be of type string",
      "currentType": "int"
    }
  ]
}
  • 语法错误
{
  "title": "foo",
}
{
  "title": "Deserialization Failed",
  "detail": "Syntax error",
  "violations": []
}

配置

# config/packages/request_input.yaml
request_input:
  enabled: true # default value true
  formats: ['json'] # default value ['json', 'xml', 'form']
  skip_validation: true # default value false

您还可以使用属性输入覆盖格式,并明确指定格式。

在控制器外创建 DTO 输入

您只需注入 InputFactoryInterface,例如:

<?php

declare(strict_types=1);

namespace App\Manager;

use App\Dto\PostInput;
use Sfmok\RequestInput\InputInterface;
use Symfony\Component\HttpFoundation\Request;
use Sfmok\RequestInput\Factory\InputFactoryInterface;

class PostManager
{
    public function __construct(private InputFactoryInterface $inputFactory)
    {
    }

    public function getInput(Request $request): InputInterface
    {
        return $this->inputFactory->createFromRequest($request, PostInput::class);
    }
}

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件