Skip to content

Commit 55284e8

Browse files
committed
Add * possibility at root of rules
1 parent b047b89 commit 55284e8

File tree

3 files changed

+86
-13
lines changed

3 files changed

+86
-13
lines changed

src/Helper.php

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,12 @@ public static function arrayHas(array $array, string $key): bool
5858
* Get an item from an array using "dot" notation.
5959
* Adapted from: https://github.com/illuminate/support/blob/v5.3.23/Arr.php#L246
6060
*
61-
* @param array $array
62-
* @param string $key
63-
* @param mixed $default
61+
* @param array $array
62+
* @param string|null $key
63+
* @param mixed $default
6464
* @return mixed
6565
*/
66-
public static function arrayGet(array $array, string $key, $default = null)
66+
public static function arrayGet(array $array, $key, $default = null)
6767
{
6868
if (is_null($key)) {
6969
return $array;
@@ -111,14 +111,21 @@ public static function arrayDot(array $array, string $prepend = ''): array
111111
* Set an item on an array or object using dot notation.
112112
* Adapted from: https://github.com/illuminate/support/blob/v5.3.23/helpers.php#L437
113113
*
114-
* @param mixed $target
115-
* @param string|array $key
116-
* @param mixed $value
117-
* @param bool $overwrite
114+
* @param mixed $target
115+
* @param string|array|null $key
116+
* @param mixed $value
117+
* @param bool $overwrite
118118
* @return mixed
119119
*/
120120
public static function arraySet(&$target, $key, $value, $overwrite = true): array
121121
{
122+
if (is_null($key)) {
123+
if ($overwrite) {
124+
return $target = array_merge($target, $value);
125+
}
126+
return $target = array_merge($value, $target);
127+
}
128+
122129
$segments = is_array($key) ? $key : explode('.', $key);
123130

124131
if (($segment = array_shift($segments)) === '*') {

src/Validation.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,9 @@ public function extractValuesForWildcards(array $data, string $attributeKey): ar
282282
* Allows us to not spin through all of the flattened data for some operations.
283283
*
284284
* @param string $attributeKey
285-
* @return string
285+
* @return string|null null when root wildcard
286286
*/
287-
protected function getLeadingExplicitAttributePath(string $attributeKey): string
287+
protected function getLeadingExplicitAttributePath(string $attributeKey)
288288
{
289289
return rtrim(explode('*', $attributeKey)[0], '.') ?: null;
290290
}
@@ -295,10 +295,10 @@ protected function getLeadingExplicitAttributePath(string $attributeKey): string
295295
*
296296
* Used to extract a sub-section of the data for faster iteration.
297297
*
298-
* @param string $attributeKey
298+
* @param string|null $attributeKey
299299
* @return array
300300
*/
301-
protected function extractDataFromPath(string $attributeKey): array
301+
protected function extractDataFromPath($attributeKey): array
302302
{
303303
$results = [];
304304

tests/ValidatorTest.php

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
use DateTime;
66
use PHPUnit\Framework\TestCase;
7+
use Rakit\Validation\Rule;
78
use Rakit\Validation\Rules\UploadedFile;
89
use Rakit\Validation\Validator;
910

1011
class ValidatorTest extends TestCase
1112
{
12-
13+
/** @var Validator */
1314
protected $validator;
1415

1516
protected function setUp()
@@ -803,6 +804,71 @@ public function testArrayAssocValidation()
803804
$this->assertNull($errors->first('user.name:required'));
804805
}
805806

807+
/**
808+
* Test root asterisk validation.
809+
*
810+
* @dataProvider rootAsteriskProvider
811+
*/
812+
public function testRootAsteriskValidation(array $data, array $rules, $errors = null)
813+
{
814+
$validation = $this->validator->validate($data, $rules);
815+
$this->assertSame(empty($errors), $validation->passes());
816+
$errorBag = $validation->errors();
817+
if (!empty($errors)) {
818+
foreach ($errors as $error) {
819+
$field = $error[0];
820+
$rule = $error[1] ?? null;
821+
$error = $errorBag->get($field);
822+
$this->assertNotEmpty($error);
823+
if ($rule !== null) {
824+
$this->assertArrayHasKey($rule, $error);
825+
}
826+
}
827+
}
828+
}
829+
830+
public function rootAsteriskProvider(): array
831+
{
832+
return [
833+
'control sample success' => [
834+
['Body' => ['a' => 1, 'b' => 2]],
835+
['Body.*' => 'integer|min:0'],
836+
],
837+
'control sample failure' => [
838+
['Body' => ['a' => 1, 'b' => -2]],
839+
['Body.*' => 'integer|min:0'],
840+
[['Body.b', 'min']],
841+
],
842+
'root field success' => [
843+
['a' => 1, 'b' => 2],
844+
['*' => 'integer|min:0'],
845+
],
846+
'root field failure' => [
847+
['a' => 1, 'b' => -2],
848+
['*' => 'integer|min:0'],
849+
[['b', 'min']],
850+
],
851+
'root array success' => [
852+
[[1], [2]],
853+
['*.*' => 'integer|min:0'],
854+
],
855+
'root array failure' => [
856+
[[1], [-2]],
857+
['*.*' => 'integer|min:0'],
858+
[['1.0', 'min']],
859+
],
860+
'root dict success' => [
861+
['a' => ['c' => 1, 'd' => 4], 'b' => ['c' => 'e', 'd' => 8]],
862+
['*.c' => 'required'],
863+
],
864+
'root dict failure' => [
865+
['a' => ['c' => 1, 'd' => 4], 'b' => ['d' => 8]],
866+
['*.c' => 'required'],
867+
[['b.c', 'required']],
868+
],
869+
];
870+
}
871+
806872
public function testArrayValidation()
807873
{
808874
$validation = $this->validator->validate([

0 commit comments

Comments
 (0)