Add validation for god names in GodInput model and endpoint#5
Open
Copilot wants to merge 3 commits into
Open
Conversation
Agent-Logs-Url: https://github.com/moller2866/MythAPI/sessions/42d6471d-6f29-4a9e-b643-5313f4e8965a Co-authored-by: moller2866 <83021349+moller2866@users.noreply.github.com>
Agent-Logs-Url: https://github.com/moller2866/MythAPI/sessions/42d6471d-6f29-4a9e-b643-5313f4e8965a Co-authored-by: moller2866 <83021349+moller2866@users.noreply.github.com>
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
There was a problem hiding this comment.
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
AddOrUpdateGodsendpoint to validate each item and returnResults.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 on lines
+30
to
+34
| foreach (var result in validationResults) | ||
| { | ||
| var key = $"[{i}].{result.MemberNames.FirstOrDefault() ?? "Unknown"}"; | ||
| errors[key] = [result.ErrorMessage ?? "Invalid value."]; | ||
| } |
Comment on lines
+23
to
+25
|
|
||
| for (var i = 0; i < gods.Count; i++) | ||
| { |
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)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No validation was enforced on god name inputs, allowing empty, excessively long, or malformed values to reach the database.
Changes
GodInputmodel — data annotation constraintsName:[Required],[MinLength(1)],[MaxLength(100)],[RegularExpression(@"^[\p{L}\s'\-\.]+$")]— letters (Unicode), spaces, hyphens, apostrophes, dots onlyDescription:[Required],[MinLength(1)],[MaxLength(500)]MythologyId:[Range(1, int.MaxValue)]AddOrUpdateGodsendpoint — explicit validation with structured error responseReplaced the one-liner pass-through with validation via
Validator.TryValidateObject, returningResults.ValidationProblem(HTTP 400) with per-field, per-index error keys on failure:Tests
MythologyId, empty description