1- // Copyright (c) Microsoft Corporation. All rights reserved.
1+
2+ // Copyright (c) Microsoft Corporation. All rights reserved.
23// Licensed under the MIT license.
34
45using System ;
89using Microsoft . OpenApi . Exceptions ;
910using Microsoft . OpenApi . Properties ;
1011using Microsoft . OpenApi . Validations . Rules ;
12+ using System . Data ;
1113
1214namespace Microsoft . OpenApi . Validations
1315{
@@ -16,16 +18,12 @@ namespace Microsoft.OpenApi.Validations
1618 /// </summary>
1719 public sealed class ValidationRuleSet
1820 {
19- private Dictionary < string , IList < ValidationRule > > _rulesDictionary = new ( ) ;
21+ private Dictionary < Type , IList < ValidationRule > > _rulesDictionary = new ( ) ;
2022
2123 private static ValidationRuleSet _defaultRuleSet ;
2224
2325 private List < ValidationRule > _emptyRules = new ( ) ;
2426
25- /// <summary>
26- /// Gets the keys in this rule set.
27- /// </summary>
28- public ICollection < string > Keys => _rulesDictionary . Keys ;
2927
3028 /// <summary>
3129 /// Gets the rules in this rule set.
@@ -45,13 +43,13 @@ public ValidationRuleSet()
4543 }
4644
4745 /// <summary>
48- /// Retrieve the rules that are related to a specific key.
46+ /// Retrieve the rules that are related to a specific type
4947 /// </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 )
5351 {
54- _rulesDictionary . TryGetValue ( key , out var results ) ;
52+ _rulesDictionary . TryGetValue ( type , out var results ) ;
5553 return results ?? _emptyRules ;
5654 }
5755
@@ -92,7 +90,7 @@ public static ValidationRuleSet GetEmptyRuleSet()
9290 /// <param name="ruleSet">The rule set to add validation rules to.</param>
9391 /// <param name="rules">The validation rules to be added to the rules set.</param>
9492 /// <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 )
9694 {
9795 if ( ruleSet == null || rules == null )
9896 {
@@ -118,15 +116,15 @@ public ValidationRuleSet(ValidationRuleSet ruleSet)
118116
119117 foreach ( var rule in ruleSet )
120118 {
121- Add ( rule . ElementType . Name , rule ) ;
119+ Add ( rule . ElementType , rule ) ;
122120 }
123121 }
124122
125123 /// <summary>
126124 /// Initializes a new instance of the <see cref="ValidationRuleSet"/> class.
127125 /// </summary>
128126 /// <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 )
130128 {
131129 if ( rules == null )
132130 {
@@ -144,7 +142,7 @@ public ValidationRuleSet(IDictionary<string, IList<ValidationRule>> rules)
144142 /// </summary>
145143 /// <param name="key">The key for the rule.</param>
146144 /// <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 )
148146 {
149147 foreach ( var rule in rules )
150148 {
@@ -158,7 +156,7 @@ public void Add(string key, IList<ValidationRule> rules)
158156 /// <param name="key">The key for the rule.</param>
159157 /// <param name="rule">The rule.</param>
160158 /// <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 )
162160 {
163161 if ( ! _rulesDictionary . ContainsKey ( key ) )
164162 {
@@ -180,7 +178,7 @@ public void Add(string key, ValidationRule rule)
180178 /// <param name="newRule">The new rule.</param>
181179 /// <param name="oldRule">The old rule.</param>
182180 /// <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 )
184182 {
185183 if ( _rulesDictionary . TryGetValue ( key , out var currentRules ) )
186184 {
@@ -195,18 +193,33 @@ public bool Update(string key, ValidationRule newRule, ValidationRule oldRule)
195193 /// </summary>
196194 /// <param name="key">The key of the collection of rules to be removed.</param>
197195 /// <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 )
199197 {
200198 return _rulesDictionary . Remove ( key ) ;
201199 }
202200
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+
203216 /// <summary>
204217 /// Removes a rule by key.
205218 /// </summary>
206219 /// <param name="key">The key of the rule to be removed.</param>
207220 /// <param name="rule">The rule to be removed.</param>
208221 /// <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 )
210223 {
211224 if ( _rulesDictionary . TryGetValue ( key , out IList < ValidationRule > validationRules ) )
212225 {
@@ -239,7 +252,7 @@ public void Clear()
239252 /// </summary>
240253 /// <param name="key">The key to locate in the rule set.</param>
241254 /// <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 )
243256 {
244257 return _rulesDictionary . ContainsKey ( key ) ;
245258 }
@@ -250,7 +263,7 @@ public bool ContainsKey(string key)
250263 /// <param name="key">The key to locate.</param>
251264 /// <param name="rule">The rule to locate.</param>
252265 /// <returns></returns>
253- public bool Contains ( string key , ValidationRule rule )
266+ public bool Contains ( Type key , ValidationRule rule )
254267 {
255268 return _rulesDictionary . TryGetValue ( key , out IList < ValidationRule > validationRules ) && validationRules . Contains ( rule ) ;
256269 }
@@ -263,35 +276,11 @@ public bool Contains(string key, ValidationRule rule)
263276 /// key is found; otherwise, an empty <see cref="IList{ValidationRule}"/> object.
264277 /// This parameter is passed uninitialized.</param>
265278 /// <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 )
267280 {
268281 return _rulesDictionary . TryGetValue ( key , out rules ) ;
269282 }
270283
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-
295284 /// <summary>
296285 /// Get the enumerator.
297286 /// </summary>
@@ -324,7 +313,7 @@ private static ValidationRuleSet BuildDefaultRuleSet()
324313 var propertyValue = property . GetValue ( null ) ; // static property
325314 if ( propertyValue is ValidationRule rule )
326315 {
327- ruleSet . Add ( rule . ElementType . Name , rule ) ;
316+ ruleSet . Add ( rule . ElementType , rule ) ;
328317 }
329318 }
330319
0 commit comments