Skip to content

Commit

Permalink
bug fixed for json validate.
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 14, 2017
1 parent 9ae5a61 commit 9812edb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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']`
Expand Down
10 changes: 8 additions & 2 deletions src/ValidatorList.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
22 changes: 22 additions & 0 deletions tests/RuleValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 [
Expand Down
16 changes: 16 additions & 0 deletions tests/ValidatorListTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}'));
}
}
2 changes: 1 addition & 1 deletion tests/boot.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* phpunit6.phar --bootstrap tests/bootstap.php tests
* phpunit6.phar --bootstrap tests/boot.php tests
*/

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

0 comments on commit 9812edb

Please sign in to comment.