https://github.com/sfmok/request-input-bundle
RequestInputBundle converts request data into DTO inputs objects with validation.
https://github.com/sfmok/request-input-bundle
dto dto-input input input-validation inputs request-dto
Last synced: about 1 month ago
JSON representation
RequestInputBundle converts request data into DTO inputs objects with validation.
- Host: GitHub
- URL: https://github.com/sfmok/request-input-bundle
- Owner: sfmok
- License: mit
- Created: 2022-11-06T10:05:44.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-10-03T11:22:47.000Z (over 1 year ago)
- Last Synced: 2025-11-10T03:26:55.201Z (3 months ago)
- Topics: dto, dto-input, input, input-validation, inputs, request-dto
- Language: PHP
- Homepage:
- Size: 74.2 KB
- Stars: 35
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
## RequestInputBundle
[](https://github.com/sfmok/request-input/actions/workflows/ci.yml)
[](https://codecov.io/github/sfmok/request-input-bundle)
[](https://packagist.org/packages/sfmok/request-input-bundle)
[](https://packagist.org/packages/sfmok/request-input-bundle)
**RequestInputBundle** converts request data into DTO inputs objects with validation.
- Request data supported: `json`, `xml` and `form` based on `Content-Type` header.
- Resolve inputs arguments for controllers actions.
- Create DTO inputs outside controllers
- Validate DTO inputs objects.
- Global YAML configuration.
- Custom Configuration via Input Attribute per controller action.
### Installation
Require the bundle with composer:
```bash
composer require sfmok/request-input-bundle
```
### How to use
- Create DTO input and implements `Sfmok\RequestInput\InputInterface`
```php
use Sfmok\RequestInput\InputInterface;
class PostInput implements InputInterface
{
#[Assert\NotBlank]
private string $title;
#[Assert\NotBlank]
private string $content;
#[Assert\NotBlank]
private array $tags;
#[SerializedName('author')]
#[Assert\NotBlank]
private string $name;
# getters and setters or make properties public
}
```
- Use DTO input in your controller action as an argument:
```php
class PostController
{
# Example with global config
#[Route(path: '/posts', name: 'create')]
public function create(PostInput $input): Response
{
dd($input);
}
# Example with specific config
#[Route(path: '/posts', name: 'create')]
#[Input(format: 'json', groups: ['create'], context: ['groups' => ['create']])]
public function create(PostInput $input): Response
{
dd($input);
}
}
```
### Validations
- Response header
```
Content-Type: application/json; charset=utf-8
```
- Response body
```json
{
"type": "https://symfony.com/errors/validation",
"title": "Validation Failed",
"detail": "title: This value should not be blank.",
"violations": [
{
"propertyPath": "title",
"title": "This value should not be blank.",
"parameters": {
"{{ value }}": "\"\""
},
"type": "urn:uuid:c1051bb4-d103-4f74-8988-acbcafc7fdc3"
}
]
}
```
### Deserialization
Whether the request data contains invalid syntax or invalid attributes types a clear 400 json response will return:
- Data Error
```json
{
"title": 12
}
```
```json
{
"title": "Deserialization Failed",
"detail": "Data error",
"violations": [
{
"propertyPath": "title",
"message": "This value should be of type string",
"currentType": "int"
}
]
}
```
- Syntax error:
```json
{
"title": "foo",
}
```
```json
{
"title": "Deserialization Failed",
"detail": "Syntax error",
"violations": []
}
```
### Configuration
```yaml
# config/packages/request_input.yaml
request_input:
enabled: true # default value true
formats: ['json'] # default value ['json', 'xml', 'form']
skip_validation: true # default value false
```
You can also override the format using attribute input and specify the format explicitly.
### Create DTO input outside controller
You just need to inject `InputFactoryInterface` e.g:
```php
inputFactory->createFromRequest($request, PostInput::class);
}
}
```
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.