Skip to content

sfmok/request-input-bundle

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RequestInputBundle

Test Latest Stable Version License

RequestInputBundle converts request data into DTO inputs objects with validation.

  • Request data supported: json, xml and form based on header content type.
  • Resolve inputs arguments for controllers actions.
  • Validate DTO inputs objects (An option to enable or disable it).
  • Global YAML configuration
  • Specific Attribute configuration per action

Installation

Require the bundle with composer:

composer require sfmok/request-input-bundle

How to use

  • Create DTO input and implements Sfmok\RequestInput\InputInterface
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:
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/problem+json; charset=utf-8
  • Response body
{
  "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"
    }
  ]
}

Configuration

  • In case you want to serve a specific input data format for all your DTO inputs with skip validation:
# 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
  • With above configuration RequestInputBundle will convert JSON request data only and skip validation process.
  • You can also use a format even if disabled globally by using attribute input and specify the format explicitly.

License

The MIT License (MIT). Please see License File for more information.

About

RequestInputBundle converts request data into DTO inputs objects with validation.

Topics

Resources

License

Stars

Watchers

Forks

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy