Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2edadf0

Browse files
feature #27917 [Validator] catch any UnexpectedValueException on validation (xabbuh)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Validator] catch any UnexpectedValueException on validation | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #12312 | License | MIT | Doc PR | Commits ------- fa35860 catch any UnexpectedValueException on validation
2 parents b452c01 + fa35860 commit 2edadf0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+112
-49
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
4.2.0
55
-----
66

7+
* added a new `UnexpectedValueException` that can be thrown by constraint validators, these exceptions are caught by
8+
the validator and are converted into constraint violations
79
* added `DivisibleBy` constraint
810
* decoupled from `symfony/translation` by using `Symfony\Contracts\Translation\TranslatorInterface`
911
* deprecated `ValidatorBuilderInterface`

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
1616
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1718

1819
/**
1920
* @author Bernhard Schussek <[email protected]>
@@ -34,7 +35,7 @@ public function validate($value, Constraint $constraint)
3435
}
3536

3637
if (!\is_array($value) && !$value instanceof \Traversable) {
37-
throw new UnexpectedTypeException($value, 'array or Traversable');
38+
throw new UnexpectedValueException($value, 'iterable');
3839
}
3940

4041
$context = $this->context;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\Validator\Constraint;
1616
use Symfony\Component\Validator\ConstraintValidator;
1717
use Symfony\Component\Validator\Exception\LogicException;
18-
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
18+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1919

2020
/**
2121
* @author Michael Hirschler <[email protected]>
@@ -34,7 +34,7 @@ public function validate($value, Constraint $constraint)
3434
}
3535

3636
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
37-
throw new UnexpectedTypeException($value, 'string');
37+
throw new UnexpectedValueException($value, 'string');
3838
}
3939

4040
$canonicalize = str_replace(' ', '', $value);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Validator\ConstraintValidator;
1616
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
1717
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
18+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1819

1920
/**
2021
* ChoiceValidator validates that the value is one of the expected values.
@@ -43,7 +44,7 @@ public function validate($value, Constraint $constraint)
4344
}
4445

4546
if ($constraint->multiple && !\is_array($value)) {
46-
throw new UnexpectedTypeException($value, 'array');
47+
throw new UnexpectedValueException($value, 'array');
4748
}
4849

4950
if ($constraint->callback) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
1616
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1718

1819
/**
1920
* @author Bernhard Schussek <[email protected]>
@@ -34,7 +35,7 @@ public function validate($value, Constraint $constraint)
3435
}
3536

3637
if (!\is_array($value) && !($value instanceof \Traversable && $value instanceof \ArrayAccess)) {
37-
throw new UnexpectedTypeException($value, 'array or Traversable and ArrayAccess');
38+
throw new UnexpectedValueException($value, 'array|(Traversable&ArrayAccess)');
3839
}
3940

4041
// We need to keep the initialized context when CollectionValidator

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

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

1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
16-
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
16+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1717

1818
/**
1919
* @author Bernhard Schussek <[email protected]>
@@ -30,7 +30,7 @@ public function validate($value, Constraint $constraint)
3030
}
3131

3232
if (!\is_array($value) && !$value instanceof \Countable) {
33-
throw new UnexpectedTypeException($value, 'array or \Countable');
33+
throw new UnexpectedValueException($value, 'array|\Countable');
3434
}
3535

3636
$count = \count($value);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Validator\ConstraintValidator;
1717
use Symfony\Component\Validator\Exception\LogicException;
1818
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1920

2021
/**
2122
* Validates whether a value is a valid country code.
@@ -38,7 +39,7 @@ public function validate($value, Constraint $constraint)
3839
}
3940

4041
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
41-
throw new UnexpectedTypeException($value, 'string');
42+
throw new UnexpectedValueException($value, 'string');
4243
}
4344

4445
if (!class_exists(Intl::class)) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Validator\ConstraintValidator;
1717
use Symfony\Component\Validator\Exception\LogicException;
1818
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1920

2021
/**
2122
* Validates whether a value is a valid currency.
@@ -39,7 +40,7 @@ public function validate($value, Constraint $constraint)
3940
}
4041

4142
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
42-
throw new UnexpectedTypeException($value, 'string');
43+
throw new UnexpectedValueException($value, 'string');
4344
}
4445

4546
if (!class_exists(Intl::class)) {

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

Lines changed: 2 additions & 1 deletion
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\UnexpectedTypeException;
16+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1617

1718
/**
1819
* @author Bernhard Schussek <[email protected]>
@@ -40,7 +41,7 @@ public function validate($value, Constraint $constraint)
4041
}
4142

4243
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
43-
throw new UnexpectedTypeException($value, 'string');
44+
throw new UnexpectedValueException($value, 'string');
4445
}
4546

4647
$value = (string) $value;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
1616
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1718

1819
/**
1920
* @author Bernhard Schussek <[email protected]>
@@ -58,7 +59,7 @@ public function validate($value, Constraint $constraint)
5859
}
5960

6061
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
61-
throw new UnexpectedTypeException($value, 'string');
62+
throw new UnexpectedValueException($value, 'string');
6263
}
6364

6465
$value = (string) $value;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Symfony\Component\Validator\ConstraintValidator;
1818
use Symfony\Component\Validator\Exception\LogicException;
1919
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
20+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
2021

2122
/**
2223
* @author Bernhard Schussek <[email protected]>
@@ -75,7 +76,7 @@ public function validate($value, Constraint $constraint)
7576
}
7677

7778
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
78-
throw new UnexpectedTypeException($value, 'string');
79+
throw new UnexpectedValueException($value, 'string');
7980
}
8081

8182
$value = (string) $value;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Validator\Constraint;
1717
use Symfony\Component\Validator\ConstraintValidator;
1818
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1920

2021
/**
2122
* @author Bernhard Schussek <[email protected]>
@@ -114,7 +115,7 @@ public function validate($value, Constraint $constraint)
114115
}
115116

116117
if (!is_scalar($value) && !$value instanceof FileObject && !(\is_object($value) && method_exists($value, '__toString'))) {
117-
throw new UnexpectedTypeException($value, 'string');
118+
throw new UnexpectedValueException($value, 'string');
118119
}
119120

120121
$path = $value instanceof FileObject ? $value->getPathname() : (string) $value;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
1616
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1718

1819
/**
1920
* @author Manuel Reinhard <[email protected]>
@@ -149,7 +150,7 @@ public function validate($value, Constraint $constraint)
149150
}
150151

151152
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
152-
throw new UnexpectedTypeException($value, 'string');
153+
throw new UnexpectedValueException($value, 'string');
153154
}
154155

155156
$value = (string) $value;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
1616
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1718

1819
/**
1920
* Validates whether a value is a valid IP address.
@@ -37,7 +38,7 @@ public function validate($value, Constraint $constraint)
3738
}
3839

3940
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
40-
throw new UnexpectedTypeException($value, 'string');
41+
throw new UnexpectedValueException($value, 'string');
4142
}
4243

4344
$value = (string) $value;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
1616
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1718

1819
/**
1920
* Validates whether the value is a valid ISBN-10 or ISBN-13.
@@ -40,7 +41,7 @@ public function validate($value, Constraint $constraint)
4041
}
4142

4243
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
43-
throw new UnexpectedTypeException($value, 'string');
44+
throw new UnexpectedValueException($value, 'string');
4445
}
4546

4647
$value = (string) $value;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
1616
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1718

1819
/**
1920
* Validates whether the value is a valid ISSN.
@@ -39,7 +40,7 @@ public function validate($value, Constraint $constraint)
3940
}
4041

4142
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
42-
throw new UnexpectedTypeException($value, 'string');
43+
throw new UnexpectedValueException($value, 'string');
4344
}
4445

4546
$value = (string) $value;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Validator\ConstraintValidator;
1717
use Symfony\Component\Validator\Exception\LogicException;
1818
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
19+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1920

2021
/**
2122
* Validates whether a value is a valid language code.
@@ -38,7 +39,7 @@ public function validate($value, Constraint $constraint)
3839
}
3940

4041
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
41-
throw new UnexpectedTypeException($value, 'string');
42+
throw new UnexpectedValueException($value, 'string');
4243
}
4344

4445
if (!class_exists(Intl::class)) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
1616
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1718

1819
/**
1920
* @author Bernhard Schussek <[email protected]>
@@ -34,7 +35,7 @@ public function validate($value, Constraint $constraint)
3435
}
3536

3637
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
37-
throw new UnexpectedTypeException($value, 'string');
38+
throw new UnexpectedValueException($value, 'string');
3839
}
3940

4041
$stringValue = (string) $value;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Validator\Constraint;
1616
use Symfony\Component\Validator\ConstraintValidator;
1717
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
18+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1819

1920
/**
2021
* Validates whether a value is a valid locale code.
@@ -37,7 +38,7 @@ public function validate($value, Constraint $constraint)
3738
}
3839

3940
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
40-
throw new UnexpectedTypeException($value, 'string');
41+
throw new UnexpectedValueException($value, 'string');
4142
}
4243

4344
$inputValue = (string) $value;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
1616
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1718

1819
/**
1920
* Validates a PAN using the LUHN Algorithm.
@@ -50,7 +51,7 @@ public function validate($value, Constraint $constraint)
5051
// Work with strings only, because long numbers are represented as floats
5152
// internally and don't work with strlen()
5253
if (!\is_string($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
53-
throw new UnexpectedTypeException($value, 'string');
54+
throw new UnexpectedValueException($value, 'string');
5455
}
5556

5657
$value = (string) $value;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
1616
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1718

1819
/**
1920
* Validates whether a value match or not given regexp pattern.
@@ -37,7 +38,7 @@ public function validate($value, Constraint $constraint)
3738
}
3839

3940
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
40-
throw new UnexpectedTypeException($value, 'string');
41+
throw new UnexpectedValueException($value, 'string');
4142
}
4243

4344
$value = (string) $value;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\Validator\Constraint;
1515
use Symfony\Component\Validator\ConstraintValidator;
1616
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
17+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1718

1819
/**
1920
* @author Bernhard Schussek <[email protected]>
@@ -58,7 +59,7 @@ public function validate($value, Constraint $constraint)
5859
}
5960

6061
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
61-
throw new UnexpectedTypeException($value, 'string');
62+
throw new UnexpectedValueException($value, 'string');
6263
}
6364

6465
$value = (string) $value;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Validator\ConstraintValidator;
1616
use Symfony\Component\Validator\Exception\InvalidOptionsException;
1717
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
18+
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1819

1920
/**
2021
* @author Bernhard Schussek <[email protected]>
@@ -53,7 +54,7 @@ public function validate($value, Constraint $constraint)
5354
}
5455

5556
if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
56-
throw new UnexpectedTypeException($value, 'string');
57+
throw new UnexpectedValueException($value, 'string');
5758
}
5859

5960
$value = (string) $value;

0 commit comments

Comments
 (0)