Skip to content

Commit 85d802f

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

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
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 `number`, `finite-number` and `finite-float` types to `Type` constraint
910

1011
6.3
1112
---

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class TypeValidator extends ConstraintValidator
2929
'float' => 'is_float',
3030
'double' => 'is_float',
3131
'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',
3235
'numeric' => 'is_numeric',
3336
'string' => 'is_string',
3437
'scalar' => 'is_scalar',
@@ -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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy