https://github.com/dimkinthepro/http-bundle
Request validation bundle for Symfony
https://github.com/dimkinthepro/http-bundle
ddd php82 request-validation symfony-bundle
Last synced: about 1 month ago
JSON representation
Request validation bundle for Symfony
- Host: GitHub
- URL: https://github.com/dimkinthepro/http-bundle
- Owner: dimkinthepro
- Created: 2023-09-10T15:57:18.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2024-09-04T16:27:26.000Z (over 1 year ago)
- Last Synced: 2025-03-08T03:47:56.082Z (about 1 year ago)
- Topics: ddd, php82, request-validation, symfony-bundle
- Language: PHP
- Homepage:
- Size: 27.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.MD
Awesome Lists containing this project
README
# Request validation bundle for Symfony
### 1. Installation:
```bash
composer require dimkinthepro/http-bundle
```
### 2. Check bundles config:
```php
# config/bundles.php
return [
#...
Dimkinthepro\Http\DimkintheproHttpBundle::class => ['all' => true],
];
```
### 3. Create bundle configuration:
```yaml
# config/packages/dimkinthepro_http.yaml
dimkinthepro_http:
request_validation_enabled: true
extra_fields_allowed: true
handle_validation_errors: true
response_error_format: json
```
### 4. Check validator config:
```yaml
# config/packages/validator.yaml
framework:
#...
validation:
#...
mapping:
paths:
#...
- '%kernel.project_dir%/config/validator/'
```
### 5. Create a controller DTO:
```php
# src/App/DTO/RequestDTO.php
namespace App\DTO;
use Dimkinthepro\Http\Domain\DTO\ValidatedDTOInterface;
class RequestDTO implements ValidatedDTOInterface
{
public string $method;
public int $parameter;
public \DateTimeImmutable $date;
public App\Enum\FooEnum $enum;
}
```
### 6. Create a validation config for DTO:
```yaml
# config/validator/RequestDTO.yaml
App\DTO\RequestDTO:
properties:
method:
- NotBlank: ~
parameter:
- NotBlank: ~
- GreaterThan: 0
- LessThanOrEqual: 255
date:
- NotBlank: ~
enum:
- NotBlank: ~
```
### 7. Use validated DTO in your controller:
```php
# src/App/Controller/Controller.php
namespace App\Controller;
use App\DTO\RequestDTO;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
class Controller extends AbstractController
{
public function __invoke(RequestDTO $DTO): JsonResponse
{
return new JsonResponse([
'method' => $DTO->method,
'parameter' => $DTO->parameter,
'date' => $DTO->date,
'enum' => $DTO->enum->value,
]);
}
}
```