Skip to content

Commit

Permalink
EES-5047: Move string length and message definitions into constants, …
Browse files Browse the repository at this point in the history
…update naming convention for "DataSet" in validation messaging.
  • Loading branch information
Tom Jones committed Dec 13, 2024
1 parent 6be80fa commit 3345df9
Show file tree
Hide file tree
Showing 14 changed files with 218 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ public async Task ValidateBulkDataArchiveFiles_IndexFileMissing_ReturnsValidatio
.AssertBadRequestWithValidationErrors([
new ErrorViewModel
{
Code = ValidationMessages.BulkDataZipMustContainDatasetNamesCsv.Code,
Message = ValidationMessages.BulkDataZipMustContainDatasetNamesCsv.Message,
Code = ValidationMessages.BulkDataZipMustContainDataSetNamesCsv.Code,
Message = ValidationMessages.BulkDataZipMustContainDataSetNamesCsv.Message,
}
]);
}
Expand Down Expand Up @@ -267,8 +267,8 @@ public async Task ValidateBulkDataArchiveFiles_IndexFileHasIncorrectHeaders_Retu
.AssertBadRequestWithValidationErrors([
new ErrorViewModel
{
Code = ValidationMessages.DatasetNamesCsvIncorrectHeaders.Code,
Message = ValidationMessages.DatasetNamesCsvIncorrectHeaders.Message,
Code = ValidationMessages.DataSetNamesCsvIncorrectHeaders.Code,
Message = ValidationMessages.DataSetNamesCsvIncorrectHeaders.Message,
},
]);
}
Expand Down Expand Up @@ -319,7 +319,7 @@ public async Task ValidateBulkDataArchiveFiles_DuplicateDataSetTitlesAndFileName
.AssertLeft()
.AssertBadRequestWithValidationErrors([
ValidationMessages.GenerateErrorDataSetTitleShouldBeUnique("Duplicate title"),
ValidationMessages.GenerateErrorDatasetNamesCsvFilenamesShouldBeUnique("one"),
ValidationMessages.GenerateErrorDataSetNamesCsvFilenamesShouldBeUnique("one"),
]);
}
}
Expand All @@ -342,7 +342,7 @@ public async Task ValidateBulkDataArchiveFiles_Fail_DataSetNamesCsvFilesnamesSho
result
.AssertLeft()
.AssertBadRequestWithValidationErrors([
ValidationMessages.GenerateErrorDatasetNamesCsvFilenamesShouldNotEndDotCsv("one.csv")
ValidationMessages.GenerateErrorDataSetNamesCsvFilenamesShouldNotEndDotCsv("one.csv")
]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.ComponentModel.DataAnnotations;
using System.Threading;
using System.Threading.Tasks;
using static GovUk.Education.ExploreEducationStatistics.Common.Constants.ValidationConstants;

namespace GovUk.Education.ExploreEducationStatistics.Admin.Controllers.Api
{
Expand Down Expand Up @@ -108,7 +109,7 @@ public async Task<ActionResult<List<DataFileInfo>>> ReorderDataFiles(Guid releas
public async Task<ActionResult<DataFileInfo>> UploadDataSet(Guid releaseVersionId,
[FromQuery(Name = "replacingFileId")] Guid? replacingFileId,
[FromQuery(Name = "title")]
[MaxLength(120, ErrorMessage = "Subject title must be 120 characters or less")]
[MaxLength(SubjectTitleMaxLength, ErrorMessage = SubjectTitleMaxLengthMessage)]
string title,
IFormFile file,
IFormFile metaFile)
Expand All @@ -128,7 +129,7 @@ public async Task<ActionResult<DataFileInfo>> UploadDataSet(Guid releaseVersionI
public async Task<ActionResult<DataFileInfo>> UploadDataSetAsZip(Guid releaseVersionId,
[FromQuery(Name = "replacingFileId")] Guid? replacingFileId,
[FromQuery(Name = "title")]
[MaxLength(120, ErrorMessage = "Subject title must be 120 characters or less")]
[MaxLength(SubjectTitleMaxLength, ErrorMessage = SubjectTitleMaxLengthMessage)]
string title,
IFormFile zipFile)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using GovUk.Education.ExploreEducationStatistics.Common.Model.Data;
using GovUk.Education.ExploreEducationStatistics.Common.Requests;
using System.Collections.Generic;
using static GovUk.Education.ExploreEducationStatistics.Common.Constants.ValidationConstants;

namespace GovUk.Education.ExploreEducationStatistics.Admin.Requests;

Expand All @@ -17,7 +18,7 @@ public record DataBlockCreateRequest

public FullTableQueryRequest Query { get; init; } = null!;

public List<IChart> Charts { get; init; } = new();
public List<IChart> Charts { get; init; } = [];

public TableBuilderConfiguration Table { get; init; } = null!;

Expand All @@ -27,8 +28,8 @@ public Validator()
{
RuleFor(request => request.Heading)
.NotEmpty()
.MaximumLength(120)
.WithMessage("Table title must be 120 characters or less");
.MaximumLength(TableTitleMaxLength)
.WithMessage(TableTitleMaxLengthMessage);

RuleFor(request => request.Name)
.NotEmpty();
Expand All @@ -49,7 +50,7 @@ public record DataBlockUpdateRequest

public FullTableQueryRequest Query { get; init; } = null!;

public List<IChart> Charts { get; init; } = new();
public List<IChart> Charts { get; init; } = [];

public TableBuilderConfiguration Table { get; init; } = null!;

Expand All @@ -59,8 +60,8 @@ public Validator()
{
RuleFor(request => request.Heading)
.NotEmpty()
.MaximumLength(120)
.WithMessage("Table title must be 120 characters or less");
.MaximumLength(TableTitleMaxLength)
.WithMessage(TableTitleMaxLengthMessage);

RuleFor(request => request.Name)
.NotEmpty();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,47 @@
#nullable enable
using FluentValidation;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using static GovUk.Education.ExploreEducationStatistics.Common.Constants.ValidationConstants;

namespace GovUk.Education.ExploreEducationStatistics.Admin.Requests;

public record DataGuidanceUpdateRequest
{
[Required]
public string Content { get; init; } = string.Empty;

[MinLength(1)]
public List<DataGuidanceDataSetUpdateRequest> DataSets { get; init; } = new();
public List<DataGuidanceDataSetUpdateRequest> DataSets { get; init; } = [];

public class Validator : AbstractValidator<DataGuidanceUpdateRequest>
{
public Validator()
{
RuleFor(request => request.Content)
.NotEmpty();

RuleFor(request => request.DataSets)
.NotEmpty();
}
}
}

public record DataGuidanceDataSetUpdateRequest
{
[Required]
public Guid FileId { get; init; }

[Required]
[MaxLength(250, ErrorMessage = "File guidance content must be 250 characters or less")]
public string Content { get; init; } = string.Empty;

public class Validator : AbstractValidator<DataGuidanceDataSetUpdateRequest>
{
public Validator()
{
RuleFor(request => request.FileId)
.NotEmpty();

RuleFor(request => request.Content)
.NotEmpty()
.MaximumLength(FileGuidanceContentMaxLength)
.WithMessage(FileGuidanceContentMaxLengthMessage);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,54 @@
#nullable enable
using FluentValidation;
using System;
using System.ComponentModel.DataAnnotations;
using static GovUk.Education.ExploreEducationStatistics.Common.Constants.ValidationConstants;

namespace GovUk.Education.ExploreEducationStatistics.Admin.Requests;

public record FeaturedTableCreateRequest
{
[Required]
[MaxLength(120, ErrorMessage = "Featured table name must be 120 characters or less")]
public string Name { get; init; } = string.Empty;

[Required]
[MaxLength(200, ErrorMessage = "Featured table description must be 200 characters or less")]
public string Description { get; set; } = string.Empty;

public Guid DataBlockId { get; set; }

public class Validator : AbstractValidator<FeaturedTableCreateRequest>
{
public Validator()
{
RuleFor(request => request.Name)
.NotEmpty()
.MaximumLength(FeaturedTableNameMaxLength)
.WithMessage(FeaturedTableNameMaxLengthMessage);

RuleFor(request => request.Description)
.NotEmpty()
.MaximumLength(FeaturedTableDescriptionMaxLength)
.WithMessage(FeaturedTableDescriptionMaxLengthMessage);
}
}
}

public record FeaturedTableUpdateRequest
{
[Required]
[MaxLength(120, ErrorMessage = "Featured table name must be 120 characters or less")]
public string Name { get; init; } = string.Empty;

[Required]
[MaxLength(200, ErrorMessage = "Featured table description must be 200 characters or less")]
public string Description { get; set; } = string.Empty;

public class Validator : AbstractValidator<FeaturedTableUpdateRequest>
{
public Validator()
{
RuleFor(request => request.Name)
.NotEmpty()
.MaximumLength(FeaturedTableNameMaxLength)
.WithMessage(FeaturedTableNameMaxLengthMessage);

RuleFor(request => request.Description)
.NotEmpty()
.MaximumLength(FeaturedTableDescriptionMaxLength)
.WithMessage(FeaturedTableDescriptionMaxLengthMessage);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#nullable enable
using FluentValidation;
using Microsoft.AspNetCore.Http;
using System.ComponentModel.DataAnnotations;
using static GovUk.Education.ExploreEducationStatistics.Common.Constants.ValidationConstants;

namespace GovUk.Education.ExploreEducationStatistics.Admin.Requests;

Expand All @@ -16,44 +16,61 @@ public class Validator : AbstractValidator<ReleaseDataFileUpdateRequest>
public Validator()
{
RuleFor(request => request.Title)
.MaximumLength(120)
.WithMessage("Subject title must be 120 characters or less");
.MaximumLength(SubjectTitleMaxLength)
.WithMessage(SubjectTitleMaxLengthMessage);
}
}
}

public record ReleaseAncillaryFileUploadRequest
{
[Required]
public string Title { get; set; } = string.Empty;

[Required] public string Summary { get; set; } = string.Empty;
public string Summary { get; set; } = string.Empty;

[Required]
public IFormFile File { get; set; } = null!;

public class Validator : AbstractValidator<ReleaseAncillaryFileUploadRequest>
{
public Validator()
{
RuleFor(request => request.Title)
.MaximumLength(120)
.WithMessage("Title must be 120 characters or less");
.NotEmpty()
.MaximumLength(TitleMaxLength)
.WithMessage(TitleMaxLengthMessage);

RuleFor(request => request.Summary)
.MaximumLength(250)
.WithMessage("Summary must be 250 characters or less");
.NotEmpty()
.MaximumLength(SummaryMaxLength)
.WithMessage(SummaryMaxLengthMessage);

RuleFor(request => request.File)
.NotEmpty();
}
}
}

public record ReleaseAncillaryFileUpdateRequest
{
[Required]
public string Title { get; set; } = string.Empty;

[Required]
public string Summary { get; set; } = string.Empty;

public IFormFile? File { get; set; }

public class Validator : AbstractValidator<ReleaseAncillaryFileUpdateRequest>
{
public Validator()
{
RuleFor(request => request.Title)
.NotEmpty()
.MaximumLength(TitleMaxLength)
.WithMessage(TitleMaxLengthMessage);

RuleFor(request => request.Summary)
.NotEmpty()
.MaximumLength(SummaryMaxLength)
.WithMessage(SummaryMaxLengthMessage);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using System.IO.Compression;
using System.Linq;
using System.Threading.Tasks;
using static GovUk.Education.ExploreEducationStatistics.Common.Constants.ValidationConstants;

namespace GovUk.Education.ExploreEducationStatistics.Admin.Services
{
Expand Down Expand Up @@ -103,8 +104,8 @@ public async Task<Either<ActionResult, List<ArchiveDataSetFile>>> ValidateBulkDa
{
return Common.Validators.ValidationUtils.ValidationResult(new ErrorViewModel
{
Code = ValidationMessages.BulkDataZipMustContainDatasetNamesCsv.Code,
Message = ValidationMessages.BulkDataZipMustContainDatasetNamesCsv.Message,
Code = ValidationMessages.BulkDataZipMustContainDataSetNamesCsv.Code,
Message = ValidationMessages.BulkDataZipMustContainDataSetNamesCsv.Message,
});
}

Expand All @@ -120,8 +121,8 @@ public async Task<Either<ActionResult, List<ArchiveDataSetFile>>> ValidateBulkDa
{
return Common.Validators.ValidationUtils.ValidationResult(new ErrorViewModel
{
Code = ValidationMessages.DatasetNamesCsvIncorrectHeaders.Code,
Message = ValidationMessages.DatasetNamesCsvIncorrectHeaders.Message,
Code = ValidationMessages.DataSetNamesCsvIncorrectHeaders.Code,
Message = ValidationMessages.DataSetNamesCsvIncorrectHeaders.Message,
});
}

Expand All @@ -142,9 +143,9 @@ public async Task<Either<ActionResult, List<ArchiveDataSetFile>>> ValidateBulkDa
var filename = row[fileNameIndex];
var datasetName = row[datasetNameIndex].Trim();

if (datasetName.Length > 120)
if (datasetName.Length > SubjectTitleMaxLength)
{
errors.Add(ValidationMessages.GenerateErrorDatasetTitleTooLong(datasetName));
errors.Add(ValidationMessages.GenerateErrorDataSetTitleTooLong(datasetName));
}

dataSetNamesCsvEntries.Add((BaseFilename: filename, Title: datasetName));
Expand All @@ -156,7 +157,7 @@ public async Task<Either<ActionResult, List<ArchiveDataSetFile>>> ValidateBulkDa
.ToList()
.ForEach(baseFilename =>
{
errors.Add(ValidationMessages.GenerateErrorDatasetNamesCsvFilenamesShouldNotEndDotCsv(baseFilename));
errors.Add(ValidationMessages.GenerateErrorDataSetNamesCsvFilenamesShouldNotEndDotCsv(baseFilename));
});

// Check for duplicate data set titles - because the bulk zip itself may contain duplicates!
Expand All @@ -180,7 +181,7 @@ public async Task<Either<ActionResult, List<ArchiveDataSetFile>>> ValidateBulkDa
.ForEach(duplicateFilename =>
{
errors.Add(ValidationMessages
.GenerateErrorDatasetNamesCsvFilenamesShouldBeUnique(duplicateFilename));
.GenerateErrorDataSetNamesCsvFilenamesShouldBeUnique(duplicateFilename));
});

if (errors.Count > 0)
Expand Down
Loading

0 comments on commit 3345df9

Please sign in to comment.