-
Notifications
You must be signed in to change notification settings - Fork 446
/
Copy pathDataValidator.cs
76 lines (70 loc) · 2.96 KB
/
DataValidator.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Concurrent;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
namespace WebApiClientCore.Implementations
{
/// <summary>
/// 数据验证器
/// 提供返回值的属性验证、参数值和参数的属性值验证
/// </summary>
static class DataValidator
{
/// <summary>
/// 类型的属性否需要验证缓存
/// </summary>
private static readonly ConcurrentDictionary<Type, bool> cache = new();
/// <summary>
/// 验证参数值输入合法性
/// 验证参数的属性值输入合法性
/// </summary>
/// <param name="parameter">参数描述</param>
/// <param name="parameterValue">参数值</param>
/// <param name="validateProperty">是否验证属性值</param>
/// <exception cref="ValidationException"></exception>
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = "允许 parameterValue 属性被裁剪")]
public static void ValidateParameter(ApiParameterDescriptor parameter, object? parameterValue, bool validateProperty)
{
var name = parameter.Name;
foreach (var validation in parameter.ValidationAttributes)
{
validation.Validate(parameterValue, name);
}
if (validateProperty && parameterValue != null && IsNeedValidateProperty(parameterValue) == true)
{
var ctx = new ValidationContext(parameterValue) { MemberName = name };
Validator.ValidateObject(parameterValue, ctx, true);
}
}
/// <summary>
/// 验证参返回的结果
/// </summary>
/// <param name="value">结果值</param>
/// <exception cref="ValidationException"></exception>
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026", Justification = "允许 value 属性被裁剪")]
public static void ValidateReturnValue(object? value)
{
if (value != null && IsNeedValidateProperty(value) == true)
{
var ctx = new ValidationContext(value);
Validator.ValidateObject(value, ctx, true);
}
}
/// <summary>
/// 返回是否需要进行属性验证
/// </summary>
/// <param name="instance">实例</param>
/// <returns></returns>
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2070", Justification = "允许 instance 属性被裁剪")]
private static bool IsNeedValidateProperty(object instance)
{
var type = instance.GetType();
if (type == typeof(string) || type.IsValueType == true)
{
return false;
}
return cache.GetOrAdd(type, t => t.GetProperties().Any(p => p.CanRead && p.IsDefined(typeof(ValidationAttribute), true)));
}
}
}