Class ValidatorRuleBuilder<T>
Fluent builder for creating validation rule sets for a specific entity type. Provides a fluent API for defining validation rules with method chaining.
public class ValidatorRuleBuilder<T>
Type Parameters
TThe type of entity to validate.
- Inheritance
-
ValidatorRuleBuilder<T>
- Inherited Members
- Extension Methods
Examples
var rules = new ValidatorRuleBuilder<User>()
.Rule(u => u.Email, "Required", email => !string.IsNullOrEmpty(email))
.Rule(u => u.Email, "Format", email => email.Contains("@"))
.Rule(u => u.Age, "MinAge", age => age >= 18)
.Build();
var result = rules.Validate(user);
Methods
AddRule(IValidatorRule<T>)
Adds a validation rule to the builder.
public ValidatorRuleBuilder<T> AddRule(IValidatorRule<T> rule)
Parameters
ruleIValidatorRule<T>The validation rule to add.
Returns
- ValidatorRuleBuilder<T>
The builder instance for method chaining.
Examples
var builder = new ValidatorRuleBuilder<User>();
var rule = new EmailValidatorRule();
builder.AddRule(rule);
Build()
Builds an immutable validation rule set from the added rules.
public ValidatorRuleSet<T> Build()
Returns
- ValidatorRuleSet<T>
An immutable ValidatorRuleSet containing all added rules.
Examples
var builder = new ValidatorRuleBuilder<User>()
.Rule(u => u.Email, "Required", "Email required", email => !string.IsNullOrEmpty(email));
var ruleSet = builder.Build(); // Returns ValidatorRuleSet<User>
RuleAsync<TProperty>(Func<T, TProperty>, string, string, Func<TProperty, Task<bool>>)
Adds a custom async validation rule defined by an async predicate function.
public ValidatorRuleBuilder<T> RuleAsync<TProperty>(Func<T, TProperty> propertySelector, string ruleName, string errorMessage, Func<TProperty, Task<bool>> validator)
Parameters
propertySelectorFunc<T, TProperty>Function to select the property to validate.
ruleNamestringThe name of the validation rule.
errorMessagestringThe error message if validation fails.
validatorFunc<TProperty, Task<bool>>The async validation predicate function.
Returns
- ValidatorRuleBuilder<T>
The builder instance for method chaining.
Type Parameters
TPropertyThe type of the property being validated.
Examples
var builder = new ValidatorRuleBuilder<User>()
.RuleAsync(u => u.Email, "UniqueEmail", "Email already exists", async email => await EmailService.IsUniqueAsync(email))
.Rule(u => u.Age, "MinAge", "Must be 18+", age => age >= 18);
Rule<TProperty>(Func<T, TProperty>, string, string, Func<TProperty, bool>)
Adds a custom validation rule defined by a predicate function.
public ValidatorRuleBuilder<T> Rule<TProperty>(Func<T, TProperty> propertySelector, string ruleName, string errorMessage, Func<TProperty, bool> validator)
Parameters
propertySelectorFunc<T, TProperty>Function to select the property to validate.
ruleNamestringThe name of the validation rule.
errorMessagestringThe error message if validation fails.
validatorFunc<TProperty, bool>The validation predicate function.
Returns
- ValidatorRuleBuilder<T>
The builder instance for method chaining.
Type Parameters
TPropertyThe type of the property being validated.
Examples
var builder = new ValidatorRuleBuilder<User>()
.Rule(u => u.Email, "Required", "Email is required", email => !string.IsNullOrEmpty(email))
.Rule(u => u.Age, "MinAge", "Must be 18+", age => age >= 18);