From 1d348b1a7956242f439d56077f881675cddad94d Mon Sep 17 00:00:00 2001 From: Jonathan Gawrych Date: Wed, 10 Apr 2019 12:42:48 -0600 Subject: [PATCH] Make inequality validation fail on different types rather than 500 There are four types of inequality validators: lt, lte, gt, gte. If these validators reference another attribute, the attribute must be of the same type. Before this change, if the payload has different types, a 500 error is given. However, the type is not determine by the rule configuration, but rather the HTTP request. Instead of throwing an error, just fail the validation. --- .../Concerns/ValidatesAttributes.php | 28 +++++++++++-------- tests/Validation/ValidationValidatorTest.php | 24 ++++++++++++++++ 2 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 28371530e633..98eda67680aa 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -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); } @@ -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); } @@ -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); } @@ -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); } @@ -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); } /** diff --git a/tests/Validation/ValidationValidatorTest.php b/tests/Validation/ValidationValidatorTest.php index bea82017deaf..a615df2e37db 100755 --- a/tests/Validation/ValidationValidatorTest.php +++ b/tests/Validation/ValidationValidatorTest.php @@ -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()); @@ -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()); @@ -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()); @@ -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());