Skip to content

Add validation for god names in GodInput model and endpoint#5

Open
Copilot wants to merge 3 commits into
mainfrom
copilot/add-validation-for-god-names
Open

Add validation for god names in GodInput model and endpoint#5
Copilot wants to merge 3 commits into
mainfrom
copilot/add-validation-for-god-names

Conversation

Copilot AI commented May 13, 2026

Copy link
Copy Markdown

No validation was enforced on god name inputs, allowing empty, excessively long, or malformed values to reach the database.

Changes

GodInput model — data annotation constraints

  • Name: [Required], [MinLength(1)], [MaxLength(100)], [RegularExpression(@"^[\p{L}\s'\-\.]+$")] — letters (Unicode), spaces, hyphens, apostrophes, dots only
  • Description: [Required], [MinLength(1)], [MaxLength(500)]
  • MythologyId: [Range(1, int.MaxValue)]

AddOrUpdateGods endpoint — explicit validation with structured error response

Replaced the one-liner pass-through with validation via Validator.TryValidateObject, returning Results.ValidationProblem (HTTP 400) with per-field, per-index error keys on failure:

// POST /api/v1/gods with [{ "name": "Zeus123", "mythologyId": 1, "description": "..." }]
// 400 Bad Request:
{
  "errors": {
    "[0].Name": ["Name may only contain letters, spaces, hyphens, apostrophes, and dots."]
  }
}

Tests

  • Unit: 6 new cases covering empty name, name too long, invalid characters, valid name with hyphen/apostrophe, invalid MythologyId, empty description
  • Integration: 4 new cases verifying HTTP 400 for invalid payloads and HTTP 200 for a valid god submission

Copilot AI linked an issue May 13, 2026 that may be closed by this pull request
Copilot AI changed the title [WIP] Add validation for god names in application Add validation for god names in GodInput model and endpoint May 13, 2026
Copilot AI requested a review from moller2866 May 13, 2026 09:13
@moller2866 moller2866 requested a review from Copilot May 13, 2026 09:15
@moller2866 moller2866 marked this pull request as ready for review May 13, 2026 09:15

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds server-side validation for GodInput fields to prevent empty/overlong/malformed god data from reaching the database, and updates the /api/v1/gods POST endpoint to return structured validation errors (HTTP 400) when inputs are invalid.

Changes:

  • Added data-annotation constraints to GodInput (Name, Description, MythologyId).
  • Updated AddOrUpdateGods endpoint to validate each item and return Results.ValidationProblem(...) on failure.
  • Added/updated unit and integration tests for invalid and valid POST payloads.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 5 comments.

File Description
src/Gods/Models/God.cs Adds data-annotation validation rules to GodInput.
src/Endpoints/v1/Gods.cs Validates GodInput list items and returns a validation problem result on errors.
tests/UnitTests/GodEndpointsTests.cs Updates unit tests for new IResult return type and adds invalid-input cases.
tests/IntegrationTests/GodsEndpointTests.cs Adds integration coverage for POSTing valid/invalid gods.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread src/Endpoints/v1/Gods.cs
Comment on lines +30 to +34
foreach (var result in validationResults)
{
var key = $"[{i}].{result.MemberNames.FirstOrDefault() ?? "Unknown"}";
errors[key] = [result.ErrorMessage ?? "Invalid value."];
}
Comment thread src/Endpoints/v1/Gods.cs
Comment on lines +23 to +25

for (var i = 0; i < gods.Count; i++)
{
Comment thread src/Gods/Models/God.cs
Comment on lines +8 to 12
[Required(ErrorMessage = "Name is required.")]
[MinLength(1, ErrorMessage = "Name must not be empty.")]
[MaxLength(100, ErrorMessage = "Name must not exceed 100 characters.")]
[RegularExpression(@"^[\p{L}\s'\-\.]+$", ErrorMessage = "Name may only contain letters, spaces, hyphens, apostrophes, and dots.")]
public string Name { get; set; } = null!;
Comment on lines +71 to +75
var result = await Gods.AddOrUpdateGods(godInputs, _mockRepository.Object);

Assert.That(result, Is.InstanceOf<IResult>());
// Repository should not be called when validation fails
_mockRepository.Verify(repo => repo.AddOrUpdateGods(It.IsAny<List<GodInput>>()), Times.Never);
Comment on lines +93 to +107
[Test]
public async Task AddOrUpdateGods_EmptyName_ShouldReturnBadRequest()
{
// Arrange
var godInputs = new List<GodInput>
{
new GodInput { Name = "", MythologyId = 1, Description = "Some description." }
};

// Act
var response = await _httpClient.PostAsJsonAsync("/api/v1/gods", godInputs);

// Assert
Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.BadRequest));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add validation for god names in application

3 participants