Skip to content

Commit

Permalink
Fixed day error message & error count (#916)
Browse files Browse the repository at this point in the history
* added day must be number error message & updated test

* Added fix for counting error

* updated logic & tests

* removed unused validation

* fixed code smell
  • Loading branch information
zhodges-nimble authored Oct 18, 2024
1 parent bfc9d01 commit bbaff7a
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class DateValidationCommandTests
[InlineData("00/10/2025", "Day must be between 1 and 31")]
[InlineData("12/00/2025", "Month must be between 1 and 12")]
[InlineData("12/13/2025", "Month must be between 1 and 12")]
[InlineData("hj/12/2050", "Enter a valid date. For example, 27/03/2021")]
public void DateValidationFails(string date, string expectedErrorMessage)
{
var dateValidation = new DateValidationCommand();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,17 @@ public async Task<BulkEditValidateResponse> Execute(BulkEditRequest request)
CurrentRowIndex = row.FileRowIndex,
Data = currentRow
});

var currentValue = IsNotNullOrEmpty(currentRow) ? header.DataInteraction.GetFromDto(currentRow) : "";
if (!validationResult.IsValid)
{

validationRowResult.Columns.Add(new ValueChangeInfo()
{
ColumnIndex = column.ColumnIndex,
CurrentValue = currentValue,
NewValue = header.DataInteraction.FormatValue(column.Value),
Error = validationResult.ErrorMessage
});

}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
namespace Dfe.ManageFreeSchoolProjects.API.UseCases.BulkEdit.Validations
using System.Text.RegularExpressions;

namespace Dfe.ManageFreeSchoolProjects.API.UseCases.BulkEdit.Validations
{
public class DateValidationCommand : IValidationCommand<BulkEditDto>
public partial class DateValidationCommand : IValidationCommand<BulkEditDto>
{
[GeneratedRegex("^[0-9/]+$")]
private static partial Regex NumbersAndForwardSlashOnlyRegex();

public ValidationResult Execute(ValidationCommandParameters<BulkEditDto> parameters)
{
var dateParts = CleanAndSplitDate(parameters.Value);
Expand All @@ -20,7 +25,7 @@ public ValidationResult Execute(ValidationCommandParameters<BulkEditDto> paramet

if (!IsValidYear(year, out var yearNumber))
return CreateValidationResult(false, "Year must be between 2000 and 2050");

if (!IsValidDay(day, yearNumber, monthNumber, out _))
return CreateValidationResult(false,
$"Day must be between 1 and {DateTime.DaysInMonth(yearNumber, monthNumber)}");
Expand All @@ -40,7 +45,7 @@ private static string[] CleanAndSplitDate(string date)
return dateParts;
}

private static bool IsValidDateFormat(string[] dateParts) => dateParts.Length == 3;
private static bool IsValidDateFormat(string[] dateParts) => dateParts.Length == 3 && Array.TrueForAll(dateParts, NumbersAndForwardSlashOnlyRegex().IsMatch);

private static string CheckForMissingDateParts(string day, string month, string year)
{
Expand All @@ -50,7 +55,7 @@ private static string CheckForMissingDateParts(string day, string month, string
if (string.IsNullOrEmpty(year)) missingParts.Add("year");

if (missingParts.Count == 3)
return "Date must include a day, month, and year";
return "Date must include a day, month, and year";
if (missingParts.Count > 0)
return $"Date must include a {string.Join(" and ", missingParts)}";
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public async Task<IActionResult> OnPostAsync()
})
});

ErrorCount = Rows.Count(x => x.Cells.Any(y => !string.IsNullOrEmpty(y.Error)));
ErrorCount = Rows.SelectMany(x => x.Cells).Count(y => !string.IsNullOrEmpty(y.Error));

if (ErrorCount == 0)
{
Expand Down

0 comments on commit bbaff7a

Please sign in to comment.