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

Skip to content

Commit 5b07e0a

Browse files
committed
feature #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

+5Lines 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
}

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

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

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

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

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
namespace Symfony\Component\Validator\Constraints;
1313

14+
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
16+
1417
/**
1518
* @author Bernhard Schussek <bschussek@gmail.com>
1619
*
@@ -19,4 +22,28 @@
1922
class DateTimeValidator extends DateValidator
2023
{
2124
const PATTERN = '/^(\d{4})-(\d{2})-(\d{2}) (0[0-9]|1[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$/';
25+
26+
/**
27+
* {@inheritDoc}
28+
*/
29+
public function validate($value, Constraint $constraint)
30+
{
31+
if (!$constraint instanceof DateTime) {
32+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\DateTime');
33+
}
34+
35+
if (null === $value || '' === $value || $value instanceof \DateTime) {
36+
return;
37+
}
38+
39+
if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
40+
throw new UnexpectedTypeException($value, 'string');
41+
}
42+
43+
$value = (string) $value;
44+
45+
if (!preg_match(static::PATTERN, $value, $matches) || !checkdate($matches[2], $matches[3], $matches[1])) {
46+
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
47+
}
48+
}
2249
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class DateValidator extends ConstraintValidator
2929
*/
3030
public function validate($value, Constraint $constraint)
3131
{
32+
if (!$constraint instanceof Date) {
33+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Date');
34+
}
35+
3236
if (null === $value || '' === $value || $value instanceof \DateTime) {
3337
return;
3438
}

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

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

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

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

2122
/**
2223
* @author Fabien Potencier <fabien@symfony.com>
@@ -44,6 +45,10 @@ public function __construct(PropertyAccessorInterface $propertyAccessor)
4445
*/
4546
public function validate($value, Constraint $constraint)
4647
{
48+
if (!$constraint instanceof Expression) {
49+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Expression');
50+
}
51+
4752
if (null === $value || '' === $value) {
4853
return;
4954
}

src/Symfony/Component/Validator/Constraints/FalseValidator.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 FalseValidator extends ConstraintValidator
2627
*/
2728
public function validate($value, Constraint $constraint)
2829
{
30+
if (!$constraint instanceof False) {
31+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\False');
32+
}
33+
2934
if (null === $value || false === $value || 0 === $value || '0' === $value) {
3035
return;
3136
}

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

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

src/Symfony/Component/Validator/Constraints/IbanValidator.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 Manuel Reinhard <manu@sprain.ch>
@@ -26,6 +27,10 @@ class IbanValidator extends ConstraintValidator
2627
*/
2728
public function validate($value, Constraint $constraint)
2829
{
30+
if (!$constraint instanceof Iban) {
31+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Iban');
32+
}
33+
2934
if (null === $value || '' === $value) {
3035
return;
3136
}

src/Symfony/Component/Validator/Constraints/ImageValidator.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\Exception\ConstraintDefinitionException;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1617

1718
/**
1819
* Validates whether a value is a valid image file and is valid
@@ -27,6 +28,10 @@ class ImageValidator extends FileValidator
2728
*/
2829
public function validate($value, Constraint $constraint)
2930
{
31+
if (!$constraint instanceof Image) {
32+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Image');
33+
}
34+
3035
$violations = count($this->context->getViolations());
3136

3237
parent::validate($value, $constraint);

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

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

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class IsbnValidator extends ConstraintValidator
2929
*/
3030
public function validate($value, Constraint $constraint)
3131
{
32+
if (!$constraint instanceof Isbn) {
33+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Isbn');
34+
}
35+
3236
if (null === $value || '' === $value) {
3337
return;
3438
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ class IssnValidator extends ConstraintValidator
2929
*/
3030
public function validate($value, Constraint $constraint)
3131
{
32+
if (!$constraint instanceof Issn) {
33+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Issn');
34+
}
35+
3236
if (null === $value || '' === $value) {
3337
return;< 10000 /div>
3438
}

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

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

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ class LengthValidator extends ConstraintValidator
2525
*/
2626
public function validate($value, Constraint $constraint)
2727
{
28+
if (!$constraint instanceof Length) {
29+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Length');
30+
}
31+
2832
if (null === $value || '' === $value) {
2933
return;
3034
}

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

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

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ class LuhnValidator extends ConstraintValidator
3535
*/
3636
public function validate($value, Constraint $constraint)
3737
{
38+
if (!$constraint instanceof Luhn) {
39+
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Luhn');
40+
}
41+
3842
if (null === $value || '' === $value) {
3943
return;
4044
}

0 commit comments

Comments
 (0)
0