-
Notifications
You must be signed in to change notification settings - Fork 11.1k
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
Array keys validation issue #48854
Comments
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
I found the logic for validating arrays in public function validateArray($attribute, $value, $parameters = [])
{
if (! is_array($value)) {
return false;
}
if (empty($parameters)) {
return true;
}
return empty(array_diff_key($value, array_fill_keys($parameters, '')));
} The way it's written, it expects the input array to contain exactly the same keys as what is in the rule, no more and no less. So even though the input contains the keys specified by the rule, |
Hello, @jakesuellentrop Thank you for providing detailed insights. I'd like to further elaborate on your observation regarding the behavior of the array_diff_key function. To illustrate this, I conducted a simple test: $emptyKeyDiff = empty(array_diff_key(['foo'=>'','bar'=>'','baz'=>''], ['foo'=>'','bar'=>''])); // Result is false
$otherEmptyKeyDiff = empty(array_diff_key(['foo'=>'','bar'=>''],['foo'=>'','bar'=>'','baz'=>''])); // Result is true From the above code, it's evident that when the input array contains extra keys, array_diff_key will return those extra keys, leading the validation to fail. This indeed raises a potential concern where the message "The user must be an array." might not be adequately descriptive. A more appropriate message could be: "The input array contains keys not specified in the rule." or another more descriptive prompt.Just a thought! I hope this provides a clearer understanding of the issue at hand, and it would be great to see a more descriptive error message in future iterations. Thank you again for your feedback! |
So if the arguments were switched around when calling |
Hi , @jakesuellentrop You're absolutely right based on the logic you've pointed out; that's how Laravel currently seems to function. However, after referring to the Laravel docs, I'm pondering if perhaps the behavior should slightly differ. That being said, it's just a personal thought, and I might be missing some nuances. I'd love to hear any insights or suggestions you might have on this. It's great collaborating and contributing to the Laravel community together. Best regards, |
Thanks all. It seems this is the expected behaviour. If you want a different behaviour you could write your own custom validation rule. |
Laravel Version
10.29.0
PHP Version
8.1.13
Database Driver & Version
No response
Description
Wrong validation message when validating array keys.
I use example from the docs https://laravel.com/docs/10.x/validation#rule-array
Steps To Reproduce
This snippet gives me validation message: The user must be an array.
But
user
IS an array.According to the docs,
So I expected validation message about invalid/unexpected key
admin
instead.Also,
validated()
method returns all the array keys, includingadmin
, even though it's not within the expected array keys in the rule:Output:
The text was updated successfully, but these errors were encountered: