Skip to content

Document validation support for minimal APIs #35090

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
captainsafia opened this issue Mar 28, 2025 · 5 comments
Open

Document validation support for minimal APIs #35090

captainsafia opened this issue Mar 28, 2025 · 5 comments
Assignees
Labels

Comments

@captainsafia
Copy link
Member

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.

@guardrex
Copy link
Collaborator

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

@captainsafia
Copy link
Member Author

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

@Rick-Anderson
Copy link
Contributor

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

@wuuer
Copy link

wuuer commented May 5, 2025

How to implement custom validation error response?

@diegosasw
Copy link

diegosasw commented May 20, 2025

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants