Skip to content

Commit

Permalink
bug fixed for: json validate, sence rules collection
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 14, 2017
1 parent f45cfd8 commit ceed770
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
8 changes: 6 additions & 2 deletions src/FieldValidation.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,17 @@ protected function collectRules()
}
// an rule for special scene.
if (!empty($rule['on'])) {
if (!$scene) {
continue;
}
$sceneList = \is_string($rule['on']) ? array_map('trim', explode(',', $rule['on'])) : (array)$rule['on'];
if ($scene && !\in_array($scene, $sceneList, true)) {
if (!\in_array($scene, $sceneList, true)) {
continue;
}
unset($rule['on']);
$this->_usedRules[] = $rule;
}

$this->_usedRules[] = $rule;
$field = array_shift($rule);
if (\is_object($rule[0])) {
(yield $field => $rule);
Expand Down
15 changes: 8 additions & 7 deletions src/ValidationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -360,18 +360,19 @@ protected function collectRules()
if (!isset($rule[1]) || !$rule[1]) {
throw new \InvalidArgumentException('The rule validator is must be setting! position: rule[1].');
}
// global rule.
if (empty($rule['on'])) {
$this->_usedRules[] = $rule;
// only use to special scene.
} else {
// only use to special scene.
if (!empty($rule['on'])) {
if (!$scene) {
continue;
}
$sceneList = \is_string($rule['on']) ? Helper::explode($rule['on']) : (array)$rule['on'];
if ($scene && !\in_array($scene, $sceneList, true)) {
if (!\in_array($scene, $sceneList, true)) {
continue;
}
unset($rule['on']);
$this->_usedRules[] = $rule;
}

$this->_usedRules[] = $rule;
$fields = array_shift($rule);

(yield $fields => $this->prepareRule($rule));
Expand Down
13 changes: 11 additions & 2 deletions src/ValidatorList.php
Original file line number Diff line number Diff line change
Expand Up @@ -562,14 +562,23 @@ public static function strList($val)

/**
* 验证字段值是否是一个有效的 JSON 字符串。
* @param string $val
* @param mixed $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;
}

$val = (string)$val;

// must start with: { OR [
if ($strict && '[' !== $val[0] && '{' !== $val[0]) {
return false;
}

json_decode($val);

return json_last_error() === JSON_ERROR_NONE;
Expand Down

0 comments on commit ceed770

Please sign in to comment.