Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Lowercase and Uppercase validation rules #587

Merged
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
18 changes: 18 additions & 0 deletions docs/advanced-usage/validation-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,15 @@ public int $closure;
public int $closure;
```

### Lowercase

[Docs](https://laravel.com/docs/9.x/validation#rule-lowercase)

```php
#[Lowercase]
public string $closure;
```

### MacAddress

[Docs](https://laravel.com/docs/9.x/validation#rule-mac)
Expand Down Expand Up @@ -930,6 +939,15 @@ public string $closure;
public string $closure;
```

### Uppercase

[Docs](https://laravel.com/docs/9.x/validation#rule-uppercase)

```php
#[Uppercase]
public string $closure;
```

### Url

[Docs](https://laravel.com/docs/9.x/validation#rule-url)
Expand Down
19 changes: 19 additions & 0 deletions src/Attributes/Validation/Lowercase.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 Lowercase extends StringValidationAttribute
{
public static function keyword(): string
{
return 'lowercase';
}

public function parameters(): array
{
return [];
}
}
19 changes: 19 additions & 0 deletions src/Attributes/Validation/Uppercase.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 Uppercase extends StringValidationAttribute
{
public static function keyword(): string
{
return 'uppercase';
}

public function parameters(): array
{
return [];
}
}
4 changes: 4 additions & 0 deletions src/Support/Validation/ValidationRuleFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
use Spatie\LaravelData\Attributes\Validation\Json;
use Spatie\LaravelData\Attributes\Validation\LessThan;
use Spatie\LaravelData\Attributes\Validation\LessThanOrEqualTo;
use Spatie\LaravelData\Attributes\Validation\Lowercase;
use Spatie\LaravelData\Attributes\Validation\MacAddress;
use Spatie\LaravelData\Attributes\Validation\Max;
use Spatie\LaravelData\Attributes\Validation\Mimes;
Expand Down Expand Up @@ -84,6 +85,7 @@
use Spatie\LaravelData\Attributes\Validation\Timezone;
use Spatie\LaravelData\Attributes\Validation\Ulid;
use Spatie\LaravelData\Attributes\Validation\Unique;
use Spatie\LaravelData\Attributes\Validation\Uppercase;
use Spatie\LaravelData\Attributes\Validation\Url;
use Spatie\LaravelData\Attributes\Validation\Uuid;
use Spatie\LaravelData\Exceptions\CouldNotCreateValidationRule;
Expand Down Expand Up @@ -154,6 +156,7 @@ protected function mapping(): array
Json::keyword() => Json::class,
LessThan::keyword() => LessThan::class,
LessThanOrEqualTo::keyword() => LessThanOrEqualTo::class,
Lowercase::keyword() => Lowercase::class,
MacAddress::keyword() => MacAddress::class,
Max::keyword() => Max::class,
Mimes::keyword() => Mimes::class,
Expand Down Expand Up @@ -186,6 +189,7 @@ protected function mapping(): array
StringType::keyword() => StringType::class,
Timezone::keyword() => Timezone::class,
Unique::keyword() => Unique::class,
Uppercase::keyword() => Uppercase::class,
Url::keyword() => Url::class,
Ulid::keyword() => Ulid::class,
Uuid::keyword() => Uuid::class,
Expand Down
12 changes: 12 additions & 0 deletions tests/Datasets/Attributes/RulesDataset.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\Lowercase;
use Spatie\LaravelData\Attributes\Validation\MacAddress;
use Spatie\LaravelData\Attributes\Validation\Max;
use Spatie\LaravelData\Attributes\Validation\Mimes;
Expand Down Expand Up @@ -87,6 +88,7 @@
use Spatie\LaravelData\Attributes\Validation\Timezone;
use Spatie\LaravelData\Attributes\Validation\Ulid;
use Spatie\LaravelData\Attributes\Validation\Unique;
use Spatie\LaravelData\Attributes\Validation\Uppercase;
use Spatie\LaravelData\Attributes\Validation\Url;
use Spatie\LaravelData\Attributes\Validation\Uuid;
use Spatie\LaravelData\Exceptions\CannotBuildValidationRule;
Expand Down Expand Up @@ -300,6 +302,11 @@ function fixature(
expected: 'lte:field',
);

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

yield fixature(
attribute: new MacAddress(),
expected: 'mac_address',
Expand Down Expand Up @@ -375,6 +382,11 @@ function fixature(
expected: 'timezone',
);

yield fixature(
attribute: new Uppercase(),
expected: 'uppercase',
);

yield fixature(
attribute: new Url(),
expected: 'url',
Expand Down
Loading