Skip to content

Fix misplaced Outer/Inner names when using named() on Each#1640

Merged
alganet merged 1 commit intoRespect:mainfrom
alganet:deinvert-each
Jan 29, 2026
Merged

Fix misplaced Outer/Inner names when using named() on Each#1640
alganet merged 1 commit intoRespect:mainfrom
alganet:deinvert-each

Conversation

@alganet
Copy link
Member

@alganet alganet commented Jan 29, 2026

The Each validator was using "Outer" names (the name applied to the parent) in child results and vice-versa.

This commit fixes it, and also streamlines the Result class, introducing helper mapChildren and mapAdjacent methods and removing unecessary recursive array_maps.

Named children now also display their names, allowing users to create more meaningful messages that do not spam nested .0....0...etc numeric keys which could be confusing. If the user does not name them, the previous behavior is kept.


This fixes #1639

@codecov
Copy link

codecov bot commented Jan 29, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.14%. Comparing base (b69beb1) to head (04fe1d0).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main    #1640      +/-   ##
============================================
+ Coverage     98.03%   98.14%   +0.11%     
- Complexity      961      963       +2     
============================================
  Files           197      197              
  Lines          2234     2213      -21     
============================================
- Hits           2190     2172      -18     
+ Misses           44       41       -3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a bug where the Each validator was inverting Outer and Inner names when using named(), and also refactors the Result class for better maintainability.

Changes:

  • Fixed Each validator to correctly apply Outer names to the parent result and Inner names to child results
  • Refactored Result class by introducing mapChildren and mapAdjacent helper methods
  • Enhanced message formatting to show named children with their names (e.g., .0 (<- Inner)) while avoiding redundancy in nested displays

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/feature/Validators/EachTest.php Updated test expectations to match corrected naming behavior where Outer names apply to parent "Each item in..." messages and Inner names apply to individual child items
src/Validators/Each.php Added withPrecedentName(false) to child results and removed incorrect withNameFrom call to fix name inversion
src/Validators/Core/FilteredNonEmptyArray.php Removed incorrect withNameFrom calls that were propagating child names to parent error results
src/Result.php Refactored with new withPrecedentName method, streamlined name/path handling, and added mapChildren/mapAdjacent helpers to eliminate redundant array_map calls
src/Message/Formatter/NestedArrayFormatter.php Enhanced to conditionally remove redundant names from children when they match the parent name in nested displays
src/Validators/Key.php Code formatting improvement (line break added for readability)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Copy link
Member

@henriquemoody henriquemoody left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome work, and great catch! I think we should get to an agreement regarding the withInput() before merging this one, though.

@alganet alganet marked this pull request as ready for review January 29, 2026 15:35
@alganet alganet requested a review from henriquemoody January 29, 2026 15:35
henriquemoody
henriquemoody previously approved these changes Jan 29, 2026
Copy link
Member

@henriquemoody henriquemoody left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have only a nitpick comment, but I'm approving

The Each validator was using "Outer" names (the name applied to
the parent) in child results and vice-versa.

This commit fixes it, and also streamlines the Result class,
introducing helper `mapChildren` and `mapAdjacent` methods and
removing unecessary recursive array_maps.

Named children now also display their names, allowing users to
create more meaningful messages that do not spam nested
`.0`...`.0`...etc numeric keys which could be confusing. If the
user does not name them, the previous behavior is kept.
@alganet alganet merged commit 7f4a4c2 into Respect:main Jan 29, 2026
7 of 8 checks passed
@alganet alganet deleted the deinvert-each branch January 29, 2026 18:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants