Skip to content

Commit 6c8d6cb

Browse files
authored
Array updates (#45)
* docs: updated max and min descriptions to match documentation * update: switch to array rule class for enum key support * docs: updated boolean descriptions to match documentation * update: added contains rule helper * docs: added version to upgrade guide
1 parent 22658a2 commit 6c8d6cb

File tree

5 files changed

+100
-11
lines changed

5 files changed

+100
-11
lines changed

UPGRADE.md

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

33
## v5
44

5+
### Upgrading from v5.1 to v5.2
6+
7+
- Minimum Laravel version increased from `11.5` to `11.8`.
8+
59
### Upgrading from v5.0 to v5.1
610

711
- Minimum Laravel version increased from `11.0.3` to `11.5`.

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

src/Rule.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Illuminate\Support\Collection;
1515
use Illuminate\Validation\ConditionalRules;
1616
use Illuminate\Validation\Rule as LaravelRule;
17+
use Illuminate\Validation\Rules\ArrayRule;
1718
use Illuminate\Validation\Rules\Can;
1819
use Illuminate\Validation\Rules\Dimensions;
1920
use Illuminate\Validation\Rules\Enum;
@@ -117,13 +118,13 @@ public static function alphaNum(): string
117118
*
118119
* @link https://laravel.com/docs/11.x/validation#rule-array
119120
*/
120-
public static function array(string ...$requiredKey): string
121+
public static function array(BackedEnum|UnitEnum|string ...$requiredKey): ArrayRule
121122
{
122123
if (count($requiredKey)) {
123-
return 'array:'.implode(',', $requiredKey);
124+
return LaravelRule::array($requiredKey);
124125
}
125126

126-
return 'array';
127+
return LaravelRule::array();
127128
}
128129

129130
/**
@@ -177,7 +178,8 @@ public static function between(float|int|string|BigNumber $min, float|int|string
177178
}
178179

179180
/**
180-
* The field under validation must be able to be cast as boolean.
181+
* The field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1", and
182+
* "0".
181183
*
182184
* @link https://laravel.com/docs/11.x/validation#rule-boolean
183185
*/
@@ -206,6 +208,16 @@ public static function confirmed(): string
206208
return 'confirmed';
207209
}
208210

211+
/**
212+
* The field under validation must be an array that contains all of the given parameter values.
213+
*
214+
* @link https://laravel.com/docs/11.x/validation#rule-contains
215+
*/
216+
public static function contains(mixed ...$value): string
217+
{
218+
return 'contains:'.implode(',', $value);
219+
}
220+
209221
/**
210222
* The field under validation must match the authenticated user's password.
211223
*
@@ -685,7 +697,8 @@ public static function macAddress(): string
685697
}
686698

687699
/**
688-
* The field under validation must be less than or equal to a maximum *value*.
700+
* The field under validation must be less than or equal to a maximum *value*. Strings, numerics, arrays, and files
701+
* are evaluated in the same fashion as the *size* rule.
689702
*
690703
* @link https://laravel.com/docs/11.x/validation#rule-max
691704
*/
@@ -726,7 +739,8 @@ public static function mimetypes(string ...$mimeType): string
726739
}
727740

728741
/**
729-
* The field under validation must have a minimum *value*.
742+
* The field under validation must have a minimum *value*. Strings, numerics, arrays, and files are evaluated in the
743+
* same fashion as the *size* rule.
730744
*
731745
* @link https://laravel.com/docs/11.x/validation#rule-min
732746
*/

src/RuleSet.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function alphaNum(): self
192192
*
193193
* @link https://laravel.com/docs/11.x/validation#rule-array
194194
*/
195-
public function array(string ...$requiredKey): self
195+
public function array(BackedEnum|UnitEnum|string ...$requiredKey): self
196196
{
197197
return $this->rule(Rule::array(...$requiredKey));
198198
}
@@ -248,7 +248,8 @@ public function between(float|int|string|BigNumber $min, float|int|string|BigNum
248248
}
249249

