Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] add list rule #50454

Merged
merged 3 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Illuminate/Translation/lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
'ipv4' => 'The :attribute field must be a valid IPv4 address.',
'ipv6' => 'The :attribute field must be a valid IPv6 address.',
'json' => 'The :attribute field must be a valid JSON string.',
'list' => 'The :attribute field must be a list.',
'lowercase' => 'The :attribute field must be lowercase.',
'lt' => [
'array' => 'The :attribute field must have less than :value items.',
Expand Down
12 changes: 12 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,18 @@ public function validateArray($attribute, $value, $parameters = [])
return empty(array_diff_key($value, array_fill_keys($parameters, '')));
}

/**
* Validate that an attribute is a list.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function validateList($attribute, $value)
{
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/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,23 @@ public function testValidateArray()
$this->assertFalse($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' => new File('/tmp/foo', false)], ['foo' => 'list']);
$this->assertFalse($v->passes());

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

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

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