22
33namespace LEGO . AsyncAPI . Validation . Rules
44{
5+ using System ;
6+ using System . Collections . Generic ;
57 using System . Linq ;
68 using System . Text . RegularExpressions ;
79 using LEGO . AsyncAPI . Models ;
@@ -10,10 +12,13 @@ namespace LEGO.AsyncAPI.Validation.Rules
1012 [ AsyncApiRule ]
1113 public static class AsyncApiDocumentRules
1214 {
15+ private static TimeSpan RegexTimeout = TimeSpan . FromSeconds ( 1 ) ;
16+
1317 /// <summary>
1418 /// The key regex.
1519 /// </summary>
16- public static Regex KeyRegex = new Regex ( @"^[a-zA-Z0-9\.\-_]+$" ) ;
20+ public static Regex KeyRegex = new Regex ( @"^[a-zA-Z0-9\.\-_]+$" , RegexOptions . None , RegexTimeout ) ;
21+ public static Regex ChannelKeyUriTemplateRegex = new Regex ( @"^(?:(?:[^\x00-\x20""'<>%\\^`{|}]|%[0-9a-f]{2})|\{[+#./;?&=,!@|]?(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?(?:,(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?)*\})*$" , RegexOptions . IgnoreCase , RegexTimeout ) ;
1722
1823 public static ValidationRule < AsyncApiDocument > DocumentRequiredFields =>
1924 new ValidationRule < AsyncApiDocument > (
@@ -30,16 +35,58 @@ public static class AsyncApiDocumentRules
3035 context . Exit ( ) ;
3136
3237 context . Enter ( "channels" ) ;
33- if ( document . Channels == null || ! document . Channels . Keys . Any ( ) )
38+ try
3439 {
35- context . CreateError (
36- nameof ( DocumentRequiredFields ) ,
37- string . Format ( Resource . Validation_FieldRequired , "channels" , "document" ) ) ;
38- }
40+ // MUST have at least 1 channel
41+ if ( document . Channels == null || ! document . Channels . Keys . Any ( ) )
42+ {
43+ context . CreateError (
44+ nameof ( DocumentRequiredFields ) ,
45+ string . Format ( Resource . Validation_FieldRequired , "channels" , "document" ) ) ;
46+ return ;
47+ }
48+ var hashSet = new HashSet < string > ( ) ;
49+ foreach ( var key in document . Channels . Keys )
50+ {
51+ // Uri-template
52+ if ( ! ChannelKeyUriTemplateRegex . IsMatch ( key ) )
53+ {
54+ context . CreateError (
55+ "ChannelKeys" ,
56+ string . Format ( Resource . Validation_KeyMustMatchRegularExpr , key , "channels" , KeyRegex . ToString ( ) ) ) ;
57+ }
3958
40- context . Exit ( ) ;
59+ // Unique channel keys
60+ var pathSignature = GetKeySignature ( key ) ;
61+ if ( ! hashSet . Add ( pathSignature ) )
62+ {
63+ context . CreateError ( "ChannelKey" , string . Format ( Resource . Validation_ChannelsMustBeUnique , pathSignature ) ) ;
64+ }
65+ }
66+ }
67+ finally
68+ {
69+ context . Exit ( ) ;
70+ }
4171 } ) ;
4272
73+ private static string GetKeySignature ( string path )
74+ {
75+ for ( int openBrace = path . IndexOf ( '{' ) ; openBrace > - 1 ; openBrace = path . IndexOf ( '{' , openBrace + 2 ) )
76+ {
77+ int closeBrace = path . IndexOf ( '}' , openBrace ) ;
78+
79+ if ( closeBrace < 0 )
80+ {
81+ return path ;
82+ }
83+
84+ path = path . Substring ( 0 , openBrace + 1 ) + path . Substring ( closeBrace ) ;
85+ }
86+
87+ return path ;
88+ }
89+
4390 public static ValidationRule < AsyncApiDocument > KeyMustBeRegularExpression =>
4491 new ValidationRule < AsyncApiDocument > (
4592 ( context , document ) =>
0 commit comments