Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/revenkroz/validating-param-converter
Symfony ParamConverter with request validation
https://github.com/revenkroz/validating-param-converter
php symfony symfony-bundle validation
Last synced: 16 days ago
JSON representation
Symfony ParamConverter with request validation
- Host: GitHub
- URL: https://github.com/revenkroz/validating-param-converter
- Owner: revenkroz
- License: mit
- Created: 2021-09-02T18:56:00.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2022-10-23T21:18:19.000Z (about 2 years ago)
- Last Synced: 2024-10-17T15:49:18.400Z (about 1 month ago)
- Topics: php, symfony, symfony-bundle, validation
- Language: PHP
- Homepage:
- Size: 10.7 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
```shell
composer require revenkroz/validating-param-converter
```Add service to your `services.yaml`:
```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`:
```php
use Revenkroz\ValidatingParamConverter\Request\ValidatableParamInterface;class YourDto implements ValidatableParamInterface
{
public static function getRequestConstraint(): Constraint
{
// ...
}
}
```Get your DTO in controller method:
```php
public function customAction(YourDto $dto, Request $request): Response {}
```To validate a query using your validation groups, use the `CustomGroupsValidatableParamInterface` interface instead:
```php
use Revenkroz\ValidatingParamConverter\Request\CustomGroupsValidatableParamInterface;class YourDto implements CustomGroupsValidatableParamInterface
{
public static function getRequestConstraint(): Constraint
{
// ...
}public static function getRequestValidationGroups(): array
{
return ['one_group', 'another_group'];
}
}
```