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

[5.8] Fix top level wildcard validation for 'distinct' #29499

Merged
merged 3 commits into from
Aug 11, 2019
Merged

[5.8] Fix top level wildcard validation for 'distinct' #29499

merged 3 commits into from
Aug 11, 2019

Conversation

me-shaon
Copy link
Contributor

When someone writes validation rules like this:

$array = [
    ['id' => 1],
    ['id' => 1],
];
$rules = [
    '*' => 'array',
    '*.id' => 'distinct',
];
Validator::make($array, $rules)->validate();

it doesn't work as expected.

Why?

The implicitAttributes array in Validator class stores the mapping of 'unparsed' rule to 'parsed' rule.
For the given rule, it is like:

[
  "*" => [
    0 => 0
    1 => 1
  ],
  "*.id" => [
    0 => "0.id"
    1 => "1.id"
  ]
]

So, when this array is searched to get the key of "0.id", it finds * instead of *.id because of loose comparison match between "0.id" and 0.
This scenario can be avoided if all the parsed rule are stored as string, which I did in this PR.
I've also added tests to verify the correctness of my solution.

This PR will fix #29190

@me-shaon me-shaon changed the title Fix top level wildcard validation for 'distinct' [5.8] Fix top level wildcard validation for 'distinct' Aug 11, 2019
@taylorotwell taylorotwell merged commit 9352f00 into laravel:5.8 Aug 11, 2019
@me-shaon me-shaon deleted the fix-distinct-validation branch August 18, 2019 09:00
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.

Distinct validation rule not working on top-level array
2 participants