11using System ;
22using System . Collections . Generic ;
3+ using System . IO ;
34using System . Linq ;
5+ using System . Text ;
46using Elasticsearch . Net ;
57using Newtonsoft . Json ;
68
@@ -15,23 +17,47 @@ public interface ICreateIndexRequest : IIndexPath<CreateIndexRequestParameters>
1517
1618 internal static class CreateIndexPathInfo
1719 {
20+ private static char [ ] _invalidChars = new [ ] { '\\ ' , '/' , '*' , '?' , '"' , '<' , '>' , '|' , ' ' , ',' , '#' } ;
21+ private static string _invalidCharsMessage = string . Join ( ", " , _invalidChars ) ;
22+
1823 public static void Update ( ElasticsearchPathInfo < CreateIndexRequestParameters > pathInfo , ICreateIndexRequest request )
1924 {
2025 pathInfo . HttpMethod = PathInfoHttpMethod . POST ;
2126 }
27+
28+ public static void Validate ( ElasticsearchPathInfo < CreateIndexRequestParameters > pathInfo , ICreateIndexRequest request )
29+ {
30+ var index = pathInfo . Index ;
31+ if ( index . StartsWith ( "_" ) )
32+ throw new DslException ( "indexname {0} may not start with an underscore" . F ( index ) ) ;
33+
34+ if ( Encoding . UTF8 . GetByteCount ( index ) > 255 )
35+ throw new DslException ( "indexname {0} exceeds maximum index name length of 255" . F ( index ) ) ;
36+
37+ if ( index . Any ( char . IsUpper ) )
38+ throw new DslException ( "indexname {0} contains uppercase characters" . F ( index ) ) ;
39+
40+ if ( index . Any ( c => _invalidChars . Contains ( c ) ) )
41+ throw new DslException ( "indexname {0} contains one of {1} invalid characters" . F ( index , _invalidCharsMessage ) ) ;
42+
43+ }
2244 }
23-
45+
2446 public partial class CreateIndexRequest : IndexPathBase < CreateIndexRequestParameters > , ICreateIndexRequest
2547 {
2648 public CreateIndexRequest ( IndexNameMarker index ) : base ( index ) { }
2749
2850 public IndexSettings IndexSettings { get ; set ; }
29-
51+
3052 protected override void UpdatePathInfo ( IConnectionSettingsValues settings , ElasticsearchPathInfo < CreateIndexRequestParameters > pathInfo )
3153 {
3254 CreateIndexPathInfo . Update ( pathInfo , this ) ;
3355 }
3456
57+ protected override void ValidatePathInfo ( ElasticsearchPathInfo < CreateIndexRequestParameters > pathInfo )
58+ {
59+ CreateIndexPathInfo . Validate ( pathInfo , this ) ;
60+ }
3561 }
3662
3763 [ DescriptorFor ( "IndicesCreate" ) ]
@@ -131,7 +157,7 @@ public CreateIndexDescriptor AddAlias(string aliasName, Func<CreateAliasDescript
131157
132158 return this ;
133159 }
134-
160+
135161
136162 private CreateIndexDescriptor RemoveMapping ( TypeNameMarker marker )
137163 {
@@ -185,7 +211,7 @@ public CreateIndexDescriptor AddMapping<T>(RootObjectMapping rootObjectMapping,
185211 }
186212 else
187213 {
188- typeMapping . Name = typeof ( T ) ;
214+ typeMapping . Name = typeof ( T ) ;
189215 }
190216
191217 this . _indexSettings . Mappings . Add ( typeMapping ) ;
@@ -220,7 +246,7 @@ public CreateIndexDescriptor Analysis(Func<AnalysisDescriptor, AnalysisDescripto
220246 this . _indexSettings . Analysis = analysis == null ? null : analysis . _AnalysisSettings ;
221247 return this ;
222248 }
223-
249+
224250 public CreateIndexDescriptor Similarity ( Func < SimilarityDescriptor , SimilarityDescriptor > similaritySelector )
225251 {
226252 similaritySelector . ThrowIfNull ( "similaritySelector" ) ;
@@ -234,5 +260,10 @@ protected override void UpdatePathInfo(IConnectionSettingsValues settings, Elast
234260 CreateIndexPathInfo . Update ( pathInfo , this ) ;
235261 }
236262
263+ protected override void ValidatePathInfo ( ElasticsearchPathInfo < CreateIndexRequestParameters > pathInfo )
264+ {
265+ CreateIndexPathInfo . Validate ( pathInfo , this ) ;
266+ }
267+
237268 }
238269}
0 commit comments