From a4b0987f7e53eee798a5491941d7955581a600ef Mon Sep 17 00:00:00 2001 From: parsilver Date: Mon, 16 Nov 2026 18:06:42 +0700 Subject: [PATCH 1/2] test(laravel): show ParentField pairing the wrong index in nested arrays 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 --- tests/Laravel/RulesTest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/Laravel/RulesTest.php b/tests/Laravel/RulesTest.php index bb61257..586ed7a 100644 --- a/tests/Laravel/RulesTest.php +++ b/tests/Laravel/RulesTest.php @@ -52,6 +52,22 @@ function validate(array $data, array $rules): array expect(array_keys($errors))->toBe(['addresses.2.admin1']); }); +it('pairs each wildcard with the index at the same depth in nested arrays', function () { + $this->fetch(); + + $data = ['items' => [ + ['country' => 'US', 'addresses' => [['country' => 'TH', 'admin1' => '50'], ['country' => 'US', 'admin1' => 'CA']]], + ['country' => 'TH', 'addresses' => [['country' => 'US', 'admin1' => 'CA'], ['country' => 'TH', 'admin1' => 'CA']]], + ]]; + + expect(array_keys(validate($data, ['items.1.addresses.*.admin1' => 'geonames_admin1:items.1.addresses.*.country']))) + ->toBe(['items.1.addresses.1.admin1']) + ->and(array_keys(validate($data, ['items.*.addresses.*.admin1' => 'geonames_admin1:items.*.addresses.*.country']))) + ->toBe(['items.1.addresses.1.admin1']) + ->and(array_keys(validate($data, ['items.*.addresses.*.admin1' => 'geonames_admin1:items.*.country']))) + ->toBe(['items.0.addresses.0.admin1', 'items.1.addresses.0.admin1', 'items.1.addresses.1.admin1']); +}); + it('says the data is not downloaded instead of throwing out of validation', function () { // Countries are bundled, admin1 is not: nothing was fetched. $errors = validate(['c' => 'TH', 'a' => '50'], ['c' => 'geonames_country', 'a' => 'geonames_admin1:c']); From 35e05ab94218691e2e941596c0b2581659e3dc4d Mon Sep 17 00:00:00 2001 From: parsilver Date: Mon, 16 Nov 2026 18:10:32 +0700 Subject: [PATCH 2/2] fix(laravel): pair ParentField wildcards by depth in nested arrays 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 --- src/Laravel/Rules/ParentField.php | 16 +++++++++------- tests/Laravel/RulesTest.php | 6 +++++- 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/src/Laravel/Rules/ParentField.php b/src/Laravel/Rules/ParentField.php index 763bfa6..233a111 100644 --- a/src/Laravel/Rules/ParentField.php +++ b/src/Laravel/Rules/ParentField.php @@ -8,8 +8,9 @@ /** * Finds the value of a parent field (`country`) for the attribute being validated. - * Wildcards pair by position: attribute `addresses.2.city` with parent - * `addresses.*.country` reads `addresses.2.country`. + * Each wildcard takes the attribute's key at the same depth: attribute + * `items.0.addresses.2.city` with parent `items.0.addresses.*.country` reads + * `items.0.addresses.2.country`. */ final class ParentField { @@ -21,13 +22,14 @@ public static function value(string $attribute, ?string $field, array $data): mi if ($field === null || $field === '') { return null; } - if (str_contains($field, '*')) { - preg_match_all('/\.(\d+)(?=\.|$)/', '.'.$attribute, $matches); - foreach ($matches[1] as $index) { - $field = (string) preg_replace('/\*/', $index, $field, 1); + $keys = explode('.', $attribute); + $segments = explode('.', $field); + foreach ($segments as $depth => $segment) { + if ($segment === '*' && isset($keys[$depth])) { + $segments[$depth] = $keys[$depth]; } } - return Arr::get($data, $field); + return Arr::get($data, implode('.', $segments)); } } diff --git a/tests/Laravel/RulesTest.php b/tests/Laravel/RulesTest.php index 586ed7a..77d0735 100644 --- a/tests/Laravel/RulesTest.php +++ b/tests/Laravel/RulesTest.php @@ -6,6 +6,7 @@ use Farzai\Geonames\Laravel\Rules\GeonamesCity; use Farzai\Geonames\Laravel\Rules\GeonamesCountry; use Farzai\Geonames\Laravel\Rules\GeonamesPostalCode; +use Farzai\Geonames\Laravel\Rules\ParentField; use Illuminate\Contracts\Validation\DataAwareRule; use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Support\Facades\Validator; @@ -65,7 +66,10 @@ function validate(array $data, array $rules): array ->and(array_keys(validate($data, ['items.*.addresses.*.admin1' => 'geonames_admin1:items.*.addresses.*.country']))) ->toBe(['items.1.addresses.1.admin1']) ->and(array_keys(validate($data, ['items.*.addresses.*.admin1' => 'geonames_admin1:items.*.country']))) - ->toBe(['items.0.addresses.0.admin1', 'items.1.addresses.0.admin1', 'items.1.addresses.1.admin1']); + ->toBe(['items.0.addresses.0.admin1', 'items.1.addresses.0.admin1', 'items.1.addresses.1.admin1']) + ->and(ParentField::value('items', 'items.*.country', $data))->toBeNull() + ->and(ParentField::value('items', null, $data))->toBeNull() + ->and(ParentField::value('items', '', $data))->toBeNull(); }); it('says the data is not downloaded instead of throwing out of validation', function () {