Skip to content

Commit

Permalink
Merge pull request #510 from oreillysean/main
Browse files Browse the repository at this point in the history
Added Ulid validation rule.
  • Loading branch information
rubenvanassche authored Aug 4, 2023
2 parents 6d1acd2 + eeebcf9 commit 26793f8
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
9 changes: 9 additions & 0 deletions docs/advanced-usage/validation-attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,15 @@ public string $closure;
public string $closure;
```

### Ulid

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

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

### Uuid

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

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 @@ -80,6 +80,7 @@
use Spatie\LaravelData\Attributes\Validation\Timezone;
use Spatie\LaravelData\Attributes\Validation\Unique;
use Spatie\LaravelData\Attributes\Validation\Url;
use Spatie\LaravelData\Attributes\Validation\Ulid;
use Spatie\LaravelData\Attributes\Validation\Uuid;
use Spatie\LaravelData\Exceptions\CouldNotCreateValidationRule;

Expand Down Expand Up @@ -178,6 +179,7 @@ protected function mapping(): array
Timezone::keyword() => Timezone::class,
Unique::keyword() => Unique::class,
Url::keyword() => Url::class,
Ulid::keyword() => Ulid::class,
Uuid::keyword() => Uuid::class,
];
}
Expand Down
6 changes: 6 additions & 0 deletions tests/Datasets/Attributes/RulesDataset.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
use Spatie\LaravelData\Attributes\Validation\Timezone;
use Spatie\LaravelData\Attributes\Validation\Unique;
use Spatie\LaravelData\Attributes\Validation\Url;
use Spatie\LaravelData\Attributes\Validation\Ulid;
use Spatie\LaravelData\Attributes\Validation\Uuid;
use Spatie\LaravelData\Exceptions\CannotBuildValidationRule;
use Spatie\LaravelData\Support\Validation\ValidationRule;
Expand Down Expand Up @@ -362,6 +363,11 @@ function fixature(
expected: 'url',
);

yield fixature(
attribute: new Ulid(),
expected: 'ulid',
);

yield fixature(
attribute: new Uuid(),
expected: 'uuid',
Expand Down

0 comments on commit 26793f8

Please sign in to comment.