Skip to content

Commit

Permalink
feature #50907 [Validator] Update Type constraint, add number, `f…
Browse files Browse the repository at this point in the history
…inite-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
  • Loading branch information
fabpot committed Jul 30, 2023
2 parents a6178e3 + c3b5709 commit f404704
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ CHANGELOG
* Deprecate passing an annotation reader to the constructor signature of `AnnotationLoader`
* Deprecate `ValidatorBuilder::setDoctrineAnnotationReader()`
* Deprecate `ValidatorBuilder::addDefaultDoctrineAnnotationReader()`
* Add `number`, `finite-number` and `finite-float` types to `Type` constraint

6.3
---
Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/Validator/Constraints/TypeValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class TypeValidator extends ConstraintValidator
'float' => 'is_float',
'double' => 'is_float',
'real' => 'is_float',
'number' => 'is_int || is_float && !is_nan',
'finite-float' => 'is_float && is_finite',
'finite-number' => 'is_int || is_float && is_finite',
'numeric' => 'is_numeric',
'string' => 'is_string',
'scalar' => 'is_scalar',
Expand Down Expand Up @@ -69,7 +72,12 @@ public function validate(mixed $value, Constraint $constraint)

foreach ($types as $type) {
$type = strtolower($type);
if (isset(self::VALIDATION_FUNCTIONS[$type]) && self::VALIDATION_FUNCTIONS[$type]($value)) {
if (isset(self::VALIDATION_FUNCTIONS[$type]) && match ($type) {
'finite-float' => \is_float($value) && is_finite($value),
'finite-number' => \is_int($value) || \is_float($value) && is_finite($value),
'number' => \is_int($value) || \is_float($value) && !is_nan($value),
default => self::VALIDATION_FUNCTIONS[$type]($value),
}) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ public static function getValidValues()
['1.5', 'numeric'],
[0, 'integer'],
[1.5, 'float'],
[\NAN, 'float'],
[\INF, 'float'],
[1.5, 'finite-float'],
[0, 'number'],
[1.5, 'number'],
[\INF, 'number'],
[1.5, 'finite-number'],
['12345', 'string'],
[[], 'array'],
[$object, 'object'],
Expand Down Expand Up @@ -135,7 +142,17 @@ public static function getInvalidValues()
['foobar', 'numeric', '"foobar"'],
['foobar', 'boolean', '"foobar"'],
['0', 'integer', '"0"'],
[\NAN, 'integer', 'NAN'],
[\INF, 'integer', 'INF'],
['1.5', 'float', '"1.5"'],
['1.5', 'finite-float', '"1.5"'],
[\NAN, 'finite-float', 'NAN'],
[\INF, 'finite-float', 'INF'],
['0', 'number', '"0"'],
[\NAN, 'number', 'NAN'],
['0', 'finite-number', '"0"'],
[\NAN, 'finite-number', 'NAN'],
[\INF, 'finite-number', 'INF'],
[12345, 'string', '12345'],
[$object, 'boolean', 'object'],
[$object, 'numeric', 'object'],
Expand Down

0 comments on commit f404704

Please sign in to comment.