Skip to content

Commit

Permalink
Merge pull request #5450 from dfe-analytical-services/EES-4122
Browse files Browse the repository at this point in the history
EES-5047: Implement character limits
  • Loading branch information
tomjonesdev authored Dec 18, 2024
2 parents f16a2c0 + dd4f0b8 commit f7d94d1
Show file tree
Hide file tree
Showing 19 changed files with 481 additions and 327 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 @@ -107,7 +107,9 @@ public async Task<ActionResult<List<DataFileInfo>>> ReorderDataFiles(Guid releas
[RequestFormLimits(ValueLengthLimit = int.MaxValue, MultipartBodyLengthLimit = int.MaxValue)]
public async Task<ActionResult<DataFileInfo>> UploadDataSet(Guid releaseVersionId,
[FromQuery(Name = "replacingFileId")] Guid? replacingFileId,
[FromQuery(Name = "title")] string title,
[FromQuery(Name = "title")]
[MaxLength(120)]
string title,
IFormFile file,
IFormFile metaFile)
{
Expand All @@ -125,7 +127,9 @@ public async Task<ActionResult<DataFileInfo>> UploadDataSet(Guid releaseVersionI
[RequestFormLimits(ValueLengthLimit = int.MaxValue, MultipartBodyLengthLimit = int.MaxValue)]
public async Task<ActionResult<DataFileInfo>> UploadDataSetAsZip(Guid releaseVersionId,
[FromQuery(Name = "replacingFileId")] Guid? replacingFileId,
[FromQuery(Name = "title")] string title,
[FromQuery(Name = "title")]
[MaxLength(120)]
string title,
IFormFile zipFile)
{
return await _releaseDataFileService
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#nullable enable
using System.Collections.Generic;
using FluentValidation;
using GovUk.Education.ExploreEducationStatistics.Common.Model.Chart;
using GovUk.Education.ExploreEducationStatistics.Common.Model.Data;
using GovUk.Education.ExploreEducationStatistics.Common.Requests;
using System.Collections.Generic;

namespace GovUk.Education.ExploreEducationStatistics.Admin.Requests;

Expand All @@ -17,7 +17,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 @@ -26,7 +26,8 @@ public class Validator : AbstractValidator<DataBlockCreateRequest>
public Validator()
{
RuleFor(request => request.Heading)
.NotEmpty();
.NotEmpty()
.MaximumLength(120);

RuleFor(request => request.Name)
.NotEmpty();
Expand All @@ -47,7 +48,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 @@ -56,7 +57,8 @@ public class Validator : AbstractValidator<DataBlockUpdateRequest>
public Validator()
{
RuleFor(request => request.Heading)
.NotEmpty();
.NotEmpty()
.MaximumLength(120);

RuleFor(request => request.Name)
.NotEmpty();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,45 @@
#nullable enable
#nullable enable
using FluentValidation;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

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]
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(250);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,26 +1,49 @@
#nullable enable
using FluentValidation;
using System;
using System.ComponentModel.DataAnnotations;

namespace GovUk.Education.ExploreEducationStatistics.Admin.Requests;

public record FeaturedTableCreateRequest
{
[Required]
public string Name { get; init; } = string.Empty;

[Required]
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(120);

RuleFor(request => request.Description)
.NotEmpty()
.MaximumLength(200);
}
}
}

public record FeaturedTableUpdateRequest
{
[Required]
public string Name { get; init; } = string.Empty;

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

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

RuleFor(request => request.Description)
.NotEmpty()
.MaximumLength(200);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#nullable enable
using System.ComponentModel.DataAnnotations;
using FluentValidation;
using Microsoft.AspNetCore.Http;

namespace GovUk.Education.ExploreEducationStatistics.Admin.Requests;
Expand All @@ -9,26 +9,62 @@ public record ReleaseDataFileUpdateRequest
public string? Title { get; set; }

public string? Summary { get; set; }

public class Validator : AbstractValidator<ReleaseDataFileUpdateRequest>
{
public Validator()
{
RuleFor(request => request.Title)
.MaximumLength(120);
}
}
}

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)
.NotEmpty()
.MaximumLength(120);

RuleFor(request => request.Summary)
.NotEmpty()
.MaximumLength(250);

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(120);

RuleFor(request => request.Summary)
.NotEmpty()
.MaximumLength(250);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,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 +120,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 @@ -134,24 +134,29 @@ public async Task<Either<ActionResult, List<ArchiveDataSetFile>>> ValidateBulkDa
rows = await CsvUtils.GetCsvRows(dataSetNamesStream);
}

var errors = new List<ErrorViewModel>();

var dataSetNamesCsvEntries = new List<(string BaseFilename, string Title)>();
foreach (var row in rows)
{
var filename = row[fileNameIndex];
var datasetName = row[datasetNameIndex].Trim();

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

dataSetNamesCsvEntries.Add((BaseFilename: filename, Title: datasetName));
}

var errors = new List<ErrorViewModel>();

dataSetNamesCsvEntries
.Select(entry => entry.BaseFilename)
.Where(baseFilename => baseFilename.EndsWith(".csv"))
.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 @@ -175,7 +180,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 f7d94d1

Please sign in to comment.