Number Rules
Number rules validate numeric values, comparable sizes, digit counts, and numeric relationships. Comparable size rules adapt to the value type: strings use character count, arrays use item count, files use kilobytes, and numeric values use the number itself.
Numeric
Requires the value to be numeric.
[Numeric]RuleFor(x => x.Amount)
.Numeric();Integer
Requires the value to be an integer.
[Integer]RuleFor(x => x.Quantity)
.Integer();Min
Requires the value to be at least the provided number. For strings, collections, and files, the number is interpreted as length, count, or kilobytes.
[Min(double value)]
[Min(3)]RuleFor(x => x.DisplayName)
.Min(3);Max
Requires the value to be no greater than the provided number. For strings, collections, and files, the number is interpreted as length, count, or kilobytes.
[Max(double value)]
[Max(255)]RuleFor(x => x.DisplayName)
.Max(255);Between
Requires the value to be between the inclusive minimum and maximum.
[Between(double min, double max)]
[Between(1, 365)]RuleFor(x => x.DurationDays)
.Between(1, 365);Size
Requires the value to match the exact numeric value, string length, collection count, or file size.
[Size(double value)]
[Size(6)]RuleFor(x => x.Code)
.Size(6);Decimal
Requires the numeric value to have the configured number of decimal places.
[Decimal(int places)]
[Decimal(2)]RuleFor(x => x.Price)
.Decimal(2);Digits
Requires the value to contain exactly the provided number of digits, counting the digits as written so a leading zero counts. Only a run of ASCII digits qualifies: a sign, a decimal point, or a thousands separator fails the rule rather than being skipped over.
[Digits(int digits)]
[Digits(8)]RuleFor(x => x.Pin)
.Digits(8);DigitsBetween
Requires the digit count to be between the inclusive minimum and maximum.
[DigitsBetween(int min, int max)]
[DigitsBetween(8, 12)]RuleFor(x => x.AccountNumber)
.DigitsBetween(8, 12);MinDigits
Requires the value to contain at least the provided number of digits.
[MinDigits(int min)]
[MinDigits(8)]RuleFor(x => x.AccountNumber)
.MinDigits(8);MaxDigits
Allows the value to contain at most the provided number of digits.
[MaxDigits(int max)]
[MaxDigits(12)]RuleFor(x => x.AccountNumber)
.MaxDigits(12);MultipleOf
Requires the numeric value to be a multiple of the provided number.
[MultipleOf(double value)]
[MultipleOf(5)]RuleFor(x => x.Quantity)
.MultipleOf(5);GreaterThan
Requires the value to be greater than the provided number.
[GreaterThan(double value)]
[GreaterThan(0)]RuleFor(x => x.Price)
.GreaterThan(0);GreaterThanOrEqual
Requires the value to be greater than or equal to the provided number.
[GreaterThanOrEqual(double value)]
[GreaterThanOrEqual(18)]RuleFor(x => x.Age)
.GreaterThanOrEqual(18);LessThan
Requires the value to be less than the provided number.
[LessThan(double value)]
[LessThan(100)]RuleFor(x => x.DiscountPercentage)
.LessThan(100);LessThanOrEqual
Requires the value to be less than or equal to the provided number.
[LessThanOrEqual(double value)]
[LessThanOrEqual(100)]RuleFor(x => x.DiscountPercentage)
.LessThanOrEqual(100);