Skip to content

Commit fdf8dfc

Browse files
renan-tarantonicolas-grekas
authored andcommitted
[Validator] String normalization options for string-based validators
1 parent cf7ac8b commit fdf8dfc

29 files changed

+544
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ CHANGELOG
88
* added UATP cards support to `CardSchemeValidator`
99
* added option `allowNull` to NotBlank constraint
1010
* added `Json` constraint
11-
11+
* added a new `normalizer` option to the string constraints and to the `NotBlank` constraint
12+
1213
4.2.0
1314
-----
1415

Constraints/Email.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Egulias\EmailValidator\EmailValidator as StrictEmailValidator;
1515
use Symfony\Component\Validator\Constraint;
16+
use Symfony\Component\Validator\Exception\InvalidArgumentException;
1617
use Symfony\Component\Validator\Exception\LogicException;
1718

1819
/**
@@ -73,6 +74,7 @@ class Email extends Constraint
7374
*/
7475
public $strict;
7576
public $mode;
77+
public $normalizer;
7678

7779
public function __construct($options = null)
7880
{
@@ -89,7 +91,7 @@ public function __construct($options = null)
8991
}
9092

9193
if (\is_array($options) && \array_key_exists('mode', $options) && !\in_array($options['mode'], self::$validationModes, true)) {
92-
throw new \InvalidArgumentException('The "mode" parameter value is not valid.');
94+
throw new InvalidArgumentException('The "mode" parameter value is not valid.');
9395
}
9496

9597
parent::__construct($options);
@@ -98,5 +100,9 @@ public function __construct($options = null)
98100
// throw new LogicException(sprintf('The "egulias/email-validator" component is required to use the "%s" constraint in strict mode.', __CLASS__));
99101
@trigger_error(sprintf('Using the "%s" constraint in strict mode without the "egulias/email-validator" component installed is deprecated since Symfony 4.2.', __CLASS__), E_USER_DEPRECATED);
100102
}
103+
104+
if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
105+
throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', \is_object($this->normalizer) ? \get_class($this->normalizer) : \gettype($this->normalizer)));
106+
}
101107
}
102108
}

Constraints/EmailValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ public function validate($value, Constraint $constraint)
8181

8282
$value = (string) $value;
8383

84+
if (null !== $constraint->normalizer) {
85+
$value = ($constraint->normalizer)($value);
86+
}
87+
8488
if (null !== $constraint->strict) {
8589
@trigger_error(sprintf('The %s::$strict property is deprecated since Symfony 4.1. Use %s::mode="%s" instead.', Email::class, Email::class, Email::VALIDATION_MODE_STRICT), E_USER_DEPRECATED);
8690

Constraints/Ip.php

Lines changed: 7 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\InvalidArgumentException;
1617

1718
/**
1819
* Validates that a value is a valid IP address.
@@ -72,6 +73,8 @@ class Ip extends Constraint
7273

7374
public $message = 'This is not a valid IP address.';
7475

76+
public $normalizer;
77+
7578
/**
7679
* {@inheritdoc}
7780
*/
@@ -82,5 +85,9 @@ public function __construct($options = null)
8285
if (!\in_array($this->version, self::$versions)) {
8386
throw new ConstraintDefinitionException(sprintf('The option "version" must be one of "%s"', implode('", "', self::$versions)));
8487
}
88+
89+
if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
90+
throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', \is_object($this->normalizer) ? \get_class($this->normalizer) : \gettype($this->normalizer)));
91+
}
8592
}
8693
}

Constraints/IpValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ public function validate($value, Constraint $constraint)
4343

4444
$value = (string) $value;
4545

46+
if (null !== $constraint->normalizer) {
47+
$value = ($constraint->normalizer)($value);
48+
}
49+
4650
switch ($constraint->version) {
4751
case Ip::V4:
4852
$flag = FILTER_FLAG_IPV4;

Constraints/Length.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Constraints;
1313

1414
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\Exception\InvalidArgumentException;
1516
use Symfony\Component\Validator\Exception\MissingOptionsException;
1617

1718
/**
@@ -39,6 +40,7 @@ class Length extends Constraint
3940
public $max;
4041
public $min;
4142
public $charset = 'UTF-8';
43+
public $normalizer;
4244

4345
public function __construct($options = null)
4446
{
@@ -54,5 +56,9 @@ public function __construct($options = null)
5456
if (null === $this->min && null === $this->max) {
5557
throw new MissingOptionsException(sprintf('Either option "min" or "max" must be given for constraint %s', __CLASS__), ['min', 'max']);
5658
}
59+
60+
if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
61+
throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', \is_object($this->normalizer) ? \get_class($this->normalizer) : \gettype($this->normalizer)));
62+
}
5763
}
5864
}

Constraints/LengthValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ public function validate($value, Constraint $constraint)
4040

4141
$stringValue = (string) $value;
4242

43+
if (null !== $constraint->normalizer) {
44+
$stringValue = ($constraint->normalizer)($stringValue);
45+
}
46+
4347
if (!$invalidCharset = !@mb_check_encoding($stringValue, $constraint->charset)) {
4448
$length = mb_strlen($stringValue, $constraint->charset);
4549
}

Constraints/NotBlank.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Constraints;
1313

1414
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\Exception\InvalidArgumentException;
1516

1617
/**
1718
* @Annotation
@@ -30,4 +31,14 @@ class NotBlank extends Constraint
3031

3132
public $message = 'This value should not be blank.';
3233
public $allowNull = false;
34+
public $normalizer;
35+
36+
public function __construct($options = null)
37+
{
38+
parent::__construct($options);
39+
40+
if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
41+
throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', \is_object($this->normalizer) ? \get_class($this->normalizer) : \gettype($this->normalizer)));
42+
}
43+
}
3344
}

Constraints/NotBlankValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public function validate($value, Constraint $constraint)
3434
return;
3535
}
3636

37+
if (\is_string($value) && null !== $constraint->normalizer) {
38+
$value = ($constraint->normalizer)($value);
39+
}
40+
3741
if (false === $value || (empty($value) && '0' != $value)) {
3842
$this->context->buildViolation($constraint->message)
3943
->setParameter('{{ value }}', $this->formatValue($value))

Constraints/Regex.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Symfony\Component\Validator\Constraints;
1313

1414
use Symfony\Component\Validator\Constraint;
15+
use Symfony\Component\Validator\Exception\InvalidArgumentException;
1516

1617
/**
1718
* @Annotation
@@ -31,6 +32,16 @@ class Regex extends Constraint
3132
public $pattern;
3233
public $htmlPattern;
3334
public $match = true;
35+
public $normalizer;
36+
37+
public function __construct($options = null)
38+
{
39+
parent::__construct($options);
40+
41+
if (null !== $this->normalizer && !\is_callable($this->normalizer)) {
42+
throw new InvalidArgumentException(sprintf('The "normalizer" option must be a valid callable ("%s" given).', \is_object($this->normalizer) ? \get_class($this->normalizer) : \gettype($this->normalizer)));
43+
}
44+
}
3445

3546
/**
3647
* {@inheritdoc}

0 commit comments

Comments
 (0)