Skip to content

Commit a940bc5

Browse files
authored
Added list rule for hyperf/validation (#6811)
1 parent 429b734 commit a940bc5

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

publish/en/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
'ipv4' => 'The :attribute must be a valid IPv4 address.',
7272
'ipv6' => 'The :attribute must be a valid IPv6 address.',
7373
'json' => 'The :attribute must be a valid JSON string.',
74+
'list' => 'The :attribute must be a list.',
7475
'lt' => [
7576
'numeric' => 'The :attribute must be less than :value',
7677
'file' => 'The :attribute must be less than :value kb',

publish/zh_CN/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
'ipv4' => ':attribute 必须是一个合法的 IPv4 地址',
7272
'ipv6' => ':attribute 必须是一个合法的 IPv6 地址',
7373
'json' => ':attribute 必须是一个合法的 JSON 字符串',
74+
'list' => ':attribute 必须是一个数组列表',
7475
'lt' => [
7576
'numeric' => ':attribute 必须小于 :value',
7677
'file' => ':attribute 必须小于 :value kb',

src/Concerns/ValidatesAttributes.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,14 @@ public function validateArray(string $attribute, $value, array $parameters = [])
254254
return empty(array_diff_key($value, array_fill_keys($parameters, '')));
255255
}
256256

257+
/**
258+
* Validate that an attribute is a list.
259+
*/
260+
public function validateList(string $attribute, mixed $value): bool
261+
{
262+
return is_array($value) && array_is_list($value);
263+
}
264+
257265
/**
258266
* Validate that an array has all of the given keys.
259267
*

tests/Cases/ValidationValidatorTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,23 @@ public function testValidateArray()
665665
$this->assertTrue($v->passes());
666666
}
667667

668+
public function testValidateList()
669+
{
670+
$trans = $this->getIlluminateArrayTranslator();
671+
672+
$v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'list']);
673+
$this->assertTrue($v->passes());
674+
675+
$v = new Validator($trans, ['foo' => [1 => 1, 2 => 2]], ['foo' => 'list']);
676+
$this->assertFalse($v->passes());
677+
678+
$v = new Validator($trans, ['foo' => [0 => 'a', 'b' => 'b', 2 => 'c']], ['foo' => 'list']);
679+
$this->assertFalse($v->passes());
680+
681+
$v = new Validator($trans, ['foo' => []], ['foo' => 'list']);
682+
$this->assertTrue($v->passes());
683+
}
684+
668685
public function testValidateFilled()
669686
{
670687
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)