Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] apply excludeUnvalidatedArrayKeys to list validation #52658

Merged
merged 1 commit into from
Sep 5, 2024

Conversation

lorenzolosa
Copy link
Contributor

Laravel 11 introduced a list validation rule (with #50454). However, the Validator::excludeUnvalidatedArrayKeys() feature was not extended to work with list; this PR fixes that.

Intuitively, I would expect list to work exactly as array + 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 the list rule and without the array rule. Even adding the array 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 or list. A test is included.

Issues with the current behaviour

Assume that we have the following data to validate:

$data = [
    'field' => [
        [
            'sub' => 'aaa',
            'extra' => 'bbb',
        ],
    ],
];

If we validate it with:

$rules = [
    'field' => 'array',
    'field.*.sub' => 'string',
];

then the extra field is not included in Validator::make($data, $rules)->validated(); but is included with the following rules:

$rules = [
    'field' => 'list',
    'field.*.sub' => 'string',
];

and not even with:

$rules = [
    'field' => 'list',
    'field.*' => 'array',
    'field.*.sub' => 'string',
];

I would argue that the list rule should be coherent with the array rule; but even if we allow it not to be, I believe that failing to exclude unvalidated array keys when the array rule is explicitly included in the last example is an issue.

@taylorotwell taylorotwell merged commit d8aabd9 into laravel:11.x Sep 5, 2024
31 checks passed
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.

2 participants