vendor/overblog/graphql-bundle/src/Validator/Formatter.php line 19

  1. <?php
  2. declare(strict_types=1);
  3. namespace Overblog\GraphQLBundle\Validator;
  4. use Overblog\GraphQLBundle\Event\ErrorFormattingEvent;
  5. use Overblog\GraphQLBundle\Validator\Exception\ArgumentsValidationException;
  6. /**
  7.  * Class Formatter.
  8.  *
  9.  * Adds validation errors to the response.
  10.  *
  11.  * @see https://github.com/overblog/GraphQLBundle/blob/master/docs/validation/index.md#error-messages
  12.  */
  13. class Formatter
  14. {
  15.     public function onErrorFormatting(ErrorFormattingEvent $event): void
  16.     {
  17.         $error $event->getError()->getPrevious();
  18.         if ($error instanceof ArgumentsValidationException) {
  19.             $validation = [];
  20.             foreach ($error->getViolations() as $violation) {
  21.                 $validation[$violation->getPropertyPath()][] = [
  22.                     'message' => $violation->getMessage(),
  23.                     'code' => $violation->getCode(),
  24.                 ];
  25.             }
  26.             $formattedError $event->getFormattedError();
  27.             $formattedError['extensions']['validation'] = $validation;
  28.         }
  29.     }
  30. }