Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add oneOf property schema restriction
- Loading branch information
1 parent
721e66c
commit 0e629fa
Showing
7 changed files
with
235 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...Bridge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaOneOfRestriction.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
<?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\Metadata\Property\Restriction; | ||
|
||
use ApiPlatform\Core\Metadata\Property\PropertyMetadata; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Constraints\AtLeastOneOf; | ||
|
||
/** | ||
* @author Alan Poulain <contact@alanpoulain.eu> | ||
*/ | ||
final class PropertySchemaOneOfRestriction implements PropertySchemaRestrictionMetadataInterface | ||
{ | ||
/** | ||
* @var iterable<PropertySchemaRestrictionMetadataInterface> | ||
*/ | ||
private $restrictionsMetadata; | ||
|
||
/** | ||
* @param iterable<PropertySchemaRestrictionMetadataInterface> $restrictionsMetadata | ||
*/ | ||
public function __construct(iterable $restrictionsMetadata = []) | ||
{ | ||
$this->restrictionsMetadata = $restrictionsMetadata; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
* | ||
* @param AtLeastOneOf $constraint | ||
*/ | ||
public function create(Constraint $constraint, PropertyMetadata $propertyMetadata): array | ||
{ | ||
$oneOfConstraints = $constraint->getNestedContraints(); | ||
$oneOfRestrictions = []; | ||
|
||
foreach ($oneOfConstraints as $oneOfConstraint) { | ||
foreach ($this->restrictionsMetadata as $restrictionMetadata) { | ||
if ($restrictionMetadata->supports($oneOfConstraint, $propertyMetadata)) { | ||
$oneOfRestrictions[] = $restrictionMetadata->create($oneOfConstraint, $propertyMetadata); | ||
} | ||
} | ||
} | ||
|
||
if (!empty($oneOfRestrictions)) { | ||
return ['oneOf' => $oneOfRestrictions]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function supports(Constraint $constraint, PropertyMetadata $propertyMetadata): bool | ||
{ | ||
if (!($constraint instanceof AtLeastOneOf)) { | ||
return false; | ||
} | ||
|
||
foreach ($constraint->getNestedContraints() as $nestedContraint) { | ||
foreach ($this->restrictionsMetadata as $restrictionMetadata) { | ||
if ($restrictionMetadata->supports($nestedContraint, $propertyMetadata)) { | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
...ge/Symfony/Validator/Metadata/Property/Restriction/PropertySchemaOneOfRestrictionTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ApiPlatform\Core\Tests\Bridge\Symfony\Validator\Metadata\Property\Restriction; | ||
|
||
use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaLengthRestriction; | ||
use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaOneOfRestriction; | ||
use ApiPlatform\Core\Bridge\Symfony\Validator\Metadata\Property\Restriction\PropertySchemaRegexRestriction; | ||
use ApiPlatform\Core\Metadata\Property\PropertyMetadata; | ||
use ApiPlatform\Core\Tests\ProphecyTrait; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\PropertyInfo\Type; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\Constraints\AtLeastOneOf; | ||
use Symfony\Component\Validator\Constraints\Length; | ||
use Symfony\Component\Validator\Constraints\Positive; | ||
|
||
/** | ||
* @author Alan Poulain <contact@alanpoulain.eu> | ||
*/ | ||
final class PropertySchemaOneOfRestrictionTest extends TestCase | ||
{ | ||
use ProphecyTrait; | ||
|
||
private $propertySchemaOneOfRestriction; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->propertySchemaOneOfRestriction = new PropertySchemaOneOfRestriction([ | ||
new PropertySchemaLengthRestriction(), | ||
new PropertySchemaRegexRestriction(), | ||
]); | ||
} | ||
|
||
/** | ||
* @dataProvider supportsProvider | ||
*/ | ||
public function testSupports(Constraint $constraint, PropertyMetadata $propertyMetadata, bool $expectedResult): void | ||
{ | ||
if (!class_exists(AtLeastOneOf::class)) { | ||
self::markTestSkipped(); | ||
} | ||
|
||
self::assertSame($expectedResult, $this->propertySchemaOneOfRestriction->supports($constraint, $propertyMetadata)); | ||
} | ||
|
||
public function supportsProvider(): \Generator | ||
{ | ||
yield 'empty' => [new AtLeastOneOf(['constraints' => []]), new PropertyMetadata(), false]; | ||
|
||
yield 'not supported constraints' => [new AtLeastOneOf(['constraints' => [new Positive(), new Length(['min' => 3])]]), new PropertyMetadata(), false]; | ||
|
||
yield 'one supported constraint' => [new AtLeastOneOf(['constraints' => [new Positive(), new Length(['min' => 3])]]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING)), true]; | ||
} | ||
|
||
/** | ||
* @dataProvider createProvider | ||
*/ | ||
public function testCreate(Constraint $constraint, PropertyMetadata $propertyMetadata, array $expectedResult): void | ||
{ | ||
if (!class_exists(AtLeastOneOf::class)) { | ||
self::markTestSkipped(); | ||
} | ||
|
||
self::assertSame($expectedResult, $this->propertySchemaOneOfRestriction->create($constraint, $propertyMetadata)); | ||
} | ||
|
||
public function createProvider(): \Generator | ||
{ | ||
yield 'empty' => [new AtLeastOneOf(['constraints' => []]), new PropertyMetadata(), []]; | ||
|
||
yield 'not supported constraints' => [new AtLeastOneOf(['constraints' => [new Positive(), new Length(['min' => 3])]]), new PropertyMetadata(), []]; | ||
|
||
yield 'one supported constraint' => [new AtLeastOneOf(['constraints' => [new Positive(), new Length(['min' => 3])]]), new PropertyMetadata(new Type(Type::BUILTIN_TYPE_STRING)), [ | ||
'oneOf' => [['minLength' => 3]], | ||
]]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?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\Tests\Fixtures; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
class DummyAtLeastOneOfValidatedEntity | ||
{ | ||
/** | ||
* @var string | ||
* | ||
* @Assert\AtLeastOneOf({ | ||
* @Assert\Regex("/#/"), | ||
* @Assert\Length(min=10) | ||
* }) | ||
*/ | ||
public $dummy; | ||
} |