private ValidationResult DoValidation(string prop = null) { if (isValidate == false) { isValidate = true; ConfigDataAnnotation(); } ValidationContext <T> context = null; IEnumerable <ValidationFailure> failures = null; if (string.IsNullOrEmpty(prop)) { context = new ValidationContext <T>(this as T, new PropertyChain(), ValidatorOptions.ValidatorSelectors.DefaultValidatorSelectorFactory()); failures = NestedValidators.SelectMany(x => x.Validate(context)); ValidationResult = new ValidationResult(failures); foreach (var item in modelProperties) { OnErrorChanged(item.Name); } return(ValidationResult); } else { context = new ValidationContext <T>(this as T, new PropertyChain(), ValidatorOptions.ValidatorSelectors.MemberNameValidatorSelectorFactory(new string[] { prop })); failures = NestedValidators.SelectMany(x => x.Validate(context)); ValidationResult = new ValidationResult(failures); OnErrorChanged(prop); return(ValidationResult); } }
public void Include(IValidator <T> rulesToInclude) { if (rulesToInclude == null) { throw new ArgumentNullException("Cannot pass null to Include"); } var rule = IncludeRule.Create <T>(rulesToInclude, () => CascadeMode); NestedValidators.Add(rule); }
public void RemoveRule <TProperty>(Expression <Func <T, TProperty> > expression) { var propList = NestedValidators.Where(x => (x as PropertyRule).Expression.GetMember() == expression.GetMember()).ToList(); if (propList != null) { for (int i = propList.Count() - 1; i >= 0; i--) { NestedValidators.Remove(propList[i]); } } }
public void RemoveRule(string prop) { var propList = NestedValidators.Where(x => (x as PropertyRule).PropertyName == prop).ToList(); if (propList != null) { for (int i = propList.Count() - 1; i >= 0; i--) { NestedValidators.Remove(propList[i]); } } }
public void When(Func <T, bool> predicate, Action action) { var propertyRules = new List <IValidationRule>(); Action <IValidationRule> onRuleAdded = propertyRules.Add; using (NestedValidators.OnItemAdded(onRuleAdded)) { action(); } propertyRules.ForEach(x => x.ApplyCondition(ctx => predicate((T)ctx.InstanceToValidate))); }
public IRuleBuilderInitial <T, TProperty> AddRule <TProperty>(Expression <Func <T, TProperty> > expression) { if (expression == null) { throw new ArgumentNullException("Cannot pass null to AddRule"); } var rule = PropertyRule.Create(expression, () => CascadeMode); NestedValidators.Add(rule); var ruleBuilder = new RuleBuilder <T, TProperty>(rule, this); return(ruleBuilder); }
public void RuleSet(string ruleSetName, Action action) { if (string.IsNullOrEmpty(ruleSetName)) { throw new ArgumentNullException("A name must be specified when calling RuleSet."); } if (action == null) { throw new ArgumentNullException("A ruleset definition must be specified when calling RuleSet."); } using (NestedValidators.OnItemAdded(r => r.RuleSet = ruleSetName)) { action(); } }
public void ConfigDataAnnotation() { if (isConfigDataAnnotation == false) { isConfigDataAnnotation = true; if (dataAnnotationValidatiors == null) { dataAnnotationValidatiors = new TrackingCollection <IValidationRule>(); AddDataAnnotationValidation(); } foreach (var item in dataAnnotationValidatiors) { NestedValidators.Add(item); } } }