-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #34334 [Validator] Allow to define a reusable set of constrai…
…nts (ogizanagi) This PR was squashed before being merged into the 5.1-dev branch (closes #34334). Discussion ---------- [Validator] Allow to define a reusable set of constraints | Q | A | ------------- | --- | Branch? | 5.1 <!-- see below --> | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | N/A <!-- prefix each issue number with "Fix #", if any --> | License | MIT | Doc PR | TODO The goal of this feature is to simplify writing a set of validation constraints to be reused consistently across the application. Which is especially useful with DTOs, as a same set of constraints can be used in different places. For instance, given multiple DTOs containing the new user password in for different use-cases (register, forgot pwd, change pwd), the same rules apply on the property. Hence with this PR, you can write a single constraint class to be reused: ```php /** * @annotation */ class MatchesPasswordRequirements extends Compound { protected function getConstraints(array $options): array { return [ new NotBlank(), new Type('string'), new Length(['min' => 12]), new NotCompromisedPassword(), ]; } } ``` I'm open to better naming and ways to expose the options to the `Compound::getConstraints` method, so options can be forwarded to the nested constraints for most specific use-cases. Commits ------- 8f1b0df [Validator] Allow to define a reusable set of constraints
- Loading branch information
Showing
7 changed files
with
225 additions
and
2 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
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,52 @@ | |||
<?php | |||
|
|||
/* | |||
* This file is part of the Symfony package. | |||
* | |||
* (c) Fabien Potencier <fabien@symfony.com> | |||
* | |||
* For the full copyright and license information, please view the LICENSE | |||
* file that was distributed with this source code. | |||
*/ | |||
|
|||
namespace Symfony\Component\Validator\Constraints; | |||
|
|||
use Symfony\Component\Validator\Constraint; | |||
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | |||
|
|||
/** | |||
* Extend this class to create a reusable set of constraints. | |||
* | |||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | |||
*/ | |||
abstract class Compound extends Composite | |||
{ | |||
/** @var Constraint[] */ | |||
public $constraints = []; | |||
|
|||
public function __construct($options = null) | |||
{ | |||
if (isset($options[$this->getCompositeOption()])) { | |||
throw new ConstraintDefinitionException(sprintf('You can\'t redefine the "%s" option. Use the %s::getConstraints() method instead.', $this->getCompositeOption(), __CLASS__)); | |||
} | |||
|
|||
$this->constraints = $this->getConstraints($this->normalizeOptions($options)); | |||
|
|||
parent::__construct($options); | |||
} | |||
|
|||
final protected function getCompositeOption() | |||
{ | |||
return 'constraints'; | |||
} | |||
|
|||
final public function validatedBy() | |||
{ | |||
return CompoundValidator::class; | |||
} | |||
|
|||
/** | |||
* @return Constraint[] | |||
*/ | |||
abstract protected function getConstraints(array $options): array; | |||
} |
35 changes: 35 additions & 0 deletions
35
src/Symfony/Component/Validator/Constraints/CompoundValidator.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,35 @@ | |||
<?php | |||
|
|||
/* | |||
* This file is part of the Symfony package. | |||
* | |||
* (c) Fabien Potencier <fabien@symfony.com> | |||
* | |||
* For the full copyright and license information, please view the LICENSE | |||
* file that was distributed with this source code. | |||
*/ | |||
|
|||
namespace Symfony\Component\Validator\Constraints; | |||
|
|||
use Symfony\Component\Validator\Constraint; | |||
use Symfony\Component\Validator\ConstraintValidator; | |||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | |||
|
|||
/** | |||
* @author Maxime Steinhausser <maxime.steinhausser@gmail.com> | |||
*/ | |||
class CompoundValidator extends ConstraintValidator | |||
{ | |||
public function validate($value, Constraint $constraint) | |||
{ | |||
if (!$constraint instanceof Compound) { | |||
throw new UnexpectedTypeException($constraint, Compound::class); | |||
} | |||
|
|||
$context = $this->context; | |||
|
|||
$validator = $context->getValidator()->inContext($context); | |||
|
|||
$validator->validate($value, $constraint->constraints); | |||
} | |||
} |
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
60 changes: 60 additions & 0 deletions
60
src/Symfony/Component/Validator/Tests/Constraints/CompoundTest.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,60 @@ | |||
<?php | |||
|
|||
/* | |||
* This file is part of the Symfony package. | |||
* | |||
* (c) Fabien Potencier <fabien@symfony.com> | |||
* | |||
* For the full copyright and license information, please view the LICENSE | |||
* file that was distributed with this source code. | |||
*/ | |||
|
|||
namespace Symfony\Component\Validator\Tests\Constraints; | |||
|
|||
use PHPUnit\Framework\TestCase; | |||
use Symfony\Component\Validator\Constraints\Compound; | |||
use Symfony\Component\Validator\Constraints\Length; | |||
use Symfony\Component\Validator\Constraints\NotBlank; | |||
use Symfony\Component\Validator\Exception\ConstraintDefinitionException; | |||
|
|||
class CompoundTest extends TestCase | |||
{ | |||
public function testItCannotRedefineConstraintsOption() | |||
{ | |||
$this->expectException(ConstraintDefinitionException::class); | |||
$this->expectExceptionMessage('You can\'t redefine the "constraints" option. Use the Symfony\Component\Validator\Constraints\Compound::getConstraints() method instead.'); | |||
new EmptyCompound(['constraints' => [new NotBlank()]]); | |||
} | |||
|
|||
public function testCanDependOnNormalizedOptions() | |||
{ | |||
$constraint = new ForwardingOptionCompound($min = 3); | |||
|
|||
$this->assertSame($min, $constraint->constraints[0]->min); | |||
} | |||
} | |||
|
|||
class EmptyCompound extends Compound | |||
{ | |||
protected function getConstraints(array $options): array | |||
{ | |||
return []; | |||
} | |||
} | |||
|
|||
class ForwardingOptionCompound extends Compound | |||
{ | |||
public $min; | |||
|
|||
public function getDefaultOption() | |||
{ | |||
return 'min'; | |||
} | |||
|
|||
protected function getConstraints(array $options): array | |||
{ | |||
return [ | |||
new Length(['min' => $options['min'] ?? null]), | |||
]; | |||
} | |||
} |
56 changes: 56 additions & 0 deletions
56
src/Symfony/Component/Validator/Tests/Constraints/CompoundValidatorTest.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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,56 @@ | |||
<?php | |||
|
|||
/* | |||
* This file is part of the Symfony package. | |||
* | |||
* (c) Fabien Potencier <fabien@symfony.com> | |||
* | |||
* For the full copyright and license information, please view the LICENSE | |||
* file that was distributed with this source code. | |||
*/ | |||
|
|||
namespace Symfony\Component\Validator\Tests\Constraints; | |||
|
|||
use Symfony\Component\Validator\Constraints\Compound; | |||
use Symfony\Component\Validator\Constraints\CompoundValidator; | |||
use Symfony\Component\Validator\Constraints\Length; | |||
use Symfony\Component\Validator\Constraints\NotBlank; | |||
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase; | |||
|
|||
class CompoundValidatorTest extends ConstraintValidatorTestCase | |||
{ | |||
protected function createValidator() | |||
{ | |||
return new CompoundValidator(); | |||
} | |||
|
|||
public function testValidValue() | |||
{ | |||
$this->validator->validate('foo', new DummyCompoundConstraint()); | |||
|
|||
$this->assertNoViolation(); | |||
} | |||
|
|||
public function testValidateWithConstraints() | |||
{ | |||
$value = 'foo'; | |||
$constraint = new DummyCompoundConstraint(); | |||
|
|||
$this->expectValidateValue(0, $value, $constraint->constraints); | |||
|
|||
$this->validator->validate($value, $constraint); | |||
|
|||
$this->assertNoViolation(); | |||
} | |||
} | |||
|
|||
class DummyCompoundConstraint extends Compound | |||
{ | |||
protected function getConstraints(array $options): array | |||
{ | |||
return [ | |||
new NotBlank(), | |||
new Length(['max' => 3]), | |||
]; | |||
} | |||
} |