8000 [Validator] Update `Type` constraint, add `number`, `finite-float` an… · symfony/symfony@9e112da · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e112da

Browse files
committed
[Validator] Update Type constraint, add number, finite-float and finite-number validations
1 parent 80f1096 commit 9e112da

File tree

3 files changed

+56
-30
lines changed

3 files changed

+56
-30
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ CHANGELOG
66

77
* Allow single integer for the `versions` option of the `Uuid` constraint
88
* Allow single constraint to be passed to the `constraints` option of the `When` constraint
9+
* Add `Finite` constraint
910

1011
6.3
1112
---

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

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,35 +21,38 @@
2121
class TypeValidator extends ConstraintValidator
2222
{
2323
private const VALIDATION_FUNCTIONS = [
24-
'bool' => 'is_bool',
25-
'boolean' => 'is_bool',
26-
'int' => 'is_int',
27-
'integer' => 'is_int',
28-
'long' => 'is_int',
29-
'float' => 'is_float',
30-
'double' => 'is_float',
31-
'real' => 'is_float',
32-
'numeric' => 'is_numeric',
33-
'string' => 'is_string',
34-
'scalar' => 'is_scalar',
35-
'array' => 'is_array',
36-
'iterable' => 'is_iterable',
37-
'countable' => 'is_countable',
38-
'callable' => 'is_callable',
39-
'object' => 'is_object',
40-
'resource' => 'is_resource',
41-
'null' => 'is_null',
42-
'alnum' => 'ctype_alnum',
43-
'alpha' => 'ctype_alpha',
44-
'cntrl' => 'ctype_cntrl',
45-
'digit' => 'ctype_digit',
46-
'graph' => 'ctype_graph',
47-
'lower' => 'ctype_lower',
48-
'print' => 'ctype_print',
49-
'punct' => 'ctype_punct',
50-
'space' => 'ctype_space',
51-
'upper' => 'ctype_upper',
52-
'xdigit' => 'ctype_xdigit',
24+
'bool' => 'is_bool',
25+
'boolean' => 'is_bool',
26+
'int' => 'is_int',
27+
'integer' => 'is_int',
28+
'long' => 'is_int',
29+
'float' => 'is_float',
30+
'double' => 'is_float',
31+
'real' => 'is_float',
32+
'number' => 'is_int || is_float',
33+
'finite-float' => 'is_float && is_finite',
34+
'finite-number' => 'is_int || is_float && is_finite',
35+
'numeric' => 'is_numeric',
36+
'string' => 'is_string',
37+
'scalar' => 'is_scalar',
38+
'array' => 'is_array',
39+
'iterable' => 'is_iterable',
40+
'countable' => 'is_countable',
41+
'callable' => 'is_callable',
42+
'object' => 'is_object',
43+
'resource' => 'is_resource',
44+
'null' => 'is_null',
45+
'alnum' => 'ctype_alnum',
46+
'alpha' => 'ctype_alpha',
47+
'cntrl' => 'ctype_cntrl',
48+
'digit' => 'ctype_digit',
49+
'graph' => 'ctype_graph',
50+
'lower' => 'ctype_lower',
51+
'print' => 'ctype_print',
52+
'punct' => 'ctype_punct',
53+
'space' => 'ctype_space',
54+
'upper' => 'ctype_upper',
55+
'xdigit' => 'ctype_xdigit',
5356
];
5457

5558
/**
@@ -69,7 +72,12 @@ public function validate(mixed $value, Constraint $constraint)
6972

7073
foreach ($types as $type) {
7174
$type = strtolower($type);
72-
if (isset(self::VALIDATION_FUNCTIONS[$type]) && self::VALIDATION_FUNCTIONS[$type]($value)) {
75+
if (isset(self::VALIDATION_FUNCTIONS[$type]) && match ($type) {
76+
'finite-float' => \is_float($value) && \is_finite($value),
77+
'finite-number' => \is_int($value) || \is_float($value) && \is_finite($value),
78+
'number' => \is_int($value) || \is_float($value),
79+
default => self::VALIDATION_FUNCTIONS[$type]($value),
80+
}) {
7381
return;
7482
}
7583

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ public static function getValidValues()
8888
['1.5', 'numeric'],
8989
[0, 'integer'],
9090
[1.5, 'float'],
91+
[\NAN, 'float'],
92+
[\INF, 'float'],
93+
[1.5, 'finite-float'],
94+
[0, 'number'],
95+
[1.5, 'number'],
96+
[\NAN, 'number'],
97+
[\INF, 'number'],
98+
[1.5, 'finite-number'],
9199
['12345', 'string'],
92100
[[], 'array'],
93101
[$object, 'object'],
@@ -135,7 +143,16 @@ public static function getInvalidValues()
135143
['foobar', 'numeric', '"foobar"'],
136144
['foobar', 'boolean', '"foobar"'],
137145
['0', 'integer', '"0"'],
146+
[\NAN, 'integer', 'NAN'],
147+
[\INF, 'integer', 'INF'],
138148
['1.5', 'float', '"1.5"'],
149+
['1.5', 'finite-float', '"1.5"'],
150+
[\NAN, 'finite-float', 'NAN'],
151+
[\INF, 'finite-float', 'INF'],
152+
['0', 'number', '"0"'],
153+
['0', 'finite-number', '"0"'],
154+
[\NAN, 'finite-number', 'NAN'],
155+
[\INF, 'finite-number', 'INF'],
139156
[12345, 'string', '12345'],
140157
[$object, 'boolean', 'object'],
141158
[$object, 'numeric', 'object'],

0 commit comments

Comments
 (0)
0