Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8837c1f

Browse files
Xint0Xint0-elab
andauthoredDec 23, 2023
[10.x] Avoid TypeError when using json validation rule when PHP < 8.3 (laravel#49474)
* test: validateJson should return false when value is null Fails with Laravel Framework 10.38.2 in PHP < 8.3, introduced in laravel#49413 * fix: validateJson should return false when value is null Return false when $value is null. Avoid TypeError: json_validate(): Argument #1 ($json) must be of type string, null given, when using symfony/polyfill-php83 in PHP < 8.3. Avoid deprecation warning: json_validate(): Passing null to parameter #1 ($json) of type string is deprecated, when using PHP 8.3. --------- Co-authored-by: Rogelio Jacinto <[email protected]>
1 parent 6c53284 commit 8837c1f

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed
 

Diff for: ‎src/Illuminate/Validation/Concerns/ValidatesAttributes.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1430,11 +1430,11 @@ public function validateMacAddress($attribute, $value)
14301430
*/
14311431
public function validateJson($attribute, $value)
14321432
{
1433-
if (is_array($value)) {
1433+
if (is_array($value) || is_null($value)) {
14341434
return false;
14351435
}
14361436

1437-
if (! is_scalar($value) && ! is_null($value) && ! method_exists($value, '__toString')) {
1437+
if (! is_scalar($value) && ! method_exists($value, '__toString')) {
14381438
return false;
14391439
}
14401440

Diff for: ‎tests/Validation/ValidationValidatorTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Illuminate\Support\Arr;
2020
use Illuminate\Support\Carbon;
2121
use Illuminate\Support\Exceptions\MathException;
22+
use Illuminate\Support\Stringable;
2223
use Illuminate\Translation\ArrayLoader;
2324
use Illuminate\Translation\Translator;
2425
use Illuminate\Validation\DatabasePresenceVerifierInterface;
@@ -2817,6 +2818,14 @@ public function testValidateJson()
28172818
$trans = $this->getIlluminateArrayTranslator();
28182819
$v = new Validator($trans, ['foo' => ['array']], ['foo' => 'json']);
28192820
$this->assertFalse($v->passes());
2821+
2822+
$trans = $this->getIlluminateArrayTranslator();
2823+
$v = new Validator($trans, ['foo' => null], ['foo' => 'json']);
2824+
$this->assertFalse($v->passes());
2825+
2826+
$trans = $this->getIlluminateArrayTranslator();
2827+
$v = new Validator($trans, ['foo' => new Stringable('[]')], ['foo' => 'json']);
2828+
$this->assertTrue($v->passes());
28202829
}
28212830

28222831
public function testValidateBoolean()

0 commit comments

Comments
 (0)
Please sign in to comment.