Skip to content

Commit 9f24287

Browse files
luck-xjlimingxinleohuangdijia
authored
Fixed bug that validateJson was incompatibility with php 8.0 (#6146)
* Fixed bug that `validateJson` cannot work when using php 8.0. * Fixed unit cases for redis cannot work. Co-authored-by: 李铭昕 <715557344@qq.com> Co-authored-by: Deeka Wong <8337659+huangdijia@users.noreply.github.com>
1 parent d962d33 commit 9f24287

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/Concerns/ValidatesAttributes.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Hyperf\Validation\ValidationData;
2828
use InvalidArgumentException;
2929
use SplFileInfo;
30+
use Stringable;
3031
use Throwable;
3132

3233
use function Hyperf\Collection\last;
@@ -743,13 +744,17 @@ public function validateIpv6(string $attribute, $value): bool
743744
*/
744745
public function validateJson(string $attribute, $value): bool
745746
{
746-
if (! is_scalar($value) && ! method_exists($value, '__toString')) {
747-
return false;
747+
if ($value instanceof Stringable) {
748+
$value = (string) $value;
748749
}
749750

750-
json_decode($value);
751+
try {
752+
json_decode($value, flags: JSON_THROW_ON_ERROR);
753+
} catch (Throwable) {
754+
return false;
755+
}
751756

752-
return json_last_error() === JSON_ERROR_NONE;
757+
return true;
753758
}
754759

755760
/**

tests/Cases/ValidateAttributesTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use HyperfTest\Validation\Cases\Stub\ValidatesAttributesStub;
1515
use PHPUnit\Framework\TestCase;
16+
use Stringable;
1617

1718
/**
1819
* @internal
@@ -68,4 +69,41 @@ public function testValidateDate()
6869

6970
$this->assertFalse($validator->validateDate('', 123));
7071
}
72+
73+
public function testValidateJson()
74+
{
75+
$validator = new ValidatesAttributesStub();
76+
// null
77+
$this->assertFalse($validator->validateJson('', null));
78+
$this->assertTrue($validator->validateJson('', 'null'));
79+
// int
80+
$this->assertTrue($validator->validateJson('', '3'));
81+
$this->assertFalse($validator->validateJson('', 3));
82+
// float
83+
$this->assertTrue($validator->validateJson('', '3.14'));
84+
$this->assertFalse($validator->validateJson('', 3.14));
85+
// string
86+
$this->assertFalse($validator->validateJson('', 'plain_text'));
87+
$this->assertTrue($validator->validateJson('', '{"foo": "bar"}'));
88+
$this->assertFalse($validator->validateJson('', '{"foo": "bar",a}'));
89+
// array
90+
$this->assertTrue($validator->validateJson('', '[3.14]'));
91+
$this->assertFalse($validator->validateJson('', [3.14]));
92+
$this->assertTrue($validator->validateJson('', '["a"]'));
93+
// object
94+
$this->assertFalse($validator->validateJson('', new class() {}));
95+
$this->assertTrue($validator->validateJson('', new class() implements Stringable {
96+
public function __toString(): string
97+
{
98+
return json_encode(['foo' => 'bar'], JSON_UNESCAPED_UNICODE);
99+
}
100+
}));
101+
102+
$this->assertTrue($validator->validateJson('', new class() {
103+
public function __toString(): string
104+
{
105+
return json_encode(['foo' => 'bar'], JSON_UNESCAPED_UNICODE);
106+
}
107+
}));
108+
}
71109
}

0 commit comments

Comments
 (0)