youniverse-center/request-validation-bundle
Composer 安装命令:
composer require youniverse-center/request-validation-bundle
包简介
Symfony Bundle for automatic validation of data provided in the request.
README 文档
README
- Add validator class implementing
Yc\RequestValidationBundle\RequestValidator\RequestValidatorInterface
- in the
getConstriantreturn constraints used by the validator component - in the
getGroupsreturn validation groups getInvalidRequestResponsemust return response that will be used if the validation has failed.
- Add attribute
Yc\RequestValidationBundle\Attributes\RequestValidatorto your controller
#[Route('/some/route', name: 'some_route')] #[RequestValidator(Create::class)] class CreateController extends AbstractController { public function __invoke($data) { // in the data is your validated request content } }
(This attribute can be also placed on a method if you have multple controllers in a class.)
- You probably want to receive data from the request for the validation in your specific way. For this purpose implement
Yc\RequestValidationBundle\DataReceiver\DataReceiverInterface
for example:
public function getData(Request $request): mixed { return json_decode($request->getContent(), true); }
- By default the data will be set in the request attribute
dataif you want to change this, implementYc\RequestValidationBundle\DataTransformer\DataTransformerInterface
for example:
public function transformData(mixed $data): array { $id = ProjectId::fromString($data['id']); return [ 'project' => new Project($id, $data['name']) ]; }
and then you can use it in controller like:
public function __invoke(Project $project) {}
统计信息
- 总下载量: 34
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-06-18