Skip to content

fix: escape dots in keyed collection array keys before building validation paths - #1206

Open
cativo23 wants to merge 1 commit into
spatie:mainfrom
cativo23:fix/1198-dotted-keyed-collection-validation
Open

cativo23 wants to merge 1 commit into
spatie:mainfrom
cativo23:fix/1198-dotted-keyed-collection-validation

Conversation

@cativo23

@cativo23 cativo23 commented Sep 9, 2026

Copy link
Copy Markdown

Fixes #1198.

A keyed collection (array<string, ChildData>) fails validation when one of the array keys contains a literal dot. resolveStaticCollectionRules() builds the rule path straight from the key, so en.greeting ends up as the rule attribute translations.en.greeting.string while Validator::parseData() has already turned the matching data key into an internal placeholder — the attribute never resolves against the payload, and required fails even though the field is there.

This escapes the key with \. before it's appended to the path, which is what Validator::encodeAttributeWithPlaceholder() already recognizes for rule attributes.

Scope: this covers the static rules branch only; resolveDynamicCollectionRules() (the Rule::forEach path) isn't touched. It also only fixes rule generation — payload introspection elsewhere in this resolver still reads keys through Arr::get()/Arr::has(), which doesn't understand the escaped form, so an Optional, nullable, or nested property under a dotted key isn't fixed by this PR (not a regression from this change, just not covered by it).

Added a test that reproduces the bug directly. Full suite still passes (1322 tests, 0 failures).

#1199 proposed the same one-line change in July. It sat without maintainer review and closed without merging. I arrived at the same fix independently and added the regression test — credit to @lazerg for finding it first, I've linked this PR from that thread.

…ation paths

resolveStaticCollectionRules() built each item's rule path directly from
the array key. Laravel's validator reads dots in a rule path as nested
segment separators, so a literal dot in a keyed collection's key (e.g.
translations['en.greeting']) got misread as translations.en.greeting.*,
pointing rule generation at the wrong path and reporting a valid payload
as missing the field.

Escape the key the same way Laravel's own dot-notation rules do (\.)
before appending it to the path.

Fixes spatie#1198
@cativo23

cativo23 commented Oct 9, 2026

Copy link
Copy Markdown
Author

Hi @rubenvanassche, small ping on this one. It fixes #1198: a dot inside an array key gets treated as a path separator when validating keyed collections. It's the same one-line fix @lazerg proposed in #1199, which closed without review, and @lazerg is fine with this PR carrying it forward.

The Run Tests and PHPStan checks show as failed, but they never ran. They were waiting for approval and GitHub marked them failed after 30 days. Could you approve or re-run them when you get a chance?

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.

Dots inside array keys break validation of keyed collections

1 participant