mkoprek/request-validation-bundle

Installs: 6 301

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 2

Forks: 2

Open Issues: 0

Type:symfony-bundle

3.0.1 2023-05-24 11:56 UTC

README

Build Status codecov

This is a simple library for easier and cleaner handling requests. You can simply define incoming payload and validation rules with it. Also you can simply cast incoming data for example to int value.

Installation

composer require mkoprek/request-validation-bundle

Usage

You need to create class which is extending AbstractRequest, then:

  • Create field you want to get from request as a class properties
  • Add validation rules as a Symfony Constraints to getValidationRules() method
  • Add casting variables to other types or object (ex. Uuid)

Request:

<?php
declare(strict_types=1);

use MKoprek\RequestValidation\Request\AbstractRequest;
use Symfony\Component\Uid\Uuid;
use Symfony\Component\Validator\Constraints as Assert;

class UserCreateRequest extends AbstractRequest
{
    protected string $id;
    protected ?string $name;

    public function getId(): Uuid
    {
        return Uuid::fromString($this->id);
    }
    
    public function getName(): ?string
    {
        return $this->name;
    }

    /**
     * @return array<Assert\Collection>
     */
    public function getValidationRules(): array
    {
        return [
            new Assert\Collection([
                'id' => new Assert\Required([
                    new Assert\NotNull(),
                    new Assert\NotBlank(),
                    new Assert\Uuid(),
                ]),
                'name' => new Assert\Required([
                    new Assert\NotNull(),
                    new Assert\NotBlank(),
                    new Assert\Type('string'),
                ]),
            ]),
        ];
    }
}

Controller:

<?php
declare(strict_types=1);

use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/api')]
class UserCreateController
{
    #[Route('/users', name: 'users.create', methods: 'POST')]
    public function post(UserCreateRequest $request): JsonResponse
    {
        $id = $request->getId();
        $name = $request->getName();

        $this->commandBus->handle(
            CreateUserCommand($request->getId(), $request->getName())
        );

        return new JsonResponse(['id' => $id->toRfc4122()], Response::HTTP_CREATED);
    }
}

Validation

Validation is done automatically before request is parsed by controller. If there will be any validation error ApiValidationException is thrown. If you want to return JSON with errors just add this to your services.yaml. It will handle ALL exceptions.

  MKoprek\RequestValidation\Response\ResponseSubscriber:
    tags:
      - { name: kernel.event_subscriber, event: kernel.exception }

Example response with validation errors:

{
    "status": 422,
    "message": "Request validation error",
    "details": [
        {
            "field": "[id]",
            "error": "This field is missing."
        },
        {
            "field": "[name]",
            "error": "This field is missing."
        },
        {
            "field": "[name2]",
            "error": "This field was not expected."
        }
    ]

Example with some other exception.

{
"status": 500,
"message": "Attempted to call an undefined method named 'notExists' of class 'UserCreateRequest'."
}

License

MIT

Author Information

Created by Maciej Koprek (mkoprek) 2021