-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
Background
In FluentValidation versions prior to 11.4, you could define precision/scale validation for a decimal by using the ScalePrecision
method. This method has caused some confusion because of the order of its parameters are different from those when defining a decimal (for example, in SQL).
A decimal in SQL defines its parameters as precision then scale, so decimal(4, 2)
defines a precision of 4 digits and a scale of 2 digits. However, validating this decimal within FluentValidation would require a call to ScalePrecision(2, 4)
(scale first, precision second), which can be confusing.
Migration Steps
If you were previously calling the ScalePrecision(scale, precision)
method, you should now call the PrecisionScale(precision, scale, ignoreTrailingZeros)
method instead.
// Before migration:
RuleFor(x => x.SomeDecimal).ScalePrecision(2, 4);
RuleFor(x => x.SomeDecimalIgnoringTrailingZeros).ScalePrecision(2, 4, true);
// After Migration:
RuleFor(x => x.SomeDecimal).PrecisionScale(4, 2, false);
RuleFor(x => x.SomeDecimalIgnoringTrailingZeros).PrecisionScale(4, 2, true);
Note that as well as the swapped order of parameters, you must also explicitly indicate whether or not you want to ignore trailing zeros. Example:
- When
ignoreTrailingZeros
isfalse
then the decimal123.4500
will be considered to have a precision of 7 and scale of 4 - When
ignoreTrailingZeros
istrue
then the decimal123.4500
will be considered to have a precision of 5 and scale of 2.
With the old ScalePrecision
methods, this parameter defaulted to false
when not specified. With the new PrecisionScale
methods you must pass in this parameter, explicitly choosing which behaviour you prefer.