Skip to content

Commit

Permalink
Merge pull request #587 from Klaas058/klaas058/add-lowercase-uppercas…
Browse files Browse the repository at this point in the history
…e-validation-rules

Add Lowercase and Uppercase validation rules
  • Loading branch information
rubenvanassche authored Dec 1, 2023
2 parents d37ef56 + 22a6ee9 commit d21e6c7
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 0 deletions.
18 changes: 18 additions & 0 deletions docs/advanced-usage/validation-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,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 @@ -989,6 +998,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 @@ -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\MaxDigits;
Expand Down Expand Up @@ -89,6 +90,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 @@ -162,6 +164,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,
MaxDigits::keyword() => MaxDigits::class,
Expand Down Expand Up @@ -196,6 +199,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 @@ -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\Lowercase;
use Spatie\LaravelData\Attributes\Validation\MacAddress;
use Spatie\LaravelData\Attributes\Validation\Max;
use Spatie\LaravelData\Attributes\Validation\MaxDigits;
Expand Down Expand Up @@ -92,6 +93,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 @@ -312,6 +314,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 @@ -397,6 +404,11 @@ function fixature(
expected: 'timezone',
);

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

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

0 comments on commit d21e6c7

Please sign in to comment.