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

Skip to content

Commit ecc57ea

Browse files
committed
[Validator] String normalization options for string-based validators
1 parent 8ab7077 commit ecc57ea

21 files changed

+210
-0
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class Email extends Constraint
5555
*/
5656
public $strict;
5757
public $mode;
58+
public $normalizer;
5859

5960
public function __construct($options = null)
6061
{

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

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

8181
$value = (string) $value;
8282

83+
if (null !== $constraint->normalizer) {
84+
$value = \call_user_func($constraint->normalizer, $value);
85+
}
86+
8387
if (null !== $constraint->strict) {
8488
@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);
8589

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ class Ip extends Constraint
7272

7373
public $message = 'This is not a valid IP address.';
7474

75+
public $normalizer;
76+
7577
/**
7678
* {@inheritdoc}
7779
*/

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

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

4343
$value = (string) $value;
4444

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Length extends Constraint
3939
public $max;
4040
public $min;
4141
public $charset = 'UTF-8';
42+
public $normalizer;
4243

4344
public function __construct($options = null)
4445
{

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

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

4040
$stringValue = (string) $value;
4141

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,5 @@ class NotBlank extends Constraint
2828
);
2929

3030
public $message = 'This value should not be blank.';
31+
public $normalizer;
3132
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ public function validate($value, Constraint $constraint)
2929
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\NotBlank');
3030
}
3131

32+
if (\is_string($value) && null !== $constraint->normalizer) {
33+
$value = \call_user_func($constraint->normalizer, $value);
34+
}
35+
3236
if (false === $value || (empty($value) && '0' != $value)) {
3337
$this->context->buildViolation($constraint->message)
3438
->setParameter('{{ value }}', $this->formatValue($value))

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Regex extends Constraint
3131
public $pattern;
3232
public $htmlPattern;
3333
public $match = true;
34+
public $normalizer;
3435

3536
/**
3637
* {@inheritdoc}

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

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

4343
$value = (string) $value;
4444

45+
if (null !== $constraint->normalizer) {
46+
$value = \call_user_func($constraint->normalizer, $value);
47+
}
48+
4549
if ($constraint->match xor preg_match($constraint->pattern, $value)) {
4650
$this->context->buildViolation($constraint->message)
4751
->setParameter('{{ value }}', $this->formatValue($value))

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ class Url extends Constraint
105105
*/
106106
public $checkDNS = self::CHECK_DNS_TYPE_NONE;
107107
public $relativeProtocol = false;
108+
public $normalizer;
108109

109110
public function __construct($options = null)
110111
{

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ public function validate($value, Constraint $constraint)
6161
return;
6262
}
6363

64+
if (null !== $constraint->normalizer) {
65+
$value = \call_user_func($constraint->normalizer, $value);
66+
}
67+
6468
$pattern = $constraint->relativeProtocol ? str_replace('(%s):', '(?:(%s):)?', static::PATTERN) : static::PATTERN;
6569
$pattern = sprintf($pattern, implode('|', $constraint->protocols));
6670

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,6 @@ class Uuid extends Constraint
7474
self::V4_RANDOM,
7575
self::V5_SHA1,
7676
);
77+
78+
public $normalizer;
7779
}

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

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

8181
$value = (string) $value;
8282

83+
if (null !== $constraint->normalizer) {
84+
$value = \call_user_func($constraint->normalizer, $value);
85+
}
86+
8387
if ($constraint->strict) {
8488
$this->validateStrict($value, $constraint);
8589

src/Symfony/Component/Validator/Tests/Constraints/EmailValidatorTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,28 @@ public function getValidEmails()
9494
);
9595
}
9696

