-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
ScalePrecision validation error message is misleading #2211
Comments
hi, thanks for the feedback. I'm not keen on changing the error message at this point because of the huge amount of work required in updating all the translations, however it sounds like it might be worth at least updating the documentation to provide additional clarification - if you'd like to open a PR for the docs then I'd be happy to review it. |
Hi, thank you for the response! I think that it can be borrowed here how sql defines what range of numbers can be stored as Note that this change would not conflict with ignoreTrailingZeros feature, since this makes importance only when decimal part somehow overflows expected decimals amount. In other words if |
Co-authored-by: Nick Howell <howellnick@gmail.com>
I've pushed out 11.9.2 with this change |
Awesome, thank you! |
FluentValidation version
11.2.2
ASP.NET version
.NET 6
Summary
It appears that
ScalePrecisionValidator
produces an error message which does not quite reflect what is actually being validated.I have a decimal property in my model class, which I want to be of precision
10
and scale2
. So in theory a value like123456789
should be fine, since by not providing any zeros after decimal point the total precision of this number is9
. And it seems thatScalePrecisionValidator
calculates precision correctly, but it still gives a validation error and a message like this:Which sounds like a nonsense. After a bit of digging the FluentValidation source code it turned out what is actually being validated by
ScalePrecisionValidator
:So it does not actually validate precision at all. What is being validated is that the integer part of the value must not overflow expected amount of digits, i.e.
Precision - Scale
amount of digits, which in my case was8
digits. And now it makes sense why validation does not pass, because indeed my number has9
digits in the integer part. But what is being communicated back to the user as validation error message and what is being validated are two different things. Without knowing this implementational detail this might be quite hard to the user to make out why validation does not pass.This issue would actually relate to the abandoned issue #1200, but the proposal stated there would not resolve the problem I think, since in this case, for example, giving the total precision and giving amount of integer digits would result in the same number
9
. So I would say that it might be better to communicate'ExpectedIntegerDigits'
as something that was expected from a decimal value to have, and what is indeed validated. And the error message would sound something like:In this manner the error message would make sense.
But also as stated in #1200, it is true that such a validation logic does not correspond with what is actually defined by scale and precision. But such a thing is discutable, and the error message problem is on the other hand more specific.
Steps to Reproduce
This can be quite easily reproduced. Assume we have a class
A
with a decimal propertyProperty
:And we want this property to be of precision
10
and scale2
:So we try to give it a number like
123456789
and to execute validation on it:And instead of not having any validation error at all or at least having an error message which would seem reasonable the result would be:
The text was updated successfully, but these errors were encountered: