Skip to content

[Validator] Checked the constraint class in constraint validators #10414

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
Mar 11, 2014
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
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public function __construct(ManagerRegistry $registry)
*/
public function validate($entity, Constraint $constraint)
{
if (!$constraint instanceof UniqueEntity) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\UniqueEntity');
}

if (!is_array($constraint->fields) && !is_string($constraint->fields)) {
throw new UnexpectedTypeException($constraint->fields, 'array');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Form\Extension\Validator\Util\ServerParams;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand Down Expand Up @@ -42,6 +43,10 @@ public function __construct(ServerParams $params = null)
*/
public function validate($form, Constraint $constraint)
{
if (!$constraint instanceof Form) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Form');
}

if (!$form instanceof FormInterface) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class UserPasswordValidator extends ConstraintValidator
{
Expand All @@ -34,6 +35,10 @@ public function __construct(SecurityContextInterface $securityContext, EncoderFa
*/
public function validate($password, Constraint $constraint)
{
if (!$constraint instanceof UserPassword) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\UserPassword');
}

$user = $this->securityContext->getToken()->getUser();

if (!$user instanceof UserInterface) {
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\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* Provides a base class for the validation of property comparisons.
Expand All @@ -26,6 +27,10 @@ abstract class AbstractComparisonValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof AbstractComparison) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\AbstractComparison');
}

if (null === $value) {
return;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/AllValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class AllValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof All) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\All');
}

if (null === $value) {
return;
}
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\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand All @@ -26,6 +27,10 @@ class BlankValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Blank) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Blank');
}

if ('' !== $value && null !== $value) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class CallbackValidator extends ConstraintValidator
*/
public function validate($object, Constraint $constraint)
{
if (!$constraint instanceof Callback) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Callback');
}

if (null === $object) {
return;
}
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\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* Validates that a card number belongs to a specified scheme.
Expand Down Expand Up @@ -103,6 +104,10 @@ class CardSchemeValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof CardScheme) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\CardScheme');
}

if (null === $value || '' === $value) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class ChoiceValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Choice) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Choice');
}

if (!$constraint->choices && !$constraint->callback) {
throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class CollectionValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Collection) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Collection');
}

if (null === $value) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class CountryValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Country) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Country');
}

if (null === $value || '' === $value) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class CurrencyValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Currency) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Currency');
}

if (null === $value || '' === $value) {
return;
}
Expand Down
27 changes: 27 additions & 0 deletions src/Symfony/Component/Validator/Constraints/DateTimeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace Symfony\Component\Validator\Constraints;

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

/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
Expand All @@ -19,4 +22,28 @@
class DateTimeValidator extends DateValidator
{
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])$/';

/**
* {@inheritDoc}
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof DateTime) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\DateTime');
}

if (null === $value || '' === $value || $value instanceof \DateTime) {
return;
}

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

$value = (string) $value;

if (!preg_match(static::PATTERN, $value, $matches) || !checkdate($matches[2], $matches[3], $matches[1])) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
}
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/DateValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class DateValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Date) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Date');
}

if (null === $value || '' === $value || $value instanceof \DateTime) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class EmailValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Email) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Email');
}

if (null === $value || '' === $value) {
return;
}
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\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\Validator\Exception\RuntimeException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* @author Fabien Potencier <fabien@symfony.com>
Expand Down Expand Up @@ -44,6 +45,10 @@ public function __construct(PropertyAccessorInterface $propertyAccessor)
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Expression) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Expression');
}

if (null === $value || '' === $value) {
return;
}
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\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand All @@ -26,6 +27,10 @@ class FalseValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof False) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\False');
}

if (null === $value || false === $value || 0 === $value || '0' === $value) {
return;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/FileValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class FileValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof File) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\File');
}

if (null === $value || '' === $value) {
return;
}
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/Validator/Constraints/IbanValidator.php
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\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* @author Manuel Reinhard <manu@sprain.ch>
Expand All @@ -26,6 +27,10 @@ class IbanValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Iban) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Iban');
}

if (null === $value || '' === $value) {
return;
}
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\ConstraintDefinitionException;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* Validates whether a value is a valid image file and is valid
Expand All @@ -27,6 +28,10 @@ class ImageValidator extends FileValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Image) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Image');
}

$violations = count($this->context->getViolations());

parent::validate($value, $constraint);
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/IpValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class IpValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Ip) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Ip');
}

if (null === $value || '' === $value) {
return;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/IsbnValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class IsbnValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Isbn) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Isbn');
}

if (null === $value || '' === $value) {
return;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/IssnValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ class IssnValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Issn) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Issn');
}

if (null === $value || '' === $value) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class LanguageValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Language) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Language');
}

if (null === $value || '' === $value) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ class LengthValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Length) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Length');
}

if (null === $value || '' === $value) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class LocaleValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Locale) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Locale');
}

if (null === $value || '' === $value) {
return;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Validator/Constraints/LuhnValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ class LuhnValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
if (!$constraint instanceof Luhn) {
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Luhn');
}

if (null === $value || '' === $value) {
return;
}
Expand Down
Loading