Skip to content

Commit 22658a2

Browse files
authored
In type fixes (#44)
* docs: Added missing documentation links * update: Updated in and not in types to match Laravel type changes * update: Updated minimum version for type fix
1 parent d0c969b commit 22658a2

File tree

5 files changed

+86
-33
lines changed

5 files changed

+86
-33
lines changed

UPGRADE.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
### Upgrading from v5.0 to v5.1
66

7-
- Minimum Laravel version increased from `11.0.3` to `11.4`.
7+
- Minimum Laravel version increased from `11.0.3` to `11.5`.
88

99
### Upgrading from v4.3 to v5.0
1010

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.2",
9-
"laravel/framework": "^11.4"
9+
"laravel/framework": "^11.5"
1010
},
1111
"require-dev": {
1212
"ext-json": "*",

src/Rule.php

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Sourcetoad\RuleHelper;
66

7+
use BackedEnum;
78
use Brick\Math\BigNumber;
89
use DateTimeInterface;
910
use Illuminate\Contracts\Support\Arrayable;
@@ -25,6 +26,7 @@
2526
use Illuminate\Validation\Rules\RequiredIf;
2627
use Illuminate\Validation\Rules\Unique;
2728
use Stringable;
29+
use UnitEnum;
2830

2931
class Rule
3032
{
@@ -561,9 +563,9 @@ public static function image(): string
561563
* list of values provided to the *in* rule.
562564
*
563565
* @link https://laravel.com/docs/11.x/validation#rule-in
564-
* @param Arrayable<array-key, mixed>|array<array-key, mixed>|string $values
566+
* @param Arrayable<array-key, BackedEnum|UnitEnum|string>|array<BackedEnum|UnitEnum|string>|BackedEnum|UnitEnum|string $values
565567
*/
566-
public static function in(Arrayable|array|string $values): In
568+
public static function in(Arrayable|BackedEnum|UnitEnum|array|string $values): In
567569
{
568570
return LaravelRule::in($values);
569571
}
@@ -632,7 +634,10 @@ public static function json(): string
632634
}
633635

634636
/**
635-
* The field under validation must be a list style array.
637+
* The field under validation must be an array that is a list. An array is considered a list if its keys consist of
638+
* consecutive numbers from 0 to count($array) - 1.
639+
*
640+
* @link https://laravel.com/docs/11.x/validation#rule-list
636641
*/
637642
public static function list(): string
638643
{
@@ -804,9 +809,9 @@ public static function multipleOf(int|float $value): string
804809
* The field under validation must not be included in the given list of values.
805810
*
806811
* @link https://laravel.com/docs/11.x/validation#rule-not-in
807-
* @param Arrayable<array-key, mixed>|array<array-key, mixed>|string $values
812+
* @param Arrayable<array-key, BackedEnum|UnitEnum|string>|array<BackedEnum|UnitEnum|string>|BackedEnum|UnitEnum|string $values
808813
*/
809-
public static function notIn(Arrayable|array|string $values): NotIn
814+
public static function notIn(Arrayable|BackedEnum|UnitEnum|array|string $values): NotIn
810815
{
811816
return LaravelRule::notIn($values);
812817
}
@@ -866,35 +871,39 @@ public static function present(): string
866871
}
867872

868873
/**
869-
* The field under validation must be present but can be empty if *anotherField* under validation is equal to a
870-
* specified value.
874+
* The field under validation must be present if the *anotherField* field is equal to any *value*.
875+
*
876+
* @link https://laravel.com/docs/11.x/validation#rule-present-if
871877
*/
872878
public static function presentIf(string $anotherField, string ...$value): string
873879
{
874880
return sprintf('present_if:%s,%s', $anotherField, implode(',', $value));
875881
}
876882

877883
/**
878-
* The field under validation must be present but can be empty unless the *anotherField* field is equal to any
879-
* *value*.
884+
* The field under validation must be present unless the *anotherField* field is equal to any *value*.
885+
*
886+
* @link https://laravel.com/docs/11.x/validation#rule-present-unless
880887
*/
881888
public static function presentUnless(string $anotherField, string ...$value): string
882889
{
883890
return sprintf('present_unless:%s,%s', $anotherField, implode(',', $value));
884891
}
885892

886893
/**
887-
* The field under validation must be present but can be empty *only if* any of the other specified fields are
888-
* present and not empty.
894+
* The field under validation must be present *only if* any of the other specified fields are present.
895+
*
896+
* @link https://laravel.com/docs/11.x/validation#rule-present-with
889897
*/
890898
public static function presentWith(string ...$field): string
891899
{
892900
return 'present_with:'.implode(',', $field);
893901
}
894902

895903
/**
896-
* The field under validation must be present but can be empty *only if* all the other specified fields are present
897-
* and not empty.
904+
* The field under validation must be present *only if* all the other specified fields are present.
905+
*
906+
* @link https://laravel.com/docs/11.x/validation#rule-present-with-all
898907
*/
899908
public static function presentWithAll(string ...$field): string
900909
{
@@ -996,7 +1005,7 @@ public static function requiredIf(mixed $callback): RequiredIf
9961005
}
9971006

9981007
/**
999-
* The field under validation must be present and not empty if the `field` field is equal to yes, on, 1, "1", true,
1008+
* The field under validation must be present and not empty if the *field* field is equal to yes, on, 1, "1", true,
10001009
* or "true".
10011010
*
10021011
* @link https://laravel.com/docs/11.x/validation#rule-required-if-accepted
@@ -1027,7 +1036,7 @@ public static function requiredIfAny(RequiredIf ...$rules): RequiredIf
10271036
}
10281037

10291038
/**
1030-
* The field under validation must be present and not empty if the `field` field is equal to "no", "off", 0, "0",
1039+
* The field under validation must be present and not empty if the *field* field is equal to "no", "off", 0, "0",
10311040
* false, or "false".
10321041
*
10331042
* @link https://laravel.com/docs/11.x/validation#rule-required-if-declined

src/RuleSet.php

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@
55
namespace Sourcetoad\RuleHelper;
66

77
use ArrayIterator;
8+
use BackedEnum;
89
use Brick\Math\BigNumber;
910
use DateTimeInterface;
1011
use Illuminate\Contracts\Support\Arrayable;
1112
use Illuminate\Contracts\Validation\InvokableRule;
1213
use Illuminate\Contracts\Validation\Rule as RuleContract;
1314
use Illuminate\Contracts\Validation\ValidationRule;
1415
use Illuminate\Validation\ConditionalRules;
15-
use Illuminate\Validation\Rules\Dimensions;
1616
use Illuminate\Validation\Rules\Password;
1717
use Illuminate\Validation\Rules\RequiredIf;
1818
use IteratorAggregate;
1919
use Stringable;
20+
use UnitEnum;
2021

2122
/**
2223
* @implements Arrayable<array-key, RuleContract|InvokableRule|ValidationRule|ConditionalRules|Stringable|string>
@@ -645,9 +646,9 @@ public function image(): self
645646
* list of values provided to the *in* rule.
646647
*
647648
* @link https://laravel.com/docs/11.x/validation#rule-in
648-
* @param Arrayable<array-key, mixed>|array<array-key, mixed>|string $values
649+
* @param Arrayable<array-key, BackedEnum|UnitEnum|string>|array<BackedEnum|UnitEnum|string>|BackedEnum|UnitEnum|string $values
649650
*/
650-
public function in(Arrayable|array|string $values): self
651+
public function in(Arrayable|BackedEnum|UnitEnum|array|string $values): self
651652
{
652653
return $this->rule(Rule::in($values));
653654
}
@@ -716,7 +717,10 @@ public function json(): self
716717
}
717718

