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

Fix: Validation. Fields with an asterisk. #5925

Closed
wants to merge 2 commits into from

Conversation

iRedds
Copy link
Collaborator

@iRedds iRedds commented Apr 26, 2022

Fixes #5922

Description
If the field contains a asterisk and is not found in the validation dataset, then array_filter() returns an empty array. This case is not handled, which may cause errors.
So I suggest setting $values = [$field => null] if the field is not found.

It also prevents a TypeError exception from being thrown if an array is passed to the function (rule) instead of a string. But this only applies to fields not found.

Signed-off-by: Andrey Pyzhikov <[email protected]>
@kenjis kenjis added bug Verified issues on the current code behavior or pull requests that will fix them tests needed Pull requests that need tests labels Apr 26, 2022
@kenjis
Copy link
Member

kenjis commented Apr 26, 2022

Thank you!
Can you add test code?

@iRedds
Copy link
Collaborator Author

iRedds commented Apr 26, 2022

I don't know how to correctly test this case. If you have suggestions or desires to take this PR from me, I will not mind)

@kenjis
Copy link
Member

kenjis commented Apr 26, 2022

This PR is a bug fix.
So first we write tests to reproduce the bug, then we fix the product code.
Otherwise, we will not know if the bug has been fixed.

How did you write the fix?

@iRedds
Copy link
Collaborator Author

iRedds commented Apr 27, 2022

I'm not sure about the test, but so far I can't think of anything else to check for a null value.

@paulbalandan paulbalandan removed the tests needed Pull requests that need tests label Apr 29, 2022
@paulbalandan paulbalandan requested a review from kenjis April 29, 2022 08:44
@kenjis
Copy link
Member

kenjis commented Apr 30, 2022

@iRedds Thank you.
But your test code is difficult to understand.
I will take over this PR.

@iRedds
Copy link
Collaborator Author

iRedds commented Apr 30, 2022

@kenjis The test expects null to be passed to the processRules method if the key is not found.

@iRedds
Copy link
Collaborator Author

iRedds commented Apr 30, 2022

@kenjis
The exception is thrown because the passed value does not match the expected value, not because the key was not found.

// This code will throw a TypeError exception.
$this->validation->setRules(['a' => 'alpha'])->run(['a' => []]);

Due to the strange behavior of the dot_array_search helper, the code below will also throw an exception. Because instead of null for a key not found, an array will be passed.

// This code will throw a TypeError exception.
$this->validation->setRules(['a.b.c' => 'alpha'])->run(['a' => ['b' => []]]);

@iRedds iRedds closed this Apr 30, 2022
@iRedds iRedds deleted the fix/validation-wildcard branch April 30, 2022 03:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: [Validation] field with asterisk (nested array) throws exception
3 participants