8000 feature #10414 [Validator] Checked the constraint class in constraint… · cordoval/symfony@5b07e0a · GitHub
[go: up one dir, main page]

Skip to content

Commit 5b07e0a

Browse files
committed
feature symfony#10414 [Validator] Checked the constraint class in constraint validators (webmozart)
This PR was merged into the 2.5-dev branch. Discussion ---------- [Validator] Checked the constraint class in constraint validators | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- df56c23 [Validator] Checked the constraint class in constraint validators
2 parents ce81199 + df56c23 commit 5b07e0a

36 files changed

+182
-0
lines changed

src/Symfony/Bridge/Doctrine/Validator/Constraints/UniqueEntityValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public function __construct(ManagerRegistry $registry)
4646
*/
4747
public function validate($entity, Constraint $constraint)
4848
{
49+
if (!$constraint instanceof UniqueEntity) {
50+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\UniqueEntity');
51+
}
52+
4953
if (!is_array($constraint->fields) && !is_string($constraint->fields)) {
5054
throw new UnexpectedTypeException($constraint->fields, 'array');
5155
}

src/Symfony/Component/Form/Extension/Validator/Constraints/FormValidator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Form\Extension\Validator\Util\ServerParams;
1616
use Symfony\Component\Validator\Constraint;
1717
use Symfony\Component\Validator\ConstraintValidator;
18+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1819

1920
/**
2021
* @author Bernhard Schussek <bschussek@gmail.com>
@@ -42,6 +43,10 @@ public function __construct(ServerParams $params = null)
4243
*/
4344
public function validate($form, Constraint $constraint)
4445
{
46+
if (!$constraint instanceof Form) {
47+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Form');
48+
}
49+
4550
if (!$form instanceof FormInterface) {
4651
return;
4752
}

src/Symfony/Component/Security/Core/Validator/Constraints/UserPasswordValidator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Validator\Constraint;
1818
use Symfony\Component\Validator\ConstraintValidator;
1919
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
20+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
2021

2122
class UserPasswordValidator extends ConstraintValidator
2223
{
@@ -34,6 +35,10 @@ public function __construct(SecurityContextInterface $securityContext, EncoderFa
3435
*/
3536
public function validate($password, Constraint $constraint)
3637
{
38+
if (!$constraint instanceof UserPassword) {
39+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\UserPassword');
40+
}
41+
3742
$user = $this->securityContext->getToken()->getUser();
3843

3944
if (!$user instanceof UserInterface) {

src/Symfony/Component/Validator/Constraints/AbstractComparisonValidator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1617

1718
/**
1819
* Provides a base class for the validation of property comparisons.
@@ -26,6 +27,10 @@ abstract class AbstractComparisonValidator extends ConstraintValidator
2627
*/
2728
public function validate($value, Constraint $constraint)
2829
{
30+
if (!$constraint instanceof AbstractComparison) {
31+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\AbstractComparison');
32+
}
33+
2934
if (null === $value) {
3035
return;
3136
}

src/Symfony/Component/Validator/Constraints/AllValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class AllValidator extends ConstraintValidator
2727
*/
2828
public function validate($value, Constraint $constraint)
2929
{
30+
if (!$constraint instanceof All) {
31+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\All');
32+
}
33+
3034
if (null === $value) {
3135
return;
3236
}

src/Symfony/Component/Validator/Constraints/BlankValidator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1617

1718
/**
1819
* @author Bernhard Schussek <bschussek@gmail.com>
@@ -26,6 +27,10 @@ class BlankValidator extends ConstraintValidator
2627
*/
2728
public function validate($value, Constraint $constraint)
2829
{
30+
if (!$constraint instanceof Blank) {
31+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Blank');
32+
}
33+
2934
if ('' !== $value && null !== $value) {
3035
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
3136
}

src/Symfony/Component/Validator/Constraints/CallbackValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ class CallbackValidator extends ConstraintValidator
3030
*/
3131
public function validate($object, Constraint $constraint)
3232
{
33+
if (!$constraint instanceof Callback) {
34+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Callback');
35+
}
36+
3337
if (null === $object) {
3438
return;
3539
}

src/Symfony/Component/Validator/Constraints/CardSchemeValidator.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1617

1718
/**
1819
* Validates that a card number belongs to a specified scheme.
@@ -103,6 +104,10 @@ class CardSchemeValidator extends ConstraintValidator
103104
*/
104105
public function validate($value, Constraint $constraint)
105106
{
107+
if (!$constraint instanceof CardScheme) {
108+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\CardScheme');
109+
}
110+
106111
if (null === $value || '' === $value) {
107112
return;
108113
}

src/Symfony/Component/Validator/Constraints/ChoiceValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ class ChoiceValidator extends ConstraintValidator
3232
*/
3333
public function validate($value, Constraint $constraint)
3434
{
35+
if (!$constraint instanceof Choice) {
36+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Choice');
37+
}
38+
3539
if (!$constraint->choices && !$constraint->callback) {
3640
throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice');
3741
}

src/Symfony/Component/Validator/Constraints/CollectionValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ class CollectionValidator extends ConstraintValidator
2727
*/
2828
public function validate($value, Constraint $constraint)
2929
{
30+
if (!$constraint instanceof Collection) {
31+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Collection');
32+
}
33+
3034
if (null === $value) {
3135
return;
3236
}

0 commit comments

Comments
 (0)
0