/// <summary>
 /// Specifies a condition limiting when the validator should run.
 /// The validator will only be executed if the <see cref="Option{T}"/> does not contain a value.
 /// </summary>
 /// <param name="rule">The current rule</param>
 /// <param name="expression">The expression representing the <see cref="Option{T}"/> to evaluate against</param>
 /// <param name="applyConditionTo">Whether the condition should be applied to the current rule or all rules in the chain</param>
 /// <typeparam name="T">The type of object being validated</typeparam>
 /// <typeparam name="TElement">The type of the property being validated</typeparam>
 /// <typeparam name="TProperty">The type of property contained in the <see cref="Option{T}"/></typeparam>
 /// <returns></returns>
 public static IRuleBuilderOptions <T, TElement> UnlessPresent <T, TElement, TProperty>(
     this IRuleBuilderOptions <T, TElement> rule,
     Expression <Func <T, Option <TProperty> > > expression,
     ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
 {
     return(rule.Unless(arg => expression.Compile()(arg).HasValue, applyConditionTo));
 }
Beispiel #2
0
 /// <summary>
 /// Specifies a condition limiting when the validator should not run.
 /// The validator will only be executed if the result of the lambda returns false.
 /// </summary>
 /// <param name="rule">The current rule</param>
 /// <param name="predicate">A lambda expression that specifies a condition for when the validator should not run</param>
 /// <param name="applyConditionTo">Whether the condition should be applied to the current rule or all rules in the chain</param>
 /// <returns></returns>
 public static IRuleBuilderOptions <T, TProperty> Unless <T, TProperty>(this IRuleBuilderOptions <T, TProperty> rule, Func <T, bool> predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
 {
     predicate.Guard("A predicate must be specified when calling Unless", nameof(predicate));
     return(rule.Unless((x, ctx) => predicate(x), applyConditionTo));
 }