8000 [Validator] Add "ltrim" and "rtrim" options to the UuidValidator · symfony/symfony@c21460a · GitHub
[go: up one dir, main page]

Skip to content

Commit c21460a

Browse files
committed
[Validator] Add "ltrim" and "rtrim" options to the UuidValidator
1 parent ee3cc2f commit c21460a

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,7 @@ class Uuid extends Constraint
7474
self::V4_RANDOM,
7575
self::V5_SHA1,
7676
);
77+
78+
public $ltrim;
79+
public $rtrim;
7780
}

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

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

8181
$value = (string) $value;
8282

83+
$value = $constraint->ltrim ? ltrim($value) : $value;
84+
$value = $constraint->rtrim ? rtrim($value) : $value;
85+
8386
if ($constraint->strict) {
8487
$this->validateStrict($value, $constraint);
8588

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

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

88+
/**
89+
* @dataProvider getValidStrictUuidsWithLeadingWhitespaces
90+
*/
91+
public function testValidStrictUuidsWithLeadingWhitespaces($uuid, $versions = null)
92+
{
93+
$constraint = new Uuid(array('ltrim' => true));
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 getValidStrictUuidsWithLeadingWhitespaces()
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("\x0A216FFF40-98D9-11E3-A5E2-0800200C9A66"), // Version 1 UUID in UPPERCASE
110+
array("\x0D456daefb-5aa6-41b5-8dbc-068b05a8b201"), // Version 4 UUID in lowercase
111+
array("\x00456daEFb-5AA6-41B5-8DBC-068B05A8B201"), // Version 4 UUID in mixed case
112+
array("\x0B\x0B456daEFb-5AA6-41B5-8DBC-068B05A8B201", array(Uuid::V4_RANDOM)),
113+
);
114+
}
115+
116+
/**
117+
* @dataProvider getValidStrictUuidsWithTrailingWhitespaces
118+
*/
119+
public function testValidStrictUuidsWithTrailingWhitespaces($uuid, $versions = null)
120+
{
121+
$constraint = new Uuid(array('rtrim' => true));
122+
123+
if (null !== $versions) {
124+
$constraint->versions = $versions;
125+
}
126+
127+
$this->validator->validate($uuid, $constraint);
128+
129+
$this->assertNoViolation();
130+
}
131+
132+
public function getValidStrictUuidsWithTrailingWhitespaces()
133+
{
134+
return array(
135+
array("216fff40-98d9-11e3-a5e2-0800200c9a66\x20"), // Version 1 UUID in lowercase
136+
array("216fff40-98d9-11e3-a5e2-0800200c9a66\x09\x09", array(Uuid::V1_MAC)),
137+
array("216FFF40-98D9-11E3-A5E2-0800200C9A66\x0A"), // Version 1 UUID in UPPERCASE
138+
array("456daefb-5aa6-41b5-8dbc-068b05a8b201\x0D"), // Version 4 UUID in lowercase
139+
array("456daEFb-5AA6-41B5-8DBC-068B05A8B201\x00"), // Version 4 UUID in mixed case
140+
array("456daEFb-5AA6-41B5-8DBC-068B05A8B201\x0B\x0B", array(Uuid::V4_RANDOM)),
141+
);
142+
}
143+
88144
/**
89145
* @dataProvider getInvalidStrictUuids
90146
*/

0 commit comments

Comments 314E
 (0)
0