コード例 #1
0
 public bool CanExecute(IValidateRule rule, ValidateContext context)
 {
     return string.IsNullOrEmpty(rule.RuleSet)
         || context.RuleSetList.IsEmptyOrNull()
             ? true
             : context.RuleSetList.Contains(rule.RuleSet);
 }
コード例 #2
0
        public async Task<IValidateResult> ValidateAsyncByFunc(ValidateContext context)
        {
            IValidateResult result = await ValidateAsyncFunc(context, ValueName, Error);
            if (NextRuleList.IsEmptyOrNull() || (!result.IsValid && context.Option != ValidateOption.Continue)) return result;

            ValidateNextRuleList(context, result);

            return result;
        }
コード例 #3
0
 private async void ValidateNextRule(ValidateContext context, IValidateResult result, int index, IValidateRule rule)
 {
     var nextResult = await rule.ValidateAsync(context);
     if (!nextResult.IsValid)
     {
         foreach (var failure in nextResult.Failures)
         {
             failure.Name = string.Format(string.IsNullOrEmpty(failure.Name) ? "{0}[{1}]{2}" : "{0}[{1}].{2}", ValueName, index, failure.Name);
         }
     }
     result.Merge(nextResult.Failures);
 }
コード例 #4
0
 private void ValidateElement(ValidateContext context, IValidateResult result, int index)
 {
     foreach (var rule in NextRuleList)
     {
         if (result.IsValid || context.Option == ValidateOption.Continue)
         {
             ValidateNextRule(context, result, index, rule);
         }
         else
             break;
     }
 }
コード例 #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);
 }
コード例 #6
0
 private async void ValidateNextRuleList(ValidateContext context, IValidateResult result)
 {
     foreach (var nextRule in NextRuleList)
     {
         if (result.IsValid || context.Option == ValidateOption.Continue)
         {
             var nextResult = await nextRule.ValidateAsync(context);
             result.Merge(nextResult.Failures);
         }
         else
             break;
     }
 }
コード例 #7
0
        public void Test_RuleSelector()
        {
            var rule = new TestValidateRule();
            var context = new ValidateContext();
            var selector = new RuleSelector();
            m_Data.ForEach(i =>
            {
                rule.RuleSet = i.Item1;
                context.RuleSetList = i.Item2;

                Assert.Equal(i.Item3, selector.CanExecute(rule, context));
            });
        }
コード例 #8
0
 private void ValidateElementList(ValidateContext context, IValidateResult result, IEnumerable list)
 {
     var index = 0;
     foreach (var item in list)
     {
         if (result.IsValid || context.Option == ValidateOption.Continue)
         {
             var ct = Validation.CreateContext(item, context.Option, context.RuleSetList.ToArray());
             ValidateElement(ct, result, index);
         }
         else
             break;
         index++;
     }
 }
コード例 #9
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;
 }
コード例 #10
0
        public async void Test_Validator_Exception()
        {
            var v = new Validator(_Validation);
            var rule = new ValidateRule(_Validation)
            {
                ValidateAsyncFunc = (c, name, error) =>
                {
                    throw new Exception();
                }
            };
            v.SetRules(new List<ValidateRule>() { rule });

            var context = new ValidateContext() { RuleSelector = new RuleSelector() };
            await Assert.ThrowsAsync<AggregateException>(async () => { var a = await v.ValidateAsync(context); });
        }
コード例 #11
0
        public async void Test_Validator_Validate()
        {
            var v = new Validator(_Validation);
            var rule = new ValidateRule(_Validation)
            {
                ValidateAsyncFunc = (c, name, error) =>
                {
                    var f = new ValidateFailure()
                    {
                        Name = name,
                        Error = error,
                        Value = c
                    };
                    return Task.FromResult<IValidateResult>(new ValidateResult(new List<ValidateFailure>() { f }));
                }
            };

            var context = new ValidateContext() { RuleSelector = new RuleSelector() };
            var result = await v.ValidateAsync(context);
            Assert.NotNull(result);
            Assert.True(result.IsValid);
            Assert.NotNull(result.Failures);
            Assert.Equal(0, result.Failures.Count);

            v.SetRules(new List<ValidateRule>() { rule });
            result = await v.ValidateAsync(context);
            Assert.NotNull(result);
            Assert.False(result.IsValid);
            Assert.NotNull(result.Failures);
            Assert.Equal(1, result.Failures.Count);

            rule.ValidateAsyncFunc = (c, name, error) =>
            {
                return Task.FromResult<IValidateResult>(new ValidateResult());
            };

            result = await v.ValidateAsync(context);
            Assert.NotNull(result);
            Assert.True(result.IsValid);
            Assert.NotNull(result.Failures);
            Assert.Equal(0, result.Failures.Count);
        }
コード例 #12
0
ファイル: Validator.cs プロジェクト: fs7744/ObjectValidator
        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);
        }
コード例 #13
0
 public Task<IValidateResult> ValidateAsync(ValidateContext context)
 {
     throw new NotImplementedException();
 }
コード例 #14
0
 public IValidateResult Validate(ValidateContext context)
 {
     throw new NotImplementedException();
 }