Skip to content

Commit 9812edb

Browse files
committed
bug fixed for json validate.
1 parent 9ae5a61 commit 9812edb

File tree

5 files changed

+48
-4
lines changed

5 files changed

+48
-4
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ public function get(string $key, $default = null)
665665
`beforeOrEqualDate` | 字段值必须是小于或等于给定日期的值 | `['publishedAt', 'beforeOrEqualDate', '2017-05-12']`
666666
`afterOrEqualDate` | 字段值必须是大于或等于给定日期的值 | `['publishedAt', 'afterOrEqualDate', '2017-05-12']`
667667
`afterDate` | 验证字段值必须是给定日期之前的值 | `['publishedAt', 'afterDate', '2017-05-12']`
668-
`json` | 验证是否是json字符串 | `['goods', 'json']`
668+
`json` | 验证是否是json字符串(默认严格验证,必须以`{` `[` 开始) | `['goods', 'json']` `['somedata', 'json', false]` - 非严格,普通字符串`eg 'test'`也会通过
669669
`file` | 验证是否是上传的文件 | `['upFile', 'file']`
670670
`image` | 验证是否是上传的图片文件 | `['avatar', 'image']`, 限定后缀名 `['avatar', 'image', 'jpg,png']`
671671
`ip` | 验证是否是 IP | `['ipAddr', 'ip']`

src/ValidatorList.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -645,15 +645,21 @@ public static function strList($val)
645645
/**
646646
* 验证字段值是否是一个有效的 JSON 字符串。
647647
* @param string $val
648+
* @param bool $strict
648649
* @return bool
649650
*/
650-
public static function json($val)
651+
public static function json($val, $strict = true)
651652
{
652653
if (!$val || (!\is_string($val) && !method_exists($val, '__toString'))) {
653654
return false;
654655
}
655656

656-
json_decode($val);
657+
// must start with: { OR [
658+
if ($strict && '[' !== $val[0] && '{' !== $val[0]) {
659+
return false;
660+
}
661+
662+
json_decode((string)$val);
657663

658664
return json_last_error() === JSON_ERROR_NONE;
659665
}

tests/RuleValidationTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,28 @@ public function testValidateString()
9595
$this->assertEquals($v->getSafe('user_name'), $val);
9696
}
9797

98+
public function testValidateJson()
99+
{
100+
$v = Validation::make([
101+
'log_level' => 'debug',
102+
'log_data' => '[23]',
103+
'log_data1' => '234',
104+
], [
105+
['log_level, log_data', 'required'],
106+
['log_level, log_data', 'string'],
107+
['log_data', 'json'],
108+
['log_data1', 'json', false],
109+
])->validate();
110+
111+
// var_dump($v->getErrors());
112+
$this->assertTrue($v->passed());
113+
$this->assertFalse($v->failed());
114+
115+
$errors = $v->getErrors();
116+
$this->assertEmpty($errors);
117+
$this->assertCount(0, $errors);
118+
}
119+
98120
protected function someRules()
99121
{
100122
return [

tests/ValidatorListTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,4 +245,20 @@ public function testStrList()
245245
$this->assertTrue(ValidatorList::strList(['abc']));
246246
$this->assertTrue(ValidatorList::strList(['abc', 565, null]));
247247
}
248+
249+
public function testJson()
250+
{
251+
$this->assertFalse(ValidatorList::json('test'));
252+
$this->assertFalse(ValidatorList::json([]));
253+
254+
$this->assertFalse(ValidatorList::json(123));
255+
$this->assertFalse(ValidatorList::json('123'));
256+
$this->assertTrue(ValidatorList::json('123', false));
257+
258+
$this->assertFalse(ValidatorList::json('{aa: 34}'));
259+
260+
$this->assertTrue(ValidatorList::json('{}'));
261+
$this->assertTrue(ValidatorList::json('[]'));
262+
$this->assertTrue(ValidatorList::json('{"aa": 34}'));
263+
}
248264
}

tests/boot.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
/**
3-
* phpunit6.phar --bootstrap tests/bootstap.php tests
3+
* phpunit6.phar --bootstrap tests/boot.php tests
44
*/
55

66
require dirname(__DIR__) . '/examples/simple-loader.php';

0 commit comments

Comments
 (0)