HttpValidationProblemDetails helper#77
Merged
IeuanWalker merged 10 commits intomasterfrom Jan 26, 2026
Merged
Conversation
Introduced the ValidationErrors<T> class to manage and collect validation errors in ASP.NET Core minimal APIs. This class supports adding errors by property (via expressions or string keys), checking for errors, and converting them to standardized HTTP problem responses. Includes helper methods for property path extraction and example usage in exception handling. Also added an unrelated placeholder Class1.
Introduce /ValidationErrors endpoint returning standardized validation errors. Update OpenAPI spec and operation IDs. Add RequestModel for sample errors. Implement integration and unit tests for endpoint and ValidationErrors<T> class, covering error addition, nested properties, and problem details response.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR introduces a ValidationErrors<T> helper class to streamline the creation of validation error responses in ASP.NET Core Minimal APIs. The helper provides a fluent API for building HttpValidationProblemDetails responses with strongly-typed property path expressions, making it easier to return standardized validation errors from endpoints.
Changes:
- Added
ValidationErrors<T>class with fluent API for building validation error responses - Included comprehensive unit tests for the new helper class
- Added example endpoint demonstrating usage patterns
- Updated OpenAPI documentation snapshots to reflect the new example endpoint
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
src/IeuanWalker.MinimalApi.Endpoints/ValidationErrors.cs |
Core implementation of the ValidationErrors helper class with expression-based property path resolution |
tests/IeuanWalker.MinimalApi.Endpoints.Tests/ValidationErrorsTests.cs |
Comprehensive unit tests covering HasErrors, Add methods, expression handling, and response generation |
example/ExampleApi/Endpoints/Validation/GetValidationErrors/GetValidationErrorsEndpoint.cs |
Example endpoint demonstrating ValidationErrors usage with nested properties |
example/ExampleApi/Endpoints/Validation/GetValidationErrors/RequestModel.cs |
Request model for the example endpoint |
tests/ExampleApi.IntegrationTests/Endpoints/Validation/ValidationErrorsTests.cs |
Integration test verifying end-to-end behavior and proper HTTP response format |
tests/ExampleApi.IntegrationTests/ExpectedOpenApi.json |
Updated OpenAPI specification including the new endpoint and adjusted operation IDs |
tests/ExampleApi.IntegrationTests/Infrastructure/OpenApiTests.Validation.cs |
Updated OpenAPI validation test with new endpoint entry and sequential operation ID adjustments |
ValidationErrors.cs |
Extraneous template file that should be removed |
Add ValidateMessages to ensure at least one message is provided when adding errors in ValidationErrors<T>. Update both Add overloads to use this check. Add unit tests to confirm ArgumentException is thrown when no messages are supplied.
Enforce non-empty key in ValidationErrors<T>.Add by throwing ArgumentNullException for null, empty, or whitespace keys. Add theory-based unit tests to verify exception behavior.
Deleted commented-out code mapping exceptions to validation error responses for UPRN, easting, and northing fields. The main ValidationErrors<T> class remains unchanged.
Deleted Class1 and its constructor from ValidationErrors.cs, leaving the file empty. This cleans up unused code.
Added detailed XML documentation comments to the ValidationErrors<T> class and its public methods, describing their purpose, parameters, return values, and exceptions. These changes improve code readability and developer guidance without affecting functionality.
Refactor GetPropertyPath to safely handle indexer expressions by checking for direct member access before casting. Now throws a clear exception for unsupported indexer usage, preventing invalid casts and improving error messages.
Enhanced ValidationErrors<T> to correctly generate property paths for nested indexers and collection items. Added comprehensive unit tests for nested indexers, object properties, and list items. Expanded test models to support new test scenarios.
Previously, assembly loading for system, ASP.NET Core, and FluentValidation references was wrapped in try-catch blocks to silently skip missing assemblies. Now, exceptions will be thrown if an assembly fails to load, making errors more visible during test setup.
SummarySummary
CoverageExampleApi - 97%
IeuanWalker.MinimalApi.Endpoints - 87.2%
IeuanWalker.MinimalApi.Endpoints.Generator - 95.9%
|
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.
Easily add/ builder errors and return the standard
ProblemHttpResult