Skip to content

Commit 331f819

Browse files
authored
Merge pull request #41 from sourcetoad/enum-rule-callback
Enum rule modifier callback
2 parents 688b3a6 + 9cb9e70 commit 331f819

File tree

5 files changed

+39
-5
lines changed

5 files changed

+39
-5
lines changed

UPGRADE.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## v4
44

5+
### Upgrading from v4.2 to v4.3
6+
7+
- Minimum Laravel version increased from `10.34` to `10.46`.
8+
59
### Upgrading from v4.1 to v4.2
610

711
- Minimum Laravel version increased from `10.33` to `10.34`.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"minimum-stability": "stable",
77
"require": {
88
"php": "^8.1",
9-
"laravel/framework": "^10.34"
9+
"laravel/framework": "^10.46"
1010
},
1111
"require-dev": {
1212
"ext-json": "*",

src/RuleSet.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,8 @@ public function digitsBetween(int $min, int $max): self
377377
* pass a callback which accepts a {@see \Illuminate\Validation\Rules\Dimensions} instance.
378378
*
379379
* @link https://laravel.com/docs/10.x/validation#rule-dimensions
380+
* @param array<string, int|float> $constraints
381+
* @param ?callable(\Illuminate\Validation\Rules\Dimensions): void $modifier
380382
*/
381383
public function dimensions(array $constraints, ?callable $modifier = null): self
382384
{
@@ -444,10 +446,17 @@ public function endsWith(string ...$value): self
444446
*
445447
* @link https://laravel.com/docs/10.x/validation#rule-enum
446448
* @param class-string $type
449+
* @param ?callable(\Illuminate\Validation\Rules\Enum): void $modifier
447450
*/
448-
public function enum(string $type): self
451+
public function enum(string $type, ?callable $modifier = null): self
449452
{
450-
return $this->rule(Rule::enum($type));
453+
$rule = Rule::enum($type);
454+
455+
if ($modifier) {
456+
$modifier($rule);
457+
}
458+
459+
return $this->rule($rule);
451460
}
452461

453462
/**
@@ -526,6 +535,7 @@ public function excludeWithout(string $anotherField): self
526535
* {@see RuleSet::rule} or pass a callback which accepts an {@see \Illuminate\Validation\Rules\Exists} instance.
527536
*
528537
* @link https://laravel.com/docs/10.x/validation#rule-exists
538+
* @param ?callable(\Illuminate\Validation\Rules\Exists): void $modifier
529539
*/
530540
public function exists(string $table, string $column = 'NULL', ?callable $modifier = null): self
531541
{
@@ -899,6 +909,7 @@ public function numeric(): self
899909
* use {@see Rule::password} with {@see RuleSet::rule}, or pass a callback which accepts a {@see Password} instance.
900910
*
901911
* @link https://laravel.com/docs/10.x/validation#validating-passwords
912+
* @param ?callable(\Illuminate\Validation\Rules\Password): void $modifier
902913
*/
903914
public function password(?int $size = null, ?callable $modifier = null): self
904915
{
@@ -972,7 +983,7 @@ public function prohibited(): self
972983
* passed in closure returns true.
973984
*
974985
* @link https://laravel.com/docs/10.x/validation#rule-prohibited-if
975-
* @param callable|bool $callback
986+
* @param bool|callable(): bool $callback
976987
*/
977988
public function prohibitedIf(mixed $callback): self
978989
{
@@ -1044,7 +1055,7 @@ public function requiredArrayKeys(string ...$key): self
10441055
* closure returns true.
10451056
*
10461057
* @link https://laravel.com/docs/10.x/validation#rule-required-if
1047-
* @param callable|bool $callback
1058+
* @param bool|callable(): bool $callback
10481059
*/
10491060
public function requiredIf(mixed $callback): self
10501061
{
@@ -1226,6 +1237,7 @@ public function ulid(): self
12261237
* {@see RuleSet::rule} or pass a callback which accepts a {@see \Illuminate\Validation\Rules\Unique} instance.
12271238
*
12281239
* @link https://laravel.com/docs/10.x/validation#rule-unique
1240+
* @param ?callable(\Illuminate\Validation\Rules\Unique): void $modifier
12291241
*/
12301242
public function unique(string $table, string $column = 'NULL', ?callable $modifier = null): self
12311243
{

tests/Stubs/ExampleStringEnum.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66

77
enum ExampleStringEnum: string
88
{
9+
case Another = 'another';
910
case Valid = 'valid';
1011
}

tests/Unit/RuleTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Illuminate\Support\Facades\Validator;
1919
use Illuminate\Support\Str;
2020
use Illuminate\Validation\Rules\Dimensions;
21+
use Illuminate\Validation\Rules\Enum;
2122
use Illuminate\Validation\Rules\Password;
2223
use Sourcetoad\RuleHelper\Rule;
2324
use Sourcetoad\RuleHelper\RuleSet;
@@ -953,6 +954,22 @@ public function ruleDataProvider(): array
953954
'rules' => fn() => RuleSet::create()->enum(ExampleIntEnum::class),
954955
'fails' => true,
955956
],
957+
'enum string constrained valid' => [
958+
'data' => 'another',
959+
'rules' => fn() => RuleSet::create()->enum(
960+
ExampleStringEnum::class,
961+
fn(Enum $rule) => $rule->only(ExampleStringEnum::Another),
962+
),
963+
'fails' => false,
964+
],
965+
'enum string constrained invalid' => [
966+
'data' => 'another',
967+
'rules' => fn() => RuleSet::create()->enum(
968+
ExampleStringEnum::class,
969+
fn(Enum $rule) => $rule->except(ExampleStringEnum::Another),
970+
),
971+
'fails' => true,
972+
],
956973
'extensions valid' => [
957974
'data' => fn() => $this->mockFile('/code/image.jpg'),
958975
'rules' => fn() => RuleSet::create()->extensions('jpg', 'png'),

0 commit comments

Comments
 (0)