Skip to content

Commit ed93514

Browse files
committed
Field names must be string
1 parent 0c94540 commit ed93514

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

src/Symfony/Component/Validator/Constraints/UniqueValidator.php

+3
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ private function reduceElementKeys(array $fields, array $element): array
7979
{
8080
$output = [];
8181
foreach ($fields as $field) {
82+
if (!\is_string($field)) {
83+
throw new UnexpectedTypeException($field, 'string');
84+
}
8285
if (isset($element[$field])) {
8386
$output[$field] = $element[$field];
8487
}

src/Symfony/Component/Validator/Tests/Constraints/UniqueValidatorTest.php

+25-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\Validator\Constraints\Unique;
1515
use Symfony\Component\Validator\Constraints\UniqueValidator;
16+
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
1617
use Symfony\Component\Validator\Exception\UnexpectedValueException;
1718
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
1819

@@ -228,10 +229,30 @@ public function testCollectionFieldsAreOptional()
228229
$this->assertNoViolation();
229230
}
230231

232+
/**
233+
* @dataProvider getInvalidFieldNames
234+
*/
235+
public function testCollectionFieldNamesMustBeString(string $type, mixed $field)
236+
{
237+
$this->expectException(UnexpectedTypeException::class);
238+
$this->expectExceptionMessage(sprintf('Expected argument of type "string", "%s" given', $type));
239+
240+
$this->validator->validate([['value' => 5], ['id' => 1, 'value' => 6]], new Unique([$field]));
241+
}
242+
243+
public function getInvalidFieldNames(): \Generator
244+
{
245+
return [
246+
yield ['stdClass', new \stdClass()],
247+
yield ['int', 2],
248+
yield ['bool', false],
249+
];
250+
}
251+
231252
/**
232253
* @dataProvider getInvalidCollectionValues
233254
*/
234-
public function testInvalidCollectionValues($value, $fields)
255+
public function testInvalidCollectionValues(array $value, array $fields)
235256
{
236257
$this->validator->validate($value, new Unique($fields, [
237258
'message' => 'myMessage',
@@ -243,18 +264,18 @@ public function testInvalidCollectionValues($value, $fields)
243264
->assertRaised();
244265
}
245266

246-
public function getInvalidCollectionValues()
267+
public function getInvalidCollectionValues(): \Generator
247268
{
248269
return [
249-
yield 'unique string' => [[['lang' => 'eng', 'translation' => 'hi'], ['lang' => 'eng', 'translation' => 'hi'],
270+
yield 'unique string' => [[['lang' => 'eng', 'translation' => 'hi'], ['lang' => 'eng', 'translation' => 'hello'],
250271
], ['lang']],
251272
yield 'unique floats' => [[
252273
['latitude' => 51.509865, 'longitude' => -0.118092, 'poi' => 'capital'],
253274
['latitude' => 52.520008, 'longitude' => 13.404954],
254275
['latitude' => 51.509865, 'longitude' => -0.118092],
255276
], ['latitude', 'longitude']],
256277
yield 'unique int' => [[
257-
['id' => 1, 'email' => 'bar@email.com'], ['id' => 1, 'email' => 'bar@email.com'],
278+
['id' => 1, 'email' => 'bar@email.com'], ['id' => 1, 'email' => 'foo@email.com'],
258279
], ['id']],
259280
];
260281
}

0 commit comments

Comments
 (0)