forked from microsoft/OpenAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidationRuleSet.cs
More file actions
335 lines (296 loc) · 12.8 KB
/
ValidationRuleSet.cs
File metadata and controls
335 lines (296 loc) · 12.8 KB
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Microsoft.OpenApi
{
/// <summary>
/// The rule set of the validation.
/// </summary>
public sealed class ValidationRuleSet
{
private Dictionary<Type, List<ValidationRule>> _rulesDictionary = new();
private static ValidationRuleSet? _defaultRuleSet;
private readonly List<ValidationRule> _emptyRules = new();
/// <summary>
/// Gets the rules in this rule set.
/// </summary>
public IList<ValidationRule> Rules => _rulesDictionary.Values.SelectMany(v => v).ToList();
/// <summary>
/// Gets the number of elements contained in this rule set.
/// </summary>
public int Count => _rulesDictionary.Count;
/// <summary>
/// Initializes a new instance of the <see cref="ValidationRuleSet"/> class.
/// </summary>
public ValidationRuleSet()
{
}
/// <summary>
/// Retrieve the rules that are related to a specific type
/// </summary>
/// <param name="type">The type that is to be validated</param>
/// <returns>Either the rules related to the type, or an empty list.</returns>
public IList<ValidationRule> FindRules(Type type)
{
_rulesDictionary.TryGetValue(type, out var results);
return results ?? _emptyRules;
}
/// <summary>
/// Gets the default validation rule sets.
/// </summary>
/// <remarks>
/// This is a method instead of a property to signal that a new default rule-set object is created
/// per call. Making this a property may be misleading callers to think the returned rule-sets from multiple calls
/// are the same objects.
/// </remarks>
public static ValidationRuleSet GetDefaultRuleSet()
{
// Reflection can be an expensive operation, so we cache the default rule set that has already been built.
_defaultRuleSet ??= BuildDefaultRuleSet();
// We create a new instance of ValidationRuleSet per call as a safeguard
// against unintentional modification of the private _defaultRuleSet.
return new(_defaultRuleSet);
}
/// <summary>
/// Return <see cref="ValidationRuleSet"/> with no rules
/// </summary>
public static ValidationRuleSet GetEmptyRuleSet()
{
// We create a new instance of ValidationRuleSet per call as a safeguard
// against unintentional modification of the private _defaultRuleSet.
return new();
}
/// <summary>
/// Add validation rules to the rule set.
/// </summary>
/// <param name="ruleSet">The rule set to add validation rules to.</param>
/// <param name="rules">The validation rules to be added to the rules set.</param>
/// <exception cref="OpenApiException">Throws a null argument exception if the arguments are null.</exception>
public static void AddValidationRules(ValidationRuleSet ruleSet, Dictionary<Type, List<ValidationRule>> rules)
{
if (ruleSet == null || rules == null)
{
throw new OpenApiException(SRResource.ArgumentNull);
}
foreach (var rule in rules)
{
ruleSet.Add(rule.Key, rule.Value);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="ValidationRuleSet"/> class.
/// </summary>
/// <param name="ruleSet">Rule set to be copied from.</param>
public ValidationRuleSet(ValidationRuleSet ruleSet)
{
if (ruleSet == null)
{
return;
}
foreach (var rule in ruleSet)
{
Add(rule.ElementType, rule);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="ValidationRuleSet"/> class.
/// </summary>
/// <param name="rules">Rules to be contained in this ruleset.</param>
public ValidationRuleSet(Dictionary<Type, List<ValidationRule>> rules)
{
if (rules == null)
{
return;
}
foreach (var rule in rules)
{
Add(rule.Key, rule.Value);
}
}
/// <summary>
/// Add the new rule into the rule set.
/// </summary>
/// <param name="key">The key for the rule.</param>
/// <param name="rules">The list of rules.</param>
public void Add(Type key, List<ValidationRule> rules)
{
foreach (var rule in rules)
{
Add(key, rule);
}
}
/// <summary>
/// Add a new rule into the rule set.
/// </summary>
/// <param name="key">The key for the rule.</param>
/// <param name="rule">The rule.</param>
/// <exception cref="OpenApiException">Exception thrown when rule already exists.</exception>
public void Add(Type key, ValidationRule rule)
{
if (!_rulesDictionary.TryGetValue(key, out var rules))
{
_rulesDictionary[key] = rules = [];
}
if (rules.Contains(rule))
{
throw new OpenApiException(SRResource.Validation_RuleAddTwice);
}
_rulesDictionary[key].Add(rule);
}
/// <summary>
/// Updates an existing rule with a new one.
/// </summary>
/// <param name="key">The key of the existing rule.</param>
/// <param name="newRule">The new rule.</param>
/// <param name="oldRule">The old rule.</param>
/// <returns>true, if the update was successful; otherwise false.</returns>
public bool Update(Type key, ValidationRule newRule, ValidationRule oldRule)
{
if (_rulesDictionary.TryGetValue(key, out var currentRules))
{
currentRules.Add(newRule);
return currentRules.Remove(oldRule);
}
return false;
}
/// <summary>
/// Removes a collection of rules.
/// </summary>
/// <param name="key">The key of the collection of rules to be removed.</param>
/// <returns>true if the collection of rules with the provided key is removed; otherwise, false.</returns>
public bool Remove(Type key)
{
return _rulesDictionary.Remove(key);
}
/// <summary>
/// Remove a rule by its name from all types it is used by.
/// </summary>
/// <param name="ruleName">Name of the rule.</param>
public void Remove(string ruleName)
{
foreach (KeyValuePair<Type, List<ValidationRule>> rule in _rulesDictionary)
{
_rulesDictionary[rule.Key] = rule.Value.Where(vr => !vr.Name.Equals(ruleName, StringComparison.Ordinal)).ToList();
}
// Remove types with no rule
_rulesDictionary = _rulesDictionary.Where(r => r.Value.Count > 0).ToDictionary(r => r.Key, r => r.Value);
}
/// <summary>
/// Removes a rule by key.
/// </summary>
/// <param name="key">The key of the rule to be removed.</param>
/// <param name="rule">The rule to be removed.</param>
/// <returns>true if the rule is successfully removed; otherwise, false.</returns>
public bool Remove(Type key, ValidationRule rule)
{
if (_rulesDictionary.TryGetValue(key, out List<ValidationRule>? validationRules))
{
return validationRules.Remove(rule);
}
return false;
}
/// <summary>
/// Removes the first rule that matches the provided rule from the list of rules.
/// </summary>
/// <param name="rule">The rule to be removed.</param>
/// <returns>true if the rule is successfully removed; otherwise, false.</returns>
public bool Remove(ValidationRule rule)
{
return _rulesDictionary.Values.FirstOrDefault(x => x.Remove(rule)) is not null;
}
/// <summary>
/// Clears all rules in this rule set.
/// </summary>
public void Clear()
{
_rulesDictionary.Clear();
}
/// <summary>
/// Determines whether the rule set contains an element with the specified key.
/// </summary>
/// <param name="key">The key to locate in the rule set.</param>
/// <returns>true if the rule set contains an element with the key; otherwise, false.</returns>
public bool ContainsKey(Type key)
{
return _rulesDictionary.ContainsKey(key);
}
/// <summary>
/// Determines whether the provided rule is contained in the specified key in the rule set.
/// </summary>
/// <param name="key">The key to locate.</param>
/// <param name="rule">The rule to locate.</param>
/// <returns></returns>
public bool Contains(Type key, ValidationRule rule)
{
return _rulesDictionary.TryGetValue(key, out List<ValidationRule>? validationRules) && validationRules.Contains(rule);
}
/// <summary>
/// Gets the rules associated with the specified key.
/// </summary>
/// <param name="key">The key whose rules to get.</param>
/// <param name="rules">When this method returns, the rules associated with the specified key, if the
/// key is found; otherwise, an empty <see cref="IList{ValidationRule}"/> object.
/// This parameter is passed uninitialized.</param>
/// <returns>true if the specified key has rules.</returns>
public bool TryGetValue(Type key, out List<ValidationRule>? rules)
{
return _rulesDictionary.TryGetValue(key, out rules);
}
/// <summary>
/// Get the enumerator.
/// </summary>
/// <returns>The enumerator.</returns>
public IEnumerator<ValidationRule> GetEnumerator()
{
foreach (var ruleList in _rulesDictionary.Values)
{
foreach (var rule in ruleList)
{
yield return rule;
}
}
}
private static ValidationRuleSet BuildDefaultRuleSet()
{
var ruleSet = new ValidationRuleSet();
var validationRuleType = typeof(ValidationRule);
var ruleTypeProperties = GetValidationRuleTypes();
foreach (var property in ruleTypeProperties)
{
if (!validationRuleType.IsAssignableFrom(property.PropertyType))
{
continue;
}
var propertyValue = property.GetValue(null); // static property
if (propertyValue is ValidationRule rule)
{
ruleSet.Add(rule.ElementType, rule);
}
}
return ruleSet;
}
internal static PropertyInfo[] GetValidationRuleTypes()
{
return [
..typeof(OpenApiComponentsRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
..typeof(OpenApiContactRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
..typeof(OpenApiDocumentRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
..typeof(OpenApiExtensibleRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
..typeof(OpenApiExternalDocsRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
..typeof(OpenApiInfoRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
..typeof(OpenApiLicenseRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
..typeof(OpenApiOAuthFlowRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
..typeof(OpenApiServerRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
..typeof(OpenApiResponseRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
..typeof(OpenApiResponsesRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
..typeof(OpenApiSchemaRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
..typeof(OpenApiTagRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
..typeof(OpenApiPathsRules).GetProperties(BindingFlags.Static | BindingFlags.Public),
..typeof(OpenApiParameterRules).GetProperties(BindingFlags.Static | BindingFlags.Public)
];
}
}
}