public RuleValidator(T parameterValue, CompiledRule <T> rule) : this(parameterValue, new List <CompiledRule <T> > { rule }) { if (rule == null) { throw new ArgumentNullException("rule"); } }
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); }
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); }
public ValidationResult Validate <T>(T value, CompiledRule <T> rule) { return(Validate(value, new List <CompiledRule <T> > { rule })); }