diff --git a/README.md b/README.md index bb52d3d..a4ff4d9 100644 --- a/README.md +++ b/README.md @@ -665,7 +665,7 @@ public function get(string $key, $default = null) `beforeOrEqualDate` | 字段值必须是小于或等于给定日期的值 | `['publishedAt', 'beforeOrEqualDate', '2017-05-12']` `afterOrEqualDate` | 字段值必须是大于或等于给定日期的值 | `['publishedAt', 'afterOrEqualDate', '2017-05-12']` `afterDate` | 验证字段值必须是给定日期之前的值 | `['publishedAt', 'afterDate', '2017-05-12']` -`json` | 验证是否是json字符串 | `['goods', 'json']` +`json` | 验证是否是json字符串(默认严格验证,必须以`{` `[` 开始) | `['goods', 'json']` `['somedata', 'json', false]` - 非严格,普通字符串`eg 'test'`也会通过 `file` | 验证是否是上传的文件 | `['upFile', 'file']` `image` | 验证是否是上传的图片文件 | `['avatar', 'image']`, 限定后缀名 `['avatar', 'image', 'jpg,png']` `ip` | 验证是否是 IP | `['ipAddr', 'ip']` diff --git a/src/ValidatorList.php b/src/ValidatorList.php index 136af12..bc5a29d 100644 --- a/src/ValidatorList.php +++ b/src/ValidatorList.php @@ -645,15 +645,21 @@ public static function strList($val) /** * 验证字段值是否是一个有效的 JSON 字符串。 * @param string $val + * @param bool $strict * @return bool */ - public static function json($val) + public static function json($val, $strict = true) { if (!$val || (!\is_string($val) && !method_exists($val, '__toString'))) { return false; } - json_decode($val); + // must start with: { OR [ + if ($strict && '[' !== $val[0] && '{' !== $val[0]) { + return false; + } + + json_decode((string)$val); return json_last_error() === JSON_ERROR_NONE; } diff --git a/tests/RuleValidationTest.php b/tests/RuleValidationTest.php index df5d79d..9255a31 100644 --- a/tests/RuleValidationTest.php +++ b/tests/RuleValidationTest.php @@ -95,6 +95,28 @@ public function testValidateString() $this->assertEquals($v->getSafe('user_name'), $val); } + public function testValidateJson() + { + $v = Validation::make([ + 'log_level' => 'debug', + 'log_data' => '[23]', + 'log_data1' => '234', + ], [ + ['log_level, log_data', 'required'], + ['log_level, log_data', 'string'], + ['log_data', 'json'], + ['log_data1', 'json', false], + ])->validate(); + + // var_dump($v->getErrors()); + $this->assertTrue($v->passed()); + $this->assertFalse($v->failed()); + + $errors = $v->getErrors(); + $this->assertEmpty($errors); + $this->assertCount(0, $errors); + } + protected function someRules() { return [ diff --git a/tests/ValidatorListTest.php b/tests/ValidatorListTest.php index 4d91464..7c26c98 100644 --- a/tests/ValidatorListTest.php +++ b/tests/ValidatorListTest.php @@ -245,4 +245,20 @@ public function testStrList() $this->assertTrue(ValidatorList::strList(['abc'])); $this->assertTrue(ValidatorList::strList(['abc', 565, null])); } + + public function testJson() + { + $this->assertFalse(ValidatorList::json('test')); + $this->assertFalse(ValidatorList::json([])); + + $this->assertFalse(ValidatorList::json(123)); + $this->assertFalse(ValidatorList::json('123')); + $this->assertTrue(ValidatorList::json('123', false)); + + $this->assertFalse(ValidatorList::json('{aa: 34}')); + + $this->assertTrue(ValidatorList::json('{}')); + $this->assertTrue(ValidatorList::json('[]')); + $this->assertTrue(ValidatorList::json('{"aa": 34}')); + } } diff --git a/tests/boot.php b/tests/boot.php index fe9000a..b8dfdcb 100644 --- a/tests/boot.php +++ b/tests/boot.php @@ -1,6 +1,6 @@