Skip to content

Commit

Permalink
Merge pull request #728 from riesjart/add-list-validation-attribute
Browse files Browse the repository at this point in the history
Add attribute for Laravel's `list` validation rule
  • Loading branch information
rubenvanassche authored Apr 4, 2024
2 parents 20cbdc1 + 61ab302 commit 707ccfc
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Attributes/Validation/ListType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Spatie\LaravelData\Attributes\Validation;

use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
class ListType extends StringValidationAttribute
{
public static function keyword(): string
{
return 'list';
}

public function parameters(): array
{
return [];
}
}
2 changes: 2 additions & 0 deletions src/Support/Validation/ValidationRuleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
use Spatie\LaravelData\Attributes\Validation\Json;
use Spatie\LaravelData\Attributes\Validation\LessThan;
use Spatie\LaravelData\Attributes\Validation\LessThanOrEqualTo;
use Spatie\LaravelData\Attributes\Validation\ListType;
use Spatie\LaravelData\Attributes\Validation\Lowercase;
use Spatie\LaravelData\Attributes\Validation\MacAddress;
use Spatie\LaravelData\Attributes\Validation\Max;
Expand Down Expand Up @@ -164,6 +165,7 @@ protected function mapping(): array
Json::keyword() => Json::class,
LessThan::keyword() => LessThan::class,
LessThanOrEqualTo::keyword() => LessThanOrEqualTo::class,
ListType::keyword() => ListType::class,
Lowercase::keyword() => Lowercase::class,
MacAddress::keyword() => MacAddress::class,
Max::keyword() => Max::class,
Expand Down
6 changes: 6 additions & 0 deletions tests/Datasets/RulesDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
use Spatie\LaravelData\Attributes\Validation\Json;
use Spatie\LaravelData\Attributes\Validation\LessThan;
use Spatie\LaravelData\Attributes\Validation\LessThanOrEqualTo;
use Spatie\LaravelData\Attributes\Validation\ListType;
use Spatie\LaravelData\Attributes\Validation\Lowercase;
use Spatie\LaravelData\Attributes\Validation\MacAddress;
use Spatie\LaravelData\Attributes\Validation\Max;
Expand Down Expand Up @@ -310,6 +311,11 @@ function fixature(
expected: 'lte:field',
);

yield fixature(
attribute: new ListType(),
expected: 'list',
);

yield fixature(
attribute: new Lowercase(),
expected: 'lowercase',
Expand Down

0 comments on commit 707ccfc

Please sign in to comment.