Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[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

Open
wants to merge 1 commit into
base: 6.3
Choose a base branch
from

Conversation

aprat84
Copy link
Contributor

@aprat84 aprat84 commented Nov 24, 2023

Q A
Branch? 6.3
Bug fix? yes
New feature? no
Deprecations? no
Issues
License MIT

The UniqueValidator crashes if:

  • fields argument is set
  • value is an array but some of the elements are not

Example value:

$value = [
    [
        "field": 1
    ],
    "abc",
];

Example constraint config:

new Unique(fields: ['field']);

Error thrown:

Symfony\Component\Validator\Constraints\UniqueValidator::reduceElementKeys(): Argument #2 ($element) must be of type array, string given, called in /var/www/html/vendor/symfony/validator/Constraints/UniqueValidator.php on line 46

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)!

@carsonbot carsonbot added this to the 6.3 milestone Nov 24, 2023
@OskarStark OskarStark changed the title [Validator] Fix type error for non-array items when Unique::fields is set [Validator] Fix type error for non-array items when Unique::fields is set Nov 24, 2023
Copy link
Member

@nicolas-grekas nicolas-grekas left a 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?

@aprat84 aprat84 force-pushed the fix/unique-validator-error branch 5 times, most recently from 7ee329e to f7158bd Compare November 27, 2023 09:55
@@ -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))) {
Copy link
Member

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.

Copy link
Contributor Author

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 😅

@aprat84 aprat84 force-pushed the fix/unique-validator-error branch from f7158bd to 6b7df72 Compare December 4, 2023 14:42
@nicolas-grekas nicolas-grekas modified the milestones: 6.3, 6.4 Feb 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants