Skip to content
Open
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
12 changes: 11 additions & 1 deletion src/Attributes/Validation/Url.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,27 @@
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';
}

public function parameters(): array
{
return [];
return $this->protocols;
}
}
28 changes: 24 additions & 4 deletions tests/Datasets/RulesDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -442,10 +442,7 @@ function fixature(
expected: 'uppercase',
);

yield fixature(
attribute: new Url(),
expected: 'url',
);
yield from urlAttributes();

yield fixature(
attribute: new Ulid(),
Expand Down Expand Up @@ -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(
Expand Down