-
-
Notifications
You must be signed in to change notification settings - Fork 970
Add validation for GraphQL mutations #1604
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Core\Bridge\Symfony\Validator; | ||
|
|
||
| use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException; | ||
| use ApiPlatform\Core\Validator\ValidatorInterface; | ||
| use Psr\Container\ContainerInterface; | ||
| use Symfony\Component\Validator\Validator\ValidatorInterface as SymfonyValidatorInterface; | ||
|
|
||
| /** | ||
| * Validates an item using the Symfony validator component. | ||
| * | ||
| * @author Kévin Dunglas <dunglas@gmail.com> | ||
| */ | ||
| class Validator implements ValidatorInterface | ||
| { | ||
| private $validator; | ||
| private $container; | ||
|
|
||
| public function __construct(SymfonyValidatorInterface $validator, ContainerInterface $container = null) | ||
| { | ||
| $this->validator = $validator; | ||
| $this->container = $container; | ||
| } | ||
|
|
||
| /** | ||
| * {@inheritdoc} | ||
| */ | ||
| public function validate($data, array $context = []) | ||
| { | ||
| if (null !== $validationGroups = $context['groups'] ?? null) { | ||
| if ( | ||
| $this->container && | ||
| \is_string($validationGroups) && | ||
| $this->container->has($validationGroups) && | ||
| ($service = $this->container->get($validationGroups)) && | ||
| \is_callable($service) | ||
| ) { | ||
| $validationGroups = $service($data); | ||
| } elseif (\is_callable($validationGroups)) { | ||
| $validationGroups = $validationGroups($data); | ||
| } | ||
|
|
||
| $validationGroups = (array) $validationGroups; | ||
| } | ||
|
|
||
| $violations = $this->validator->validate($data, null, $validationGroups); | ||
| if (0 !== \count($violations)) { | ||
| throw new ValidationException($violations); | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Core\Validator\EventListener; | ||
|
|
||
| use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException; | ||
| use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; | ||
| use ApiPlatform\Core\Util\RequestAttributesExtractor; | ||
| use ApiPlatform\Core\Validator\ValidatorInterface; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
| use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; | ||
|
|
||
| /** | ||
| * Validates data. | ||
| * | ||
| * @author Kévin Dunglas <dunglas@gmail.com> | ||
| */ | ||
| final class ValidateListener | ||
| { | ||
| private $validator; | ||
| private $resourceMetadataFactory; | ||
|
|
||
| public function __construct(ValidatorInterface $validator, ResourceMetadataFactoryInterface $resourceMetadataFactory) | ||
| { | ||
| $this->validator = $validator; | ||
| $this->resourceMetadataFactory = $resourceMetadataFactory; | ||
| } | ||
|
|
||
| /** | ||
| * Validates data returned by the controller if applicable. | ||
| * | ||
| * @throws ValidationException | ||
| */ | ||
| public function onKernelView(GetResponseForControllerResultEvent $event) | ||
| { | ||
| $request = $event->getRequest(); | ||
| if ( | ||
| $request->isMethodSafe(false) | ||
| || $request->isMethod(Request::METHOD_DELETE) | ||
| || !($attributes = RequestAttributesExtractor::extractAttributes($request)) | ||
| || !$attributes['receive'] | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| $data = $event->getControllerResult(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dunglas consider validating controller argument
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure to understand, the validation occurs after the execution of the controller.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you need to validate the data before executing your logic, just inject
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's related to this: #1590 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, @alanpoulain is right. IMHO validating input data before controller execution is much logical.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing this would be a big BC break and will provide less flexibility. IMO the data must be valid before being persisted, but after the custom logic has been executed. |
||
| $resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']); | ||
|
|
||
| if (isset($attributes['collection_operation_name'])) { | ||
| $validationGroups = $resourceMetadata->getCollectionOperationAttribute($attributes['collection_operation_name'], 'validation_groups', null, true); | ||
| } else { | ||
| $validationGroups = $resourceMetadata->getItemOperationAttribute($attributes['item_operation_name'], 'validation_groups', null, true); | ||
| } | ||
|
|
||
| $this->validator->validate($data, ['groups' => $validationGroups]); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| <?php | ||
|
|
||
| /* | ||
| * This file is part of the API Platform project. | ||
| * | ||
| * (c) Kévin Dunglas <dunglas@gmail.com> | ||
| * | ||
| * For the full copyright and license information, please view the LICENSE | ||
| * file that was distributed with this source code. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace ApiPlatform\Core\Validator\Exception; | ||
|
|
||
| use ApiPlatform\Core\Exception\RuntimeException; | ||
|
|
||
| /** | ||
| * Thrown when a validation error occurs. | ||
| * | ||
| * @author Kévin Dunglas <dunglas@gmail.com> | ||
| */ | ||
| class ValidationException extends RuntimeException | ||
| { | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want to warn the user if there is no validator?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No (we would just make this property not nullable otherwise), it allows to create micro-frameworks on top of API Platform and it mimics the behavior of the
ValidateListener(which is optional).