diff --git a/src/modeler/AutoRest.Swagger/Validation/OperationNameValidation.cs b/src/modeler/AutoRest.Swagger/Validation/OperationNameValidation.cs index d398c1993d..462c2de208 100644 --- a/src/modeler/AutoRest.Swagger/Validation/OperationNameValidation.cs +++ b/src/modeler/AutoRest.Swagger/Validation/OperationNameValidation.cs @@ -2,17 +2,23 @@ // Licensed under the MIT License. See License.txt in the project root for license information. using System; -using System.Collections.Generic; using AutoRest.Core.Logging; using AutoRest.Core.Properties; using AutoRest.Core.Validation; -using AutoRest.Swagger.Model; +using System.Text.RegularExpressions; namespace AutoRest.Swagger.Validation { public class OperationNameValidation : TypedRule { - private const string NOUN_VERB_PATTERN = "^(\\w+)?_(\\w+)$"; + private static readonly Regex GET_NOUN_VERB_PATTERN = new Regex(@"^(\w+)_(Get|List)", RegexOptions.IgnoreCase); + private static readonly Regex GET_VERB_PATTERN = new Regex(@"^(Get|List)", RegexOptions.IgnoreCase); + private static readonly Regex PUT_NOUN_VERB_PATTERN = new Regex(@"^(\w+)_(Create)", RegexOptions.IgnoreCase); + private static readonly Regex PUT_VERB_PATTERN = new Regex(@"^(Create)", RegexOptions.IgnoreCase); + private static readonly Regex PATCH_NOUN_VERB_PATTERN = new Regex(@"^(\w+)_(Update)", RegexOptions.IgnoreCase); + private static readonly Regex PATCH_VERB_PATTERN = new Regex(@"^(Update)", RegexOptions.IgnoreCase); + private static readonly Regex DELETE_NOUN_VERB_PATTERN = new Regex(@"^(\w+)_(Delete)", RegexOptions.IgnoreCase); + private static readonly Regex DELETE_VERB_PATTERN = new Regex(@"^(Delete)", RegexOptions.IgnoreCase); /// /// The template message for this Rule. @@ -50,20 +56,19 @@ public override bool IsValid(string entity, RuleContext context) if (httpVerb.Equals("GET", StringComparison.InvariantCultureIgnoreCase)) { - isOperationNameValid = (entity.Contains("_") && (entity.EndsWith("_Get") || entity.Contains("_List"))) || - (entity.StartsWith("get") || entity.StartsWith("list")); + isOperationNameValid = GET_NOUN_VERB_PATTERN.IsMatch(entity) || GET_VERB_PATTERN.IsMatch(entity); } else if (httpVerb.Equals("PUT", StringComparison.InvariantCultureIgnoreCase)) { - isOperationNameValid = (entity.Contains("_") && entity.EndsWith("_Create")) || entity.StartsWith("create"); + isOperationNameValid = PUT_NOUN_VERB_PATTERN.IsMatch(entity) || PUT_VERB_PATTERN.IsMatch(entity); } else if (httpVerb.Equals("PATCH", StringComparison.InvariantCultureIgnoreCase)) { - isOperationNameValid = (entity.Contains("_") && entity.EndsWith("_Update")) || entity.StartsWith("update"); + isOperationNameValid = PATCH_NOUN_VERB_PATTERN.IsMatch(entity) || PATCH_VERB_PATTERN.IsMatch(entity); } else if (httpVerb.Equals("DELETE", StringComparison.InvariantCultureIgnoreCase)) { - isOperationNameValid = (entity.Contains("_") && entity.EndsWith("_Delete")) || entity.StartsWith("update"); + isOperationNameValid = DELETE_NOUN_VERB_PATTERN.IsMatch(entity) || DELETE_VERB_PATTERN.IsMatch(entity); } return isOperationNameValid;