Skip to content

Announcement: ScalePrecision methods replaced with PrecisionScale.  #2030

@JeremySkinner

Description

@JeremySkinner

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 is false then the decimal 123.4500 will be considered to have a precision of 7 and scale of 4
  • When ignoreTrailingZeros is true then the decimal 123.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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions