Skip to content

Commit 67ea544

Browse files
committed
1. fix README.md的isEmpty的使用方式、删除重复信息
2. fix 内置的isEmpty、isCheckRequired的判断逻辑 3. add test case
1 parent d2397ed commit 67ea544

File tree

5 files changed

+57
-15
lines changed

5 files changed

+57
-15
lines changed

README.md

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ validate 同时支持两种规则配置方式,对应了两种规则的收集
4141
### 单字段多规则
4242

4343
- `FieldValidation`: **每条规则中,只能有一个字段,但允许多个验证器。** (_规则配置类似于Laravel_)
44+
- `FieldValidation`: 不支持验证器`each`
4445

4546
配置示例:
4647

@@ -570,21 +571,12 @@ $v->validate();
570571

571572
```php
572573
['users.*.id', 'each', 'required', 'isEmpty' => function($value) {
573-
if ($value instanceof \Inhere\Validate\Exception\ArrayValueNotExists) {
574-
return true;
575-
}
576-
// your code here ...
577-
}]
578-
```
579-
规则包含 .* 时的自定义验证,如: users.*.id
580-
```php
581-
['users.*.id', 'string', 'isEmpty' => function(array $value) {
574+
// each value must be verified
582575
foreach ($value as $item) {
583576
if ($item instanceof \Inhere\Validate\Exception\ArrayValueNotExists) {
584577
return true;
585-
}
586-
// your code here ...
587578
}
579+
// your code here ...
588580
}]
589581
```
590582

src/Traits/ScopedValidatorsTrait.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,11 +663,12 @@ public static function isCheckFile(string $name): bool
663663

664664
/**
665665
* @param string $name
666-
*
666+
* @param array $args
667667
* @return bool
668668
*/
669-
public static function isCheckRequired(string $name): bool
669+
public static function isCheckRequired(string $name, array $args = []): bool
670670
{
671+
$name = $name === 'each' ? (strval($args[0] ?? '')) : $name;
671672
return 0 === strpos($name, 'required');
672673
}
673674

src/ValidationTrait.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ protected function applyRule($fields, array $rule, array $onlyChecked, bool $sto
316316
}
317317

318318
// required*系列字段检查 || 文件资源检查
319-
if (self::isCheckRequired($validator) || self::isCheckFile($validator)) {
319+
if (self::isCheckRequired($validator, $args) || self::isCheckFile($validator)) {
320320
$result = $this->fieldValidate($field, $value, $validator, $args, $defMsg);
321321
if (false === $result && $stopOnError) {
322322
break;
@@ -375,6 +375,9 @@ protected function fieldValidate(string $field, $value, string $validator, array
375375
// other required* methods
376376
} elseif (method_exists($this, $validator)) {
377377
$passed = $this->$validator($field, $value, ...array_values($args));
378+
} elseif (method_exists($this, $method = $validator . 'Validator')) {
379+
$passed = $this->$method($value, ...$args);
380+
// if $validator is a global custom validator {@see UserValidators::$validators}.
378381
} else {
379382
throw new InvalidArgumentException("The validator [$validator] is not exists!");
380383
}

src/Validators.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,14 @@ public static function isEmpty($val): bool
125125
{
126126
if (is_string($val)) {
127127
$val = trim($val);
128+
} elseif (is_array($val)) {
129+
// each value must be verified
130+
foreach ($val as $item) {
131+
if (($item instanceof ArrayValueNotExists)) {
132+
$val = [];
133+
break;
134+
}
135+
}
128136
} elseif (is_object($val)) {
129137
if ($val instanceof ArrayValueNotExists) {
130138
$val = '';

test/RuleValidationTest.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -770,7 +770,6 @@ public function testIssues21(): void
770770
],
771771
];
772772
$rs = [
773-
['users.*.id', 'required'], // will passed. values like: [ArrayValueNotExists, ArrayValueNotExists]
774773
['users.*.id', 'each', 'required'],
775774
];
776775

@@ -790,6 +789,18 @@ public function testIssues21(): void
790789
$this->assertFalse($v->isOk());
791790
$this->assertSame('users.*.id each value must be through the "required" verify', $v->firstError());
792791

792+
$rs = [
793+
['users.*.id', 'required'], // will not pass.
794+
];
795+
796+
$v = RV::check($d1, $rs);
797+
//parameter users.*.id is required!
798+
$this->assertFalse($v->isOk());
799+
800+
$v = RV::check($d2, $rs);
801+
//parameter users.*.id is required!
802+
$this->assertFalse($v->isOk());
803+
793804
$d3 = [
794805
'users' => [
795806
['id' => 1, 'name' => 'n1'],
@@ -803,4 +814,31 @@ public function testIssues21(): void
803814

804815
$this->assertTrue($v->isOk());
805816
}
817+
818+
/**
819+
* @link https://github.com/inhere/php-validate/issues/33
820+
*/
821+
public function testIssues33(): void
822+
{
823+
$d = [
824+
'users' => [
825+
['id' => 34, 'name' => 'tom'],
826+
['id' => 89],
827+
],
828+
];
829+
830+
$rs1 = [
831+
['users.*.name', 'each', 'required'],
832+
['users.*.name', 'each', 'string']
833+
];
834+
$v2 = RuleValidation::check($d, $rs1);
835+
836+
$this->assertTrue($v2->isFail());
837+
838+
$rs2[] = ['users.*.name', 'each', 'string'];
839+
840+
$v1 = RuleValidation::check($d, $rs2);
841+
$this->assertFalse($v1->isFail());
842+
$this->assertTrue($v1->isOk());
843+
}
806844
}

0 commit comments

Comments
 (0)