-
Notifications
You must be signed in to change notification settings - Fork 24.7k
Enhance MEV documentation #37320
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
Youssef1313
wants to merge
2
commits into
main
Choose a base branch
from
dev/ygerges/val
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Enhance MEV documentation #37320
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| --- | ||
| title: Validation in Minimal API apps | ||
| author: Youssef1313 | ||
| description: Use Microsoft.Extensions.Validation in Minimal API apps to validate API models. | ||
| ms.author: ygerges | ||
| ms.date: 07/08/2026 | ||
| monikerRange: '>= aspnetcore-10.0' | ||
| uid: fundamentals/minimal-apis/validation | ||
|
|
||
| # customer intent: As an ASP.NET developer, I want to have automatic validation of models in Minimal API parameters. | ||
| --- | ||
|
|
||
| # Validation in Minimal API apps | ||
|
|
||
| In .NET 10, Microsoft.Extensions.Validation was introduced to support complex model validation. | ||
|
|
||
| To enable validation, call `AddValidation` on the `IServiceCollection` instance in the web application entry point. | ||
|
|
||
| ```csharp | ||
| builder.Services.AddValidation(); | ||
| ``` | ||
|
|
||
| ## Parameter validation | ||
|
|
||
| Parameter validation is the first step in the validation pipeline in minimal API endpoints. It involves the following steps: | ||
|
|
||
| 1. Validate `ValidationAttribute`s applied to the minimal API parameter. | ||
| 1. If the parameter type is `IEnumerable`, validate the type for all non-null elements. Otherwise, validate the type for the value. | ||
|
|
||
| > [!NOTE] | ||
| > There is a known limitation currently that nullable value types declared as minimal API parameters don't get validated. | ||
| > For more information, see [dotnet/aspnetcore#67033](https://github.com/dotnet/aspnetcore/issues/67033). | ||
|
|
||
| If the minimal API parameter type is `IEnumerable`, a type validation for all non-null elements happens. Otherwise, a single type validation for the value happens. | ||
|
|
||
| ## Type validation | ||
|
|
||
| Type validation is the next step after parameter validation. It involves the following steps: | ||
|
|
||
| 1. Validate properties on the type. If any errors are found, the validation process stops. | ||
| 1. Validate type-level `ValidationAttribute`s. If any errors are found, the validation process stops. | ||
| 1. Validate `IValidatableObject`, if it's implemented. | ||
|
|
||
| ## Property validation | ||
|
|
||
| Property validation happens as part of the type validation as explained in the previous section. It involves the following steps: | ||
|
|
||
| 1. Validate `ValidationAttribute`s applied on the property. | ||
| 1. If the property value is `IEnumerable`, perform type validation for all non-null elements. Otherwise, perform a single type validation for the value. | ||
|
|
||
| ## Explicit validation skipping | ||
|
|
||
| When needed, you can skip validation for a specific parameter, type, or property by applying the `SkipValidationAttribute`. | ||
|
|
||
| ## Force-generate validatable type information | ||
|
|
||
| The Microsoft.Extensions.Validation package works via a Roslyn source generator that detects the object graph and types for minimal API endpoint parameters. | ||
|
|
||
| In some cases, not all types that will be part of the object graph can be determined at compile time. In these cases, you can force the source generator to consider a type for validation by applying `ValidatableTypeAttribute` to that type. | ||
|
|
||
| ## Async validation support | ||
|
|
||
| Starting in .NET 11, Microsoft.Extensions.Validation supports async validation. You can apply custom implementations of `AsyncValidationAttribute` to parameters, types, or properties, and they will be called asynchronously. In addition, types can implement `IAsyncValidatableObject` as well. | ||
|
|
||
| > [!IMPORTANT] | ||
| > Both `IAsyncValidatableObject` and `AsyncValidationAttribute` require you to implement the validation logic synchronously **and** asynchronously. | ||
| > For minimal API validation using `Microsoft.Extensions.Validation`, the framework always calls the async path and never the sync path. | ||
| > The sync and async paths are never intended to both be called together. If your implementation can't support the sync path, throw `InvalidOperationException`. | ||
|
|
||
| When validating properties on a type, we start all validation tasks concurrently. Similarly, when we validate `IEnumerable`s, we start validation tasks for elements concurrently. | ||
This file was deleted.
Oops, something went wrong.
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
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.