Class ValidatorRuleBuilderExtensions
Extension methods on ValidatorRuleBuilder<T> providing a native DSL for common validation rules (string, numeric, collection, null checks).
public static class ValidatorRuleBuilderExtensions
- Inheritance
-
ValidatorRuleBuilderExtensions
- Inherited Members
Examples
var validator = new ValidatorRuleBuilder<CreateUserRequest>()
.NotEmpty(u => u.Name)
.MaxLength(u => u.Name, 100)
.EmailAddress(u => u.Email)
.Range(u => u.Age, 18, 120)
.Build();
Remarks
Rules use Expression<TDelegate> selectors so the property name can be inferred automatically for default error messages. The expression is compiled once at build time — acceptable because builders are configured at startup, not in hot loops.
Methods
Contains<T>(ValidatorRuleBuilder<T>, Expression<Func<T, string?>>, string, StringComparison, string?)
Validates that the selected string property contains value.
The comparison is ordinal and case-sensitive by default.
null fails this rule.
public static ValidatorRuleBuilder<T> Contains<T>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, string?>> selector, string value, StringComparison comparison = StringComparison.Ordinal, string? errorMessage = null)
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, string>>valuestringcomparisonStringComparisonerrorMessagestring
Returns
Type Parameters
T
EmailAddress<T>(ValidatorRuleBuilder<T>, Expression<Func<T, string?>>, string?)
Validates that the selected string property is a valid email address.
null fails this rule.
public static ValidatorRuleBuilder<T> EmailAddress<T>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, string?>> selector, string? errorMessage = null)
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, string>>errorMessagestring
Returns
Type Parameters
T
EndsWith<T>(ValidatorRuleBuilder<T>, Expression<Func<T, string?>>, string, StringComparison, string?)
Validates that the selected string property ends with suffix.
The comparison is ordinal and case-sensitive by default.
null fails this rule.
public static ValidatorRuleBuilder<T> EndsWith<T>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, string?>> selector, string suffix, StringComparison comparison = StringComparison.Ordinal, string? errorMessage = null)
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, string>>suffixstringcomparisonStringComparisonerrorMessagestring
Returns
Type Parameters
T
GreaterThan<T, TNum>(ValidatorRuleBuilder<T>, Expression<Func<T, TNum>>, TNum, string?)
Validates that the selected comparable property is strictly greater than min.
Works for int, long, double, decimal, and any IComparable<T>.
public static ValidatorRuleBuilder<T> GreaterThan<T, TNum>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, TNum>> selector, TNum min, string? errorMessage = null) where TNum : IComparable<TNum>
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, TNum>>minTNumerrorMessagestring
Returns
Type Parameters
TTNum
Length<T>(ValidatorRuleBuilder<T>, Expression<Func<T, string?>>, int, int, string?)
Validates that the selected string property length is between min
and max characters (inclusive). null fails this rule.
public static ValidatorRuleBuilder<T> Length<T>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, string?>> selector, int min, int max, string? errorMessage = null)
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, string>>minintmaxinterrorMessagestring
Returns
Type Parameters
T
LessThan<T, TNum>(ValidatorRuleBuilder<T>, Expression<Func<T, TNum>>, TNum, string?)
Validates that the selected comparable property is strictly less than max.
Works for int, long, double, decimal, and any IComparable<T>.
public static ValidatorRuleBuilder<T> LessThan<T, TNum>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, TNum>> selector, TNum max, string? errorMessage = null) where TNum : IComparable<TNum>
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, TNum>>maxTNumerrorMessagestring
Returns
Type Parameters
TTNum
Matches<T>(ValidatorRuleBuilder<T>, Expression<Func<T, string?>>, string, string?)
Validates that the selected string property matches the given regular expression
pattern. null fails this rule.
public static ValidatorRuleBuilder<T> Matches<T>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, string?>> selector, string pattern, string? errorMessage = null)
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, string>>patternstringerrorMessagestring
Returns
Type Parameters
T
MaxCount<T, TItem>(ValidatorRuleBuilder<T>, Expression<Func<T, IEnumerable<TItem>?>>, int, string?)
Validates that the selected collection property has at most max items.
null passes this rule — combine with NotEmpty<T, TItem>(ValidatorRuleBuilder<T>, Expression<Func<T, IEnumerable<TItem>?>>, string?) if null should be rejected.
public static ValidatorRuleBuilder<T> MaxCount<T, TItem>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, IEnumerable<TItem>?>> selector, int max, string? errorMessage = null)
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, IEnumerable<TItem>>>maxinterrorMessagestring
Returns
Type Parameters
TTItem
MaxLength<T>(ValidatorRuleBuilder<T>, Expression<Func<T, string?>>, int, string?)
Validates that the selected string property does not exceed max characters.
null passes this rule — combine with NotEmpty<T>(ValidatorRuleBuilder<T>, Expression<Func<T, string?>>, string?) if null should be rejected.
public static ValidatorRuleBuilder<T> MaxLength<T>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, string?>> selector, int max, string? errorMessage = null)
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, string>>maxinterrorMessagestring
Returns
Type Parameters
T
MinCount<T, TItem>(ValidatorRuleBuilder<T>, Expression<Func<T, IEnumerable<TItem>?>>, int, string?)
Validates that the selected collection property has at least min items.
null fails this rule.
public static ValidatorRuleBuilder<T> MinCount<T, TItem>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, IEnumerable<TItem>?>> selector, int min, string? errorMessage = null)
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, IEnumerable<TItem>>>mininterrorMessagestring
Returns
Type Parameters
TTItem
MinLength<T>(ValidatorRuleBuilder<T>, Expression<Func<T, string?>>, int, string?)
Validates that the selected string property has at least min characters.
null fails this rule.
public static ValidatorRuleBuilder<T> MinLength<T>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, string?>> selector, int min, string? errorMessage = null)
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, string>>mininterrorMessagestring
Returns
Type Parameters
T
NonNegative<T, TNum>(ValidatorRuleBuilder<T>, Expression<Func<T, TNum>>, string?)
Validates that the selected comparable property is zero or positive (non-negative).
Works for int, long, double, decimal, and any IComparable<T>
whose zero value is default(TNum).
public static ValidatorRuleBuilder<T> NonNegative<T, TNum>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, TNum>> selector, string? errorMessage = null) where TNum : struct, IComparable<TNum>
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, TNum>>errorMessagestring
Returns
Type Parameters
TTNum
NotEmpty<T>(ValidatorRuleBuilder<T>, Expression<Func<T, string?>>, string?)
Validates that the selected string property is not null or empty ("").
public static ValidatorRuleBuilder<T> NotEmpty<T>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, string?>> selector, string? errorMessage = null)
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, string>>errorMessagestring
Returns
Type Parameters
T
NotEmpty<T, TItem>(ValidatorRuleBuilder<T>, Expression<Func<T, IEnumerable<TItem>?>>, string?)
Validates that the selected collection property is not null or empty.
public static ValidatorRuleBuilder<T> NotEmpty<T, TItem>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, IEnumerable<TItem>?>> selector, string? errorMessage = null)
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, IEnumerable<TItem>>>errorMessagestring
Returns
Type Parameters
TTItem
NotNull<T, TProp>(ValidatorRuleBuilder<T>, Expression<Func<T, TProp?>>, string?)
Validates that the selected reference-type property is not null.
public static ValidatorRuleBuilder<T> NotNull<T, TProp>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, TProp?>> selector, string? errorMessage = null) where TProp : class
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, TProp>>errorMessagestring
Returns
Type Parameters
TTProp
NotWhiteSpace<T>(ValidatorRuleBuilder<T>, Expression<Func<T, string?>>, string?)
Validates that the selected string property is not null, empty, or whitespace.
public static ValidatorRuleBuilder<T> NotWhiteSpace<T>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, string?>> selector, string? errorMessage = null)
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, string>>errorMessagestring
Returns
Type Parameters
T
Positive<T, TNum>(ValidatorRuleBuilder<T>, Expression<Func<T, TNum>>, string?)
Validates that the selected comparable property is strictly positive (greater than zero).
Works for int, long, double, decimal, and any IComparable<T>
whose zero value is default(TNum).
public static ValidatorRuleBuilder<T> Positive<T, TNum>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, TNum>> selector, string? errorMessage = null) where TNum : struct, IComparable<TNum>
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, TNum>>errorMessagestring
Returns
Type Parameters
TTNum
Range<T, TNum>(ValidatorRuleBuilder<T>, Expression<Func<T, TNum>>, TNum, TNum, string?)
Validates that the selected comparable property is between min
and max (inclusive).
Works for int, long, double, decimal, and any IComparable<T>.
public static ValidatorRuleBuilder<T> Range<T, TNum>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, TNum>> selector, TNum min, TNum max, string? errorMessage = null) where TNum : IComparable<TNum>
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, TNum>>minTNummaxTNumerrorMessagestring
Returns
Type Parameters
TTNum
StartsWith<T>(ValidatorRuleBuilder<T>, Expression<Func<T, string?>>, string, StringComparison, string?)
Validates that the selected string property starts with prefix.
The comparison is ordinal and case-sensitive by default.
null fails this rule.
public static ValidatorRuleBuilder<T> StartsWith<T>(this ValidatorRuleBuilder<T> builder, Expression<Func<T, string?>> selector, string prefix, StringComparison comparison = StringComparison.Ordinal, string? errorMessage = null)
Parameters
builderValidatorRuleBuilder<T>selectorExpression<Func<T, string>>prefixstringcomparisonStringComparisonerrorMessagestring
Returns
Type Parameters
T