public static AggregateValidationException CreateFor(Type type, IEnumerable <ValidationResult> results = null)
 {
     return(new AggregateValidationException(
                string.Format(DefaultErrorMessage, type.Name),
                AggregateValidationResult.CreateFor(type, results)
                ));
 }
Esempio 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);
        }
Esempio n. 3
0
        /// <summary>
        /// Determines whether the specified object is valid and returns an list of ValidationResults.
        /// </summary>
        /// <param name="obj">The object.</param>
        /// <param name="validateAllProperties">
        /// If <c>true</c>, evaluates all the properties, otherwise just checks that
        /// ones marked with <see cref="RequiredAttribute"/> are not null.
        /// </param>
        /// <returns></returns>
        /// <exception cref="System.NotImplementedException"></exception>
        public static IEnumerable <ValidationResult> Validate(object obj, bool validateAllProperties)
        {
            var items   = CreateContextItems(validateAllProperties);
            var context = new ValidationContext(obj, serviceProvider: null, items: items);
            var results = new List <ValidationResult>();

            if (!validateAllProperties)
            {
                results.AddRange(ValidateRecursing(obj, context));
            }
            else
            {
                Validator.TryValidateObject(obj, context, results, validateAllProperties);
            }

            return(AggregateValidationResult.Flatten(results));
        }
        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)));
        }
 public AggregateValidationException(string errorMessage, AggregateValidationResult validationResult)
     : base(validationResult, null, null)
 {
     ValidationResults    = validationResult.Results;
     ValidationExceptions = validationResult.Flatten().Select(x => new ValidationException(x.ErrorMessage));
 }