diff --git a/api/Hmcr.Api/Extensions/IServiceCollectionExtensions.cs b/api/Hmcr.Api/Extensions/IServiceCollectionExtensions.cs index 803f2d07..d57069c3 100644 --- a/api/Hmcr.Api/Extensions/IServiceCollectionExtensions.cs +++ b/api/Hmcr.Api/Extensions/IServiceCollectionExtensions.cs @@ -165,6 +165,9 @@ public static void AddHmcrTypes(this IServiceCollection services) //FieldValidationService as Singleton services.AddSingleton(); + //RegexDefs as Singleton + services.AddSingleton(); + //Jwt Bearer Handler services.AddScoped(); } diff --git a/api/Hmcr.Api/Startup.cs b/api/Hmcr.Api/Startup.cs index 042c66dc..c0599bf5 100644 --- a/api/Hmcr.Api/Startup.cs +++ b/api/Hmcr.Api/Startup.cs @@ -60,12 +60,11 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ISubmiss app.UseAuthentication(); app.UseRouting(); app.UseHmcrEndpoints(); - app.UseHangfireDashboard(); app.UseHmcrSwagger(env, Configuration.GetSection("Constants:SwaggerApiUrl").Value); //Register Hangfire Recurring Jobs - var serviceAreas = svcAreaService.GetAllServiceAreas().Where(x => x.ServiceAreaNumber == 10); - SubmissionObjectJobService.RegisterReportingJobs(serviceAreas); + //var serviceAreas = svcAreaService.GetAllServiceAreas(); + //SubmissionObjectJobService.RegisterReportingJobs(serviceAreas); } } } diff --git a/api/Hmcr.Domain/RegexExp.cs b/api/Hmcr.Domain/RegexExp.cs deleted file mode 100644 index bc62c46b..00000000 --- a/api/Hmcr.Domain/RegexExp.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Hmcr.Domain -{ - public static class RegexExp - { - public const string Email = @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"; - public const string QREA = @"^[QREA]$"; - public const string D5_2 = @"^\-?\d{1,5}(\.\d{1,2})?$"; - public const string D5_6 = @"^\-?\d{1,5}(\.\d{1,6})?$"; - public const string D4_3 = @"^\-?\d{1,4}(\.\d{1,3})?$"; - public const string Dollar6_2 = @"^\$?\d{1,6}(\.\d{1,2})?$"; - public const string SiteNumber = @"^[ABDLRSTWX]\d{6}$"; - } -} diff --git a/api/Hmcr.Domain/Services/FieldValidatorService.cs b/api/Hmcr.Domain/Services/FieldValidatorService.cs index a4d2a501..e23e3af5 100644 --- a/api/Hmcr.Domain/Services/FieldValidatorService.cs +++ b/api/Hmcr.Domain/Services/FieldValidatorService.cs @@ -16,10 +16,13 @@ public interface IFieldValidatorService public class FieldValidatorService : IFieldValidatorService { HashSet _rules; + RegexDefs _regex; - public FieldValidatorService() + public FieldValidatorService(RegexDefs regex) { _rules = new HashSet(); + _regex = regex; + LoadUserEntityRules(); LoadRoleEntityRules(); LoadWorkReportD2Rules(); @@ -27,6 +30,7 @@ public FieldValidatorService() LoadWorkReportD3SiteRules(); LoadWorkReportD4Rules(); LoadWorkReportD4SiteRules(); + } public IEnumerable GetFieldValidationRules(string entityName) { @@ -39,7 +43,7 @@ private void LoadUserEntityRules() _rules.Add(new FieldValidationRule(Entities.User, Fields.UserType, FieldTypes.String, true, 1, 30, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.User, Fields.FirstName, FieldTypes.String, true, 1, 150, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.User, Fields.LastName, FieldTypes.String, true, 1, 150, null, null, null, null, null, null)); - _rules.Add(new FieldValidationRule(Entities.User, Fields.Email, FieldTypes.String, true, 1, 100, null, null, null, null, RegexExp.Email, null)); + _rules.Add(new FieldValidationRule(Entities.User, Fields.Email, FieldTypes.String, true, 1, 100, null, null, null, null, _regex.GetRegexInfo(RegexDefs.Email), null)); _rules.Add(new FieldValidationRule(Entities.User, Fields.EndDate, FieldTypes.Date, false, null, null, null, null, new DateTime(1900, 1, 1), new DateTime(9999, 12, 31), null, null)); } @@ -52,13 +56,13 @@ private void LoadRoleEntityRules() private void LoadWorkReportD2Rules() { - _rules.Add(new FieldValidationRule(Entities.WorkReportD2, Fields.RecordType, FieldTypes.String, true, 1, 1, null, null, null, null, RegexExp.QREA, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD2, Fields.RecordType, FieldTypes.String, true, 1, 1, null, null, null, null, _regex.GetRegexInfo(RegexDefs.QREA), null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD2, Fields.RecordNumber, FieldTypes.String, true, 1, 8, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD2, Fields.TaskNumber, FieldTypes.String, false, 0, 6, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD2, Fields.ActivityNumber, FieldTypes.String, true, 6, 6, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD2, Fields.StartDate, FieldTypes.Date, false, null, null, null, null, new DateTime(1900, 1, 1), new DateTime(9999, 12, 31), null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD2, Fields.EndDate, FieldTypes.Date, true, null, null, null, null, new DateTime(1900, 1, 1), new DateTime(9999, 12, 31), null, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD2, Fields.Accomplishment, FieldTypes.String, true, null, null, null, null, null, null, RegexExp.D5_2, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD2, Fields.Accomplishment, FieldTypes.String, true, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.D5_2), null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD2, Fields.UnitOfMeasure, FieldTypes.String, true, 1, 3, null, null, null, null, null, null)); //todo lookup _rules.Add(new FieldValidationRule(Entities.WorkReportD2, Fields.PostedDate, FieldTypes.Date, true, null, null, null, null, new DateTime(1900, 1, 1), new DateTime(9999, 12, 31), null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD2, Fields.HighwayUnique, FieldTypes.String, false, 0, 16, null, null, null, null, null, null)); @@ -67,92 +71,92 @@ private void LoadWorkReportD2Rules() private void LoadWorkReportD3Rules() { - _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.RecordType, FieldTypes.String, true, 1, 1, null, null, null, null, RegexExp.QREA, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.RecordType, FieldTypes.String, true, 1, 1, null, null, null, null, _regex.GetRegexInfo(RegexDefs.QREA), null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.RecordNumber, FieldTypes.String, true, 1, 8, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.TaskNumber, FieldTypes.String, false, 0, 6, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.ActivityNumber, FieldTypes.String, true, 6, 6, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.StartDate, FieldTypes.Date, false, null, null, null, null, new DateTime(1900, 1, 1), new DateTime(9999, 12, 31), null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.EndDate, FieldTypes.Date, true, null, null, null, null, new DateTime(1900, 1, 1), new DateTime(9999, 12, 31), null, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.Accomplishment, FieldTypes.String, true, null, null, null, null, null, null, RegexExp.D5_2, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.Accomplishment, FieldTypes.String, true, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.D5_2), null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.UnitOfMeasure, FieldTypes.String, true, 1, 3, null, null, null, null, null, null)); //todo lookup _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.PostedDate, FieldTypes.Date, true, null, null, null, null, new DateTime(1900, 1, 1), new DateTime(9999, 12, 31), null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.HighwayUnique, FieldTypes.String, true, 0, 16, null, null, null, null, null, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.StartLatitude, FieldTypes.String, true, null, null, null, null, null, null, RegexExp.D5_6, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.StartLongitude, FieldTypes.String, true, null, null, null, null, null, null, RegexExp.D5_6, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.EndLatitude, FieldTypes.String, true, null, null, null, null, null, null, RegexExp.D5_6, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.EndLongitude, FieldTypes.String, true, null, null, null, null, null, null, RegexExp.D5_6, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.StartLatitude, FieldTypes.String, true, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.D5_6), null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.StartLongitude, FieldTypes.String, true, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.D5_6), null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.EndLatitude, FieldTypes.String, true, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.D5_6), null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.EndLongitude, FieldTypes.String, true, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.D5_6), null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.ValueOfWork, FieldTypes.String, true, null, null, null, null, null, null, RegexExp.Dollar6_2, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.ValueOfWork, FieldTypes.String, true, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.Dollar6_2), null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD3, Fields.Comments, FieldTypes.String, false, 0, 1024, null, null, null, null, null, null)); } private void LoadWorkReportD3SiteRules() { - _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.RecordType, FieldTypes.String, true, 1, 1, null, null, null, null,RegexExp.QREA, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.RecordType, FieldTypes.String, true, 1, 1, null, null, null, null,_regex.GetRegexInfo(RegexDefs.QREA), null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.RecordNumber, FieldTypes.String, true, 1, 8, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.TaskNumber, FieldTypes.String, false, 0, 6, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.ActivityNumber, FieldTypes.String, true, 6, 6, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.StartDate, FieldTypes.Date, false, null, null, null, null, new DateTime(1900, 1, 1), new DateTime(9999, 12, 31), null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.EndDate, FieldTypes.Date, true, null, null, null, null, new DateTime(1900, 1, 1), new DateTime(9999, 12, 31), null, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.Accomplishment, FieldTypes.String, true, null, null, null, null, null, null, RegexExp.D5_2, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.Accomplishment, FieldTypes.String, true, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.D5_2), null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.UnitOfMeasure, FieldTypes.String, true, 1, 3, null, null, null, null, null, null)); //todo lookup _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.PostedDate, FieldTypes.Date, true, null, null, null, null, new DateTime(1900, 1, 1), new DateTime(9999, 12, 31), null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.HighwayUnique, FieldTypes.String, true, 0, 16, null, null, null, null, null, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.StartLatitude, FieldTypes.String, true, null, null, null, null, null, null, RegexExp.D5_6, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.StartLongitude, FieldTypes.String, true, null, null, null, null, null, null, RegexExp.D5_6, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.EndLatitude, FieldTypes.String, true, null, null, null, null, null, null, RegexExp.D5_6, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.EndLongitude, FieldTypes.String, true, null, null, null, null, null, null, RegexExp.D5_6, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.StartLatitude, FieldTypes.String, true, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.D5_6), null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.StartLongitude, FieldTypes.String, true, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.D5_6), null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.EndLatitude, FieldTypes.String, true, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.D5_6), null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.EndLongitude, FieldTypes.String, true, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.D5_6), null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.StructureNumber, FieldTypes.String, true, 0, 5, null, null, null, null, null, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.SiteNumber, FieldTypes.String, true, 0, 8, null, null, null, null, RegexExp.SiteNumber, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.SiteNumber, FieldTypes.String, true, 0, 8, null, null, null, null, _regex.GetRegexInfo(RegexDefs.SiteNumber), null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.ValueOfWork, FieldTypes.String, true, null, null, null, null, null, null, RegexExp.Dollar6_2, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.ValueOfWork, FieldTypes.String, true, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.Dollar6_2), null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD3Site, Fields.Comments, FieldTypes.String, false, 0, 1024, null, null, null, null, null, null)); } private void LoadWorkReportD4Rules() { - _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.RecordType, FieldTypes.String, true, 1, 1, null, null, null, null,RegexExp.QREA, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.RecordType, FieldTypes.String, true, 1, 1, null, null, null, null,_regex.GetRegexInfo(RegexDefs.QREA), null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.RecordNumber, FieldTypes.String, true, 1, 8, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.TaskNumber, FieldTypes.String, false, 0, 6, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.ActivityNumber, FieldTypes.String, true, 6, 6, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.StartDate, FieldTypes.Date, false, null, null, null, null, new DateTime(1900, 1, 1), new DateTime(9999, 12, 31), null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.EndDate, FieldTypes.Date, true, null, null, null, null, new DateTime(1900, 1, 1), new DateTime(9999, 12, 31), null, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.Accomplishment, FieldTypes.String, true, null, null, null, null, null, null, RegexExp.D5_2, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.Accomplishment, FieldTypes.String, true, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.D5_2), null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.UnitOfMeasure, FieldTypes.String, true, 1, 3, null, null, null, null, null, null)); //todo lookup _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.PostedDate, FieldTypes.Date, true, null, null, null, null, new DateTime(1900, 1, 1), new DateTime(9999, 12, 31), null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.HighwayUnique, FieldTypes.String, true, 0, 16, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.Landmark, FieldTypes.String, false, 0, 8, null, null, null, null, null, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.StartOffset, FieldTypes.String, false, null, null, null, null, null, null, RegexExp.D4_3, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.EndOffset, FieldTypes.String, false, null, null, null, null, null, null, RegexExp.D4_3, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.ValueOfWork, FieldTypes.String, true, null, null, null, null, null, null, RegexExp.Dollar6_2, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.StartOffset, FieldTypes.String, false, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.D4_3), null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.EndOffset, FieldTypes.String, false, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.D4_3), null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.ValueOfWork, FieldTypes.String, true, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.Dollar6_2), null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4, Fields.Comments, FieldTypes.String, false, 0, 1024, null, null, null, null, null, null)); } private void LoadWorkReportD4SiteRules() { - _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.RecordType, FieldTypes.String, true, 1, 1, null, null, null, null,RegexExp.QREA, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.RecordType, FieldTypes.String, true, 1, 1, null, null, null, null,_regex.GetRegexInfo(RegexDefs.QREA), null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.RecordNumber, FieldTypes.String, true, 1, 8, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.TaskNumber, FieldTypes.String, false, 0, 6, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.ActivityNumber, FieldTypes.String, true, 6, 6, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.StartDate, FieldTypes.Date, false, null, null, null, null, new DateTime(1900, 1, 1), new DateTime(9999, 12, 31), null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.EndDate, FieldTypes.Date, true, null, null, null, null, new DateTime(1900, 1, 1), new DateTime(9999, 12, 31), null, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.Accomplishment, FieldTypes.String, true, null, null, null, null, null, null, RegexExp.D5_2, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.Accomplishment, FieldTypes.String, true, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.D5_2), null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.UnitOfMeasure, FieldTypes.String, true, 1, 3, null, null, null, null, null, null)); //todo lookup _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.PostedDate, FieldTypes.Date, true, null, null, null, null, new DateTime(1900, 1, 1), new DateTime(9999, 12, 31), null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.HighwayUnique, FieldTypes.String, true, 0, 16, null, null, null, null, null, null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.Landmark, FieldTypes.String, false, 0, 8, null, null, null, null, null, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.StartOffset, FieldTypes.String, false, null, null, null, null, null, null, RegexExp.D4_3, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.EndOffset, FieldTypes.String, false, null, null, null, null, null, null, RegexExp.D4_3, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.StartOffset, FieldTypes.String, false, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.D4_3), null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.EndOffset, FieldTypes.String, false, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.D4_3), null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.StructureNumber, FieldTypes.String, true, 0, 5, null, null, null, null, null, null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.SiteNumber, FieldTypes.String, true, 0, 8, null, null, null, null, RegexExp.SiteNumber, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.SiteNumber, FieldTypes.String, true, 0, 8, null, null, null, null, _regex.GetRegexInfo(RegexDefs.SiteNumber), null)); - _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.ValueOfWork, FieldTypes.String, true, null, null, null, null, null, null, RegexExp.Dollar6_2, null)); + _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.ValueOfWork, FieldTypes.String, true, null, null, null, null, null, null, _regex.GetRegexInfo(RegexDefs.Dollar6_2), null)); _rules.Add(new FieldValidationRule(Entities.WorkReportD4Site, Fields.Comments, FieldTypes.String, false, 0, 1024, null, null, null, null, null, null)); } @@ -217,11 +221,11 @@ private List ValidateStringField(FieldValidationRule rule, T val) } } - if (rule.Regex.IsNotEmpty()) + if (rule.Regex != null) { - if (!Regex.IsMatch(value, rule.Regex)) + if (!Regex.IsMatch(value, rule.Regex.Regex)) { - messages.Add($"{rule.FieldName} field must match the regular expression [{rule.Regex}]."); + messages.Add($"{rule.FieldName} {rule.Regex.ErrorMessage}."); } } diff --git a/api/Hmcr.Model/FieldValidationRule.cs b/api/Hmcr.Model/FieldValidationRule.cs index 790f867a..de9594e0 100644 --- a/api/Hmcr.Model/FieldValidationRule.cs +++ b/api/Hmcr.Model/FieldValidationRule.cs @@ -16,10 +16,10 @@ public class FieldValidationRule public decimal? MaxValue { get; set; } public DateTime? MinDate { get; set; } public DateTime? MaxDate { get; set; } - public string Regex { get; set; } + public RegexInfo Regex { get; set; } public string LookUpCode { get; set; } - public FieldValidationRule(string entityName, string fieldName, string fieldType, bool required, int? minLength, int? maxLength, decimal? minValue, decimal? maxValue, DateTime? minDate, DateTime? maxDate, string regex, string lookUpCode) + public FieldValidationRule(string entityName, string fieldName, string fieldType, bool required, int? minLength, int? maxLength, decimal? minValue, decimal? maxValue, DateTime? minDate, DateTime? maxDate, RegexInfo regex, string lookUpCode) { EntityName = entityName; FieldName = fieldName; diff --git a/api/Hmcr.Model/RegexDefs.cs b/api/Hmcr.Model/RegexDefs.cs new file mode 100644 index 00000000..b1b5df0f --- /dev/null +++ b/api/Hmcr.Model/RegexDefs.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Hmcr.Model +{ + public class RegexInfo + { + public string Regex { get; set; } + public string ErrorMessage { get; set; } + } + + public class RegexDefs + { + public const string Email = "Email"; + public const string QREA = "QREA"; + public const string D5_2 = "D5_2"; + public const string D5_6 = "D5_6"; + public const string D4_3 = "D4_3"; + public const string Dollar6_2 = "Dollar6_2"; + public const string SiteNumber = "SiteNumber"; + + private Dictionary _regexInfos; + + public RegexDefs() + { + _regexInfos = new Dictionary(); + + _regexInfos.Add(Email, new RegexInfo { Regex = @"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$", ErrorMessage = "Wrong email address" }); + _regexInfos.Add(QREA, new RegexInfo { Regex = @"^[QREA]$", ErrorMessage = "Value must be one of these [QREA] letters" }); + _regexInfos.Add(D5_2, new RegexInfo { Regex = @"^\-?\d{1,5}(\.\d{1,2})?$", ErrorMessage = "Value must be a number of less than 5 digits optionally with maximum 2 decimal digits" }); + _regexInfos.Add(D5_6, new RegexInfo { Regex = @"^\-?\d{1,5}(\.\d{1,6})?$", ErrorMessage = "Value must be a number of less than 5 digits optionally with maximum 6 decimal digits" }); + _regexInfos.Add(D4_3, new RegexInfo { Regex = @"^\-?\d{1,4}(\.\d{1,3})?$", ErrorMessage = "Value must be a number of less than 4 digits optionally with maximum 3 decimal digits" }); + _regexInfos.Add(Dollar6_2, new RegexInfo { Regex = @"^\$?\d{1,6}(\.\d{1,2})?$", ErrorMessage = "Value must be a number of less than 6 digits optionally with maximum 2 decimal digits" }); + _regexInfos.Add(SiteNumber, new RegexInfo { Regex = @"^[ABDLRSTWX]\d{6}$", ErrorMessage = "Value must start with one of these [ABDLRSTWX] letters followed by 6 digit number" }); + } + + public RegexInfo GetRegexInfo(string name) + { + if (!_regexInfos.TryGetValue(name, out RegexInfo regexInfo)) + { + throw new Exception($"RegexInfo for {name} does not exist."); + } + + return regexInfo; + } + } +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 00000000..48e341a0 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,3 @@ +{ + "lockfileVersion": 1 +}