Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions library/Rules/Each.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Respect\Validation\Result;
use Respect\Validation\Rules\Core\FilteredNonEmptyArray;

use function array_map;
use function array_reduce;

#[Attribute(Attribute::TARGET_PROPERTY | Attribute::IS_REPEATABLE)]
Expand All @@ -27,7 +26,10 @@ final class Each extends FilteredNonEmptyArray
/** @param non-empty-array<mixed> $input */
protected function evaluateNonEmptyArray(array $input): Result
{
$children = array_map(fn ($item) => $this->rule->evaluate($item), $input);
$children = [];
foreach ($input as $key => $value) {
$children[] = $this->rule->evaluate($value)->withUnchangeableId((string) $key);
}
$isValid = array_reduce($children, static fn ($carry, $childResult) => $carry && $childResult->isValid, true);
if ($isValid) {
return Result::passed($input, $this)->withChildren(...$children);
Expand Down
6 changes: 3 additions & 3 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

use function PHPUnit\Framework\assertStringMatchesFormat;

/** @param array<string, mixed> $messages */
/** @param array<string|int, mixed> $messages */
function expectAll(Closure $callback, string $message, string $fullMessage, array $messages): Closure
{
return function () use ($callback, $message, $fullMessage, $messages): void {
Expand All @@ -27,7 +27,7 @@ function expectAll(Closure $callback, string $message, string $fullMessage, arra
};
}

/** @param array<string, mixed> $messages */
/** @param array<string|int, mixed> $messages */
function expectAllToMatch(Closure $callback, string $message, string $fullMessage, array $messages): Closure
{
return function () use ($callback, $message, $fullMessage, $messages): void {
Expand Down Expand Up @@ -70,7 +70,7 @@ function expectFullMessage(Closure $callback, string $fullMessage): Closure
};
}

/** @param array<string, mixed> $messages */
/** @param array<string|int, mixed> $messages */
function expectMessages(Closure $callback, array $messages): Closure
{
return function () use ($callback, $messages): void {
Expand Down
6 changes: 3 additions & 3 deletions tests/feature/Issues/Issue1033Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
FULL_MESSAGE,
[
'__root__' => 'Each item in `["A", "B", "B"]` must be valid',
'equals.1' => '"A" must be equal to 1',
'equals.2' => '"B" must be equal to 1',
'equals.3' => '"B" must be equal to 1',
0 => '"A" must be equal to 1',
1 => '"B" must be equal to 1',
2 => '"B" must be equal to 1',
]
));
2 changes: 1 addition & 1 deletion tests/feature/Issues/Issue1289Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
- description must be a string value
FULL_MESSAGE,
[
'allOf' => [
0 => [
'__root__' => 'These rules must pass for `["default": 2, "description": [], "children": ["nope"]]`',
'default' => [
'__root__' => 'Only one of these rules must pass for default',
Expand Down
6 changes: 3 additions & 3 deletions tests/feature/Issues/Issue1334Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ function (): void {
[
'each' => [
'__root__' => 'Each item in `[["region": "Oregon", "country": "USA", "other": 123], ["street": "", "region": "Oregon", "country": "USA"], ["s ... ]` must be valid',
'allOf.1' => [
0 => [
'__root__' => 'These rules must pass for `["region": "Oregon", "country": "USA", "other": 123]`',
'street' => 'street must be present',
'other' => 'other must be a string or must be null',
],
'allOf.2' => 'street must not be empty',
'allOf.3' => 'street must be a string',
1 => 'street must not be empty',
2 => 'street must be a string',
],
]
));
4 changes: 2 additions & 2 deletions tests/feature/Issues/Issue1348Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
[
'each' => [
'__root__' => 'Each item in `[["manufacturer": "Honda", "model": "Accord"], ["manufacturer": "Toyota", "model": "Rav4"], ["manufacturer": "Fo ... ]` must be valid',
'oneOf.3' => [
2 => [
'__root__' => 'Only one of these rules must pass for `["manufacturer": "Ford", "model": "not real"]`',
'allOf.1' => [
'__root__' => 'All the required rules must pass for `["manufacturer": "Ford", "model": "not real"]`',
Expand All @@ -63,7 +63,7 @@
],
'allOf.3' => 'model must be in `["F150", "Bronco"]`',
],
'oneOf.4' => [
3 => [
'__root__' => 'Only one of these rules must pass for `["manufacturer": "Honda", "model": "not valid"]`',
'allOf.1' => 'model must be in `["Accord", "Fit"]`',
'allOf.2' => [
Expand Down
4 changes: 2 additions & 2 deletions tests/feature/Issues/Issue1469Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ function (): void {
[
'keySet' => [
'__root__' => 'Each item in order_items must be valid',
'keySet.1' => 'quantity must be an integer value',
'keySet.2' => [
0 => 'quantity must be an integer value',
1 => [
'__root__' => 'order_items contains both missing and extra keys',
'product_title' => 'product_title must be present',
'quantity' => 'quantity must be present',
Expand Down
76 changes: 38 additions & 38 deletions tests/feature/Rules/EachTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
FULL_MESSAGE,
[
'__root__' => 'Each item in `["a", "b", "c"]` must be valid',
'intType.1' => '"a" must be an integer',
'intType.2' => '"b" must be an integer',
'intType.3' => '"c" must be an integer',
0 => '"a" must be an integer',
1 => '"b" must be an integer',
2 => '"c" must be an integer',
]
));

Expand All @@ -49,9 +49,9 @@
FULL_MESSAGE,
[
'__root__' => 'Each item in `[1, 2, 3]` must be invalid',
'intType.1' => '1 must not be an integer',
'intType.2' => '2 must not be an integer',
'intType.3' => '3 must not be an integer',
0 => '1 must not be an integer',
1 => '2 must not be an integer',
2 => '3 must not be an integer',
]
));

Expand Down Expand Up @@ -80,9 +80,9 @@
FULL_MESSAGE,
[
'__root__' => 'Each item in Wrapped must be valid',
'intType.1' => 'Wrapped must be an integer',
'intType.2' => 'Wrapped must be an integer',
'intType.3' => 'Wrapped must be an integer',
0 => 'Wrapped must be an integer',
1 => 'Wrapped must be an integer',
2 => 'Wrapped must be an integer',
]
));

Expand All @@ -97,9 +97,9 @@
FULL_MESSAGE,
[
'__root__' => 'Each item in Wrapped must be invalid',
'intType.1' => 'Wrapped must not be an integer',
'intType.2' => 'Wrapped must not be an integer',
'intType.3' => 'Wrapped must not be an integer',
0 => 'Wrapped must not be an integer',
1 => 'Wrapped must not be an integer',
2 => 'Wrapped must not be an integer',
]
));

Expand All @@ -114,9 +114,9 @@
FULL_MESSAGE,
[
'__root__' => 'Each item in Wrapper must be valid',
'intType.1' => 'Wrapper must be an integer',
'intType.2' => 'Wrapper must be an integer',
'intType.3' => 'Wrapper must be an integer',
0 => 'Wrapper must be an integer',
1 => 'Wrapper must be an integer',
2 => 'Wrapper must be an integer',
]
));

Expand All @@ -131,9 +131,9 @@
FULL_MESSAGE,
[
'__root__' => 'Each item in Wrapper must be invalid',
'intType.1' => 'Wrapper must not be an integer',
'intType.2' => 'Wrapper must not be an integer',
'intType.3' => 'Wrapper must not be an integer',
0 => 'Wrapper must not be an integer',
1 => 'Wrapper must not be an integer',
2 => 'Wrapper must not be an integer',
]
));

Expand All @@ -148,9 +148,9 @@
FULL_MESSAGE,
[
'__root__' => 'Each item in Not must be invalid',
'intType.1' => 'Not must not be an integer',
'intType.2' => 'Not must not be an integer',
'intType.3' => 'Not must not be an integer',
0 => 'Not must not be an integer',
1 => 'Not must not be an integer',
2 => 'Not must not be an integer',
]
));

Expand Down Expand Up @@ -192,9 +192,9 @@
->setTemplates([
'each' => [
'__root__' => 'Here a sequence of items that did not pass the validation',
'intType.1' => 'First item should have been an integer',
'intType.2' => 'Second item should have been an integer',
'intType.3' => 'Third item should have been an integer',
0 => 'First item should have been an integer',
1 => 'Second item should have been an integer',
2 => 'Third item should have been an integer',
],
])
->assert(['a', 'b', 'c']),
Expand All @@ -207,9 +207,9 @@
FULL_MESSAGE,
[
'__root__' => 'Here a sequence of items that did not pass the validation',
'intType.1' => 'First item should have been an integer',
'intType.2' => 'Second item should have been an integer',
'intType.3' => 'Third item should have been an integer',
0 => 'First item should have been an integer',
1 => 'Second item should have been an integer',
2 => 'Third item should have been an integer',
]
));

Expand All @@ -219,9 +219,9 @@
->setTemplates([
'Wrapped' => [
'__root__' => 'Here a sequence of items that did not pass the validation',
'Wrapped.1' => 'First item should have been an integer',
'Wrapped.2' => 'Second item should have been an integer',
'Wrapped.3' => 'Third item should have been an integer',
0 => 'First item should have been an integer',
1 => 'Second item should have been an integer',
2 => 'Third item should have been an integer',
],
])
->assert(['a', 'b', 'c']),
Expand All @@ -234,9 +234,9 @@
FULL_MESSAGE,
[
'__root__' => 'Each item in Wrapped must be valid',
'intType.1' => 'Wrapped must be an integer',
'intType.2' => 'Wrapped must be an integer',
'intType.3' => 'Wrapped must be an integer',
0 => 'Wrapped must be an integer',
1 => 'Wrapped must be an integer',
2 => 'Wrapped must be an integer',
]
));

Expand All @@ -254,12 +254,12 @@
FULL_MESSAGE,
[
'__root__' => 'Each item in `[2, 4]` must be valid',
'allOf.1' => [
0 => [
'__root__' => 'All the required rules must pass for 2',
'between' => '2 must be between 5 and 7',
'odd' => '2 must be an odd number',
],
'allOf.2' => [
1 => [
'__root__' => 'All the required rules must pass for 4',
'between' => '4 must be between 5 and 7',
'odd' => '4 must be an odd number',
Expand All @@ -282,9 +282,9 @@
FULL_MESSAGE,
[
'__root__' => 'Each item in `[["not_int": "wrong"], ["my_int": 2], "not an array"]` must be valid',
'allOf.1' => 'my_int must be present',
'allOf.2' => 'my_int must be an odd number',
'allOf.3' => [
0 => 'my_int must be present',
1 => 'my_int must be an odd number',
2 => [
'__root__' => 'All the required rules must pass for "not an array"',
'arrayType' => '"not an array" must be an array',
'my_int' => 'my_int must be present',
Expand Down
Loading