8000 [Validator] Define which collection keys should be checked for uniqueness by wkania · Pull Request #42403 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Define which collection keys should be checked for uniqueness #42403

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

Closed
wants to merge 12 commits into from
Prev Previous commit
Next Next commit
After CR fixes
  • Loading branch information
wkania committed Apr 3, 2022
commit 567e703fad3a7012cdd6ed833b28d978d2cdf1f8
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@ public function validate(mixed $value, Constraint $constraint)
$collectionElements = [];
$normalizer = $this->getNormalizer($constraint);
foreach ($value as $element) {
if (!empty($fields)) {
$element = $this->reduceElementKeys($fields, $element);
if (empty($element)) {
continue;
}
if ($fields && !$element = $this->reduceElementKeys($fields, $element)) {
continue;
}

$element = $normalizer($element);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ public function testExpectsValidCaseInsensitiveComparison()

public function testCollectionFieldsAreOptional()
{
$this->validator->validate([['value' => 5], ['id' => 1, 'value' => 6]], new Unique('id'));
$this->validator->validate([['value' => 5], ['id' => 1, 'value' => 6]], new Unique(fields: 'id'));

$this->assertNoViolation();
}
Expand All @@ -237,15 +237,15 @@ public function testCollectionFieldNamesMustBeString(string $type, mixed $field)
$this->expectException(UnexpectedTypeException::class);
$this->expectExceptionMessage(sprintf('Expected argument of type "string", "%s" given', $type));

$this->validator->validate([['value' => 5], ['id' => 1, 'value' => 6]], new Unique([$field]));
$this->validator->validate([['value' => 5], ['id' => 1, 'value' => 6]], new Unique(fields: [$field]));
}

public function getInvalidFieldNames(): \Generator
public function getInvalidFieldNames(): array
{
return [
yield ['stdClass', new \stdClass()],
yield ['int', 2],
yield ['bool', false],
['stdClass', new \stdClass()],
['int', 2],
['bool', false],
];
}

Expand All @@ -254,27 +254,27 @@ public function getInvalidFieldNames(): \Generator
*/
public function testInvalidCollectionValues(array $value, array $fields)
{
$this->validator->validate($value, new Unique($fields, [
$this->validator->validate($value, new Unique([
'message' => 'myMessage',
]));
], fields: $fields));

$this->buildViolation('myMessage')
->setParameter('{{ value }}', 'array')
->setCode(Unique::IS_NOT_UNIQUE)
->assertRaised();
}

public function getInvalidCollectionValues(): \Generator
public function getInvalidCollectionValues(): array
{
return [
yield 'unique string' => [[['lang' => 'eng', 'translation' => 'hi'], ['lang' => 'eng', 'translation' => 'hello'],
'unique string' => [[['lang' => 'eng', 'translation' => 'hi'], ['lang' => 'eng', 'translation' => 'hello'],
], ['lang']],
yield 'unique floats' => [[
'unique floats' => [[
['latitude' => 51.509865, 'longitude' => -0.118092, 'poi' => 'capital'],
['latitude' => 52.520008, 'longitude' => 13.404954],
['latitude' => 51.509865, 'longitude' => -0.118092],
], ['latitude', 'longitude']],
yield 'unique int' => [[
'unique int' => [[
['id' => 1, 'email' => 'bar@email.com'], ['id' => 1, 'email' => 'foo@email.com'],
], ['id']],
];
Expand Down
0