Skip to content

Commit f404704

Browse files
committed
feature #50907 [Validator] Update Type constraint, add number, finite-float and finite-number validations (guillaume-a)
This PR was merged into the 6.4 branch. Discussion ---------- [Validator] Update `Type` constraint, add `number`, `finite-float` and `finite-number` validations | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #50782 | License | MIT | Doc PR | symfony/symfony-docs#18527 Contraint `Type(['float'])` can produce positives with INF or NAN. This new types car narrow validation and limit validation to finite numbers. ``` #[Assert\Type(['int', 'finite-float'])] private $value; ``` Thank you for helping me with this one. Commits ------- c3b5709 [Validator] Update `Type` constraint, add `number`, `finite-float` and `finite-number` validations
2 parents a6178e3 + c3b5709 commit f404704

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
@@ -10,6 +10,7 @@ CHANGELOG
1010
* Deprecate passing an annotation reader to the constructor signature of `AnnotationLoader`
1111
* Deprecate `ValidatorBuilder::setDoctrineAnnotationReader()`
1212
* Deprecate `ValidatorBuilder::addDefaultDoctrineAnnotationReader()`
13+
* Add `number`, `finite-number` and `finite-float` types to `Type` constraint
1314

1415
6.3
1516
---

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 && !is_nan',
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) && !is_nan($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,13 @@ 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+
[\INF, 'number'],
97+
[1.5, 'finite-number'],
9198
['12345', 'string'],
9299
[[], 'array'],
93100
[$object, 'object'],
@@ -135,7 +142,17 @@ public static function getInvalidValues()
135142
['foobar', 'numeric', '"foobar"'],
136143
['foobar', 'boolean', '"foobar"'],
137144
['0', 'integer', '"0"'],
145+
[\NAN, 'integer', 'NAN'],
146+
[\INF, 'integer', 'INF'],
138147
['1.5', 'float', '"1.5"'],
148+
['1.5', 'finite-float', '"1.5"'],
149+
[\NAN, 'finite-float', 'NAN'],
150+
[\INF, 'finite-float', 'INF'],
151+
['0', 'number', '"0"'],
152+
[\NAN, 'number', 'NAN'],
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