Skip to content

Commit 49dd2f5

Browse files
authored
[11.x] Fix validation to not throw incompatible validation exception (#55963)
* [11.x] Fix validation to not throw incompatible validation exception fixes #55944 Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> * wip Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com> --------- Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
1 parent 0a5f42b commit 49dd2f5

File tree

2 files changed

+77
-25
lines changed

2 files changed

+77
-25
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 75 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -471,10 +471,14 @@ public function validateBetween($attribute, $value, $parameters)
471471
{
472472
$this->requireParameterCount(2, $parameters, 'between');
473473

474-
return with(
475-
BigNumber::of($this->getSize($attribute, $value)),
476-
fn ($size) => $size->isGreaterThanOrEqualTo($this->trim($parameters[0])) && $size->isLessThanOrEqualTo($this->trim($parameters[1]))
477-
);
474+
try {
475+
return with(
476+
BigNumber::of($this->getSize($attribute, $value)),
477+
fn ($size) => $size->isGreaterThanOrEqualTo($this->trim($parameters[0])) && $size->isLessThanOrEqualTo($this->trim($parameters[1]))
478+
);
479+
} catch (MathException) {
480+
return false;
481+
}
478482
}
479483

480484
/**
@@ -1223,22 +1227,34 @@ public function validateGt($attribute, $value, $parameters)
12231227
$this->shouldBeNumeric($attribute, 'Gt');
12241228

12251229
if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
1226-
return BigNumber::of($this->getSize($attribute, $value))->isGreaterThan($this->trim($parameters[0]));
1230+
try {
1231+
return BigNumber::of($this->getSize($attribute, $value))->isGreaterThan($this->trim($parameters[0]));
1232+
} catch (MathException) {
1233+
return false;
1234+
}
12271235
}
12281236

12291237
if (is_numeric($parameters[0])) {
12301238
return false;
12311239
}
12321240

12331241
if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) {
1234-
return BigNumber::of($this->trim($value))->isGreaterThan($this->trim($comparedToValue));
1242+
try {
1243+
return BigNumber::of($this->trim($value))->isGreaterThan($this->trim($comparedToValue));
1244+
} catch (MathException) {
1245+
return false;
1246+
}
12351247
}
12361248

12371249
if (! $this->isSameType($value, $comparedToValue)) {
12381250
return false;
12391251
}
12401252

1241-
return $this->getSize($attribute, $value) > $this->getSize($attribute, $comparedToValue);
1253+
try {
1254+
return $this->getSize($attribute, $value) > $this->getSize($attribute, $comparedToValue);
1255+
} catch (MathException) {
1256+
return false;
1257+
}
12421258
}
12431259

12441260
/**
@@ -1258,7 +1274,11 @@ public function validateLt($attribute, $value, $parameters)
12581274
$this->shouldBeNumeric($attribute, 'Lt');
12591275

12601276
if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
1261-
return BigNumber::of($this->getSize($attribute, $value))->isLessThan($this->trim($parameters[0]));
1277+
try {
1278+
return BigNumber::of($this->getSize($attribute, $value))->isLessThan($this->trim($parameters[0]));
1279+
} catch (MathException) {
1280+
return false;
1281+
}
12621282
}
12631283

12641284
if (is_numeric($parameters[0])) {
@@ -1273,7 +1293,11 @@ public function validateLt($attribute, $value, $parameters)
12731293
return false;
12741294
}
12751295

1276-
return $this->getSize($attribute, $value) < $this->getSize($attribute, $comparedToValue);
1296+
try {
1297+
return $this->getSize($attribute, $value) < $this->getSize($attribute, $comparedToValue);
1298+
} catch (MathException) {
1299+
return false;
1300+
}
12771301
}
12781302

12791303
/**
@@ -1293,22 +1317,34 @@ public function validateGte($attribute, $value, $parameters)
12931317
$this->shouldBeNumeric($attribute, 'Gte');
12941318

12951319
if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
1296-
return BigNumber::of($this->getSize($attribute, $value))->isGreaterThanOrEqualTo($this->trim($parameters[0]));
1320+
try {
1321+
return BigNumber::of($this->getSize($attribute, $value))->isGreaterThanOrEqualTo($this->trim($parameters[0]));
1322+
} catch (MathException) {
1323+
return false;
1324+
}
12971325
}
12981326

12991327
if (is_numeric($parameters[0])) {
13001328
return false;
13011329
}
13021330

13031331
if ($this->hasRule($attribute, $this->numericRules) && is_numeric($value) && is_numeric($comparedToValue)) {
1304-
return BigNumber::of($this->trim($value))->isGreaterThanOrEqualTo($this->trim($comparedToValue));
1332+
try {
1333+
return BigNumber::of($this->trim($value))->isGreaterThanOrEqualTo($this->trim($comparedToValue));
1334+
} catch (MathException) {
1335+
return false;
1336+
}
13051337
}
13061338

13071339
if (! $this->isSameType($value, $comparedToValue)) {
13081340
return false;
13091341
}
13101342

1311-
return $this->getSize($attribute, $value) >= $this->getSize($attribute, $comparedToValue);
1343+
try {
1344+
return $this->getSize($attribute, $value) >= $this->getSize($attribute, $comparedToValue);
1345+
} catch (MathException) {
1346+
return false;
1347+
}
13121348
}
13131349

13141350
/**
@@ -1328,7 +1364,11 @@ public function validateLte($attribute, $value, $parameters)
13281364
$this->shouldBeNumeric($attribute, 'Lte');
13291365

13301366
if (is_null($comparedToValue) && (is_numeric($value) && is_numeric($parameters[0]))) {
1331-
return BigNumber::of($this->getSize($attribute, $value))->isLessThanOrEqualTo($this->trim($parameters[0]));
1367+
try {
1368+
return BigNumber::of($this->getSize($attribute, $value))->isLessThanOrEqualTo($this->trim($parameters[0]));
1369+
} catch (MathException) {
1370+
return false;
1371+
}
13321372
}
13331373

13341374
if (is_numeric($parameters[0])) {
@@ -1343,7 +1383,11 @@ public function validateLte($attribute, $value, $parameters)
13431383
return false;
13441384
}
13451385

1346-
return $this->getSize($attribute, $value) <= $this->getSize($attribute, $comparedToValue);
1386+
try {
1387+
return $this->getSize($attribute, $value) <= $this->getSize($attribute, $comparedToValue);
1388+
} catch (MathException) {
1389+
return false;
1390+
}
13471391
}
13481392

13491393
/**
@@ -1544,7 +1588,11 @@ public function validateMax($attribute, $value, $parameters)
15441588
return false;
15451589
}
15461590

1547-
return BigNumber::of($this->getSize($attribute, $value))->isLessThanOrEqualTo($this->trim($parameters[0]));
1591+
try {
1592+
return BigNumber::of($this->getSize($attribute, $value))->isLessThanOrEqualTo($this->trim($parameters[0]));
1593+
} catch (MathException) {
1594+
return false;
1595+
}
15481596
}
15491597

15501598
/**
@@ -1646,7 +1694,11 @@ public function validateMin($attribute, $value, $parameters)
16461694
{
16471695
$this->requireParameterCount(1, $parameters, 'min');
16481696

1649-
return BigNumber::of($this->getSize($attribute, $value))->isGreaterThanOrEqualTo($this->trim($parameters[0]));
1697+
try {
1698+
return BigNumber::of($this->getSize($attribute, $value))->isGreaterThanOrEqualTo($this->trim($parameters[0]));
1699+
} catch (MathException) {
1700+
return false;
1701+
}
16501702
}
16511703

16521704
/**
@@ -2466,7 +2518,11 @@ public function validateSize($attribute, $value, $parameters)
24662518
{
24672519
$this->requireParameterCount(1, $parameters, 'size');
24682520

2469-
return BigNumber::of($this->getSize($attribute, $value))->isEqualTo($this->trim($parameters[0]));
2521+
try {
2522+
return BigNumber::of($this->getSize($attribute, $value))->isEqualTo($this->trim($parameters[0]));
2523+
} catch (MathException) {
2524+
return false;
2525+
}
24702526
}
24712527

24722528
/**
@@ -2738,6 +2794,8 @@ protected function trim($value)
27382794
* @param string $attribute
27392795
* @param mixed $value
27402796
* @return mixed
2797+
*
2798+
* @throws \Illuminate\Support\Exceptions\MathException
27412799
*/
27422800
protected function ensureExponentWithinAllowedRange($attribute, $value)
27432801
{

tests/Validation/ValidationValidatorTest.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Illuminate\Database\Eloquent\Model;
1919
use Illuminate\Support\Arr;
2020
use Illuminate\Support\Carbon;
21-
use Illuminate\Support\Exceptions\MathException;
2221
use Illuminate\Support\Stringable;
2322
use Illuminate\Translation\ArrayLoader;
2423
use Illuminate\Translation\Translator;
@@ -9534,10 +9533,7 @@ public function testItLimitsLengthOfScientificNotationExponent($value)
95349533
$trans = $this->getIlluminateArrayTranslator();
95359534
$validator = new Validator($trans, ['foo' => $value], ['foo' => 'numeric|min:3']);
95369535

9537-
$this->expectException(MathException::class);
9538-
$this->expectExceptionMessage('Scientific notation exponent outside of allowed range.');
9539-
9540-
$validator->passes();
9536+
$this->assertFalse($validator->passes());
95419537
}
95429538

95439539
public static function outsideRangeExponents()
@@ -9592,10 +9588,8 @@ public function testItCanConfigureAllowedExponentRange()
95929588
$this->assertSame('1.0e-1000', $value);
95939589

95949590
$withinRange = false;
9595-
$this->expectException(MathException::class);
9596-
$this->expectExceptionMessage('Scientific notation exponent outside of allowed range.');
95979591

9598-
$validator->passes();
9592+
$this->assertFalse($validator->passes());
95999593
}
96009594

96019595
protected function getTranslator()

0 commit comments

Comments
 (0)