Closed
Description
Symfony version(s) affected: 4.3.3
Description
When running a validator on a collection that only has optional fields it does not give violations on extra fields. It does work however if I make one of the fields required.
How to reproduce
$toValidate = ['a' => 'b'];
$constraints = new Assert\Collection([
'firstName' => new Assert\Optional(),
'lastName' => new Assert\Optional(),
'username' => new Assert\Optional(),
]);
$validator = Validation::createValidator();
$violations = $validator->validate($toValidate, $constraints, new Assert\GroupSequence(['Default', 'custom']));
// $violations is empty
If I change firstName to new Assert\Positive()
as example, then it does work.
{"errors":["[firstName]: This field is missing.","[a]: This field was not expected."]}