moving logic together and adding unit test
MODIFY
src/WebJobs.Script.WebHost/Controllers/FunctionsController.cs
src/WebJobs.Script.WebHost/Controllers/FunctionsController.cs
using System.IO.Abstractions;
using System.IO.Compression;
using System.Linq;
-using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
@@ -34,7 +33,6 @@ public class FunctionsController : Controller
private readonly IWebFunctionsManager _functionsManager;
private readonly IWebJobsRouter _webJobsRouter;
private readonly ILogger _logger;
- private static readonly Regex FunctionNameValidationRegex = new Regex(@"^[a-z][a-z0-9_\-]{0,127}$(?<!^host$)", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
public FunctionsController(IWebFunctionsManager functionsManager, IWebJobsRouter webJobsRouter, ILoggerFactory loggerFactory)
{
@@ -69,7 +67,7 @@ public async Task<IActionResult> Get(string name)
[Authorize(Policy = PolicyNames.AdminAuthLevel)]
public async Task<IActionResult> CreateOrUpdate(string name, [FromBody] FunctionMetadataResponse functionMetadata)
{
- if (!FunctionNameValidationRegex.IsMatch(name))
+ if (!Utility.IsValidFunctionName(name))
{
return BadRequest($"{name} is not a valid function name");
}

MODIFY
src/WebJobs.Script/Host/FunctionMetadataManager.cs
src/WebJobs.Script/Host/FunctionMetadataManager.cs
using System.IO;
using System.IO.Abstractions;
using System.Linq;
-using System.Text.RegularExpressions;
using Microsoft.Azure.WebJobs.Logging;
using Microsoft.Azure.WebJobs.Script.Description;
using Microsoft.Azure.WebJobs.Script.Diagnostics.Extensions;
@@ -21,7 +20,6 @@ namespace Microsoft.Azure.WebJobs.Script
{
public class FunctionMetadataManager : IFunctionMetadataManager
{
- private static readonly Regex FunctionNameValidationRegex = new Regex(@"^[a-z][a-z0-9_\-]{0,127}$(?<!^host$)", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
private readonly Dictionary<string, ICollection<string>> _functionErrors = new Dictionary<string, ICollection<string>>();
private readonly Lazy<ImmutableArray<FunctionMetadata>> _metadata;
private readonly IEnumerable<WorkerConfig> _workerConfigs;
@@ -131,7 +129,7 @@ internal static FunctionMetadata ReadFunctionMetadata(string scriptDir, ICollect
internal static void ValidateName(string name, bool isProxy = false)
{
- if (!FunctionNameValidationRegex.IsMatch(name))
+ if (!Utility.IsValidFunctionName(name))
{
throw new InvalidOperationException(string.Format("'{0}' is not a valid {1} name.", name, isProxy ? "proxy" : "function"));
}

MODIFY
src/WebJobs.Script/Utility.cs
src/WebJobs.Script/Utility.cs
using System.Linq;
using System.Reflection;
using System.Text;
+using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Script.Description;
@@ -29,6 +30,7 @@ public static class Utility
// i.e.: "f-<functionname>"
public const string AssemblyPrefix = "f-";
public const string AssemblySeparator = "__";
+ private static readonly Regex FunctionNameValidationRegex = new Regex(@"^[a-z][a-z0-9_\-]{0,127}$(?<!^host$)", RegexOptions.Compiled | RegexOptions.IgnoreCase | RegexOptions.CultureInvariant);
private static readonly string UTF8ByteOrderMark = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
private static readonly FilteredExpandoObjectConverter _filteredExpandoObjectConverter = new FilteredExpandoObjectConverter();
@@ -165,6 +167,11 @@ public static string GetFunctionShortName(string functionName)
return functionName;
}
+ public static bool IsValidFunctionName(string functionName)
+ {
+ return FunctionNameValidationRegex.IsMatch(functionName);
+ }
+
// "Namespace.Class.Method" --> "Namespace.Class"
public static string GetFullClassName(string fullFunctionName)
{

moving logic together and adding unit test