diff --git a/src/Attributes/Validation/Url.php b/src/Attributes/Validation/Url.php index 743c8723..9ae71a47 100644 --- a/src/Attributes/Validation/Url.php +++ b/src/Attributes/Validation/Url.php @@ -3,10 +3,20 @@ namespace Spatie\LaravelData\Attributes\Validation; use Attribute; +use Illuminate\Support\Arr; +use Spatie\LaravelData\Support\Validation\References\ExternalReference; #[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)] class Url extends StringValidationAttribute { + protected array $protocols; + + public function __construct( + string|array|ExternalReference ...$protocols + ) { + $this->protocols = Arr::flatten($protocols); + } + public static function keyword(): string { return 'url'; @@ -14,6 +24,6 @@ public static function keyword(): string public function parameters(): array { - return []; + return $this->protocols; } } diff --git a/tests/Datasets/RulesDataset.php b/tests/Datasets/RulesDataset.php index b44c2473..819ee5a7 100644 --- a/tests/Datasets/RulesDataset.php +++ b/tests/Datasets/RulesDataset.php @@ -442,10 +442,7 @@ function fixature( expected: 'uppercase', ); - yield fixature( - attribute: new Url(), - expected: 'url', - ); + yield from urlAttributes(); yield fixature( attribute: new Ulid(), @@ -492,6 +489,29 @@ function acceptedIfAttributes(): Generator ); } +function urlAttributes(): Generator +{ + yield fixature( + attribute: new Url(), + expected: 'url', + ); + + yield fixature( + attribute: new Url('http'), + expected: 'url:http', + ); + + yield fixature( + attribute: new Url(['http', 'https']), + expected: 'url:http,https', + ); + + yield fixature( + attribute: new Url('http', 'https'), + expected: 'url:http,https', + ); +} + function afterAttributes(): Generator { yield fixature(