Beispiel #1
0
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            StringBuilder sb = new StringBuilder();

            if (context.PropertyValue.IsNullOrDefault())
            {
                return(true);
            }

            foreach (var value in context.PropertyValue)
            {
                if (!_forEachPredicate(value))
                {
                    sb.AppendLine(CreateErrorMessage(value));
                }
            }

            if (sb.Length > 0)
            {
                Message = sb.ToString();
                notification.Errors.Add(ValidationResultFactory.Create(this, context, null, null));
                return(false);
            }
            else
            {
                return(true);
            }
        }
        public override bool Validate(RuleValidatorContext <T, TProperty> context, SpecificationContainer specificationContainer, ValidationNotification notification)
        {
            ValidationResult collectionValidationResult = null;

            //Check if the Collection is null/default
            if (context.PropertyValue.IsNullOrDefault())
            {
                return(true);
            }


            var itemsNestedValidationResult = new List <ValidationResult>();

            var propertyEnumerable = ((IEnumerable)(context.PropertyValue));

            if (propertyEnumerable == null)
            {
                throw new ArgumentException("Property must be IEnumerable");
            }

            int index = 1;

            foreach (var item in propertyEnumerable)
            {
                if (item == null)
                {
                    throw new NullReferenceException(string.Format("One of the items is null in the list {0} of the instance {1}", context.PropertyName, context.Instance.GetType().FullName));
                }

                var innerNotfication = new ValidationNotification();

                Type objectType = item.GetType();

                var specification = specificationContainer.GetSpecification(objectType);

                if (!specification.Validate(item, specificationContainer, innerNotfication))
                {
                    var propertyName = String.IsNullOrEmpty(ItemName) ? item.GetType().Name : ItemName;

                    Message = String.Format("{0} {1} in {2} is invalid.", propertyName, index, context.PropertyName);

                    var childContext = new RuleValidatorContext(item, propertyName, item, context.Level,
                                                                item.GetType() as MemberInfo, context);

                    var itemError = ValidationResultFactory.Create(this, childContext, new List <object>(), MessageKey);

                    //var itemError = ValidationResultFactory.Create(this, context, Parameters, MessageKey);
                    itemError.NestedValidationResults = innerNotfication.Errors;
                    itemsNestedValidationResult.Add(itemError);
                }
                index++;
            }

            if (itemsNestedValidationResult.Any())
            {
                //Errors were found on at least one item in the collection to return a ValidationResult for the Collection property
                Message = "{PropertyName} is invalid.";
                collectionValidationResult = ValidationResultFactory.Create(this, context, new List <object>(), MessageKey);
                collectionValidationResult.NestedValidationResults = itemsNestedValidationResult;
                notification.Errors.Add(collectionValidationResult);
                return(false);
            }
            else
            {
                return(true);
            }
        }