-
-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(validation):
ArrayList
rule (#745)
- Loading branch information
1 parent
fd59089
commit ddda992
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Validation\Rules; | ||
|
||
use Attribute; | ||
use Tempest\Validation\Rule; | ||
|
||
#[Attribute] | ||
final readonly class ArrayList implements Rule | ||
{ | ||
public function isValid(mixed $value): bool | ||
{ | ||
return is_array($value) && array_is_list($value); | ||
} | ||
|
||
public function message(): string | ||
{ | ||
return 'Value must be a list'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tempest\Validation\Tests\Rules; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Tempest\Validation\Rules\ArrayList; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ArrayListTest extends TestCase | ||
{ | ||
public function test_array_list(): void | ||
{ | ||
$rule = new ArrayList(); | ||
|
||
$this->assertFalse($rule->isValid(['foo' => 'bar'])); | ||
$this->assertTrue($rule->isValid([])); | ||
$this->assertTrue($rule->isValid(['a', 'b', 'c'])); | ||
$this->assertFalse($rule->isValid([0 => 'a', 1 => 'b', 3 => 'c'])); | ||
$this->assertSame('Value must be a list', $rule->message()); | ||
} | ||
} |