revenkroz / validating-param-converter

Symfony ParamConverter with request validation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Symfony ParamConverter with request validation

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'];
    }
}

About

Symfony ParamConverter with request validation

License:MIT License


Languages

Language:PHP 100.0%