Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[Validator] UpdateType constraint, addnumber,finite-float andfinite-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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
222910f to71dd624CompareUh oh!
There was an error while loading.Please reload this page.
stof left a comment
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
Uh oh!
There was an error while loading.Please reload this page.
fabpot commentedJul 7, 2023
@guillaume-a Can you fix fabbot errors? |
derrabus commentedJul 8, 2023
No, actually that constraint works as advertised because NaN and infinity are valid We should change the PR description. |
derrabus left a comment
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 OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/Validator/Tests/Constraints/FiniteValidatorTest.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/Validator/Tests/Constraints/FiniteValidatorTest.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
src/Symfony/Component/Validator/Tests/Constraints/FiniteValidatorTest.php OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
679acd9 to7019c40Compareguillaume-a commentedJul 8, 2023
Fixed fabbot errors.
|
Uh oh!
There was an error while loading.Please reload this page.
nicolas-grekas commentedJul 13, 2023
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 constraintguillaume-a commentedJul 13, 2023
Renamed @nicolas-grekas originally I looked that way, but IMO it will add lots of complexity to But i'm open to discussion. |
nicolas-grekas commentedJul 13, 2023 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
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, addnumber,finite-float andfinite-number validationsUh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
c043d02 to85d802fCompareUh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
38bfc13 to4d1e2b5Compareguillaume-a commentedJul 17, 2023
Can someone explain me why we must write |
nicolas-grekas commentedJul 20, 2023
nicolas-grekas commentedJul 20, 2023
(can you have a look at the test failure?) |
…d `finite-number` validations
guillaume-a commentedJul 24, 2023
Done. (And rebased too.) |
fabpot commentedJul 30, 2023
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` validationsDocumentation for PR*symfony/symfony#50907Related to*symfony/symfony#50782This 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
Uh oh!
There was an error while loading.Please reload this page.
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.