1
- // Copyright (c) Microsoft Corporation. All rights reserved.
1
+
2
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
3
// Licensed under the MIT license.
3
4
4
5
using System ;
8
9
using Microsoft . OpenApi . Exceptions ;
9
10
using Microsoft . OpenApi . Properties ;
10
11
using Microsoft . OpenApi . Validations . Rules ;
12
+ using System . Data ;
11
13
12
14
namespace Microsoft . OpenApi . Validations
13
15
{
@@ -16,16 +18,12 @@ namespace Microsoft.OpenApi.Validations
16
18
/// </summary>
17
19
public sealed class ValidationRuleSet
18
20
{
19
- private Dictionary < string , IList < ValidationRule > > _rulesDictionary = new ( ) ;
21
+ private Dictionary < Type , IList < ValidationRule > > _rulesDictionary = new ( ) ;
20
22
21
23
private static ValidationRuleSet _defaultRuleSet ;
22
24
23
25
private List < ValidationRule > _emptyRules = new ( ) ;
24
26
25
- /// <summary>
26
- /// Gets the keys in this rule set.
27
- /// </summary>
28
- public ICollection < string > Keys => _rulesDictionary . Keys ;
29
27
30
28
/// <summary>
31
29
/// Gets the rules in this rule set.
@@ -45,13 +43,13 @@ public ValidationRuleSet()
45
43
}
46
44
47
45
/// <summary>
48
- /// Retrieve the rules that are related to a specific key.
46
+ /// Retrieve the rules that are related to a specific type
49
47
/// </summary>
50
- /// <param name="key ">The key of the rules to search for. </param>
51
- /// <returns>Either the rules related to the given key , or an empty list.</returns>
52
- public IList < ValidationRule > FindRules ( string key )
48
+ /// <param name="type ">The type that is to be validated </param>
49
+ /// <returns>Either the rules related to the type , or an empty list.</returns>
50
+ public IList < ValidationRule > FindRules ( Type type )
53
51
{
54
- _rulesDictionary . TryGetValue ( key , out var results ) ;
52
+ _rulesDictionary . TryGetValue ( type , out var results ) ;
55
53
return results ?? _emptyRules ;
56
54
}
57
55
@@ -92,7 +90,7 @@ public static ValidationRuleSet GetEmptyRuleSet()
92
90
/// <param name="ruleSet">The rule set to add validation rules to.</param>
93
91
/// <param name="rules">The validation rules to be added to the rules set.</param>
94
92
/// <exception cref="OpenApiException">Throws a null argument exception if the arguments are null.</exception>
95
- public static void AddValidationRules ( ValidationRuleSet ruleSet , IDictionary < string , IList < ValidationRule > > rules )
93
+ public static void AddValidationRules ( ValidationRuleSet ruleSet , IDictionary < Type , IList < ValidationRule > > rules )
96
94
{
97
95
if ( ruleSet == null || rules == null )
98
96
{
@@ -118,15 +116,15 @@ public ValidationRuleSet(ValidationRuleSet ruleSet)
118
116
119
117
foreach ( var rule in ruleSet )
120
118
{
121
- Add ( rule . ElementType . Name , rule ) ;
119
+ Add ( rule . ElementType , rule ) ;
122
120
}
123
121
}
124
122
125
123
/// <summary>
126
124
/// Initializes a new instance of the <see cref="ValidationRuleSet"/> class.
127
125
/// </summary>
128
126
/// <param name="rules">Rules to be contained in this ruleset.</param>
129
- public ValidationRuleSet ( IDictionary < string , IList < ValidationRule > > rules )
127
+ public ValidationRuleSet ( IDictionary < Type , IList < ValidationRule > > rules )
130
128
{
131
129
if ( rules == null )
132
130
{
@@ -144,7 +142,7 @@ public ValidationRuleSet(IDictionary<string, IList<ValidationRule>> rules)
144
142
/// </summary>
145
143
/// <param name="key">The key for the rule.</param>
146
144
/// <param name="rules">The list of rules.</param>
147
- public void Add ( string key , IList < ValidationRule > rules )
145
+ public void Add ( Type key , IList < ValidationRule > rules )
148
146
{
149
147
foreach ( var rule in rules )
150
148
{
@@ -158,7 +156,7 @@ public void Add(string key, IList<ValidationRule> rules)
158
156
/// <param name="key">The key for the rule.</param>
159
157
/// <param name="rule">The rule.</param>
160
158
/// <exception cref="OpenApiException">Exception thrown when rule already exists.</exception>
161
- public void Add ( string key , ValidationRule rule )
159
+ public void Add ( Type key , ValidationRule rule )
162
160
{
163
161
if ( ! _rulesDictionary . ContainsKey ( key ) )
164
162
{
@@ -180,7 +178,7 @@ public void Add(string key, ValidationRule rule)
180
178
/// <param name="newRule">The new rule.</param>
181
179
/// <param name="oldRule">The old rule.</param>
182
180
/// <returns>true, if the update was successful; otherwise false.</returns>
183
- public bool Update ( string key , ValidationRule newRule , ValidationRule oldRule )
181
+ public bool Update ( Type key , ValidationRule newRule , ValidationRule oldRule )
184
182
{
185
183
if ( _rulesDictionary . TryGetValue ( key , out var currentRules ) )
186
184
{
@@ -195,18 +193,33 @@ public bool Update(string key, ValidationRule newRule, ValidationRule oldRule)
195
193
/// </summary>
196
194
/// <param name="key">The key of the collection of rules to be removed.</param>
197
195
/// <returns>true if the collection of rules with the provided key is removed; otherwise, false.</returns>
198
- public bool Remove ( string key )
196
+ public bool Remove ( Type key )
199
197
{
200
198
return _rulesDictionary . Remove ( key ) ;
201
199
}
202
200
201
+ /// <summary>
202
+ /// Remove a rule by its name from all types it is used by.
203
+ /// </summary>
204
+ /// <param name="ruleName">Name of the rule.</param>
205
+ public void Remove ( string ruleName )
206
+ {
207
+ foreach ( KeyValuePair < Type , IList < ValidationRule > > rule in _rulesDictionary )
208
+ {
209
+ _rulesDictionary [ rule . Key ] = rule . Value . Where ( vr => ! vr . Name . Equals ( ruleName , StringComparison . Ordinal ) ) . ToList ( ) ;
210
+ }
211
+
212
+ // Remove types with no rule
213
+ _rulesDictionary = _rulesDictionary . Where ( r => r . Value . Any ( ) ) . ToDictionary ( r => r . Key , r => r . Value ) ;
214
+ }
215
+
203
216
/// <summary>
204
217
/// Removes a rule by key.
205
218
/// </summary>
206
219
/// <param name="key">The key of the rule to be removed.</param>
207
220
/// <param name="rule">The rule to be removed.</param>
208
221
/// <returns>true if the rule is successfully removed; otherwise, false.</returns>
209
- public bool Remove ( string key , ValidationRule rule )
222
+ public bool Remove ( Type key , ValidationRule rule )
210
223
{
211
224
if ( _rulesDictionary . TryGetValue ( key , out IList < ValidationRule > validationRules ) )
212
225
{
@@ -239,7 +252,7 @@ public void Clear()
239
252
/// </summary>
240
253
/// <param name="key">The key to locate in the rule set.</param>
241
254
/// <returns>true if the rule set contains an element with the key; otherwise, false.</returns>
242
- public bool ContainsKey ( string key )
255
+ public bool ContainsKey ( Type key )
243
256
{
244
257
return _rulesDictionary . ContainsKey ( key ) ;
245
258
}
@@ -250,7 +263,7 @@ public bool ContainsKey(string key)
250
263
/// <param name="key">The key to locate.</param>
251
264
/// <param name="rule">The rule to locate.</param>
252
265
/// <returns></returns>
253
- public bool Contains ( string key , ValidationRule rule )
266
+ public bool Contains ( Type key , ValidationRule rule )
254
267
{
255
268
return _rulesDictionary . TryGetValue ( key , out IList < ValidationRule > validationRules ) && validationRules . Contains ( rule ) ;
256
269
}
@@ -263,35 +276,11 @@ public bool Contains(string key, ValidationRule rule)
263
276
/// key is found; otherwise, an empty <see cref="IList{ValidationRule}"/> object.
264
277
/// This parameter is passed uninitialized.</param>
265
278
/// <returns>true if the specified key has rules.</returns>
266
- public bool TryGetValue ( string key , out IList < ValidationRule > rules )
279
+ public bool TryGetValue ( Type key , out IList < ValidationRule > rules )
267
280
{
268
281
return _rulesDictionary . TryGetValue ( key , out rules ) ;
269
282
}
270
283
271
- /// <summary>
272
- /// Remove a rule by its name from all types it is used by.
273
- /// </summary>
274
- /// <param name="ruleName">Name of the rule.</param>
275
- public void Remove ( string ruleName )
276
- {
277
- foreach ( KeyValuePair < Type , IList < ValidationRule > > rule in _rules )
278
- {
279
- _rules [ rule . Key ] = rule . Value . Where ( vr => ! vr . Name . Equals ( ruleName , StringComparison . Ordinal ) ) . ToList ( ) ;
280
- }
281
-
282
- // Remove types with no rule
283
- _rules = _rules . Where ( r => r . Value . Any ( ) ) . ToDictionary ( r => r . Key , r => r . Value ) ;
284
- }
285
-
286
- /// <summary>
287
- /// Remove a rule by element type.
288
- /// </summary>
289
- /// <param name="type">Type of the rule.</param>
290
- public void Remove ( Type type )
291
- {
292
- _rules . Remove ( type ) ;
293
- }
294
-
295
284
/// <summary>
296
285
/// Get the enumerator.
297
286
/// </summary>
@@ -324,7 +313,7 @@ private static ValidationRuleSet BuildDefaultRuleSet()
324
313
var propertyValue = property . GetValue ( null ) ; // static property
325
314
if ( propertyValue is ValidationRule rule )
326
315
{
327
- ruleSet . Add ( rule . ElementType . Name , rule ) ;
316
+ ruleSet . Add ( rule . ElementType , rule ) ;
328
317
}
329
318
}
330
319
0 commit comments