Skip to content

Commit

Permalink
Merge c3fb44a into 6f88fb4
Browse files Browse the repository at this point in the history
  • Loading branch information
mtarld committed Mar 4, 2021
2 parents 6f88fb4 + c3fb44a commit c48911e
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,7 @@

## 2.7.0

* Validator: Add custom constraint set
* **BC**: Change `api_platform.listener.request.add_format` priority from 7 to 28 to execute it before firewall (priority 8) (#3599)
* Doctrine: Better exception to find which resource is linked to an exception (#3965)
* Doctrine: Allow mixed type value for date filter (notice if invalid) (#3870)
Expand Down
1 change: 1 addition & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/validator.xml
Expand Up @@ -10,6 +10,7 @@
<argument type="service" id="service_container" />
</service>
<service id="ApiPlatform\Core\Validator\ValidatorInterface" alias="api_platform.validator" />
<service id="ApiPlatform\Core\Validator\ConstraintValidatorInterface" alias="api_platform.validator" />

<service id="api_platform.metadata.property.metadata_factory.validator" class="ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\ValidatorPropertyMetadataFactory" decorates="api_platform.metadata.property.metadata_factory" decoration-priority="20" public="false">
<argument type="service" id="validator" />
Expand Down
8 changes: 4 additions & 4 deletions src/Bridge/Symfony/Validator/Validator.php
Expand Up @@ -14,7 +14,7 @@
namespace ApiPlatform\Core\Bridge\Symfony\Validator;

use ApiPlatform\Core\Bridge\Symfony\Validator\Exception\ValidationException;
use ApiPlatform\Core\Validator\ValidatorInterface;
use ApiPlatform\Core\Validator\ConstraintValidatorInterface;
use Psr\Container\ContainerInterface;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Validator\ValidatorInterface as SymfonyValidatorInterface;
Expand All @@ -26,7 +26,7 @@
*
* @final
*/
class Validator implements ValidatorInterface
class Validator implements ConstraintValidatorInterface
{
private $validator;
private $container;
Expand All @@ -40,7 +40,7 @@ public function __construct(SymfonyValidatorInterface $validator, ContainerInter
/**
* {@inheritdoc}
*/
public function validate($data, array $context = [])
public function validate($data, array $context = [], ?array $constraints = null)
{
if (null !== $validationGroups = $context['groups'] ?? null) {
if (
Expand All @@ -64,7 +64,7 @@ public function validate($data, array $context = [])
}
}

$violations = $this->validator->validate($data, null, $validationGroups);
$violations = $this->validator->validate($data, $constraints, $validationGroups);
if (0 !== \count($violations)) {
throw new ValidationException($violations);
}
Expand Down
34 changes: 34 additions & 0 deletions src/Validator/ConstraintValidatorInterface.php
@@ -0,0 +1,34 @@
<?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;

use ApiPlatform\Core\Validator\Exception\ValidationException;
use Symfony\Component\Validator\Constraint;

/**
* Validates an item.
*
* @author Mathias Arlaud <mathias.arlaud@gmail.com>
*/
interface ConstraintValidatorInterface extends ValidatorInterface
{
/**
* Validates an item with an optional set of constraints.
*
* @param Constraint[]|null $constraints
*
* @throws ValidationException
*/
public function validate($data, array $context = [], ?array $constraints = null);
}
Expand Up @@ -88,6 +88,7 @@
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface;
use ApiPlatform\Core\Tests\Fixtures\TestBundle\TestBundle;
use ApiPlatform\Core\Tests\ProphecyTrait;
use ApiPlatform\Core\Validator\ConstraintValidatorInterface;
use ApiPlatform\Core\Validator\ValidatorInterface;
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
use Doctrine\ORM\OptimisticLockException;
Expand Down Expand Up @@ -1384,6 +1385,7 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
PaginationExtension::class => 'api_platform.doctrine.orm.query_extension.pagination',
OrderExtension::class => 'api_platform.doctrine.orm.query_extension.order',
ValidatorInterface::class => 'api_platform.validator',
ConstraintValidatorInterface::class => 'api_platform.validator',
SearchFilter::class => 'api_platform.doctrine.orm.search_filter',
OrderFilter::class => 'api_platform.doctrine.orm.order_filter',
RangeFilter::class => 'api_platform.doctrine.orm.range_filter',
Expand Down
20 changes: 20 additions & 0 deletions tests/Bridge/Symfony/Validator/ValidatorTest.php
Expand Up @@ -20,6 +20,7 @@
use ApiPlatform\Core\Tests\ProphecyTrait;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface as SymfonyValidatorInterface;

Expand Down Expand Up @@ -142,4 +143,23 @@ public function testValidatorWithScalarGroup()
$validator = new Validator($symfonyValidator, $containerProphecy->reveal());
$validator->validate(new DummyEntity(), ['groups' => 'foo']);
}

public function testValidateCustomConstraints()
{
$this->expectException(ValidationException::class);

$data = new DummyEntity();

$constraintViolationListProphecy = $this->prophesize(ConstraintViolationListInterface::class);
$constraintViolationListProphecy->rewind()->shouldBeCalled();
$constraintViolationListProphecy->valid()->shouldBeCalled();
$constraintViolationListProphecy->count()->willReturn(1)->shouldBeCalled();

$symfonyValidatorProphecy = $this->prophesize(SymfonyValidatorInterface::class);
$symfonyValidatorProphecy->validate($data, [new NotBlank()], null)->willReturn($constraintViolationListProphecy->reveal())->shouldBeCalled();
$symfonyValidator = $symfonyValidatorProphecy->reveal();

$validator = new Validator($symfonyValidator);
$validator->validate($data, [], [new NotBlank()]);
}
}

0 comments on commit c48911e

Please sign in to comment.