/// <summary> /// Specifies an asynchronous condition limiting when the validator should run. /// The validator will only be executed if the result of the lambda returns true. /// </summary> /// <param name="rule">The current rule</param> /// <param name="predicate">A lambda expression that specifies a condition for when the validator should 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 IRuleBuilderOptionsConditions <T, TProperty> WhenAsync <T, TProperty>(this IRuleBuilderOptionsConditions <T, TProperty> rule, Func <T, ValidationContext <T>, CancellationToken, Task <bool> > predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators) { predicate.Guard("A predicate must be specified when calling WhenAsync.", nameof(predicate)); // Default behaviour for When/Unless as of v1.3 is to apply the condition to all previous validators in the chain. Configurable(rule).ApplyAsyncCondition((ctx, ct) => predicate((T)ctx.InstanceToValidate, ValidationContext <T> .GetFromNonGenericContext(ctx), ct), applyConditionTo); return(rule); }
/// <summary> /// Specifies an asynchronous 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 IRuleBuilderOptionsConditions <T, TProperty> UnlessAsync <T, TProperty>(this IRuleBuilderOptionsConditions <T, TProperty> rule, Func <T, ValidationContext <T>, CancellationToken, Task <bool> > predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators) { predicate.Guard("A predicate must be specified when calling UnlessAsync", nameof(predicate)); return(rule.WhenAsync(async(x, ctx, ct) => !await predicate(x, ctx, ct), applyConditionTo)); }
/// <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 IRuleBuilderOptionsConditions <T, TProperty> Unless <T, TProperty>(this IRuleBuilderOptionsConditions <T, TProperty> rule, Func <T, ValidationContext <T>, bool> predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators) { predicate.Guard("A predicate must be specified when calling Unless", nameof(predicate)); return(rule.When((x, ctx) => !predicate(x, ctx), applyConditionTo)); }