97+
/**
98+
* @dataProvider getValidEmailsWithWhitespaces
99+
*/
100+
public function testValidNormalizedEmails($email)
101+
{
102+
$this->validator->validate($email, new Email(array('normalizer' => 'trim')));
103+
104+
$this->assertNoViolation();
105+
}
106+
107+
public function getValidEmailsWithWhitespaces()
108+
{
109+
return array(
110+
array("\x20[email protected]\x20"),
111+
array("\x09\x09[email protected]\x09\x09"),
112+
array("\x0A{}~!@!@£$%%^&*().!@£$%^&*()\x0A"),
113+
array("\x0D\x0D[email protected]\x0D\x0D"),
114+
array("\x00[email protected]"),
115+
array("[email protected]\x0B\x0B"),
116+
);
117+
}
118+
97119
/**
98120
* @dataProvider getValidEmailsHtml5
99121
*/

src/Symfony/Component/Validator/Tests/Constraints/IpValidatorTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,31 @@ public function getValidIpsV4()
8080
);
8181
}
8282

83+
/**
84+
* @dataProvider getValidIpsV4WithWhitespaces
85+
*/
86+
public function testValidIpsV4WithWhitespaces($ip)
87+
{
88+
$this->validator->validate($ip, new Ip(array(
89+
'version' => Ip::V4,
90+
'normalizer' => 'trim',
91+
)));
92+
93+
$this->assertNoViolation();
94+
}
95+
96+
public function getValidIpsV4WithWhitespaces()
97+
{
98+
return array(
99+
array("\x200.0.0.0"),
100+
array("\x09\x0910.0.0.0"),
101+
array("123.45.67.178\x0A"),
102+
array("172.16.0.0\x0D\x0D"),
103+
array("\x00192.168.1.0\x00"),
104+
array("\x0B\x0B224.0.0.1\x0B\x0B"),
105+
);
106+
}
107+
83108
/**
84109
* @dataProvider getValidIpsV6
85110
*/

src/Symfony/Component/Validator/Tests/Constraints/LengthValidatorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@ public function getOneCharset()
9292
);
9393
}
9494

95+
public function getThreeCharactersWithWhitespaces()
96+
{
97+
return array(
98+
array("\x20ccc"),
99+
array("\x09c\x09c"),
100+
array("\x0Accc\x0A"),
101+
array("ccc\x0D\x0D"),
102+
array("\x00ccc\x00"),
103+
array("\x0Bc\x0Bc\x0B"),
104+
);
105+
}
106+
95107
/**
96108
* @dataProvider getFiveOrMoreCharacters
97109
*/
@@ -125,6 +137,17 @@ public function testValidValuesExact($value)
125137
$this->assertNoViolation();
126138
}
127139

140+
/**
141+
* @dataProvider getThreeCharactersWithWhitespaces
142+
*/
143+
public function testValidNormalizedValues($value)
144+
{
145+
$constraint = new Length(array('min' => 3, 'max' => 3, 'normalizer' => 'trim'));
146+
$this->validator->validate($value, $constraint);
147+
148+
$this->assertNoViolation();
149+
}
150+
128151
/**
129152
* @dataProvider getThreeOrLessCharacters
130153
*/

src/Symfony/Component/Validator/Tests/Constraints/NotBlankValidatorTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,34 @@ public function testEmptyArrayIsInvalid()
9898
->setCode(NotBlank::IS_BLANK_ERROR)
9999
->assertRaised();
100100
}
101+
102+
/**
103+
* @dataProvider getWhitespaces
104+
*/
105+
public function testNormalizedStringIsInvalid($value)
106+
{
107+
$constraint = new NotBlank(array(
108+
'message' => 'myMessage',
109+
'normalizer' => 'trim',
110+
));
111+
112+
$this->validator->validate($value, $constraint);
113+
114+
$this->buildViolation('myMessage')
115+
->setParameter('{{ value }}', '""')
116+
->setCode(NotBlank::IS_BLANK_ERROR)
117+
->assertRaised();
118+
}
119+
120+
public function getWhitespaces()
121+
{
122+
return array(
123+
array("\x20"),
124+
array("\x09\x09"),
125+
array("\x0A"),
126+
array("\x0D\x0D"),
127+
array("\x00"),
128+
array("\x0B\x0B"),
129+
);
130+
}
101131
}

src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,17 @@ public function testValidValues($value)
5555
$this->assertNoViolation();
5656
}
5757

