< Summary - SonghayCore

Information
Class: Songhay.Extensions.ValidationContextExtensions
Assembly: SonghayCore
File(s): /home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/ValidationContextExtensions.cs
Line coverage
73%
Covered lines: 30
Uncovered lines: 11
Coverable lines: 41
Total lines: 137
Line coverage: 73.1%
Branch coverage
43%
Covered branches: 7
Total branches: 16
Branch coverage: 43.7%
Method coverage

Method coverage is only available for sponsors.

Upgrade to PRO version

Metrics

MethodBranch coverage Cyclomatic complexity Line coverage
ToDisplayText(...)50%2100%
ToDisplayText(...)66.66%6100%
ToValidationContext(...)100%1100%
ToValidationResults(...)100%10%
ToValidationResults(...)100%1100%
ToValidationResults(...)50%4100%
ToValidationResults(...)100%10%
ToValidationResults(...)0%40%

File(s)

/home/rasx/sourceRoot/SonghayCore/SonghayCore/Extensions/ValidationContextExtensions.cs

#LineLine coverage
 1using System.ComponentModel.DataAnnotations;
 2
 3namespace Songhay.Extensions;
 4
 5/// <summary>
 6/// Extensions of <see cref="ValidationResult" /> and <see cref="IValidatableObject" />,
 7/// returning a <see cref="ValidationContext"/>.
 8/// </summary>
 9/// <remarks>
 10/// The use of these methods should be the last resort
 11/// after deferring to a NuGet package like FluentValidation.
 12/// </remarks>
 13public static class ValidationContextExtensions
 14{
 15    /// <summary>
 16    /// Converts the <see cref="ValidationResult"/> into a display text.
 17    /// </summary>
 18    /// <param name="result">The result.</param>
 119    public static string ToDisplayText(this ValidationResult? result) => result == null
 120        ? DisplayErrorMessage
 121        : $"Message: {result.ErrorMessage};Properties: {string.Join(",", result.MemberNames).Trim(new[] {','})}";
 22
 23    /// <summary>
 24    /// Converts the <see cref="IEnumerable{ValidationResult}"/> into a display text.
 25    /// </summary>
 26    /// <param name="results">The results.</param>
 27    public static string ToDisplayText(this IEnumerable<ValidationResult>? results)
 128    {
 129        if (results == null) return DisplayErrorMessage;
 30
 131        results = results.ToArray();
 32
 133        var resultsCount = results.Count();
 34
 135        if (resultsCount == 0) return DisplayErrorMessage;
 36
 137        var builder = new StringBuilder();
 138        builder.Append($"Count: {resultsCount}");
 139        builder.AppendLine();
 40
 541        foreach (var result in results)
 142        {
 143            builder.Append(result.ToDisplayText());
 144            builder.AppendLine();
 145        }
 46
 147        return builder.ToString();
 148    }
 49
 50    /// <summary>
 51    /// Converts the <see cref="Object"/> into a validation context.
 52    /// </summary>
 53    /// <param name="objectToValidate">The object to validate.</param>
 54    public static ValidationContext ToValidationContext(this IValidatableObject objectToValidate)
 155    {
 156        ArgumentNullException.ThrowIfNull(objectToValidate);
 57
 158        return new ValidationContext(objectToValidate);
 159    }
 60
 61    /// <summary>
 62    /// Converts the <see cref="Object" /> into a validation results.
 63    /// </summary>
 64    /// <param name="objectToValidate">The object to validate.</param>
 65    /// <remarks>
 66    /// This member will validate all properties;<c>validateAllProperties == true</c>.
 67    /// </remarks>
 68    public static IEnumerable<ValidationResult> ToValidationResults(this IValidatableObject? objectToValidate) =>
 069        objectToValidate.ToValidationResults(validateAllProperties: true, validationContext: null);
 70
 71    /// <summary>
 72    /// Converts the <see cref="Object" /> into a validation results.
 73    /// </summary>
 74    /// <param name="objectToValidate">The object to validate.</param>
 75    /// <param name="validationContext">the <see cref="ValidationContext"/></param>
 76    /// <remarks>
 77    /// This member will validate all properties;<c>validateAllProperties == true</c>.
 78    /// </remarks>
 79    public static IEnumerable<ValidationResult> ToValidationResults(this IValidatableObject? objectToValidate,
 80        ValidationContext? validationContext) =>
 181        objectToValidate.ToValidationResults(validateAllProperties: true, validationContext: validationContext);
 82
 83    /// <summary>
 84    /// Converts the <see cref="Object" /> into a validation results.
 85    /// </summary>
 86    /// <param name="objectToValidate">The object to validate.</param>
 87    /// <param name="validateAllProperties"><c>true</c> to validate all properties;if <c>false</c>, only required attrib
 88    /// <param name="validationContext">the <see cref="ValidationContext"/></param>
 89    public static IEnumerable<ValidationResult> ToValidationResults(this IValidatableObject? objectToValidate,
 90        bool validateAllProperties, ValidationContext? validationContext)
 191    {
 192        if (objectToValidate == null) return Enumerable.Empty<ValidationResult>();
 193        validationContext ??= objectToValidate.ToValidationContext();
 94
 195        var results = new List<ValidationResult>();
 96
 197        Validator.TryValidateObject(objectToValidate, validationContext, results, validateAllProperties);
 98
 199        return results;
 1100    }
 101
 102    /// <summary>
 103    /// Converts the <see cref="Object"/> into a validation results.
 104    /// </summary>
 105    /// <param name="objectToValidate">The object to validate.</param>
 106    /// <param name="propertyName">Name of the property.</param>
 107    /// <param name="propertyValue">The property value.</param>
 108    public static IEnumerable<ValidationResult> ToValidationResults(this IValidatableObject? objectToValidate,
 109        string? propertyName, object? propertyValue) =>
 0110        objectToValidate.ToValidationResults(propertyName, propertyValue, validationContext: null);
 111
 112    /// <summary>
 113    /// Converts the <see cref="Object"/> into a validation results.
 114    /// </summary>
 115    /// <param name="objectToValidate">The object to validate.</param>
 116    /// <param name="propertyName">Name of the property.</param>
 117    /// <param name="propertyValue">The property value.</param>
 118    /// <param name="validationContext">the <see cref="ValidationContext"/></param>
 119    public static IEnumerable<ValidationResult> ToValidationResults(this IValidatableObject? objectToValidate,
 120        string? propertyName, object? propertyValue, ValidationContext? validationContext)
 0121    {
 0122        if (objectToValidate == null) return Enumerable.Empty<ValidationResult>();
 123
 0124        validationContext ??= objectToValidate.ToValidationContext();
 125
 0126        propertyName.ThrowWhenNullOrWhiteSpace();
 127
 0128        var results = new List<ValidationResult>();
 0129        validationContext.MemberName = propertyName;
 130
 0131        Validator.TryValidateProperty(propertyValue, validationContext, results);
 132
 0133        return results;
 0134    }
 135
 136    internal const string DisplayErrorMessage = $"[Unable to display {nameof(ValidationResult)}s]";
 137}