250250
/**
251-
* The field under validation must be able to be cast as boolean.
251+
* The field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1", and
252+
* "0".
252253
*
253254
* @link https://laravel.com/docs/11.x/validation#rule-boolean
254255
*/
@@ -277,6 +278,16 @@ public function confirmed(): self
277278
return $this->rule(Rule::confirmed());
278279
}
279280

281+
/**
282+
* The field under validation must be an array that contains all of the given parameter values.
283+
*
284+
* @link https://laravel.com/docs/11.x/validation#rule-contains
285+
*/
286+
public function contains(mixed ...$value): self
287+
{
288+
return $this->rule(Rule::contains(...$value));
289+
}
290+
280291
/**
281292
* The field under validation must match the authenticated user's password.
282293
*
@@ -768,7 +779,8 @@ public function macAddress(): self
768779
}
769780

770781
/**
771-
* The field under validation must be less than or equal to a maximum *value*.
782+
* The field under validation must be less than or equal to a maximum *value*. Strings, numerics, arrays, and files
783+
* are evaluated in the same fashion as the *size* rule.
772784
*
773785
* @link https://laravel.com/docs/11.x/validation#rule-max
774786
*/
@@ -809,7 +821,8 @@ public function mimetypes(string ...$mimeType): self
809821
}
810822

811823
/**
812-
* The field under validation must have a minimum *value*.
824+
* The field under validation must have a minimum *value*. Strings, numerics, arrays, and files are evaluated in the
825+
* same fashion as the *size* rule.
813826
*
814827
* @link https://laravel.com/docs/11.x/validation#rule-min
815828
*/

tests/Unit/RuleTest.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,28 @@ public static function ruleDataProvider(): array
304304
],
305305
'fails' => true,
306306
],
307+
'array with enum keys valid' => [
308+
'data' => [
309+
'field' => [
310+
ExampleStringEnum::Valid->value => 'value',
311+
],
312+
],
313+
'rules' => fn() => [
314+
'field' => RuleSet::create()->array('what', ExampleStringEnum::Valid),
315+
],
316+
'fails' => false,
317+
],
318+
'array with enum keys invalid' => [
319+
'data' => [
320+
'field' => [
321+
ExampleStringEnum::Valid->value => 'value',
322+
],
323+
],
324+
'rules' => fn() => [
325+
'field' => RuleSet::create()->array(ExampleStringEnum::Another),
326+
],
327+
'fails' => true,
328+
],
307329
'ascii valid' => [
308330
'data' => 'Ascii',
309331
'rules' => fn() => RuleSet::create()->ascii(),
@@ -497,6 +519,42 @@ public static function ruleDataProvider(): array
497519
],
498520
'fails' => true,
499521
],
522+
'contains valid' => [
523+
'data' => [
524+
'field' => ['a', 'b', 'c'],
525+
],
526+
'rules' => fn() => [
527+
'field' => RuleSet::create()->contains('b', 'c'),
528+
],
529+
'fails' => false,
530+
],
531+
'contains invalid' => [
532+
'data' => [
533+
'field' => ['a', 'b', 'c'],
534+
],
535+
'rules' => fn() => [
536+
'field' => RuleSet::create()->contains('b', 'c', 'd'),
537+
],
538+
'fails' => true,
539+
],
540+
'contains valid not strict' => [
541+
'data' => [
542+
'field' => ['1', '2'],
543+
],
544+
'rules' => fn() => [
545+
'field' => RuleSet::create()->contains(1),
546+
],
547+
'fails' => false,
548+
],
549+
'contains invalid not array' => [
550+
'data' => [
551+
'field' => 'what',
552+
],
553+
'rules' => fn() => [
554+
'field' => RuleSet::create()->contains('w'),
555+
],
556+
'fails' => true,
557+
],
500558
'currentPassword valid' => [
501559
'data' => 'password-one',
502560
'rules' => function () {

0 commit comments

Comments
 (0)