| | 1 | | using System.ComponentModel.DataAnnotations; |
| | 2 | |
|
| | 3 | | namespace 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> |
| | 13 | | public 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> |
| 1 | 19 | | public static string ToDisplayText(this ValidationResult? result) => result == null |
| 1 | 20 | | ? DisplayErrorMessage |
| 1 | 21 | | : $"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) |
| 1 | 28 | | { |
| 1 | 29 | | if (results == null) return DisplayErrorMessage; |
| | 30 | |
|
| 1 | 31 | | results = results.ToArray(); |
| | 32 | |
|
| 1 | 33 | | var resultsCount = results.Count(); |
| | 34 | |
|
| 1 | 35 | | if (resultsCount == 0) return DisplayErrorMessage; |
| | 36 | |
|
| 1 | 37 | | var builder = new StringBuilder(); |
| 1 | 38 | | builder.Append($"Count: {resultsCount}"); |
| 1 | 39 | | builder.AppendLine(); |
| | 40 | |
|
| 5 | 41 | | foreach (var result in results) |
| 1 | 42 | | { |
| 1 | 43 | | builder.Append(result.ToDisplayText()); |
| 1 | 44 | | builder.AppendLine(); |
| 1 | 45 | | } |
| | 46 | |
|
| 1 | 47 | | return builder.ToString(); |
| 1 | 48 | | } |
| | 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) |
| 1 | 55 | | { |
| 1 | 56 | | ArgumentNullException.ThrowIfNull(objectToValidate); |
| | 57 | |
|
| 1 | 58 | | return new ValidationContext(objectToValidate); |
| 1 | 59 | | } |
| | 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) => |
| 0 | 69 | | 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) => |
| 1 | 81 | | 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) |
| 1 | 91 | | { |
| 1 | 92 | | if (objectToValidate == null) return Enumerable.Empty<ValidationResult>(); |
| 1 | 93 | | validationContext ??= objectToValidate.ToValidationContext(); |
| | 94 | |
|
| 1 | 95 | | var results = new List<ValidationResult>(); |
| | 96 | |
|
| 1 | 97 | | Validator.TryValidateObject(objectToValidate, validationContext, results, validateAllProperties); |
| | 98 | |
|
| 1 | 99 | | return results; |
| 1 | 100 | | } |
| | 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) => |
| 0 | 110 | | 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) |
| 0 | 121 | | { |
| 0 | 122 | | if (objectToValidate == null) return Enumerable.Empty<ValidationResult>(); |
| | 123 | |
|
| 0 | 124 | | validationContext ??= objectToValidate.ToValidationContext(); |
| | 125 | |
|
| 0 | 126 | | propertyName.ThrowWhenNullOrWhiteSpace(); |
| | 127 | |
|
| 0 | 128 | | var results = new List<ValidationResult>(); |
| 0 | 129 | | validationContext.MemberName = propertyName; |
| | 130 | |
|
| 0 | 131 | | Validator.TryValidateProperty(propertyValue, validationContext, results); |
| | 132 | |
|
| 0 | 133 | | return results; |
| 0 | 134 | | } |
| | 135 | |
|
| | 136 | | internal const string DisplayErrorMessage = $"[Unable to display {nameof(ValidationResult)}s]"; |
| | 137 | | } |