Skip to content

Commit ceed770

Browse files
committed
bug fixed for: json validate, sence rules collection
1 parent f45cfd8 commit ceed770

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

src/FieldValidation.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,17 @@ protected function collectRules()
4747
}
4848
// an rule for special scene.
4949
if (!empty($rule['on'])) {
50+
if (!$scene) {
51+
continue;
52+
}
5053
$sceneList = \is_string($rule['on']) ? array_map('trim', explode(',', $rule['on'])) : (array)$rule['on'];
51-
if ($scene && !\in_array($scene, $sceneList, true)) {
54+
if (!\in_array($scene, $sceneList, true)) {
5255
continue;
5356
}
5457
unset($rule['on']);
55-
$this->_usedRules[] = $rule;
5658
}
59+
60+
$this->_usedRules[] = $rule;
5761
$field = array_shift($rule);
5862
if (\is_object($rule[0])) {
5963
(yield $field => $rule);

src/ValidationTrait.php

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -360,18 +360,19 @@ protected function collectRules()
360360
if (!isset($rule[1]) || !$rule[1]) {
361361
throw new \InvalidArgumentException('The rule validator is must be setting! position: rule[1].');
362362
}
363-
// global rule.
364-
if (empty($rule['on'])) {
365-
$this->_usedRules[] = $rule;
366-
// only use to special scene.
367-
} else {
363+
// only use to special scene.
364+
if (!empty($rule['on'])) {
365+
if (!$scene) {
366+
continue;
367+
}
368368
$sceneList = \is_string($rule['on']) ? Helper::explode($rule['on']) : (array)$rule['on'];
369-
if ($scene && !\in_array($scene, $sceneList, true)) {
369+
if (!\in_array($scene, $sceneList, true)) {
370370
continue;
371371
}
372372
unset($rule['on']);
373-
$this->_usedRules[] = $rule;
374373
}
374+
375+
$this->_usedRules[] = $rule;
375376
$fields = array_shift($rule);
376377

377378
(yield $fields => $this->prepareRule($rule));

src/ValidatorList.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -562,14 +562,23 @@ public static function strList($val)
562562

563563
/**
564564
* 验证字段值是否是一个有效的 JSON 字符串。
565-
* @param string $val
565+
* @param mixed $val
566+
* @param bool $strict
566567
* @return bool
567568
*/
568-
public static function json($val)
569+
public static function json($val, $strict = true)
569570
{
570571
if (!$val || (!\is_string($val) && !method_exists($val, '__toString'))) {
571572
return false;
572573
}
574+
575+
$val = (string)$val;
576+
577+
// must start with: { OR [
578+
if ($strict && '[' !== $val[0] && '{' !== $val[0]) {
579+
return false;
580+
}
581+
573582
json_decode($val);
574583

575584
return json_last_error() === JSON_ERROR_NONE;

0 commit comments

Comments
 (0)