8000 [Validator] handle null values properly · symfony/symfony@73e1e81 · GitHub
[go: up one dir, main page]

Skip to content

Commit 73e1e81

Browse files
committed
[Validator] handle null values properly
1 parent 0b6e408 commit 73e1e81

8 files changed

+44
-0
lines changed

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

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

34+
if (null === $value) {
35+
return;
36+
}
37+
3438
if (!\is_int($value) && !\is_float($value) && !\is_string($value)) {
3539
throw new UnexpectedTypeException($value, 'int, float or string');
3640
}

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

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

34+
if (null === $value) {
35+
return;
36+
}
37+
3438
if (!\is_int($value) && !\is_float($value) && !\is_string($value)) {
3539
throw new UnexpectedTypeException($value, 'int, float or string');
3640
}

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

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

34+
if (null === $value) {
35+
return;
36+
}
37+
3438
if (!\is_int($value) && !\is_float($value) && !\is_string($value)) {
3539
throw new UnexpectedTypeException($value, 'int, float or string');
3640
}

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

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

34+
if (null === $value) {
35+
return;
36+
}
37+
3438
if (!\is_int($value) && !\is_float($value) && !\is_string($value)) {
3539
throw new UnexpectedTypeException($value, 'int, float or string');
3640
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,11 @@ public function getWrongTypeValues()
9191
array(new \stdClass()),
9292
);
9393
}
94+
95+
public function testItShouldDoNothingForNullValue()
96+
{
97+
$this->validator->validate(null, new NegativeOrZero());
98+
99+
$this->assertNoViolation();
100+
}
94101
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,11 @@ public function getWrongTypeValues()
114114
array(new \stdClass()),
115115
);
116116
}
117+
118+
public function testItShouldDoNothingForNullValue()
119+
{
120+
$this->validator->validate(null, new Negative());
121+
122+
$this->assertNoViolation();
123+
}
117124
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,4 +89,11 @@ public function getWrongTypeValues()
8989
array(new \stdClass()),
9090
);
9191
}
92+
93+
public function testItShouldDoNothingForNullValue()
94+
{
95+
$this->validator->validate(null, new PositiveOrZero());
96+
97+
$this->assertNoViolation();
98+
}
9299
}
9A3A

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,4 +114,11 @@ public function getWrongTypeValues()
114114
array(new \stdClass()),
115115
);
116116
}
117+
118+
public function testItShouldDoNothingForNullValue()
119+
{
120+
$this->validator->validate(null, new Positive());
121+
122+
$this->assertNoViolation();
123+
}
117124
}

0 commit comments

Comments
 (0)
0