-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathValidationHelper.cs
45 lines (37 loc) · 1.79 KB
/
ValidationHelper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Text;
namespace Polly.Utils;
[ExcludeFromCodeCoverage]
internal static class ValidationHelper
{
public static string[]? GetMemberName(this ValidationContext? validationContext) =>
#pragma warning disable S1168 // Empty arrays and collections should be returned instead of null
validationContext?.MemberName is { } memberName
? new[] { memberName }
: null;
#pragma warning restore S1168 // Empty arrays and collections should be returned instead of null
public static string GetDisplayName(this ValidationContext? validationContext)
=> validationContext?.DisplayName ?? string.Empty;
[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof(TimeSpan))]
[UnconditionalSuppressMessage(
"Trimming",
"IL2026:Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code",
Justification = "The member of options are preserved and not trimmed. See builder.AddStrategy() extension.")]
public static void ValidateObject(ResilienceValidationContext context)
{
Guard.NotNull(context);
var errors = new List<ValidationResult>();
if (!Validator.TryValidateObject(context.Instance, new ValidationContext(context.Instance), errors, true))
{
var stringBuilder = new StringBuilder(context.PrimaryMessage);
stringBuilder.AppendLine();
stringBuilder.AppendLine("Validation Errors:");
foreach (var error in errors)
{
stringBuilder.AppendLine(error.ErrorMessage);
}
throw new ValidationException(stringBuilder.ToString());
}
}
}