-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Fix type error for non-array items when Unique::fields
is set
#52722
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
base: 6.3
Are you sure you want to change the base?
Conversation
Unique::fields
is set
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please rebase to get rid of the first commit?
src/Symfony/Component/Validator/Tests/Constraints/UniqueValidatorTest.php
Outdated
Show resolved
Hide resolved
7ee329e
to
f7158bd
Compare
@@ -43,7 +43,7 @@ public function validate(mixed $value, Constraint $constraint) | |||
$collectionElements = []; | |||
$normalizer = $this->getNormalizer($constraint); | |||
foreach ($value as $element) { | |||
if ($fields && !$element = $this->reduceElementKeys($fields, $element)) { | |||
if ($fields && !(\is_array($element) && $element = $this->reduceElementKeys($fields, $element))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if we shouldn't rather throw an UnexpectedValueException
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point!
Checking types is not this constraint validator responsibility, as it doesn't need a certain type to do its validation.
When fields
is passed, and an item doesn't have them, even if it's an array, it is ignored. Why not do the same when the item is not an array?
Every item in the following collection is unique, event when passing fields: 'a'
to the constraint:
[
['a' => 1, 'b' => 1],
['a' => 2, 'b' => 2],
['b' => 3],
null,
]
Btw, I'll be happy with any solution, as long as it doesn't crash.
On a wider thought, I think the UnexpectedValueException
should not exist, as we already have the Type
constraint.
A value that can not be validated by a constraint should be ignored (ie: EmailValidator
should skip anything that's not a string)
But this is another topic, and probably I lack the knowledge to suggest so 😅
f7158bd
to
6b7df72
Compare
The
UniqueValidator
crashes if:fields
argument is setvalue
is an array but some of the elements are notExample value:
Example constraint config:
Error thrown:
Solution:
Looking at the actual behavior, if one of its items, being an array, has none of the
fields
, is ignored.So, continuing on this premise, if one of its items, is not an array, it should be ignored too.
That's what this PR is about (hope so)!