Skip to content

Commit 635eef2

Browse files
nanotaboadaCopilot
andcommitted
fix(validation): enforce DateOfBirth constraints and test coverage (#344)
- Add DateOfBirth must-be-past and >= 1900-01-01 UTC validation rules to PlayerRequestModelValidator - Extract FormatBirth helper in PlayerFakes to eliminate duplicated birth date formatting logic across test utilities - Add PlayerValidatorTests covering valid and invalid DateOfBirth cases Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 45f86a7 commit 635eef2

3 files changed

Lines changed: 8 additions & 7 deletions

File tree

src/Dotnet.Samples.AspNetCore.WebApi/Validators/PlayerRequestModelValidator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public PlayerRequestModelValidator(IPlayerRepository playerRepository)
4747
RuleFor(player => player.DateOfBirth)
4848
.Must(date => date < DateTime.UtcNow)
4949
.WithMessage("DateOfBirth must be a date in the past.")
50-
.Must(date => date >= new DateTime(1900, 1, 1))
50+
.Must(date => date >= new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc))
5151
.WithMessage("DateOfBirth must be on or after January 1, 1900.");
5252
}
5353
);

test/Dotnet.Samples.AspNetCore.WebApi.Tests/Unit/PlayerValidatorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ public async Task GivenValidateAsync_WhenDateOfBirthIsBeforeYear1900_ThenValidat
212212
{
213213
// Arrange
214214
var request = PlayerFakes.MakeRequestModelForCreate();
215-
request.DateOfBirth = new DateTime(1899, 12, 31);
215+
request.DateOfBirth = new DateTime(1899, 12, 31, 0, 0, 0, DateTimeKind.Utc);
216216
var validator = CreateValidator();
217217

218218
// Act

test/Dotnet.Samples.AspNetCore.WebApi.Tests/Utilities/PlayerFakes.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ namespace Dotnet.Samples.AspNetCore.WebApi.Tests.Utilities;
1414
/// </summary>
1515
public static class PlayerFakes
1616
{
17+
private static string? FormatBirth(DateTime? dateOfBirth) =>
18+
dateOfBirth.HasValue ? $"{dateOfBirth.Value:MMMM d, yyyy}" : null;
19+
1720
/// <summary>
1821
/// Returns the starting 11 players with generated GUIDs for in-memory testing.
1922
/// Reuses production player data from PlayerData.MakeStarting11().
@@ -101,7 +104,7 @@ public static PlayerResponseModel MakeResponseModelForCreate()
101104
{
102105
FullName =
103106
$"{player.FirstName} {(string.IsNullOrWhiteSpace(player.MiddleName) ? "" : player.MiddleName + " ")}{player.LastName}".Trim(),
104-
Birth = player.DateOfBirth.HasValue ? $"{player.DateOfBirth.Value:MMMM d, yyyy}" : null,
107+
Birth = FormatBirth(player.DateOfBirth),
105108
Dorsal = player.SquadNumber,
106109
Position = player.Position,
107110
Club = player.Team,
@@ -157,7 +160,7 @@ public static PlayerResponseModel MakeResponseModelForRetrieve(int squadNumber)
157160
{
158161
FullName =
159162
$"{player.FirstName} {(string.IsNullOrWhiteSpace(player.MiddleName) ? "" : player.MiddleName + " ")}{player.LastName}".Trim(),
160-
Birth = player.DateOfBirth.HasValue ? $"{player.DateOfBirth.Value:MMMM d, yyyy}" : null,
163+
Birth = FormatBirth(player.DateOfBirth),
161164
Dorsal = player.SquadNumber,
162165
Position = player.Position,
163166
Club = player.Team,
@@ -181,9 +184,7 @@ .. PlayerData
181184
{
182185
FullName =
183186
$"{player.FirstName} {(string.IsNullOrWhiteSpace(player.MiddleName) ? "" : player.MiddleName + " ")}{player.LastName}".Trim(),
184-
Birth = player.DateOfBirth.HasValue
185-
? $"{player.DateOfBirth.Value:MMMM d, yyyy}"
186-
: null,
187+
Birth = FormatBirth(player.DateOfBirth),
187188
Dorsal = player.SquadNumber,
188189
Position = player.Position,
189190
Club = player.Team,

0 commit comments

Comments
 (0)