mkoprek/request-validation-bundle
最新稳定版本:3.0.1
Composer 安装命令:
composer require mkoprek/request-validation-bundle
包简介
README 文档
README
This is a simple library for easier and cleaner handling requests. You can simply define incoming payload and validation rules with it. Also you can simply cast incoming data for example to int value.
Installation
composer require mkoprek/request-validation-bundle
Usage
You need to create class which is extending AbstractRequest, then:
- Create field you want to get from request as a class properties
- Add validation rules as a Symfony Constraints to
getValidationRules()method - Add casting variables to other types or object (ex. Uuid)
Request:
<?php declare(strict_types=1); use MKoprek\RequestValidation\Request\AbstractRequest; use Symfony\Component\Uid\Uuid; use Symfony\Component\Validator\Constraints as Assert; class UserCreateRequest extends AbstractRequest { protected string $id; protected ?string $name; public function getId(): Uuid { return Uuid::fromString($this->id); } public function getName(): ?string { return $this->name; } /** * @return array<Assert\Collection> */ public function getValidationRules(): array { return [ new Assert\Collection([ 'id' => new Assert\Required([ new Assert\NotNull(), new Assert\NotBlank(), new Assert\Uuid(), ]), 'name' => new Assert\Required([ new Assert\NotNull(), new Assert\NotBlank(), new Assert\Type('string'), ]), ]), ]; } }
Controller:
<?php declare(strict_types=1); use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Annotation\Route; #[Route('/api')] class UserCreateController { #[Route('/users', name: 'users.create', methods: 'POST')] public function post(UserCreateRequest $request): JsonResponse { $id = $request->getId(); $name = $request->getName(); $this->commandBus->handle( CreateUserCommand($request->getId(), $request->getName()) ); return new JsonResponse(['id' => $id->toRfc4122()], Response::HTTP_CREATED); } }
Validation
Validation is done automatically before request is parsed by controller. If there will be any validation error ApiValidationException is thrown.
If you want to return JSON with errors just add this to your services.yaml. It will handle ALL exceptions.
MKoprek\RequestValidation\Response\ResponseSubscriber: tags: - { name: kernel.event_subscriber, event: kernel.exception }
Example response with validation errors:
{
"status": 422,
"message": "Request validation error",
"details": [
{
"field": "[id]",
"error": "This field is missing."
},
{
"field": "[name]",
"error": "This field is missing."
},
{
"field": "[name2]",
"error": "This field was not expected."
}
]
Example with some other exception.
{
"status": 500,
"message": "Attempted to call an undefined method named 'notExists' of class 'UserCreateRequest'."
}
License
MIT
Author Information
Created by Maciej Koprek (mkoprek) 2021
统计信息
- 总下载量: 7.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-12-01