From 6c8d6cbfc0c4c2dbcd20a9f3397dc14207148fee Mon Sep 17 00:00:00 2001 From: Erik Perri <46399654+erik-perri@users.noreply.github.com> Date: Tue, 21 May 2024 17:19:25 -0400 Subject: [PATCH] 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 --- UPGRADE.md | 4 +++ composer.json | 2 +- src/Rule.php | 26 +++++++++++++----- src/RuleSet.php | 21 ++++++++++++--- tests/Unit/RuleTest.php | 58 +++++++++++++++++++++++++++++++++++++++++ 5 files changed, 100 insertions(+), 11 deletions(-) diff --git a/UPGRADE.md b/UPGRADE.md index f3a61fc..8105cfe 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -2,6 +2,10 @@ ## v5 +### Upgrading from v5.1 to v5.2 + +- Minimum Laravel version increased from `11.5` to `11.8`. + ### Upgrading from v5.0 to v5.1 - Minimum Laravel version increased from `11.0.3` to `11.5`. diff --git a/composer.json b/composer.json index b300bb2..6dcc5e3 100644 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "minimum-stability": "stable", "require": { "php": "^8.2", - "laravel/framework": "^11.5" + "laravel/framework": "^11.8" }, "require-dev": { "ext-json": "*", diff --git a/src/Rule.php b/src/Rule.php index 002b309..e84c346 100644 --- a/src/Rule.php +++ b/src/Rule.php @@ -14,6 +14,7 @@ use Illuminate\Support\Collection; use Illuminate\Validation\ConditionalRules; use Illuminate\Validation\Rule as LaravelRule; +use Illuminate\Validation\Rules\ArrayRule; use Illuminate\Validation\Rules\Can; use Illuminate\Validation\Rules\Dimensions; use Illuminate\Validation\Rules\Enum; @@ -117,13 +118,13 @@ public static function alphaNum(): string * * @link https://laravel.com/docs/11.x/validation#rule-array */ - public static function array(string ...$requiredKey): string + public static function array(BackedEnum|UnitEnum|string ...$requiredKey): ArrayRule { if (count($requiredKey)) { - return 'array:'.implode(',', $requiredKey); + return LaravelRule::array($requiredKey); } - return 'array'; + return LaravelRule::array(); } /** @@ -177,7 +178,8 @@ public static function between(float|int|string|BigNumber $min, float|int|string } /** - * The field under validation must be able to be cast as boolean. + * The field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1", and + * "0". * * @link https://laravel.com/docs/11.x/validation#rule-boolean */ @@ -206,6 +208,16 @@ public static function confirmed(): string return 'confirmed'; } + /** + * The field under validation must be an array that contains all of the given parameter values. + * + * @link https://laravel.com/docs/11.x/validation#rule-contains + */ + public static function contains(mixed ...$value): string + { + return 'contains:'.implode(',', $value); + } + /** * The field under validation must match the authenticated user's password. * @@ -685,7 +697,8 @@ public static function macAddress(): string } /** - * The field under validation must be less than or equal to a maximum *value*. + * The field under validation must be less than or equal to a maximum *value*. Strings, numerics, arrays, and files + * are evaluated in the same fashion as the *size* rule. * * @link https://laravel.com/docs/11.x/validation#rule-max */ @@ -726,7 +739,8 @@ public static function mimetypes(string ...$mimeType): string } /** - * The field under validation must have a minimum *value*. + * The field under validation must have a minimum *value*. Strings, numerics, arrays, and files are evaluated in the + * same fashion as the *size* rule. * * @link https://laravel.com/docs/11.x/validation#rule-min */ diff --git a/src/RuleSet.php b/src/RuleSet.php index 67c66cf..3addbc2 100644 --- a/src/RuleSet.php +++ b/src/RuleSet.php @@ -192,7 +192,7 @@ public function alphaNum(): self * * @link https://laravel.com/docs/11.x/validation#rule-array */ - public function array(string ...$requiredKey): self + public function array(BackedEnum|UnitEnum|string ...$requiredKey): self { return $this->rule(Rule::array(...$requiredKey)); } @@ -248,7 +248,8 @@ public function between(float|int|string|BigNumber $min, float|int|string|BigNum } /** - * The field under validation must be able to be cast as boolean. + * The field under validation must be able to be cast as a boolean. Accepted input are true, false, 1, 0, "1", and + * "0". * * @link https://laravel.com/docs/11.x/validation#rule-boolean */ @@ -277,6 +278,16 @@ public function confirmed(): self return $this->rule(Rule::confirmed()); } + /** + * The field under validation must be an array that contains all of the given parameter values. + * + * @link https://laravel.com/docs/11.x/validation#rule-contains + */ + public function contains(mixed ...$value): self + { + return $this->rule(Rule::contains(...$value)); + } + /** * The field under validation must match the authenticated user's password. * @@ -768,7 +779,8 @@ public function macAddress(): self } /** - * The field under validation must be less than or equal to a maximum *value*. + * The field under validation must be less than or equal to a maximum *value*. Strings, numerics, arrays, and files + * are evaluated in the same fashion as the *size* rule. * * @link https://laravel.com/docs/11.x/validation#rule-max */ @@ -809,7 +821,8 @@ public function mimetypes(string ...$mimeType): self } /** - * The field under validation must have a minimum *value*. + * The field under validation must have a minimum *value*. Strings, numerics, arrays, and files are evaluated in the + * same fashion as the *size* rule. * * @link https://laravel.com/docs/11.x/validation#rule-min */ diff --git a/tests/Unit/RuleTest.php b/tests/Unit/RuleTest.php index d0b1e20..63a9218 100644 --- a/tests/Unit/RuleTest.php +++ b/tests/Unit/RuleTest.php @@ -304,6 +304,28 @@ public static function ruleDataProvider(): array ], 'fails' => true, ], + 'array with enum keys valid' => [ + 'data' => [ + 'field' => [ + ExampleStringEnum::Valid->value => 'value', + ], + ], + 'rules' => fn() => [ + 'field' => RuleSet::create()->array('what', ExampleStringEnum::Valid), + ], + 'fails' => false, + ], + 'array with enum keys invalid' => [ + 'data' => [ + 'field' => [ + ExampleStringEnum::Valid->value => 'value', + ], + ], + 'rules' => fn() => [ + 'field' => RuleSet::create()->array(ExampleStringEnum::Another), + ], + 'fails' => true, + ], 'ascii valid' => [ 'data' => 'Ascii', 'rules' => fn() => RuleSet::create()->ascii(), @@ -497,6 +519,42 @@ public static function ruleDataProvider(): array ], 'fails' => true, ], + 'contains valid' => [ + 'data' => [ + 'field' => ['a', 'b', 'c'], + ], + 'rules' => fn() => [ + 'field' => RuleSet::create()->contains('b', 'c'), + ], + 'fails' => false, + ], + 'contains invalid' => [ + 'data' => [ + 'field' => ['a', 'b', 'c'], + ], + 'rules' => fn() => [ + 'field' => RuleSet::create()->contains('b', 'c', 'd'), + ], + 'fails' => true, + ], + 'contains valid not strict' => [ + 'data' => [ + 'field' => ['1', '2'], + ], + 'rules' => fn() => [ + 'field' => RuleSet::create()->contains(1), + ], + 'fails' => false, + ], + 'contains invalid not array' => [ + 'data' => [ + 'field' => 'what', + ], + 'rules' => fn() => [ + 'field' => RuleSet::create()->contains('w'), + ], + 'fails' => true, + ], 'currentPassword valid' => [ 'data' => 'password-one', 'rules' => function () {