Skip to content

Commit e628496

Browse files
authored
Merge pull request #17 from sourcetoad/pre-1.0
Docs update and `required_array_keys` helper
2 parents 44e877b + cd2d091 commit e628496

File tree

5 files changed

+27
-2
lines changed

5 files changed

+27
-2
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ Adds helpers to make building Laravel rule arrays easier by providing helper met
55
## Installation
66

77
```shell
8-
$ composer config repositories.sourcetoad/rule-helper-for-laravel.git vcs https://github.com/sourcetoad/rule-helper-for-laravel.git
98
$ composer require sourcetoad/rule-helper-for-laravel
109
```
1110

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"require": {
88
"php": "^7.4|^8.0",
99
"egulias/email-validator": "^2.1.21",
10-
"laravel/framework": "^8.77"
10+
"laravel/framework": "^8.82"
1111
},
1212
"require-dev": {
1313
"ext-json": "*",

src/Rule.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,14 @@ public static function required(): string
663663
return 'required';
664664
}
665665

666+
/**
667+
* The field under validation must be an array and must contain at least the specified keys.
668+
*/
669+
public static function requiredArrayKeys(string ...$key): string
670+
{
671+
return sprintf('required_array_keys:%s', implode(',', $key));
672+
}
673+
666674
/**
667675
* The field must be present if all the criteria are true.
668676
*/

src/RuleSet.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,14 @@ public function required(): self
768768
return $this->rule(Rule::required());
769769
}
770770

771+
/**
772+
* The field under validation must be an array and must contain at least the specified keys.
773+
*/
774+
public function requiredArrayKeys(string ...$key): self
775+
{
776+
return $this->rule(Rule::requiredArrayKeys(...$key));
777+
}
778+
771779
/**
772780
* The field under validation must be present in the input data if a true boolean is passed in or the passed in
773781
* closure returns true.

tests/Unit/RuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,6 +1264,16 @@ public function ruleDataProvider(): array
12641264
'rules' => fn() => RuleSet::create()->required(),
12651265
'fails' => true,
12661266
],
1267+
'requiredArrayKeys valid' => [
1268+
'data' => ['field' => ['a' => '1', 'b' => '2', 'c' => '3']],
1269+
'rules' => fn() => ['field' => RuleSet::create()->requiredArrayKeys('a', 'b', 'c')],
1270+
'fails' => false,
1271+
],
1272+
'requiredArrayKeys invalid' => [
1273+
'data' => ['field' => ['a' => '1', 'c' => '3']],
1274+
'rules' => fn() => ['field' => RuleSet::create()->requiredArrayKeys('a', 'b', 'c')],
1275+
'fails' => true,
1276+
],
12671277
'requiredIf bool valid' => [
12681278
'data' => [
12691279
'field-a' => 'a',

0 commit comments

Comments
 (0)