Skip to content

Report nested validation errors under their path - #4

Merged
happyDemon merged 2 commits into
mainfrom
nested-validation-paths
Nov 11, 2026
Merged

happyDemon merged 2 commits into
mainfrom
nested-validation-paths

Conversation

@happyDemon

Copy link
Copy Markdown
Member

A nested struct validates its own payload, so its errors came back keyed by the nested field alone: an invalid label in the second item of details failed as label, not details.1.label. A client can't tell which item was wrong.

Generated structs with nested struct fields now hydrate them through a private hydrateNested(), which re-keys the nested ValidationException under the field path and list index. Every level adds its own segment, so deeper nesting composes (nested_addresses.0.1.city).

  • The messages are left as the nested struct produced them, so attribute names and translations are unchanged.
  • The exception is rebuilt on a plain Validator with the original translator, not with ValidationException::withMessages(), which needs Laravel's facades. Generated objects also run without Laravel, and the integration test covers that.
  • Covered for a direct struct, a list, a list of optional structs, and a list of lists.

happyDemon and others added 2 commits November 11, 2026 10:06
A nested struct validated its own payload, so an invalid field in the second
item of `details` failed as `label` instead of `details.1.label`. Clients could
not tell which item was wrong.

Nested structs are now hydrated through a generated `hydrateNested()`, which
re-keys their errors under the field path and list index. Each level adds its
own segment, so deeper nesting composes. The exception is rebuilt on a plain
Validator, not through ValidationException::withMessages(), because that needs
Laravel's facades and generated objects also run without them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The array rule also accepts maps, so a nested struct list may arrive with
string keys. Typing the index as int made that a TypeError under strict
types instead of a validation error.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@happyDemon
happyDemon merged commit 3a9b49a into main Nov 11, 2026
4 checks passed
@happyDemon
happyDemon deleted the nested-validation-paths branch November 11, 2026 09:30
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.

1 participant