Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bfe542d

Browse files
author
Florian Hermann
committed
[Validator] Add list type to Type constraint
1 parent c4e97eb commit bfe542d

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
7.1
5+
---
6+
7+
* Add `list` type to `Type` constraint
8+
49
7.0
510
---
611

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class TypeValidator extends ConstraintValidator
3636
'string' => 'is_string',
3737
'scalar' => 'is_scalar',
3838
'array' => 'is_array',
39+
'list' => 'is_array && array_is_list',
3940
'iterable' => 'is_iterable',
4041
'countable' => 'is_countable',
4142
'callable' => 'is_callable',
@@ -73,6 +74,7 @@ public function validate(mixed $value, Constraint $constraint): void
7374
'finite-float' => \is_float($value) && is_finite($value),
7475
'finite-number' => \is_int($value) || \is_float($value) && is_finite($value),
7576
'number' => \is_int($value) || \is_float($value) && !is_nan($value),
77+
'list' => \is_array($value) && array_is_list($value),
7678
default => self::VALIDATION_FUNCTIONS[$type]($value),
7779
}) {
7880
return;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ public static function getValidValues()
9797
[1.5, 'finite-number'],
9898
['12345', 'string'],
9999
[[], 'array'],
100+
[[], 'list'],
101+
[[1, 2, 3], 'list'],
100102
[$object, 'object'],
101103
[$object, 'stdClass'],
102104
[$file, 'resource'],
@@ -166,6 +168,8 @@ public static function getInvalidValues()
166168
[$file, 'float', 'resource'],
167169
[$file, 'string', 'resource'],
168170
[$file, 'object', 'resource'],
171+
[['1' => 1], 'list', 'array'],
172+
['abcd1', 'list', '"abcd1"'],
169173
['12a34', 'digit', '"12a34"'],
170174
['1a#23', 'alnum', '"1a#23"'],
171175
['abcd1', 'alpha', '"abcd1"'],

0 commit comments

Comments
 (0)