Skip to content

Commit

Permalink
Merge pull request #28174 from JonathanGawrych/5.8-inequality-validat…
Browse files Browse the repository at this point in the history
…ion-fail-not-500

[5.8] Make inequality validation fail on different types rather than 500
  • Loading branch information
taylorotwell authored Apr 11, 2019
2 parents 176d5d3 + 1d348b1 commit 9cf256c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,9 @@ public function validateGt($attribute, $value, $parameters)
return $this->getSize($attribute, $value) > $parameters[0];
}

$this->requireSameType($value, $comparedToValue);
if (! $this->isSameType($value, $comparedToValue)) {
return false;
}

return $this->getSize($attribute, $value) > $this->getSize($attribute, $comparedToValue);
}
Expand All @@ -916,7 +918,9 @@ public function validateLt($attribute, $value, $parameters)
return $this->getSize($attribute, $value) < $parameters[0];
}

$this->requireSameType($value, $comparedToValue);
if (! $this->isSameType($value, $comparedToValue)) {
return false;
}

return $this->getSize($attribute, $value) < $this->getSize($attribute, $comparedToValue);
}
Expand All @@ -941,7 +945,9 @@ public function validateGte($attribute, $value, $parameters)
return $this->getSize($attribute, $value) >= $parameters[0];
}

$this->requireSameType($value, $comparedToValue);
if (! $this->isSameType($value, $comparedToValue)) {
return false;
}

return $this->getSize($attribute, $value) >= $this->getSize($attribute, $comparedToValue);
}
Expand All @@ -966,7 +972,9 @@ public function validateLte($attribute, $value, $parameters)
return $this->getSize($attribute, $value) <= $parameters[0];
}

$this->requireSameType($value, $comparedToValue);
if (! $this->isSameType($value, $comparedToValue)) {
return false;
}

return $this->getSize($attribute, $value) <= $this->getSize($attribute, $comparedToValue);
}
Expand Down Expand Up @@ -1721,19 +1729,15 @@ public function requireParameterCount($count, $parameters, $rule)
}

/**
* Require comparison values to be of the same type.
* Check if the parameters are of the same type.
*
* @param mixed $first
* @param mixed $second
* @return void
*
* @throws \InvalidArgumentException
* @return bool
*/
protected function requireSameType($first, $second)
protected function isSameType($first, $second)
{
if (gettype($first) != gettype($second)) {
throw new InvalidArgumentException('The values under comparison must be of the same type');
}
return gettype($first) == gettype($second);
}

/**
Expand Down
24 changes: 24 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,12 @@ public function testGreaterThan()
$v = new Validator($trans, ['lhs' => 15, 'rhs' => 10], ['lhs' => 'numeric|gt:rhs']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['lhs' => 15, 'rhs' => 'string'], ['lhs' => 'numeric|gt:rhs']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|gt:rhs']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'gt:rhs']);
$this->assertTrue($v->passes());

Expand All @@ -1108,6 +1114,12 @@ public function testLessThan()
$v = new Validator($trans, ['lhs' => 15, 'rhs' => 10], ['lhs' => 'numeric|lt:rhs']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['lhs' => 15, 'rhs' => 'string'], ['lhs' => 'numeric|lt:rhs']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|lt:rhs']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'lt:rhs']);
$this->assertTrue($v->fails());

Expand All @@ -1131,6 +1143,12 @@ public function testGreaterThanOrEqual()
$v = new Validator($trans, ['lhs' => 15, 'rhs' => 15], ['lhs' => 'numeric|gte:rhs']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['lhs' => 15, 'rhs' => 'string'], ['lhs' => 'numeric|gte:rhs']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|gte:rhs']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'gte:rhs']);
$this->assertTrue($v->passes());

Expand All @@ -1154,6 +1172,12 @@ public function testLessThanOrEqual()
$v = new Validator($trans, ['lhs' => 15, 'rhs' => 15], ['lhs' => 'numeric|lte:rhs']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['lhs' => 15, 'rhs' => 'string'], ['lhs' => 'numeric|lte:rhs']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['lhs' => 15], ['lhs' => 'numeric|lte:rhs']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['lhs' => 'longer string', 'rhs' => 'string'], ['lhs' => 'lte:rhs']);
$this->assertTrue($v->fails());

Expand Down

0 comments on commit 9cf256c

Please sign in to comment.