58+
/**
59+
* @dataProvider getValidValuesWithWhitespaces
60+
*/
61+
public function testValidValuesWithWhitespaces($value)
62+
{
63+
$constraint = new Regex(array('pattern' => '/^[0-9]+$/', 'normalizer' => 'trim'));
64+
$this->validator->validate($value, $constraint);
65+
66+
$this->assertNoViolation();
67+
}
68+
5869
public function getValidValues()
5970
{
6071
return array(
@@ -65,6 +76,18 @@ public function getValidValues()
6576
);
6677
}
6778

79+
public function getValidValuesWithWhitespaces()
80+
{
81+
return array(
82+
array("\x207"),
83+
array("\x09\x09070707\x09\x09"),
84+
array("70707\x0A"),
85+
array("7\x0D\x0D"),
86+
array("\x00070707\x00"),
87+
array("\x0B\x0B70707\x0B\x0B"),
88+
);
89+
}
90+
6891
/**
6992
* @dataProvider getInvalidValues
7093
*/

src/Symfony/Component/Validator/Tests/Constraints/UrlValidatorTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,16 @@ public function testValidUrls($url)
6565
$this->assertNoViolation();
6666
}
6767

68+
/**
69+
* @dataProvider getValidUrlsWithWhitespaces
70+
*/
71+
public function testValidUrlsWithWhitespaces($url)
72+
{
73+
$this->validator->validate($url, new Url(array('normalizer' => 'trim')));
74+
75+
$this->assertNoViolation();
76+
}
77+
6878
/**
6979
* @dataProvider getValidRelativeUrls
7080
* @dataProvider getValidUrls
@@ -154,6 +164,18 @@ public function getValidUrls()
154164
);
155165
}
156166

167+
public function getValidUrlsWithWhitespaces()
168+
{
169+
return array(
170+
array("\x20http://www.google.com"),
171+
array("\x09\x09http://www.google.com."),
172+
array("http://symfony.fake/blog/\x0A"),
173+
array("http://symfony.com/search?type=&q=url+validator\x0D\x0D"),
174+
array("\x00https://google.com:80\x00"),
175+
array("\x0B\x0Bhttp://username:[email protected]\x0B\x0B"),
176+
);
177+
}
178+
157179
/**
158180
* @dataProvider getInvalidUrls
159181
*/

src/Symfony/Component/Validator/Tests/Constraints/UuidValidatorTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,34 @@ public function getValidStrictUuids()
8585
);
8686
}
8787

88+
/**
89+
* @dataProvider getValidStrictUuidsWithWhitespaces
90+
*/
91+
public function testValidStrictUuidsWithWhitespaces($uuid, $versions = null)
92+
{
93+
$constraint = new Uuid(array('normalizer' => 'trim'));
94+
95+
if (null !== $versions) {
96+
$constraint->versions = $versions;
97+
}
98+
99+
$this->validator->validate($uuid, $constraint);
100+
101+
$this->assertNoViolation();
102+
}
103+
104+
public function getValidStrictUuidsWithWhitespaces()
105+
{
106+
return array(
107+
array("\x20216fff40-98d9-11e3-a5e2-0800200c9a66"), // Version 1 UUID in lowercase
108+
array("\x09\x09216fff40-98d9-11e3-a5e2-0800200c9a66", array(Uuid::V1_MAC)),
109+
array("216FFF40-98D9-11E3-A5E2-0800200C9A66\x0A"), // Version 1 UUID in UPPERCASE
110+
array("456daefb-5aa6-41b5-8dbc-068b05a8b201\x0D\x0D"), // Version 4 UUID in lowercase
111+
array("\x00456daEFb-5AA6-41B5-8DBC-068B05A8B201\x00"), // Version 4 UUID in mixed case
112+
array("\x0B\x0B456daEFb-5AA6-41B5-8DBC-068B05A8B201\x0B\x0B", array(Uuid::V4_RANDOM)),
113+
);
114+
}
115+
88116
/**
89117
* @dataProvider getInvalidStrictUuids
90118
*/

0 commit comments

Comments
 (0)