Skip to content

Commit 0a99802

Browse files
authored
WN .NET 10 Prev 3: Validation Support Minimal API (#35188)
* WN .NET 10 Prev 3: Validation Support Minimal API * Added include to What's New topic for .NET 10 Preview 3 * Update with correct links * Format link for attribute * Correct DataAnnotations link * Add review suggestions, remove future tense and lines * Minor edit * Remove line breaks * fixed line break
1 parent 4eea63b commit 0a99802

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

Diff for: aspnetcore/release-notes/aspnetcore-10.0.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ This section describes new features for minimal APIs.
3737

3838
[!INCLUDE[](~/release-notes/aspnetcore-10/includes/MinApiEmptyStringInFormPost.md)]
3939

40+
[!INCLUDE[](~/release-notes/aspnetcore-10/includes/ValidationSupportMinAPI.md)]
41+
4042
[!INCLUDE[](~/release-notes/aspnetcore-10/includes/sse.md)]
4143

4244
## OpenAPI
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,33 @@
11
### Validation support in Minimal APIs
22

3-
<!-- https://github.com/captainsafia/minapi-validation-support -->
3+
Support for validation in Minimal APIs is now available. This feature allows you to request validation of data sent to your API endpoints. Enabling validation allows the ASP.NET Core runtime to perform any validations defined on the:
44

5-
Support for validation in Minimal APIs is now available. This feature allows you to request validation of data
6-
sent to your API endpoints. When validation is enabled, the ASP.NET Core runtime will perform any validations
7-
defined on query, header, and route parameters, as well as on the request body.
8-
Validations can be defined using attributes in the `System.ComponentModel.DataAnnotations` namespace.
9-
Developers can customize the behavior of the validation system by:
5+
* Query
6+
* Header
7+
* Request body
108

11-
- creating custom [ValidationAttribute](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.validationattribute?view=net-9.0) implementations
12-
- implement the [IValidatableObject](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.ivalidatableobject?view=net-9.0) interface for complex validation logic
9+
Validations are defined using attributes in the [`DataAnnotations`](xref:System.ComponentModel.DataAnnotations) namespace. Developers customize the behavior of the validation system by:
1310

14-
When validation fails, the runtime will return a 400 Bad Request response with
15-
details of the validation errors.
11+
* Creating custom [`[Validation]`](xref:System.ComponentModel.DataAnnotations.ValidationAttribute) attribute implementations.
12+
* Implementing the [`IValidatableObject`](xref:System.ComponentModel.DataAnnotations.IValidatableObject) interface for complex validation logic.
1613

17-
To enable built-in validation support for minimal APIs, call the `AddValidation` extension method to register
18-
the required services into the service container for your application.
14+
If validation fails, the runtime returns a 400 Bad Request response with details of the validation errors.
15+
16+
#### Enable built-in validation support for minimal APIs
17+
18+
Enable the built-in validation support for minimal APIs by calling the `AddValidation` extension method to register the required services in the service container for your application:
1919

2020
```csharp
2121
builder.Services.AddValidation();
2222
```
2323

24-
The implementation automatically discovers types that are defined in minimal API handlers or as base types of types defined in minimal API handlers. Validation is then performed on these types by an endpoint filter that is added for each endpoint.
24+
The implementation automatically discovers types that are defined in minimal API handlers or as base types of types defined in minimal API handlers. An endpoint filter performs validation on these types and is added for each endpoint.
2525

26-
Validation can be disabled for specific endpoints by using the `DisableValidation` extension method.
26+
Validation can be disabled for specific endpoints by using the `DisableValidation` extension method, as in the following example:
2727

2828
```csharp
2929
app.MapPost("/products",
3030
([EvenNumber(ErrorMessage = "Product ID must be even")] int productId, [Required] string name)
3131
=> TypedResults.Ok(productId))
3232
.DisableValidation();
33-
```
33+
```

0 commit comments

Comments
 (0)