Esempio n. 1
0
 public RuleValidator(T parameterValue, CompiledRule <T> rule) : this(parameterValue, new List <CompiledRule <T> > {
     rule
 })
 {
     if (rule == null)
     {
         throw new ArgumentNullException("rule");
     }
 }
Esempio n. 2
0
        private ValidationResult _InvokeRule(CompiledRule <T> rule, ValidationResult result)
        {
            var isPassed = rule.Rule.Invoke(_parameterValue);

            result.Passed = isPassed;
            result.ValidationMessageSummary = isPassed ? string.Empty : rule.ValidationMessage;
            result.ValidationDetails.Add(new ValidationDetail
            {
                ValidationMessage = rule.ValidationMessage,
                Field             = rule.Field,
                Passed            = isPassed
            });
            return(result);
        }
Esempio n. 3
0
        public CompiledRule <T> CompileRules <T>(IRule rule)
        {
            if (rule == null)
            {
                throw new ArgumentNullException("rule");
            }

            var          param      = Expression.Parameter(typeof(T));
            IRuleBuilder builder    = new ConditionalRuleBuilder <T>(rule, param);
            var          expression = builder.Build();

            if (expression == null)
            {
                throw new Exception("Cannot construct rule.");
            }

            var func = ((Expression <Func <T, bool> >)expression).Compile();

            var compiledRule = new CompiledRule <T> {
                Rule = func, Field = rule.LeftNode.ToString(), ValidationMessage = rule.ValidationMessage
            };

            return(compiledRule);
        }
Esempio n. 4
0
 public ValidationResult Validate <T>(T value, CompiledRule <T> rule)
 {
     return(Validate(value, new List <CompiledRule <T> > {
         rule
     }));
 }