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

Appearance settings

[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
Field names must be string
  • Loading branch information
wkania committed Apr 3, 2022
commit 5169f4acd335dcd8fc0a9fe9f2a427e817016f22
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ private function reduceElementKeys(array $fields, array $element): array
{
$output = [];
foreach ($fields as $field) {
if (!\is_string($field)) {
throw new UnexpectedTypeException($field, 'string');
}
if (isset($element[$field])) {
$output[$field] = $element[$field];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Validator\Constraints\Unique;
use Symfony\Component\Validator\Constraints\UniqueValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

Expand Down Expand Up @@ -228,10 +229,30 @@ public function testCollectionFieldsAreOptional()
$this->assertNoViolation();
}

/**
* @dataProvider getInvalidFieldNames
*/
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]));
}

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

/**
* @dataProvider getInvalidCollectionValues
*/
public function testInvalidCollectionValues($value, $fields)
public function testInvalidCollectionValues(array $value, array $fields)
{
$this->validator->validate($value, new Unique($fields, [
'message' => 'myMessage',
Expand All @@ -243,18 +264,18 @@ public function testInvalidCollectionValues($value, $fields)
->assertRaised();
}

public function getInvalidCollectionValues()
public function getInvalidCollectionValues(): \Generator
{
return [
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
yield 'unique string' => [[['lang' => 'eng', 'translation' => 'hi'], ['lang' => 'eng', 'translation' => 'hi'],
yield 'unique string' => [[['lang' => 'eng', 'translation' => 'hi'], ['lang' => 'eng', 'translation' => 'hello'],
], ['lang']],
nicolas-grekas marked this conversation as resolved.
Show resolved Hide resolved
yield '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' => [[
['id' => 1, 'email' => 'bar@email.com'], ['id' => 1, 'email' => 'bar@email.com'],
['id' => 1, 'email' => 'bar@email.com'], ['id' => 1, 'email' => 'foo@email.com'],
], ['id']],
];
}
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.