AspNetCore.Docs
AspNetCore.Docs copied to clipboard
Document validation support for minimal APIs
In .NET 10 Preivew 3, we're introducing support for minimal API validation support.
I've created a sample repo and some notes on the implementation to serve as source material here: https://github.com/captainsafia/minapi-validation-support.
One thing to note is that this feature has a user-facing component for developers who are building minimal APIs and a framework component for developers who are building frameworks or their own validation libraries.
I think our first priority is developers building APIs since that's where the biggest product gap it exists.
@captainsafia ... I'll move this one as well. Dan would like to track each preview's work items on one issue for visibility to make sure nothing falls through the cracks.
@guardrex Sure -- I surfaced this here since we'll want to track documenting this permanently as part of the minimal API docs before we GA. This is in addition to the point-in-time stuff for Preview 3 what's new.
@wadepickett use Ctrl+Alt+G on the appropriate doc to add meta-data to this issue. Update the bottom of this comment with 2 check boxes, this issue and the WN.3 issue.
How to implement custom validation error response?
I gave it a try with preview 4. Validation only kicks in when endpoints are in the same assembly. Is there any workaround for minimal endpoints which are in a different project and properly identified by openApi and showing in swagger UI, etc.?
Example:
Assuming service.AddValidation() and <InterceptorsNamespaces>$(InterceptorsNamespaces);Microsoft.AspNetCore.Http.Validation.Generated</InterceptorsNamespaces> in main project's csproj.
When having this in main project (where Program.cs is) it works as expected.
public class SampleRequest
{
[Required]
public string? Reference { get; init; }
}
app.MapPost("/sample", (
[FromBody] SampleRequest sampleRequest,
CancellationToken cancellationToken)
=> TypedResults.Ok(sampleRequest));
When moving endpoint to another assembly, for example with an extension method, validation does not kick in.
In Program.cs
app.MapSampleEndpoint();
In another project
public static class SampleEndpoint
{
public static void MapSampleEndpoint(this IEndpointRouteBuilder app)
{
app
.MapPost("/sample", (
[FromBody] SampleRequest sampleRequest,
CancellationToken cancellationToken)
=> TypedResults.Ok(sampleRequest));
}
}
I wonder how we could extend it and resolve validators to be used manually, for example:
public class CustomerBusinessService(
ICustomerRepository repository,
IValidator<Customer> validator,
ILogger<CustomerBusinessService logger)
{
}
Is there any workaround for the validation to work when endpoints and request/response models live in a different assembly? I've tried with .NET 10 RC but unfortunately the problem remains.
https://github.com/dotnet/AspNetCore.Docs/issues/35090#issuecomment-2893866282
I read somewhere about this limitation due to source generator not finding models in different assemblies. But not sure if there's a workaround and documentation does not mention it.