718719
/**
719-
* The field under validation must be a list style array.
720+
* The field under validation must be an array that is a list. An array is considered a list if its keys consist of
721+
* consecutive numbers from 0 to count($array) - 1.
722+
*
723+
* @link https://laravel.com/docs/11.x/validation#rule-list
720724
*/
721725
public function list(): self
722726
{
@@ -888,9 +892,9 @@ public function multipleOf(int|float $value): self
888892
* The field under validation must not be included in the given list of values.
889893
*
890894
* @link https://laravel.com/docs/11.x/validation#rule-not-in
891-
* @param Arrayable<array-key, mixed>|array<array-key, mixed>|string $values
895+
* @param Arrayable<array-key, BackedEnum|UnitEnum|string>|array<BackedEnum|UnitEnum|string>|BackedEnum|UnitEnum|string $values
892896
*/
893-
public function notIn(Arrayable|array|string $values): self
897+
public function notIn(Arrayable|BackedEnum|UnitEnum|array|string $values): self
894898
{
895899
return $this->rule(Rule::notIn($values));
896900
}
@@ -958,35 +962,39 @@ public function present(): self
958962
}
959963

960964
/**
961-
* The field under validation must be present but can be empty if *anotherField* under validation is equal to a
962-
* specified value.
965+
* The field under validation must be present if the *anotherField* field is equal to any *value*.
966+
*
967+
* @link https://laravel.com/docs/11.x/validation#rule-present-if
963968
*/
964969
public function presentIf(string $anotherField, string ...$value): self
965970
{
966971
return $this->rule(Rule::presentIf($anotherField, ...$value));
967972
}
968973

