Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Validator] Fix type error for non-array items whenUnique::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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
aprat84 wants to merge1 commit intosymfony:6.3
base:6.3
Choose a base branch
Loading
fromaprat84:fix/unique-validator-error

Conversation

aprat84
Copy link
Contributor

@aprat84aprat84 commentedNov 24, 2023
edited
Loading

QA
Branch?6.3
Bug fix?yes
New feature?no
Deprecations?no
Issues
LicenseMIT

TheUniqueValidator 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:

newUnique(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 thefields, 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)!

wkania reacted with thumbs up emoji
@carsonbotcarsonbot added this to the6.3 milestoneNov 24, 2023
@OskarStarkOskarStark changed the title[Validator] Fix type error for non-array items when Unique::fields is set[Validator] Fix type error for non-array items whenUnique::fields is setNov 24, 2023
Copy link
Member

@nicolas-grekasnicolas-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 reacted with thumbs up emoji
@aprat84aprat84force-pushed thefix/unique-validator-error branch 5 times, most recently from7ee329e tof7158bdCompareNovember 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 anUnexpectedValueException instead.

Copy link
ContributorAuthor

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.

Whenfields 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 passingfields: '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 theUnexpectedValueException should not exist, as we already have theType 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 😅

@aprat84aprat84force-pushed thefix/unique-validator-error branch fromf7158bd to6b7df72CompareDecember 4, 2023 14:42
@nicolas-grekasnicolas-grekas modified the milestones:6.3,6.4Feb 1, 2024
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@nicolas-grekasnicolas-grekasnicolas-grekas left review comments

@xabbuhxabbuhxabbuh left review comments

Assignees
No one assigned
Projects
None yet
Milestone
6.4
Development

Successfully merging this pull request may close these issues.

4 participants
@aprat84@nicolas-grekas@xabbuh@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp