public static AggregateValidationException CreateFor(Type type, IEnumerable <ValidationResult> results = null)
 {
     return(new AggregateValidationException(
                string.Format(DefaultErrorMessage, type.Name),
                AggregateValidationResult.CreateFor(type, results)
                ));
 }
Exemplo n.º 2
0
        protected override ValidationResult IsValid(object value, ValidationContext context)
        {
            var results = ExtendedValidator.ValidateRecursing(value, context);

            if (results.Count() != 0)
            {
                //_Log.DebugFormat("Validation failed for: {0} | {1} | {2}", validationContext.ObjectType, validationContext.DisplayName, validationContext.MemberName);

                return(AggregateValidationResult.CreateFor(context, results));
            }

            return(ValidationResult.Success);
        }
        protected override ValidationResult IsValid(object value, ValidationContext context)
        {
            if (value == null)
            {
                return(ValidationResult.Success);
            }

            if (!(value is IEnumerable))
            {
                throw new ValidationException("Property {0} is not enumerable.".Format(context.MemberName as object));
            }

            var property = MetadataType.GetRuntimeProperties().SingleOrDefault(x => x.Name == Property);

            if (property == null)
            {
                throw new ValidationException("Metadata Object of type {0} is missing property: {1}".Format(MetadataType.FullName as object, Property));
            }

            var attributes = property.GetCustomAttributes <ValidationAttribute>(true);
            var results    = new List <ValidationResult>();

            if (attributes.Any())
            {
                var idx = 0;
                var valueAsEnumerable = value as IEnumerable;
                foreach (var item in valueAsEnumerable)
                {
                    var idx2   = idx++;
                    var newctx = new ValidationContext(item, null, context.Items)
                    {
                        DisplayName = context.DisplayName.IfNotNull(x => string.Format("{0}[{1}]", x, idx2)),
                        MemberName  = context.MemberName.IfNotNull(x => string.Format("{0}[{1}]", x, idx2))
                    };
                    results.AddRange(ValidateClass(item, newctx, attributes));
                    //results.AddRange(ValidateProperties(item, newctx));
                }
            }

            return(results.Count == 0 ?
                   ValidationResult.Success :
                   AggregateValidationResult.CreateFor(
                       FormatErrorMessage(context.DisplayName.IsNullOrEmpty() ? context.MemberName : context.DisplayName), results));
        }
        private static AggregateValidationResult AggregateResultsFrom(string message, IEnumerable <ValidationException> exceptions)
        {
            Guard.IsNotNull(exceptions, "exceptions");

            return(AggregateValidationResult.CreateFor(message, exceptions.Select(x => x.ValidationResult)));
        }