public IFluentRuleBuilder <T, TProperty> RuleFor <TProperty>(Expression <Func <T, TProperty> > expression)
        {
            ParamHelper.CheckParamNull(expression, "expression", "Can't be null");
            var builder = Validation.Provider.GetService <IRuleBuilder <T, TProperty> >();

            builder.SetValueGetter(expression);
            Builders.Add(builder as IValidateRuleBuilder);
            return(builder);
        }
Exemple #2
0
        public static IRuleMessageBuilder <T, TProperty> When <T, TProperty>(this IRuleMessageBuilder <T, TProperty> builder, Func <TProperty, bool> func)
        {
            ParamHelper.CheckParamNull(func, "func", "Can't be null");
            var ruleBuilder = builder as IRuleBuilder <T, TProperty>;

            ruleBuilder.Condition = (context) =>
            {
                var value = ruleBuilder.ValueGetter(context.ValidateObject);
                return(func(value));
            };
            return(builder);
        }
        public virtual IRuleMessageBuilder <T, TProperty> SetValidate(IFluentRuleBuilder <T, TProperty> builder)
        {
            ParamHelper.CheckParamNull(builder, "builder", "Can't be null");
            var build = builder as IRuleBuilder <T, TProperty>;

            build.ValidateAsyncFunc = async(context, name, error) =>
            {
                var value  = build.ValueGetter(context.ValidateObject);
                var result = Validation.Provider.GetService <IValidateResult>();
                return(await ValidateAsync(result, value, name, error));
            };
            return(build as IRuleMessageBuilder <T, TProperty>);
        }
Exemple #4
0
        public override IRuleMessageBuilder <T, IEnumerable <TProperty> > SetValidate(IFluentRuleBuilder <T, IEnumerable <TProperty> > builder)
        {
            ParamHelper.CheckParamNull(builder, "builder", "Can't be null");
            var build = builder as IRuleValueGetterBuilder <T, IEnumerable <TProperty> >;

            build.ValidateAsyncFunc = async(context, name, error) =>
            {
                ValidateRule.ValueName = name;
                var value  = build.ValueGetter(context.ValidateObject);
                var result = Validation.Provider.GetService <IValidateResult>();
                var ct     = Validation.CreateContext(value, context.Option, context.RuleSetList.ToArray());
                return(await ValidateRule.ValidateAsync(ct));
            };
            return(build as IRuleMessageBuilder <T, IEnumerable <TProperty> >);
        }
Exemple #5
0
        public override Task <IValidateResult> ValidateAsync(ValidateContext context)
        {
            ParamHelper.CheckParamNull(context, "context", "Can't be null");
            IValidateResult result = Validation.Provider.GetService <IValidateResult>();
            var             list   = context.ValidateObject as IEnumerable;

            if (Condition == null || Condition(context))
            {
                if (list != null)
                {
                    ValidateElementList(context, result, list);
                }
            }
            return(Task.FromResult(result));
        }
Exemple #6
0
        public void Test_CheckParamNull()
        {
            ParamHelper.CheckParamNull("", "test3", "info3");
            ParamHelper.CheckParamNull(string.Empty, "test3", "info3");
            ParamHelper.CheckParamNull(1, "test3", "info3");
            ParamHelper.CheckParamNull(1d, "test3", "info3");
            ParamHelper.CheckParamNull(1m, "test3", "info3");
            ParamHelper.CheckParamNull(new ParamHelper(), "test3", "info3");

            var ex = Assert.Throws <ArgumentNullException>(() => ParamHelper.CheckParamNull(null, "test", "info"));

            Assert.NotNull(ex);
            Assert.Equal("test", ex.ParamName);
            Assert.True(ex.Message.Contains("info"));
            Assert.True(ex.Message.Contains("test"));
        }
Exemple #7
0
        public async virtual Task <IValidateResult> ValidateAsync(ValidateContext context)
        {
            ParamHelper.CheckParamNull(ValidateAsyncFunc, nameof(ValidateAsyncFunc), "Can't be null");
            ParamHelper.CheckParamNull(context, nameof(context), "Can't be null");
            IValidateResult result = null;

            if (Condition == null || Condition(context))
            {
                result = await ValidateAsyncByFunc(context);
            }
            else
            {
                result = Validation.Provider.GetService <IValidateResult>();
            }
            return(result);
        }
        public void RuleSet(string ruleSet, Action <IValidatorBuilder <T> > action)
        {
            ParamHelper.CheckParamEmptyOrNull(ruleSet, "ruleSet", "Can't be null");
            ParamHelper.CheckParamNull(action, "action", "Can't be null");

            var upRuleSet     = ruleSet.ToUpper();
            var updateRuleSet = new NotifyCollectionChangedEventHandler <IValidateRuleBuilder>((o, e) =>
            {
                if (e.Action != NotifyCollectionChangedAction.Add)
                {
                    return;
                }
                foreach (var item in e.NewItems)
                {
                    item.RuleSet = upRuleSet;
                }
            });

            Builders.CollectionChanged += updateRuleSet;
            action(this);
            Builders.CollectionChanged -= updateRuleSet;
        }
Exemple #9
0
        public Task <IValidateResult> ValidateAsync(ValidateContext context)
        {
            ParamHelper.CheckParamNull(context, "context", "Can't be null");
            var list = context.RuleSetList;

            if (!list.IsEmptyOrNull())
            {
                context.RuleSetList = list.Where(i => !string.IsNullOrEmpty(i)).Select(i => i.ToUpper()).ToArray();
            }
            var rules  = m_Rules.Where(i => context.RuleSelector.CanExecute(i, context)).ToArray();
            var result = Validation.Provider.GetService <IValidateResult>();

            if (!rules.IsEmptyOrNull())
            {
                var tasks    = rules.Select(async i => await i.ValidateAsync(context)).ToArray();
                var failures = tasks.Where(i => i.IsCompleted)
                               .SelectMany(i => i.Result.Failures);
                result.Merge(failures);
            }

            return(Task.FromResult(result));
        }
 public InListChecker(IEnumerable <TProperty> value, Validation validation)
     : base(default(TProperty), validation)
 {
     ParamHelper.CheckParamNull(value, "value", "Can't be null");
     m_Value = value;
 }
Exemple #11
0
 public CustomChecker(Func <TProperty, IEnumerable <ValidateFailure> > func, Validation validation) : base(validation)
 {
     ParamHelper.CheckParamNull(func, nameof(func), "Can't be null");
     m_Func = func;
 }
Exemple #12
0
 public void SetRules(IEnumerable <IValidateRule> rules)
 {
     ParamHelper.CheckParamNull(rules, "rules", "Can't be null");
     m_Rules.AddRange(rules);
 }
Exemple #13
0
 public MustChecker(Func <TProperty, bool> func, Validation validation) : base(validation)
 {
     ParamHelper.CheckParamNull(func, nameof(func), "Can't be null");
     m_MustBeTrue = func;
 }