Ejemplo n.º 1
0
        /// <summary>
        /// Takes a validation result and applies the rule results relevant to the <see cref="Validatable{T}"/>object.
        /// </summary>
        /// <param name="valResult">The original Fluent <see cref="ValidationResult"/> from a class instance that contains the value of the individual <see cref="Validatable{T}"/>object property.</param>
        /// <param name="validatableObj">The <see cref="Validatable{T}"/>object property to apply results to.</param>
        /// <returns>An <see cref="OverallValidationResult"/> summarizing the original results (on the entire instance) as well as any rule results that were not captured by the <see cref="Validatable{T}"/>object.</returns>
        public static OverallValidationResult ApplyResultsTo(this ValidationResult valResult, IValidity validatableObj)
        {
            var overallResult = new OverallValidationResult
            {
                IsValidOverall = valResult.IsValid,
                AllErrors      = valResult.Errors.Select(e => e.ErrorMessage).ToList()
            };

            overallResult.NonSplitErrors = overallResult.AllErrors;

            validatableObj.IsValid = true;

            var relevantFailures = valResult.Errors
                                   .Where(e => e.PropertyName == validatableObj.ClassPropertyName).ToList();

            if (!relevantFailures.Any())
            {
                return(overallResult);
            }

            var errors = relevantFailures.Select(e => e.ErrorMessage).ToList();

            validatableObj.Errors  = errors;
            validatableObj.IsValid = false;

            relevantFailures.ForEach(failure => valResult.Errors.Remove(failure));
            overallResult.NonSplitErrors           = valResult.Errors.Select(e => e.ErrorMessage).ToList();
            overallResult.IsValidForNonSplitErrors = overallResult.NonSplitErrors.Count == 0;

            return(overallResult);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Takes a validation result and splits it into individual results for each <see cref="Validatable{T}"/> object.
        /// </summary>
        /// <param name="valResult">The original Fluent <see cref="ValidationResult"/> from a class instance comprised of the values of the individual <see cref="Validatable{T}"/> object properties.</param>
        /// <param name="validatableGroup">The <see cref="Validatable{T}"/> object properties to apply results to.</param>
        /// <returns>An <see cref="OverallValidationResult"/> summarizing the original results (on the entire instance) as well as any rule results that were not captured by the <see cref="Validatable{T}"/> objects.</returns>
        public static OverallValidationResult ApplyResultsTo(this ValidationResult valResult, Validatables validatableGroup)
        {
            var overallResult = new OverallValidationResult
            {
                IsValidOverall = valResult.IsValid,
                AllErrors      = valResult.Errors.Select(e => e.ErrorMessage).ToList()
            };

            validatableGroup.AreValid = true;

            foreach (var obj in validatableGroup.Objects)
            {
                obj.IsValid = true;

                var relevantFailures = valResult.Errors
                                       .Where(e => e.PropertyName == obj.ClassPropertyName).ToList();

                if (!relevantFailures.Any())
                {
                    continue;
                }

                var errors = relevantFailures.Select(e => e.ErrorMessage).ToList();
                obj.Errors  = errors;
                obj.IsValid = false;

                validatableGroup.Errors.AddRange(errors);

                relevantFailures.ForEach(failure => valResult.Errors.Remove(failure));
            }

            if (validatableGroup.Errors.Any())
            {
                validatableGroup.AreValid = false;
            }

            overallResult.NonSplitErrors           = valResult.Errors.Select(e => e.ErrorMessage).ToList();
            overallResult.IsValidForNonSplitErrors = overallResult.NonSplitErrors.Count == 0;

            return(overallResult);
        }