Skip to content

Commit

Permalink
[Validator] handle null values properly
Browse files Browse the repository at this point in the history
  • Loading branch information
jschaedl committed Sep 30, 2018
1 parent 0b6e408 commit 73e1e81
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 0 deletions.
Expand Up @@ -31,6 +31,10 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\NegativeOrZero');
}

if (null === $value) {
return;
}

if (!\is_int($value) && !\is_float($value) && !\is_string($value)) {
throw new UnexpectedTypeException($value, 'int, float or string');
}
Expand Down
Expand Up @@ -31,6 +31,10 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Negative');
}

if (null === $value) {
return;
}

if (!\is_int($value) && !\is_float($value) && !\is_string($value)) {
throw new UnexpectedTypeException($value, 'int, float or string');
}
Expand Down
Expand Up @@ -31,6 +31,10 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\PositiveOrZero');
}

if (null === $value) {
return;
}

if (!\is_int($value) && !\is_float($value) && !\is_string($value)) {
throw new UnexpectedTypeException($value, 'int, float or string');
}
Expand Down
Expand Up @@ -31,6 +31,10 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Positive');
}

if (null === $value) {
return;
}

if (!\is_int($value) && !\is_float($value) && !\is_string($value)) {
throw new UnexpectedTypeException($value, 'int, float or string');
}
Expand Down
Expand Up @@ -91,4 +91,11 @@ public function getWrongTypeValues()
array(new \stdClass()),
);
}

public function testItShouldDoNothingForNullValue()
{
$this->validator->validate(null, new NegativeOrZero());

$this->assertNoViolation();
}
}
Expand Up @@ -114,4 +114,11 @@ public function getWrongTypeValues()
array(new \stdClass()),
);
}

public function testItShouldDoNothingForNullValue()
{
$this->validator->validate(null, new Negative());

$this->assertNoViolation();
}
}
Expand Up @@ -89,4 +89,11 @@ public function getWrongTypeValues()
array(new \stdClass()),
);
}

public function testItShouldDoNothingForNullValue()
{
$this->validator->validate(null, new PositiveOrZero());

$this->assertNoViolation();
}
}
Expand Up @@ -114,4 +114,11 @@ public function getWrongTypeValues()
array(new \stdClass()),
);
}

public function testItShouldDoNothingForNullValue()
{
$this->validator->validate(null, new Positive());

$this->assertNoViolation();
}
}

0 comments on commit 73e1e81

Please sign in to comment.