revenkroz / validating-param-converter
带有请求验证的参数转换器
v1.2
2022-10-23 21:17 UTC
Requires
- php: ^8.0
- sensio/framework-extra-bundle: ^6
- symfony/http-kernel: ^5.3|^6.0
- symfony/property-access: ^5.3|^6.0
- symfony/serializer: ^5.3|^6.0
- symfony/validator: ^5.3|^6.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpunit/phpunit: ^9.5
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']; } }