-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Validator] Update Type constraint, add number, finite-float and finite-number validations
#50907
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
222910f to
71dd624
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is missing tests
|
@guillaume-a Can you fix fabbot errors? |
No, actually that constraint works as advertised because NaN and infinity are valid We should change the PR description. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if that validator encounters…
- … a non-numeric string?
- …
nullor an empty string? - … something completely different lika an array or object?
Can we cover those scenarios with tests?
src/Symfony/Component/Validator/Tests/Constraints/FiniteValidatorTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Tests/Constraints/FiniteValidatorTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Tests/Constraints/FiniteValidatorTest.php
Outdated
Show resolved
Hide resolved
src/Symfony/Component/Validator/Tests/Constraints/FiniteValidatorTest.php
Outdated
Show resolved
Hide resolved
679acd9 to
7019c40
Compare
|
Fixed fabbot errors.
|
|
What about adding new types to the Type constraint instead? #[Assert\Type(['finite-float'])]
#[Assert\Type(['number'])] # this would be (is_int || is_float) && is_finite |
Finite constraintIsFinite constraint
|
Renamed @nicolas-grekas originally I looked that way, but IMO it will add lots of complexity to But i'm open to discussion. |
|
It looks like this would be enough to support my proposal. Not too much complexity IMHO. --- a/src/Symfony/Component/Validator/Constraints/TypeValidator.php
+++ b/src/Symfony/Component/Validator/Constraints/TypeValidator.php
@@ -26,9 +26,11 @@ class TypeValidator extends ConstraintValidator
'int' => 'is_int',
'integer' => 'is_int',
'long' => 'is_int',
+ 'finite-float' => 'is_float && is_finite',
'float' => 'is_float',
'double' => 'is_float',
'real' => 'is_float',
+ 'number' => 'is_int || is_float && is_finite',
'numeric' => 'is_numeric',
'string' => 'is_string',
'scalar' => 'is_scalar',
@@ -69,7 +71,13 @@ class TypeValidator extends ConstraintValidator
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 (self::VALIDATION_FUNCTIONS[$type]) {
+ 'finite-float' => \is_float($value) && \is_finite($value),
+ 'number' => \is_int($value) || \is_float($value) && \is_finite($value),
+ default => self::VALIDATION_FUNCTIONS[$type]($value),
+ }) {
return;
} |
IsFinite constraintType constraint, add number, finite-float and finite-number validations
c043d02 to
85d802f
Compare
38bfc13 to
4d1e2b5
Compare
|
Can someone explain me why we must write |
|
(can you have a look at the test failure?) |
…d `finite-number` validations
Done. (And rebased too.) |
|
Thank you @guillaume-a. |
…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 Documentation for PR * symfony/symfony#50907 Related to * symfony/symfony#50782 This is my first sf-docs PR, please tell me if I did anything wrong. Thank you. Commits ------- 458fdd5 [Validator] Update `Type` constraint, add `number`, `finite-float` and `finite-number` validations
Contraint
Type(['float'])can produce positives with INF or NAN.This new types car narrow validation and limit validation to finite numbers.
Thank you for helping me with this one.