Repository navigation
fix(laravel): pair ParentField wildcards by depth in nested arrays - #65
Conversation
A parent field such as items.1.addresses.*.country takes its wildcard from the first numeric segment of the attribute (the item index), so a row is checked against another row's country. This test fails until the pairing goes by depth. Refs #59
Each * in the parent field now takes the attribute's key at the same depth. Filling wildcards with numeric segments in order put the outer index into the inner wildcard whenever the parent had a fixed outer index, so items.1.addresses.*.country read item 1's address 1 for every address. Closes #59
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #65 +/- ##
============================================
+ Coverage 83.23% 83.33% +0.10%
- Complexity 532 533 +1
============================================
Files 43 43
Lines 1145 1146 +1
============================================
+ Hits 953 955 +2
+ Misses 192 191 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
parsilver
left a comment
There was a problem hiding this comment.
No Blocker.
ParentField::value now replaces each * in the parent path with the attribute's key at the same depth, instead of taking the attribute's numeric indexes left to right. So items.1.addresses.0.admin1 with parent items.*.addresses.*.country reads items.1.addresses.0.country, and a parent shallower than the attribute (items.*.country) reads its own row's value. A * with no key at that depth stays and reads null, as before. The new test uses two items with two addresses each and different countries, and fails against the old pairing. It also covers the shallower-parent case and the null and empty parent. CI green on PHP 8.3 to 8.5, prefer-lowest and prefer-stable, codecov patch included.
Nit: the class doc could say the parent path is assumed to share the attribute's prefix up to each wildcard (true for every rule this package documents).
Merging; this ships as 3.0.1.
Closes #59
What was wrong
ParentFieldfilled each*in the parent field with the attribute's numeric segments in order. A parent with a fixed outer index, such asitems.1.addresses.*.country, took the item index (1) for its wildcard, so every address in item 1 was checked against address 1's country.The fix
Each
*now takes the attribute's key at the same depth (items.1.addresses.0.admin1givesitems.1.addresses.0.country). A wildcard deeper than the attribute stays unresolved and reads as no parent, as before. Single-level pairing (addresses.*.country) and plain field names behave the same.Acceptance
it pairs each wildcard with the index at the same depth in nested arraysintests/Laravel/RulesTest.php. It failed on the first commit of this PR (it reporteditems.1.addresses.0.admin1as well asitems.1.addresses.1.admin1) and passes with the fix. It also covers two wildcards and a parent one level up (items.*.country).pairs wildcard arrays by positionand the rest pass.Checks run
vendor/bin/pest: 87 passed, 1 skipped (the live network test), 5569 assertions, after rebasing on main.vendor/bin/phpstan analyse: no errors.vendor/bin/pint --teston the changed files: passed.ParentField.php: no violations. Cyclomatic complexity ofvalue()is 6 and every line runs under the rule tests, so CRAP is about 6.src: 14 clones, the same 14 as on main; none inParentField.php.value(). 6 killed. The survivor turns$field === null || $field === ''into$field === null, which is equivalent: an empty field readsArr::get($data, ''), which is null too.Risk
Small. The only behaviour change is which index fills a wildcard when the parent's shape differs from the attribute's numeric segments. A parent that sits at a different path than the attribute (for example attribute
addresses.2.citywith parentmeta.addresses.*.country) used to borrow the first index and now pairs by depth; that layout was never documented.Not verified