I have configured ApiPlatform for CQRS using Symfony messenger, as described here: https://api-platform.com/docs/core/messenger/#using-messenger-with-an-input-object . My entity looks similar to:
#[
ORM\Entity(),
ORM\Table(name: '`presents`'),
ApiResource(
collectionOperations: [
'get',
'post' => [
'messenger' => 'input',
'input' => AddPresentRequest::class,
],
],
itemOperations: ['get'],
),
]
class Present
{
// ...
I've then added some symfony constraints to the DTO object:
use Symfony\Component\Validator\Constraints as Assert;
final class AddPresentRequest
{
#[Assert\NotBlank]
#[Assert\Length(
max: 180
)]
private string $name;
// ...
Now ApiPlatform automatically validates the request entity before passing it to the message bus.
I want it to not do that.
The reason is that while validation works great for API requests, it works only for API requests. I want the validation to also run when doing this:
/** @var MessageBusInterface $messageBus */
$messageBus->dispatch(new AddPresentRequest($name)); // api-platform doesn't trigger validation
Adding validation to the message bus is easy, all I need is to enable ValidationMiddleware as described in this symfony-docs issue. With that, the messenger always triggers the validation.
However, this means that for API requests, the validator runs twice—once triggered by ApiPlatform, and then again by ValidationMiddleware. This is why I need to tell ApiPlatform to chill and that I got this.
I could not find anything in ApiPlatform docs about how to disable automatic validation. The docs mention that I can replace the validator with something custom, which I thought might give a clue, but unfortunately there is no actual information about how to go about replacing it.
One workaround that I have found is that I can set a bogus validation groups on the entity: validationGroups: ['non-existent']. This is imperfect though, because while the constraints are not checked, the validator still runs and parses the entity twice.
How can I disable ApiPlatform's automatic validation, so that I can handle the validation elsewhere?