[11.x] apply excludeUnvalidatedArrayKeys to list validation #52658
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Laravel 11 introduced a
list
validation rule (with #50454). However, theValidator::excludeUnvalidatedArrayKeys()
feature was not extended to work withlist
; this PR fixes that.Intuitively, I would expect
list
to work exactly asarray
+ ensuring that the array is a list. However, in the case of a list of arrays, extra fields in subarrays are always included when validating the top-level field with thelist
rule and without thearray
rule. Even adding thearray
rule to the subarrays doesn't solve the issue.With this PR, extra fields are excluded from arrays, regardless of whether the top-level field is
array
orlist
. A test is included.Issues with the current behaviour
Assume that we have the following data to validate:
If we validate it with:
then the
extra
field is not included inValidator::make($data, $rules)->validated()
; but is included with the following rules:and not even with:
I would argue that the
list
rule should be coherent with thearray
rule; but even if we allow it not to be, I believe that failing to exclude unvalidated array keys when thearray
rule is explicitly included in the last example is an issue.