The Conditional Rule is very generic allows you to customize the condition to validate the value using lambda expressions.
Create an instance
To create an instance of the Conditional rule, either you use the default constructor or use the static method WithCondition. Example below, to set a conditional rule for integer value that must be greater than 0.
1
2
3
4
5
// Default rule constructor
new ConditionalRule<int>(value=> value> 0);
// With static method
ConditionalRule<int>.WithCondition(value => value > 0);
Set the validation error message
You can customize the validation error message to show to the end-user if the validation fails by calling the method WithValidationError(string validationError).
1
myConditionalRule = myConditionalRule.WithValidationError("My custom validation error message");
Example
1
2
ConditionalRule<int>.WithCondition(value => value > 18)
.WithValidationError("Age must be greater than 18 years old.");