Esempio n. 1
0
        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);
            }
        }
Esempio n. 2
0
        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);
        }
Esempio n. 3
0
        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]);
                }
            }
        }
Esempio n. 4
0
        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]);
                }
            }
        }
Esempio n. 5
0
        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)));
        }
Esempio n. 6
0
        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);
        }
Esempio n. 7
0
        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();
            }
        }
Esempio n. 8
0
        public void ConfigDataAnnotation()
        {
            if (isConfigDataAnnotation == false)
            {
                isConfigDataAnnotation = true;
                if (dataAnnotationValidatiors == null)
                {
                    dataAnnotationValidatiors = new TrackingCollection <IValidationRule>();
                    AddDataAnnotationValidation();
                }

                foreach (var item in dataAnnotationValidatiors)
                {
                    NestedValidators.Add(item);
                }
            }
        }