Skip to content

Commit

Permalink
Array updates (#45)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
erik-perri authored May 21, 2024
1 parent 22658a2 commit 6c8d6cb
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 11 deletions.
4 changes: 4 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"minimum-stability": "stable",
"require": {
"php": "^8.2",
"laravel/framework": "^11.5"
"laravel/framework": "^11.8"
},
"require-dev": {
"ext-json": "*",
Expand Down
26 changes: 20 additions & 6 deletions src/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down
21 changes: 17 additions & 4 deletions src/RuleSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down
58 changes: 58 additions & 0 deletions tests/Unit/RuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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 () {
Expand Down

0 comments on commit 6c8d6cb

Please sign in to comment.