Skip to content

Commit ae22b59

Browse files
gunndabadJames Gunn
authored andcommitted
Use FluentValidation for Add alert journey
1 parent a38866d commit ae22b59

File tree

38 files changed

+348
-73
lines changed

38 files changed

+348
-73
lines changed

TeachingRecordSystem/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
<PackageVersion Include="EFCore.NamingConventions" Version="9.0.0" />
2525
<PackageVersion Include="EntityFrameworkCore.Projectables" Version="4.0.0" />
2626
<PackageVersion Include="Faker.Net" Version="2.0.154" />
27+
<PackageVersion Include="FluentValidation" Version="11.11.0" />
2728
<PackageVersion Include="FluentValidation.AspNetCore" Version="11.3.0" />
2829
<PackageVersion Include="Google.Cloud.Storage.V1" Version="4.10.0" />
2930
<PackageVersion Include="GovUk.OneLogin.AspNetCore" Version="0.4.0" />

TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240307/Validators/CreateTrnRequestRequestValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public CreateTrnRequestRequestValidator(IClock clock)
3232
{
3333
if (value >= DateOnly.FromDateTime(clock.UtcNow) || value < new DateOnly(1940, 1, 1))
3434
{
35-
ctx.AddFailure(ctx.PropertyName, StringResources.ErrorMessages_BirthDateIsOutOfRange);
35+
ctx.AddFailure(ctx.PropertyPath, StringResources.ErrorMessages_BirthDateIsOutOfRange);
3636
}
3737
});
3838

@@ -45,7 +45,7 @@ public CreateTrnRequestRequestValidator(IClock clock)
4545
{
4646
if (!string.IsNullOrEmpty(value) && !NationalInsuranceNumber.TryParse(value, out _))
4747
{
48-
ctx.AddFailure(ctx.PropertyName, StringResources.ErrorMessages_EnterNinoNumberInCorrectFormat);
48+
ctx.AddFailure(ctx.PropertyPath, StringResources.ErrorMessages_EnterNinoNumberInCorrectFormat);
4949
}
5050
});
5151
}

TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20240606/Validators/CreateTrnRequestRequestValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public CreateTrnRequestRequestValidator(IClock clock)
3232
{
3333
if (value >= DateOnly.FromDateTime(clock.UtcNow) || value < new DateOnly(1940, 1, 1))
3434
{
35-
ctx.AddFailure(ctx.PropertyName, StringResources.ErrorMessages_BirthDateIsOutOfRange);
35+
ctx.AddFailure(ctx.PropertyPath, StringResources.ErrorMessages_BirthDateIsOutOfRange);
3636
}
3737
});
3838

@@ -47,7 +47,7 @@ public CreateTrnRequestRequestValidator(IClock clock)
4747
{
4848
if (!string.IsNullOrEmpty(value) && !NationalInsuranceNumber.TryParse(value, out _))
4949
{
50-
ctx.AddFailure(ctx.PropertyName, StringResources.ErrorMessages_EnterNinoNumberInCorrectFormat);
50+
ctx.AddFailure(ctx.PropertyPath, StringResources.ErrorMessages_EnterNinoNumberInCorrectFormat);
5151
}
5252
});
5353
}

TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20250203/Validators/CreateTrnRequestRequestValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public CreateTrnRequestRequestValidator(IClock clock)
3131
{
3232
if (value >= DateOnly.FromDateTime(clock.UtcNow) || value < new DateOnly(1940, 1, 1))
3333
{
34-
ctx.AddFailure(ctx.PropertyName, StringResources.ErrorMessages_BirthDateIsOutOfRange);
34+
ctx.AddFailure(ctx.PropertyPath, StringResources.ErrorMessages_BirthDateIsOutOfRange);
3535
}
3636
});
3737

@@ -46,7 +46,7 @@ public CreateTrnRequestRequestValidator(IClock clock)
4646
{
4747
if (!string.IsNullOrEmpty(value) && !NationalInsuranceNumber.TryParse(value, out _))
4848
{
49-
ctx.AddFailure(ctx.PropertyName, StringResources.ErrorMessages_EnterNinoNumberInCorrectFormat);
49+
ctx.AddFailure(ctx.PropertyPath, StringResources.ErrorMessages_EnterNinoNumberInCorrectFormat);
5050
}
5151
});
5252

