Skip to content

Commit

Permalink
RegexDef class to handle error message
Browse files Browse the repository at this point in the history
  • Loading branch information
ychung-mot committed Jan 3, 2020
1 parent 61e5971 commit 378b1a2
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 55 deletions.
3 changes: 3 additions & 0 deletions api/Hmcr.Api/Extensions/IServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ public static void AddHmcrTypes(this IServiceCollection services)
//FieldValidationService as Singleton
services.AddSingleton<IFieldValidatorService, FieldValidatorService>();

//RegexDefs as Singleton
services.AddSingleton<RegexDefs>();

//Jwt Bearer Handler
services.AddScoped<HmcrJwtBearerEvents>();
}
Expand Down
5 changes: 2 additions & 3 deletions api/Hmcr.Api/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
17 changes: 0 additions & 17 deletions api/Hmcr.Domain/RegexExp.cs

This file was deleted.

70 changes: 37 additions & 33 deletions api/Hmcr.Domain/Services/FieldValidatorService.cs

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions api/Hmcr.Model/FieldValidationRule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
48 changes: 48 additions & 0 deletions api/Hmcr.Model/RegexDefs.cs
Original file line number Diff line number Diff line change
@@ -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<string, RegexInfo> _regexInfos;

public RegexDefs()
{
_regexInfos = new Dictionary<string, RegexInfo>();

_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;
}
}
}
3 changes: 3 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 378b1a2

Please sign in to comment.