Example #1
0
 /// <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, bool> predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
 {
     predicate.Guard("A predicate must be specified when calling When.");
     // 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(predicate.CoerceToNonGeneric(), applyConditionTo);
     }));
 }
Example #2
0
 /// <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, 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.
     return(rule.Configure(config => {
         config.ApplyAsyncCondition((ctx, ct) => predicate((T)ctx.Instance, ct), applyConditionTo);
     }));
 }
Example #3
0
 /// <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 IRuleBuilderOptions <T, TProperty> UnlessAsync <T, TProperty>(this IRuleBuilderOptions <T, TProperty> rule, Func <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, ct) => !await predicate(x, ct), applyConditionTo));
 }
 public void ApplyCondition(Func <PropertyValidatorContext, bool> predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators) => throw new NotImplementedException( );
 public void ApplyAsyncCondition(Func <PropertyValidatorContext, CancellationToken, Task <bool> > predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators) => throw new NotImplementedException( );
 public static IRuleBuilderOptions <T, TProperty> WhenAwait <T, TProperty>(this IRuleBuilderOptions <T, TProperty> rule,
                                                                           Func <T, Task <bool> > predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
 {
     return(rule.When((x) => predicate(x).Result, applyConditionTo));
 }
Example #7
0
 /// <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 IRuleBuilderOptionsConditions <T, TProperty> When <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 When.", 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).ApplyCondition(ctx => predicate((T)ctx.InstanceToValidate, ValidationContext <T> .GetFromNonGenericContext(ctx)), applyConditionTo);
     return(rule);
 }
        /// <summary>
        /// Applies the condition to the rule asynchronously
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="applyConditionTo"></param>
        public void ApplyAsyncCondition(Func <object, CancellationToken, Task <bool> > predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
        {
            // Default behaviour for When/Unless as of v1.3 is to apply the condition to all previous validators in the chain.
            if (applyConditionTo == ApplyConditionTo.AllValidators)
            {
                foreach (var validator in Validators.ToList())
                {
                    var wrappedValidator = new DelegatingValidator(predicate, validator);
                    ReplaceValidator(validator, wrappedValidator);
                }

                foreach (var dependentRule in DependentRules.ToList())
                {
                    dependentRule.ApplyAsyncCondition(predicate, applyConditionTo);
                }
            }
            else
            {
                var wrappedValidator = new DelegatingValidator(predicate, CurrentValidator);
                ReplaceValidator(CurrentValidator, wrappedValidator);
            }
        }
Example #9
0
 public static IRuleBuilderOptions <TIn, TOut> WhenNotValidated <TIn, TOut>(this IRuleBuilderOptions <TIn, TOut> builder,
                                                                            SafeValidator <TIn> validator, Func <TIn, object> objProvider,
                                                                            ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
 {
     return(builder.When((obj) => validator.AddValidated(objProvider(obj)), applyConditionTo));
 }
 public void ApplyCondition(Func <object, bool> predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
 {
     throw new NotImplementedException();
 }
Example #11
0
        public static IRuleBuilderOptions <T, TProperty> WhenAsync <T, TProperty>(this IRuleBuilderOptions <T, TProperty> rule, Func <T, Task <bool> > predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
        {
            predicate.Guard("A predicate must be specified when calling WhenAsync.");

            var newPredicate = new Func <ValidationContext, CancellationToken, Task <bool> >((ctx, ct) => predicate((T)ctx.InstanceToValidate));

            return(rule.Configure(config => {
                config.ApplyAsyncCondition(newPredicate, applyConditionTo);
            }));
        }
        /// <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, bool> predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
        {
            predicate.Guard("A predicate must be specified when calling When.");

            return(rule.Configure(config => {
                // Default behaviour for When/Unless as of v1.3 is to apply the condition to all previous validators in the chain.
                if (applyConditionTo == ApplyConditionTo.AllValidators)
                {
                    foreach (var validator in config.Validators.ToList())
                    {
                        var wrappedValidator = new DelegatingValidator(x => predicate((T)x), validator);
                        config.ReplaceValidator(validator, wrappedValidator);
                    }
                }
                else
                {
                    var wrappedValidator = new DelegatingValidator(x => predicate((T)x), config.CurrentValidator);
                    config.ReplaceValidator(config.CurrentValidator, wrappedValidator);
                }
            }));
        }
        /// <summary>
        /// Applies a condition asynchronously to the validator
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="applyConditionTo"></param>
        public void ApplyAsyncCondition(Func <ValidationContext, CancellationToken, Task <bool> > predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
        {
            // For custom rules within the DelegateValidator, we ignore ApplyConditionTo - this is only relevant to chained rules using RuleFor.
            var originalCondition = this.asyncCondition;

            this.asyncCondition = async(x, ct) => {
                var result = await predicate(x, ct);

                if (!result)
                {
                    return(false);
                }
                if (originalCondition == null)
                {
                    return(true);
                }
                return(await originalCondition(x, ct));
            };
        }
Example #14
0
 public static IRuleBuilderInitial <T, TProperty> When <T, TProperty>(this IRuleBuilderInitial <T, TProperty> rule, Func <T, bool> predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
 {
     return(rule.Configure(config =>
     {
         PropertyRule propertyRule = config;
         int num = (int)applyConditionTo;
         propertyRule.ApplyCondition(ctx => predicate((T)ctx.InstanceToValidate), (ApplyConditionTo)num);
     }));
 }
Example #15
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");
     return(rule.When(x => !predicate(x), applyConditionTo));
 }
Example #16
0
 /// <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 IRuleBuilderOptionsConditions <T, TProperty> When <T, TProperty>(this IRuleBuilderOptionsConditions <T, TProperty> rule, Func <T, bool> predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
 {
     predicate.Guard("A predicate must be specified when calling When.", nameof(predicate));
     return(rule.When((x, ctx) => predicate(x), applyConditionTo));
 }
 public void ApplyCondition(Func<object, bool> predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
 {
 }
Example #18
0
 public static IRuleBuilderOptions <T, TProperty> When <T, TProperty>(this IRuleBuilderOptions <T, TProperty> rule, Func <T, TProperty, bool> predicate, ApplyConditionTo applyConditionTo = ApplyConditionTo.AllValidators)
 {
     return(rule.Configure(config => {
         config.ApplyCondition(ctx => predicate((T)ctx.Instance, (TProperty)ctx.PropertyValue), applyConditionTo);
     }));
 }