/// <summary>
 /// Specifies a 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 IRuleBuilderOptions <T, TProperty> When <T, TProperty>(this IRuleBuilderOptions <T, TProperty> rule, Func <T, ValidationContext <T>, bool> predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
 {
     predicate.Guard("A predicate must be specified when calling When.", nameof(predicate));
     // Default behaviour for When/Unless as of v1.3 is to apply the condition to all previous validators in the chain.
     return(rule.Configure(config => {
         config.ApplyCondition(ctx => predicate((T)ctx.InstanceToValidate, ValidationContext <T> .GetFromNonGenericContext(ctx.ParentContext)), applyConditionTo);
     }));
 }
Example #2
0
        /// <summary>
        /// Uses the Service Provider to inject the default validator for the property type.
        /// </summary>
        /// <param name="ruleBuilder"></param>
        /// <param name="callback"></param>
        /// <param name="ruleSets"></param>
        /// <typeparam name="T"></typeparam>
        /// <typeparam name="TProperty"></typeparam>
        /// <returns></returns>
        public static IRuleBuilderOptions <T, TProperty> InjectValidator <T, TProperty>(this IRuleBuilder <T, TProperty> ruleBuilder, Func <IServiceProvider, ValidationContext <T>, IValidator <TProperty> > callback, params string[] ruleSets)
        {
            var adaptor = new ChildValidatorAdaptor(context => {
                var actualContext   = (PropertyValidatorContext)context;
                var serviceProvider = actualContext.ParentContext.GetServiceProvider();
                var contextToUse    = ValidationContext <T> .GetFromNonGenericContext(actualContext.ParentContext);
                var validator       = callback(serviceProvider, contextToUse);
                return(validator);
            }, typeof(IValidator <TProperty>));

            adaptor.RuleSets = ruleSets;

            return(ruleBuilder.SetValidator(adaptor));
        }
 /// <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 IRuleBuilderOptions <T, TProperty> WhenAsync <T, TProperty>(this IRuleBuilderOptions <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);
 }