revenkroz/validating-param-converter

Param Converter with request validation

v1.2 2022-10-23 21:17 UTC

This package is auto-updated.

Last update: 2025-06-24 02:48:56 UTC


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