AspNetCore.Docs icon indicating copy to clipboard operation
AspNetCore.Docs copied to clipboard

Document validation support for minimal APIs

Open captainsafia opened this issue 8 months ago • 7 comments

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 avatar Mar 28 '25 23:03 captainsafia

@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 avatar Mar 28 '25 23:03 guardrex

@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.

captainsafia avatar Mar 28 '25 23:03 captainsafia

@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.

Rick-Anderson avatar Apr 09 '25 21:04 Rick-Anderson

How to implement custom validation error response?

wuuer avatar May 05 '25 02:05 wuuer

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));
    }
}

diegosasw avatar May 20 '25 10:05 diegosasw

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)
{
}

weitzhandler avatar Jul 17 '25 14:07 weitzhandler

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.

diegosasw avatar Nov 07 '25 17:11 diegosasw