TeachingRecordSystem/src/TeachingRecordSystem.Api/V3/V20250425/Validators/CreateTrnRequestRequestValidator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public CreateTrnRequestRequestValidator(IClock clock)
3131
{
3232
if (value >= clock.Today || value < new DateOnly(1940, 1, 1))
3333
{
34-
ctx.AddFailure(ctx.PropertyName, StringResources.ErrorMessages_BirthDateIsOutOfRange);
34+
ctx.AddFailure(ctx.PropertyPath, StringResources.ErrorMessages_BirthDateIsOutOfRange);
3535
}
3636
});
3737

@@ -46,7 +46,7 @@ public CreateTrnRequestRequestValidator(IClock clock)
4646
{
4747
if (!string.IsNullOrEmpty(value) && !NationalInsuranceNumber.TryParse(value, out _))
4848
{
49-
ctx.AddFailure(ctx.PropertyName, StringResources.ErrorMessages_EnterNinoNumberInCorrectFormat);
49+
ctx.AddFailure(ctx.PropertyPath, StringResources.ErrorMessages_EnterNinoNumberInCorrectFormat);
5050
}
5151
});
5252

TeachingRecordSystem/src/TeachingRecordSystem.Api/packages.lock.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,6 @@
175175
"resolved": "4.0.0",
176176
"contentHash": "QYeAIEmNEpROosThGtU/t3yZJoCcAC9kxhfHnHAp2PmVIFIajaHcuo79CCeEiOxo2zm+mS3wfIIj6d00id7/cQ=="
177177
},
178-
"FluentValidation": {
179-
"type": "Transitive",
180-
"resolved": "11.5.1",
181-
"contentHash": "0h1Q5lNOLLyYTWMJmyNoMqhY4CBRvvUWvJP1R4F2CnmmzuWwvB0A8aVmw5+lOuwYnwUwCRrdeMLbc81F38ahNQ=="
182-
},
183178
"FluentValidation.DependencyInjectionExtensions": {
184179
"type": "Transitive",
185180
"resolved": "11.5.1",
@@ -2272,6 +2267,7 @@
22722267
"DistributedLock.FileSystem": "[1.0.2, )",
22732268
"EFCore.NamingConventions": "[9.0.0, )",
22742269
"EntityFrameworkCore.Projectables": "[4.0.0, )",
2270+
"FluentValidation": "[11.11.0, )",
22752271
"Google.Cloud.Storage.V1": "[4.10.0, )",
22762272
"GovukNotify": "[6.1.0, )",
22772273
"Hangfire.Core": "[1.8.21, )",
@@ -2451,6 +2447,12 @@
24512447
"Microsoft.EntityFrameworkCore": "8.0.0"
24522448
}
24532449
},
2450+
"FluentValidation": {
2451+
"type": "CentralTransitive",
2452+
"requested": "[11.11.0, )",
2453+
"resolved": "11.11.0",
2454+
"contentHash": "cyIVdQBwSipxWG8MA3Rqox7iNbUNUTK5bfJi9tIdm4CAfH71Oo5ABLP4/QyrUwuakqpUEPGtE43BDddvEehuYw=="
2455+
},
24542456
"Google.Cloud.Storage.V1": {
24552457
"type": "CentralTransitive",
24562458
"requested": "[4.10.0, )",

TeachingRecordSystem/src/TeachingRecordSystem.AuthorizeAccess/packages.lock.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2507,6 +2507,7 @@
25072507
"DistributedLock.FileSystem": "[1.0.2, )",
25082508
"EFCore.NamingConventions": "[9.0.0, )",
25092509
"EntityFrameworkCore.Projectables": "[4.0.0, )",
2510+
"FluentValidation": "[11.11.0, )",
25102511
"Google.Cloud.Storage.V1": "[4.10.0, )",
25112512
"GovukNotify": "[6.1.0, )",
25122513
"Hangfire.Core": "[1.8.21, )",
@@ -2677,6 +2678,12 @@
26772678
"Microsoft.EntityFrameworkCore": "8.0.0"
26782679
}
26792680
},
2681+
"FluentValidation": {
2682+
"type": "CentralTransitive",
2683+
"requested": "[11.11.0, )",
2684+
"resolved": "11.11.0",
2685+
"contentHash": "cyIVdQBwSipxWG8MA3Rqox7iNbUNUTK5bfJi9tIdm4CAfH71Oo5ABLP4/QyrUwuakqpUEPGtE43BDddvEehuYw=="
2686+
},
26802687
"Google.Cloud.Storage.V1": {
26812688
"type": "CentralTransitive",
26822689
"requested": "[4.10.0, )",

TeachingRecordSystem/src/TeachingRecordSystem.Cli/packages.lock.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,7 @@
20152015
"DistributedLock.FileSystem": "[1.0.2, )",
20162016
"EFCore.NamingConventions": "[9.0.0, )",
20172017
"EntityFrameworkCore.Projectables": "[4.0.0, )",
2018+
"FluentValidation": "[11.11.0, )",
20182019
"Google.Cloud.Storage.V1": "[4.10.0, )",
20192020
"GovukNotify": "[6.1.0, )",
20202021
"Hangfire.Core": "[1.8.21, )",
@@ -2150,6 +2151,12 @@
21502151
"Microsoft.EntityFrameworkCore": "8.0.0"
21512152
}
21522153
},
2154+
"FluentValidation": {
2155+
"type": "CentralTransitive",
2156+
"requested": "[11.11.0, )",
2157+
"resolved": "11.11.0",
2158+
"contentHash": "cyIVdQBwSipxWG8MA3Rqox7iNbUNUTK5bfJi9tIdm4CAfH71Oo5ABLP4/QyrUwuakqpUEPGtE43BDddvEehuYw=="
2159+
},
21532160
"Google.Cloud.Storage.V1": {
21542161
"type": "CentralTransitive",
21552162
"requested": "[4.10.0, )",

TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Models/Alert.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using EntityFrameworkCore.Projectables;
2+
using FluentValidation;
23
using TeachingRecordSystem.Core.Events.Legacy;
34

45
namespace TeachingRecordSystem.Core.DataStore.Postgres.Models;
@@ -10,6 +11,8 @@ public class Alert
1011
public const string PersonIdIndexName = "ix_alerts_person_id";
1112
public const string PersonForeignKeyName = "fk_alerts_person";
1213

14+
public const int DetailsMaxLength = 4000;
15+
1316
public required Guid AlertId { get; init; }
1417
public AlertType? AlertType { get; }
1518
public required Guid AlertTypeId { get; init; }
@@ -137,3 +140,41 @@ public void Update(
137140
};
138141
}
139142
}
143+
144+
public static class AlertValidationExtensions
145+
{
146+
public static IRuleBuilderOptions<T, string?> AlertDetails<T>(
147+
this IRuleBuilder<T, string?> ruleBuilder,
148+
Func<int, string> maxLengthMessage)
149+
{
150+
return ruleBuilder
151+
.MaximumLength(Alert.DetailsMaxLength).WithMessage(maxLengthMessage(Alert.DetailsMaxLength));
152+
}
153+
154+
public static IRuleBuilderOptions<T, string?> AlertLink<T>(
155+
this IRuleBuilder<T, string?> ruleBuilder,
156+
string invalidUrlMessage)
157+
{
158+
return ruleBuilder
159+
.Must(link => TrsUriHelper.TryCreateWebsiteUri(link, out _)).WithMessage(invalidUrlMessage);
160+
}
161+
162+
public static IRuleBuilderOptions<T, DateOnly?> AlertStartDate<T>(
163+
this IRuleBuilder<T, DateOnly?> ruleBuilder,
164+
DateOnly today,
165+
string requiredMessage,
166+
string dateInFutureMessage)
167+
{
168+
return ruleBuilder
169+
.NotNull().WithMessage(requiredMessage)
170+
.LessThanOrEqualTo(today).WithMessage(dateInFutureMessage);
171+
}
172+
173+
public static IRuleBuilderOptions<T, Guid?> AlertType<T>(
174+
this IRuleBuilder<T, Guid?> ruleBuilder,
175+
string requiredMessage)
176+
{
177+
return ruleBuilder
178+
.NotNull().WithMessage(requiredMessage);
179+
}
180+
}

TeachingRecordSystem/src/TeachingRecordSystem.Core/TeachingRecordSystem.Core.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@
283283
<PackageReference Include="DistributedLock.FileSystem" />
284284
<PackageReference Include="EFCore.NamingConventions" />
285285
<PackageReference Include="EntityFrameworkCore.Projectables" />
286+
<PackageReference Include="FluentValidation" />
286287
<PackageReference Include="Google.Cloud.Storage.V1" />
287288
<PackageReference Include="GovukNotify" />
288289
<PackageReference Include="Hangfire.Core" />

0 commit comments

Comments
 (0)