8000 [Validator] catch any UnexpectedValueException on validation by xabbuh · Pull Request #27917 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] catch any UnexpectedValueException on validation #27917

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ CHANGELOG
4.2.0
-----

* added a new `UnexpectedValueException` that can be thrown by constraint validators, these exceptions are caught by
the validator and are converted into constraint violations
* added `DivisibleBy` constraint
* decoupled from `symfony/translation` by using `Symfony\Contracts\Translation\TranslatorInterface`
* deprecated `ValidatorBuilderInterface`
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Validator/Constraints/AllValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand All @@ -34,7 +35,7 @@ public function validate($value, Constraint $constraint)
}

if (!\is_array($value) && !$value instanceof \Traversable) {
throw new UnexpectedTypeException($value, 'array or Traversable');
throw new UnexpectedValueException($value, 'iterable');
}

$context = $this->context;
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Validator/Constraints/BicValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\LogicException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* @author Michael Hirschler <michael.vhirsch@gmail.com>
Expand All @@ -34,7 +34,7 @@ public function validate($value, Constraint $constraint)
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}

$canonicalize = str_replace(' ', '', $value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

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

if ($constraint->multiple && !\is_array($value)) {
throw new UnexpectedTypeException($value, 'array');
throw new UnexpectedValueException($value, 'array');
}

if ($constraint->callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand All @@ -34,7 +35,7 @@ public function validate($value, Constraint $constraint)
}

if (!\is_array($value) && !($value instanceof \Traversable && $value instanceof \ArrayAccess)) {
throw new UnexpectedTypeException($value, 'array or Traversable and ArrayAccess');
throw new UnexpectedValueException($value, 'array|(Traversable&ArrayAccess)');
}

// We need to keep the initialized context when CollectionValidator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand All @@ -30,7 +30,7 @@ public function validate($value, Constraint $constraint)
}

if (!\is_array($value) && !$value instanceof \Countable) {
throw new UnexpectedTypeException($value, 'array or \Countable');
throw new UnexpectedValueException($value, 'array|\Countable');
}

$count = \count($value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\LogicException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

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

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}

if (!class_exists(Intl::class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\LogicException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

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

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}

if (!class_exists(Intl::class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand Down Expand Up @@ -40,7 +41,7 @@ public function validate($value, Constraint $constraint)
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}

$value = (string) $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand Down Expand Up @@ -58,7 +59,7 @@ public function validate($value, Constraint $constraint)
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}

$value = (string) $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\LogicException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand Down Expand Up @@ -75,7 +76,7 @@ public function validate($value, Constraint $constraint)
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}

$value = (string) $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand Down Expand Up @@ -114,7 +115,7 @@ public function validate($value, Constraint $constraint)
}

if (!is_scalar($value) && !$value instanceof FileObject && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}

$path = $value instanceof FileObject ? $value->getPathname() : (string) $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* @author Manuel Reinhard <manu@sprain.ch>
Expand Down Expand Up @@ -149,7 +150,7 @@ public function validate($value, Constraint $constraint)
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}

$value = (string) $value;
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Validator/Constraints/IpValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

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

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}

$value = (string) $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

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

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}

$value = (string) $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

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

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}

$value = (string) $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\LogicException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

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

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}

if (!class_exists(Intl::class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand All @@ -34,7 +35,7 @@ public function validate($value, Constraint $constraint)
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}

$stringValue = (string) $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

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

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}

$inputValue = (string) $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

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

$value = (string) $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

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

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}

$value = (string) $value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand Down Expand Up @@ -58,7 +59,7 @@ public function validate($value, Constraint $constraint)
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}

$value = (string) $value;
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/Validator/Constraints/UrlValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\InvalidOptionsException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand Down Expand Up @@ -53,7 +54,7 @@ public function validate($value, Constraint $constraint)
}

if (!is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
throw new UnexpectedValueException($value, 'string');
}

$value = (string) $value;
Expand Down
Loading
0