-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Add constraint on unique elements collection(Assert\Unique) #26555
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
|
||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
use Symfony\Component\Validator\Exception\UnexpectedValueException; | ||
|
||
/** | ||
* @author Yevgeniy Zholkevskiy <zhenya.zholkevskiy@gmail.com> | ||
|
@@ -33,13 +33,13 @@ public function validate($value, Constraint $constraint) | |
return; | ||
} | ||
|
||
if (!is_array($value) && !$value instanceof \IteratorAggregate) { | ||
throw new UnexpectedTypeException($value, 'IteratorAggregate'); | ||
if (!\is_array($value) && !$value instanceof \IteratorAggregate) { | ||
throw new UnexpectedValueException($value, 'array|IteratorAggregate'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see review chain There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
? was it a blocker for this PR? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i can pass a broken/unrewindbable iterator as aggregate just as easy. But i cant pass a working iterator/iterable directly. That's poor DX IMHO. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consequencies of this validator being applied on non-rewindable iterators are much bigger than in #25506 so yes (generally, after validator validates the data, data is processed) |
||
} | ||
|
||
$collectionElements = array(); | ||
$collectionElements = []; | ||
foreach ($value as $element) { | ||
if (in_array($element, $collectionElements, true)) { | ||
if (\in_array($element, $collectionElements, true)) { | ||
$this->context->buildViolation($constraint->message) | ||
->setParameter('{{ value }}', $this->formatValue($value)) | ||
->setCode(Unique::IS_NOT_UNIQUE) | ||
|
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.
UniqueCollection
?