youniverse-center/request-validation-bundle

Symfony Bundle for automatic validation of data provided in the request.

0.2.1 2021-06-20 14:17 UTC

This package is auto-updated.

Last update: 2024-10-20 21:23:13 UTC


README

  1. Add validator class implementing Yc\RequestValidationBundle\RequestValidator\RequestValidatorInterface
  • in the getConstriant return constraints used by the validator component
  • in the getGroups return validation groups
  • getInvalidRequestResponse must return response that will be used if the validation has failed.
  1. Add attribute Yc\RequestValidationBundle\Attributes\RequestValidator to your controller
#[Route('/some/route', name: 'some_route')]
#[RequestValidator(Create::class)]
class CreateController extends AbstractController
{
  public function __invoke($data)
  {
    // in the data is your validated request content
  }
}

(This attribute can be also placed on a method if you have multple controllers in a class.)

  1. You probably want to receive data from the request for the validation in your specific way. For this purpose implement Yc\RequestValidationBundle\DataReceiver\DataReceiverInterface

for example:

public function getData(Request $request): mixed
{
    return json_decode($request->getContent(), true);
}
  1. By default the data will be set in the request attribute data if you want to change this, implement Yc\RequestValidationBundle\DataTransformer\DataTransformerInterface

for example:

public function transformData(mixed $data): array
{
    $id = ProjectId::fromString($data['id']);

    return [
        'project' => new Project($id, $data['name'])
    ];
}

and then you can use it in controller like:

public function __invoke(Project $project) {}