BryanWilhite / SonghayCore

core reusable, opinionated concerns for *all* 🧐 of my C# projects
http://songhayblog.azurewebsites.net/
MIT License
1 stars 0 forks source link

consolidate `ValidationContextExtensions._.cs` into one file #130

Closed BryanWilhite closed 2 years ago

BryanWilhite commented 2 years ago

consolidate :octocat: https://github.com/BryanWilhite/SonghayCore/blob/master/SonghayCore/Extensions/ValidationContextExtensions._.cs

this is now possible because .NET 5 (and beyond) supports all the types

    /// <summary> 
    /// Extensions of <see cref="ValidationContext"/> 
    /// </summary> 
    public static class ValidationContextExtensions 
    { 
        /// <summary> 
        /// Converts the <see cref="ValidationResult"/> into a display text. 
        /// </summary> 
        /// <param name="result">The result.</param> 
        public static string ToDisplayText(this ValidationResult result) 
        { 
            if (result == null) return DISPLAY_ERROR_MESSAGE; 

            return string.Format("Message: {0}; Properties: {1}",
                result.ErrorMessage, string.Join(",", result.MemberNames).Trim(new[] { ',' })); 
        } 

        /// <summary> 
        /// Converts the <see cref="IEnumerable{ValidationResult}"/> into a display text. 
        /// </summary> 
        /// <param name="results">The results.</param> 
        public static string ToDisplayText(this IEnumerable<ValidationResult> results) 
        { 
            if (results == null) return DISPLAY_ERROR_MESSAGE; 
            if (!results.Any()) return DISPLAY_ERROR_MESSAGE; 

            var builder = new StringBuilder(); 
            builder.AppendFormat("Count: {0}", results.Count()); 
            builder.AppendLine(); 

            foreach (var result in results) 
            { 
                builder.Append(result.ToDisplayText()); 
                builder.AppendLine(); 
            } 

            return builder.ToString(); 
        } 

        /// <summary> 
        /// Converts the <see cref="Object"/> into a validation context. 
        /// </summary> 
        /// <param name="objectToValidate">The object to validate.</param> 
        /// <returns></returns> 
        /// <exception cref="NullReferenceException">The expected object to validate is not here.</exception> 
        public static ValidationContext ToValidationContext(this IValidatableObject objectToValidate) 
        { 
            if (objectToValidate == null) throw new NullReferenceException("The expected object to validate is not here."); 

            return new ValidationContext(objectToValidate); 
        } 

        /// <summary> 
        /// Converts the <see cref="Object" /> into a validation results. 
        /// </summary> 
        /// <param name="objectToValidate">The object to validate.</param> 
        /// <remarks> 
        /// This member will validate all properties; <c>validateAllProperties == true</c>. 
        /// </remarks> 
        /// <returns></returns> 
        public static IEnumerable<ValidationResult> ToValidationResults(this IValidatableObject objectToValidate) 
        { 
            return objectToValidate.ToValidationResults(validateAllProperties: true); 
        } 

        /// <summary> 
        /// Converts the <see cref="Object" /> into a validation results. 
        /// </summary> 
        /// <param name="objectToValidate">The object to validate.</param> 
        /// <param name="validateAllProperties"><c>true</c> to validate all properties; if <c>false</c>, only required attributes are validated.</param> 
        /// <returns></returns> 
        public static IEnumerable<ValidationResult> ToValidationResults(this IValidatableObject objectToValidate, bool validateAllProperties) 
        { 

            if (objectToValidate == null) return Enumerable.Empty<ValidationResult>(); 
            var results = new List<ValidationResult>(); 

            Validator.TryValidateObject(objectToValidate, objectToValidate.ToValidationContext(), results, validateAllProperties); 

            return results; 
        } 

        /// <summary> 
        /// Converts the <see cref="Object"/> into a validation results. 
        /// </summary> 
        /// <param name="objectToValidate">The object to validate.</param> 
        /// <param name="propertyName">Name of the property.</param> 
        /// <param name="propertyValue">The property value.</param> 
        /// <returns></returns> 
        /// <exception cref="ArgumentNullException">propertyName</exception> 
        public static IEnumerable<ValidationResult> ToValidationResults(this IValidatableObject objectToValidate, string propertyName, object propertyValue) 
        { 
            if (objectToValidate == null) return Enumerable.Empty<ValidationResult>(); 
            if (string.IsNullOrWhiteSpace(propertyName)) throw new ArgumentNullException(nameof(propertyName)); 

            var results = new List<ValidationResult>(); 
            var context = objectToValidate.ToValidationContext(); 
            context.MemberName = propertyName; 

            Validator.TryValidateProperty(propertyValue, context, results); 

            return results; 
        } 

        internal static string DISPLAY_ERROR_MESSAGE = $"[Unable to display {nameof(ValidationResult)}s]"; 
    }