Skip to content

Commit

Permalink
Added list rule for hyperf/validation (#6811)
Browse files Browse the repository at this point in the history
  • Loading branch information
guandeng committed May 30, 2024
1 parent 429b734 commit a940bc5
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions publish/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
'ipv4' => 'The :attribute must be a valid IPv4 address.',
'ipv6' => 'The :attribute must be a valid IPv6 address.',
'json' => 'The :attribute must be a valid JSON string.',
'list' => 'The :attribute must be a list.',
'lt' => [
'numeric' => 'The :attribute must be less than :value',
'file' => 'The :attribute must be less than :value kb',
Expand Down
1 change: 1 addition & 0 deletions publish/zh_CN/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
'ipv4' => ':attribute 必须是一个合法的 IPv4 地址',
'ipv6' => ':attribute 必须是一个合法的 IPv6 地址',
'json' => ':attribute 必须是一个合法的 JSON 字符串',
'list' => ':attribute 必须是一个数组列表',
'lt' => [
'numeric' => ':attribute 必须小于 :value',
'file' => ':attribute 必须小于 :value kb',
Expand Down
8 changes: 8 additions & 0 deletions src/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,14 @@ public function validateArray(string $attribute, $value, array $parameters = [])
return empty(array_diff_key($value, array_fill_keys($parameters, '')));
}

/**
* Validate that an attribute is a list.
*/
public function validateList(string $attribute, mixed $value): bool
{
return is_array($value) && array_is_list($value);
}

/**
* Validate that an array has all of the given keys.
*
Expand Down
17 changes: 17 additions & 0 deletions tests/Cases/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,23 @@ public function testValidateArray()
$this->assertTrue($v->passes());
}

public function testValidateList()
{
$trans = $this->getIlluminateArrayTranslator();

$v = new Validator($trans, ['foo' => [1, 2, 3]], ['foo' => 'list']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['foo' => [1 => 1, 2 => 2]], ['foo' => 'list']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['foo' => [0 => 'a', 'b' => 'b', 2 => 'c']], ['foo' => 'list']);
$this->assertFalse($v->passes());

$v = new Validator($trans, ['foo' => []], ['foo' => 'list']);
$this->assertTrue($v->passes());
}

public function testValidateFilled()
{
$trans = $this->getIlluminateArrayTranslator();
Expand Down

0 comments on commit a940bc5

Please sign in to comment.