Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.6k
[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
to71dd624
CompareUh oh!
There was an error while loading.Please reload this page.
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.
@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?
- …
null
or 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
to7019c40
CompareFixed fabbot errors.
|
Uh oh!
There was an error while loading.Please reload this page.
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
constraintRenamed @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
to85d802f
CompareUh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
38bfc13
to4d1e2b5
CompareCan 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` 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.