969974
/**
970-
* The field under validation must be present but can be empty unless the *anotherField* field is equal to any
971-
* *value*.
975+
* The field under validation must be present unless the *anotherField* field is equal to any *value*.
976+
*
977+
* @link https://laravel.com/docs/11.x/validation#rule-present-unless
972978
*/
973979
public function presentUnless(string $anotherField, string ...$value): self
974980
{
975981
return $this->rule(Rule::presentUnless($anotherField, ...$value));
976982
}
977983

978984
/**
979-
* The field under validation must be present but can be empty *only if* any of the other specified fields are
980-
* present and not empty.
985+
* The field under validation must be present *only if* any of the other specified fields are present.
986+
*
987+
* @link https://laravel.com/docs/11.x/validation#rule-present-with
981988
*/
982989
public function presentWith(string ...$field): self
983990
{
984991
return $this->rule(Rule::presentWith(...$field));
985992
}
986993

987994
/**
988-
* The field under validation must be present but can be empty *only if* all the other specified fields are present
989-
* and not empty.
995+
* The field under validation must be present *only if* all the other specified fields are present.
996+
*
997+
* @link https://laravel.com/docs/11.x/validation#rule-present-with-all
990998
*/
991999
public function presentWithAll(string ...$field): self
9921000
{
@@ -1088,7 +1096,7 @@ public function requiredIf(mixed $callback): self
10881096
}
10891097

10901098
/**
1091-
* The field under validation must be present and not empty if the `field` field is equal to yes, on, 1, "1", true,
1099+
* The field under validation must be present and not empty if the *field* field is equal to yes, on, 1, "1", true,
10921100
* or "true".
10931101
*
10941102
* @link https://laravel.com/docs/11.x/validation#rule-required-if-accepted
@@ -1115,7 +1123,7 @@ public function requiredIfAny(RequiredIf ...$rules): self
11151123
}
11161124

11171125
/**
1118-
* The field under validation must be present and not empty if the `field` field is equal to "no", "off", 0, "0",
1126+
* The field under validation must be present and not empty if the *field* field is equal to "no", "off", 0, "0",
11191127
* false, or "false".
11201128
*
11211129
* @link https://laravel.com/docs/11.x/validation#rule-required-if-declined

tests/Unit/RuleTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1150,6 +1150,24 @@ public static function ruleDataProvider(): array
11501150
],
11511151
'fails' => true,
11521152
],
1153+
'in enum valid' => [
1154+
'data' => [
1155+
'field-a' => ExampleStringEnum::Valid->value,
1156+
],
1157+
'rules' => fn() => [
1158+
'field-a' => RuleSet::create()->in(ExampleStringEnum::Valid),
1159+
],
1160+
'fails' => false,
1161+
],
1162+
'in enum invalid' => [
1163+
'data' => [
1164+
'field-a' => ExampleStringEnum::Another->value,
1165+
],
1166+
'rules' => fn() => [
1167+
'field-a' => RuleSet::create()->in(ExampleStringEnum::Valid),
1168+
],
1169+
'fails' => true,
1170+
],
11531171
'inArray valid' => [
11541172
'data' => [
11551173
'field-a' => 'a',
@@ -1664,6 +1682,24 @@ public static function ruleDataProvider(): array
16641682
],
16651683
'fails' => true,
16661684
],
1685+
'notIn enum valid' => [
1686+
'data' => [
1687+
'field-a' => ExampleStringEnum::Another->value,
1688+
],
1689+
'rules' => fn() => [
1690+
'field-a' => RuleSet::create()->notIn(ExampleStringEnum::Valid),
1691+
],
1692+
'fails' => false,
1693+
],
1694+
'notIn enum invalid' => [
1695+
'data' => [
1696+
'field-a' => ExampleStringEnum::Valid->value,
1697+
],
1698+
'rules' => fn() => [
1699+
'field-a' => RuleSet::create()->notIn(ExampleStringEnum::Valid),
1700+
],
1701+
'fails' => true,
1702+
],
16671703
'notRegex valid' => [
16681704
'data' => 'value-1',
16691705
'rules' => fn() => RuleSet::create()->notRegex('/[a-z]+$/'),

0 commit comments

Comments
 (0)