Skip to content

Commit

Permalink
feat(validation): ArrayList rule (#745)
Browse files Browse the repository at this point in the history
  • Loading branch information
hungthai1401 authored Nov 19, 2024
1 parent fd59089 commit ddda992
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Tempest/Validation/src/Rules/ArrayList.php
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';
}
}
25 changes: 25 additions & 0 deletions src/Tempest/Validation/tests/Rules/ArrayListTest.php
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());
}
}

0 comments on commit ddda992

Please # to comment.