question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Design validation for Minimal APIs

See original GitHub issue

Idea:

// Step 5: Input validation
app.MapPost("/todos", async (Validated<Todo> inputTodo, TodoDb db) =>
{
    var (todo, isValid) = inputTodo;
    if (!isValid) return Problem(inputTodo);

    db.Todos.Add(todo);
    await db.SaveChangesAsync();

    return CreatedAt(routes.GetTodo.Url(todo.Id), todo);
};

From: https://github.com/dotnet/aspnetcore/issues/30580

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:22 (22 by maintainers)

github_iconTop GitHub Comments

2reactions
davidfowlcommented, May 26, 2022

In .NET 7 you’ll be able to write a filter that can run validation on all the parameters trivially. In fact @DamianEdwards, you should add that filter to the Minimal.Extensions.

I was convinced, In your opinion, if we change MVC validations In a way that only validate IValidatable objects and optimize this changes in MinimalApis Is this possible?

MVC already has a validation system so we wouldn’t change it. It does what it does. As for minimal APIs, doing it by default means we need to have an abstraction so others can plug in (for things like fluent validation etc) or a way to turn it off. The way I see it the extensibility point for plugging in validation is filters, so you can plug in to do it automagically.

2reactions
DamianEdwardscommented, Mar 15, 2022

Doing some “customer research” https://twitter.com/DamianEdwards/status/1486871181252833284

Result: image

Read more comments on GitHub >

github_iconTop Results From Across the Web

Minimal API Validation with FluentValidation
DependencyInjectionExtensions . This package holds the extension methods that allow you to register all validators in a Minimal APIs web ...
Read more >
Use model validation in minimal APIs in ASP.NET Core 6
There is no built-in support for model validation in minimal APIs (unlike in ASP.NET Core MVC and Razor Pages). Hence, you will need...
Read more >
FluentValidation in minimal APIs in ASP.NET Core 7.0
In this article, we will learn how to implement FluentValidation to our model classes in ASP.NET core 7 with minimal APIs.
Read more >
Using ASP.NET Core 7 Minimal APIs: Request Filters ... - InfoQ
Minimal APIs can use bespoke middleware that allows validation of the request and the capability to execute code before or after processing ...
Read more >
Tutorial: Create a minimal API with ASP.NET Core
Learn how to build a minimal API with ASP.NET Core.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found