Symfony 4 中 Rest Api 的验证

Validation for Rest Api in Symfony 4

我要为我的项目编写 REST API。我正在使用 symfony 4。我看到了几个例子,但没有一个适合我。

  1. 使用表单对象进行验证。它对我不起作用,因为它是 API,没有表格。我不想为了支持此功能而编写虚拟 类。
  2. 在这个页面 https://symfony.com/doc/current/validation.html 他们建议了 4 种方式:注释、yml、xml、php。这个解决方案不适合我,因为这个验证与实体有关,API - 模式更宽:它有限制、偏移、过滤器和其他不属于实体的字段。

所以,我想我需要编写验证器,它对所有可能的字段都有一组约束。我只是不知道呈现这个的最佳方式是什么。你见过类似的东西吗?

P.S。在写这篇文章之前 post 我使用了 Whosebug 搜索。我没有找到有用的答案。

看看你的例子 (example.com/api/categories?limit=20&offset=300&filter=something) 我想你的动作应该是这样的:

public function getCategories(?int $limit, ?int $offset, ?string $filter)
{
    //...
}

集合验证

您可以将约束定义为一个数组(然后将其抽象为自己的 class),并将其作为第二个参数传递给您的验证器。

$constraint = new Assert\Collection([
    'limit' => [
        new Assert\Range(['min' => 0, 'max' => 999]),
        new Assert\DivisibleBy(0.5)
    ],
    'offset' => new Assert\Range(['min' => 0, 'max' => 999]),
    'filter' => new Assert\Regex("/^\w+/")
]);

$validationResult = $this->validator->validate(
    ['limit' => $limit, 'offset' => $offset, 'filter' => $filter],
    $constraint
);

文档 link.

一一验证

将约束作为第二个参数传递给验证器,用于要验证的每个参数。

$offsetValidationResult = $this->validator->validate(
    $offset,
    new Assert\Range(['min' => 0, 'max' => 999])
);
//...

文档 link.

对象验证

创建一个包含 3 个字段的 class。

class FilterParameters
{
    public function __construct($limit, $offset, $filter)
    {
        $this->limit = $limit;
        $this->offset = $offset;
        $this->filter = $filter;
    }

    // No getters/setters for brevity
    /**
     * @Assert\DivisibleBy(0.25)
     */
    public $limit;
    /**
     * @Assert\Range(min = 0, max = 999)
     */
    public $offset;
    /**
     * @Assert\Regex("/^\w+/")
     */
    public $filter;
}

实例化并验证它。

$validationResult = $this->validator->validate(
    new FilterParameters($limit, $offset, $filter)
);

文档 link.

我认为像往常一样使用表格是非常干净和漂亮的。 https://codereviewvideos.com/course/beginners-guide-back-end-json-api-front-end-2018/video/validating-json-data-symfony

我选择这个 api,因为它是我测试中最快的。 你不必购买课程(但如果你喜欢代码,你可以),只需按照本系列中的 "raw symfony 4" 文章(你也不需要 behat 部分)

"Limit"、"offset" 和 "filter" 功能属于您的存储库。与您将此处的 id 传递到存储库的方式相同

/**
 * Class AlbumController
 * @package App\Controller
 */
class AlbumController extends AbstractController
{
    // ....

    /**
     * @Route(
     *     path         = "/api/album/{id}",
     *     name         = "get_album",
     *     methods      = {"GET"},
     *     requirements = {"id"="\d+"}
     * )
     * @param int $id
     *
     * @return JsonResponse
     */
    public function get($id)
    {
        return new JsonResponse($this->findAlbumById($id), JsonResponse::HTTP_OK);
    }  

    /**
     * @param $id
     *
     * @return Album|null
     * @throws NotFoundHttpException
     */
    private function findAlbumById($id)
    {
        $album = $this->albumRepository->find($id);

        if ($album === null) {
            throw new NotFoundHttpException();
        }

        return $album;
    }