Skip to content

[Validator] Improve TypeValidator to handle array of types #31351

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

4.4.0
-----

* added support for checking an array of types in `TypeValidator`

4.3.0
-----

Expand Down
27 changes: 15 additions & 12 deletions src/Symfony/Component/Validator/Constraints/TypeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,25 @@ public function validate($value, Constraint $constraint)
return;
}

$type = strtolower($constraint->type);
$type = 'boolean' == $type ? 'bool' : $constraint->type;
$isFunction = 'is_'.$type;
$ctypeFunction = 'ctype_'.$type;

if (\function_exists($isFunction) && $isFunction($value)) {
return;
} elseif (\function_exists($ctypeFunction) && $ctypeFunction($value)) {
return;
} elseif ($value instanceof $constraint->type) {
return;
$types = (array) $constraint->type;

foreach ($types as $type) {
$type = strtolower($type);
$type = 'boolean' === $type ? 'bool' : $type;
$isFunction = 'is_'.$type;
$ctypeFunction = 'ctype_'.$type;
if (\function_exists($isFunction) && $isFunction($value)) {
return;
} elseif (\function_exists($ctypeFunction) && $ctypeFunction($value)) {
return;
} elseif ($value instanceof $type) {
return;
}
}

$this->context->buildViolation($constraint->message)
->setParameter('{{ value }}', $this->formatValue($value))
->setParameter('{{ type }}', $constraint->type)
->setParameter('{{ type }}', implode('|', $types))
->setCode(Type::INVALID_TYPE_ERROR)
->addViolation();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,52 @@ public function getInvalidValues()
];
}

/**
* @dataProvider getValidValuesMultipleTypes
*/
public function testValidValuesMultipleTypes($value, array $types)
{
$constraint = new Type(['type' => $types]);

$this->validator->validate($value, $constraint);

$this->assertNoViolation();
}

public function getValidValuesMultipleTypes()
{
return [
['12345', ['array', 'string']],
[[], ['array', 'string']],
];
}

/**
* @dataProvider getInvalidValuesMultipleTypes
*/
public function testInvalidValuesMultipleTypes($value, $types, $valueAsString)
{
$constraint = new Type([
'type' => $types,
'message' => 'myMessage',
]);

$this->validator->validate($value, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', $valueAsString)
->setParameter('{{ type }}', implode('|', $types))
->setCode(Type::INVALID_TYPE_ERROR)
->assertRaised();
}

public function getInvalidValuesMultipleTypes()
{
return [
['12345', ['boolean', 'array'], '"12345"'],
];
}

protected function createFile()
{
if (!static::$file) {
Expand Down