revenkroz / validating-param-converter
Param Converter with request validation
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
The idea is to validate a raw payload and then map the request to object. It just adds the validation step between decoding and denormalization.
Installation
composer require revenkroz/validating-param-converter
Add service to your services.yaml
:
Revenkroz\ValidatingParamConverter\Request\ParamConverter\ValidatingParamConverter: class: Revenkroz\ValidatingParamConverter\Request\ParamConverter\ValidatingParamConverter tags: - { name: 'request.param_converter', priority: false }
Usage
Create a DTO that implements ValidatableParamInterface
:
use Revenkroz\ValidatingParamConverter\Request\ValidatableParamInterface; class YourDto implements ValidatableParamInterface { public static function getRequestConstraint(): Constraint { // ... } }
Get your DTO in controller method:
public function customAction(YourDto $dto, Request $request): Response {}
To validate a query using your validation groups, use the CustomGroupsValidatableParamInterface
interface instead:
use Revenkroz\ValidatingParamConverter\Request\CustomGroupsValidatableParamInterface; class YourDto implements CustomGroupsValidatableParamInterface { public static function getRequestConstraint(): Constraint { // ... } public static function getRequestValidationGroups(): array { return ['one_group', 'another_group']; } }