Skip to content

Commit c1d5ff1

Browse files
snemeckayovaYoussef1313wadepickett
authored
Document validation analyzers (#37540)
* Document validation analyzers * Update aspnetcore/diagnostics/asp0033.md Co-authored-by: Youssef Fahmy <youssefvictor00@gmail.com> * Update metadata for ASP0033 diagnostics document Very minor: Just set the metadata to the required order. --------- Co-authored-by: Youssef Fahmy <youssefvictor00@gmail.com> Co-authored-by: Wade Pickett <wpickett@microsoft.com>
1 parent 7179308 commit c1d5ff1

8 files changed

Lines changed: 343 additions & 1 deletion

File tree

aspnetcore/diagnostics/asp0033.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
---
2+
title: "ASP0033: ValidatableType is applied to an inaccessible type"
3+
author: snemeckayova
4+
description: "Learn about analysis rule ASP0033: ValidatableType is applied to an inaccessible type."
5+
monikerRange: '>= aspnetcore-11.0'
6+
ms.author: snemeckayova
7+
ms.date: 08/26/2026
8+
uid: diagnostics/asp0033
9+
---
10+
# ASP0033: `[ValidatableType]` is applied to an inaccessible type
11+
12+
| | Value |
13+
| - | - |
14+
| **Rule ID** | ASP0033 |
15+
| **Category** | Usage |
16+
| **Severity** | Warning |
17+
18+
## Cause
19+
20+
The <xref:Microsoft.Extensions.Validation.ValidatableTypeAttribute> attribute is applied to a type that the validation source generator can't access. The type, or one of its containing types, is private or file-local.
21+
22+
## Rule description
23+
24+
The validation source generator emits code in a separate file. A validatable type and all of its containing types must be public or internal and can't be file-local. Otherwise, the generator silently skips validation for the type.
25+
26+
The following code produces this diagnostic:
27+
28+
```csharp
29+
[ValidatableType]
30+
file sealed class Asp0033FileLocalValidatableType
31+
{
32+
[Required]
33+
public string Name { get; set; } = string.Empty;
34+
}
35+
```
36+
37+
## How to fix violations
38+
39+
Make the attributed type and each of its containing types public or internal. For example, change `Asp0033FileLocalValidatableType` to an internal type:
40+
41+
```csharp
42+
[ValidatableType]
43+
internal sealed class Asp0033FileLocalValidatableType
44+
{
45+
[Required]
46+
public string Name { get; set; } = string.Empty;
47+
}
48+
```
49+
50+
For more information, see <xref:fundamentals/validation>.

aspnetcore/diagnostics/asp0034.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
title: "ASP0034: Endpoint parameter type is inaccessible from generated code"
3+
description: "Learn about analysis rule ASP0034: Endpoint parameter type is inaccessible from generated code."
4+
monikerRange: '>= aspnetcore-11.0'
5+
ms.date: 08/25/2026
6+
author: snemeckayova
7+
ms.author: snemeckayova
8+
uid: diagnostics/asp0034
9+
---
10+
# ASP0034: Endpoint parameter type is inaccessible from generated code
11+
12+
| | Value |
13+
| - | - |
14+
| **Rule ID** | ASP0034 |
15+
| **Category** | Usage |
16+
| **Severity** | Warning |
17+
18+
## Cause
19+
20+
A Minimal API endpoint has a parameter whose type isn't accessible to the validation source generator. The type, or one of its containing types, is private or file-local.
21+
22+
## Rule description
23+
24+
When <xref:Microsoft.Extensions.DependencyInjection.ValidationServiceCollectionExtensions.AddValidation%2A> enables validation, the validation source generator discovers model types used by Minimal API endpoints. The generated code can't access private or file-local types, so validation is silently skipped for an inaccessible endpoint parameter type.
25+
26+
The following code produces this diagnostic:
27+
28+
```csharp
29+
app.MapPost("/orders", (Order order) => Results.Ok(order));
30+
31+
file class Order
32+
{
33+
[Required]
34+
public string? CustomerName { get; set; }
35+
}
36+
```
37+
38+
## How to fix violations
39+
40+
Make the parameter type and each of its containing types public or internal and don't declare them as file-local:
41+
42+
```csharp
43+
app.MapPost("/orders", (Order order) => Results.Ok(order));
44+
45+
internal class Order
46+
{
47+
[Required]
48+
public string? CustomerName { get; set; }
49+
}
50+
```
51+
52+
If the endpoint shouldn't validate the parameter, disable validation for the endpoint or apply <xref:Microsoft.Extensions.Validation.SkipValidationAttribute> to the parameter.
53+
54+
For more information, see <xref:fundamentals/validation>.

aspnetcore/diagnostics/asp0035.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
title: "ASP0035: Validatable property or its type on a ValidatableType is not accessible"
3+
description: "Learn about analysis rule ASP0035: A validatable property or its type on a ValidatableType isn't accessible."
4+
monikerRange: '>= aspnetcore-11.0'
5+
ms.date: 08/25/2026
6+
author: snemeckayova
7+
ms.author: snemeckayova
8+
uid: diagnostics/asp0035
9+
---
10+
# ASP0035: Validatable property or its type on a `[ValidatableType]` is not accessible
11+
12+
| | Value |
13+
| - | - |
14+
| **Rule ID** | ASP0035 |
15+
| **Category** | Usage |
16+
| **Severity** | Warning |
17+
18+
## Cause
19+
20+
A property on a type marked with <xref:Microsoft.Extensions.Validation.ValidatableTypeAttribute> declares validation, but the property, its getter, or its type isn't accessible to the validation source generator.
21+
22+
## Rule description
23+
24+
The validation source generator can only generate validation for public properties with public getters and accessible property types. An inaccessible property that has validation attributes, implements `IValidatableObject`, or contains a type that declares validation is silently skipped.
25+
26+
The following code produces this diagnostic because `Child` isn't public:
27+
28+
```csharp
29+
[ValidatableType]
30+
public class Order
31+
{
32+
internal Customer Child { get; } = new();
33+
34+
public class Customer
35+
{
36+
[Required]
37+
public string? Name { get; set; }
38+
}
39+
}
40+
```
41+
42+
## How to fix violations
43+
44+
Make the property and its getter public. Also make the property type and each of its containing types public or internal and don't declare them as file-local:
45+
46+
```csharp
47+
[ValidatableType]
48+
public class Order
49+
{
50+
public Customer Child { get; } = new();
51+
52+
public class Customer
53+
{
54+
[Required]
55+
public string? Name { get; set; }
56+
}
57+
}
58+
```
59+
60+
If the property shouldn't be validated, apply <xref:Microsoft.Extensions.Validation.SkipValidationAttribute> to it.
61+
62+
For more information, see <xref:fundamentals/validation>.

aspnetcore/diagnostics/asp0036.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
---
2+
title: "ASP0036: Validatable property or its type on an endpoint parameter type is not accessible"
3+
description: "Learn about analysis rule ASP0036: A validatable property or its type on an endpoint parameter type isn't accessible."
4+
monikerRange: '>= aspnetcore-11.0'
5+
ms.date: 08/25/2026
6+
author: snemeckayova
7+
ms.author: snemeckayova
8+
uid: diagnostics/asp0036
9+
---
10+
# ASP0036: Validatable property or its type on an endpoint parameter type is not accessible
11+
12+
| | Value |
13+
| - | - |
14+
| **Rule ID** | ASP0036 |
15+
| **Category** | Usage |
16+
| **Severity** | Warning |
17+
18+
## Cause
19+
20+
A property on a Minimal API endpoint parameter type declares validation, but the property, its getter, or its type isn't accessible to the validation source generator.
21+
22+
## Rule description
23+
24+
The validation source generator can only generate validation for public properties with public getters and accessible property types. An inaccessible property that has validation attributes, implements `IValidatableObject`, or contains a type that declares validation is silently skipped.
25+
26+
The following code produces this diagnostic because `Child` isn't public:
27+
28+
```csharp
29+
app.MapPost("/orders", (Order order) => Results.Ok(order));
30+
31+
public class Order
32+
{
33+
internal Customer Child { get; } = new();
34+
35+
public class Customer
36+
{
37+
[Required]
38+
public string? Name { get; set; }
39+
}
40+
}
41+
```
42+
43+
## How to fix violations
44+
45+
Make the property and its getter public. Also make the property type and each of its containing types public or internal and don't declare them as file-local:
46+
47+
```csharp
48+
public class Order
49+
{
50+
public Customer Child { get; } = new();
51+
52+
public class Customer
53+
{
54+
[Required]
55+
public string? Name { get; set; }
56+
}
57+
}
58+
```
59+
60+
If the property shouldn't be validated, apply <xref:Microsoft.Extensions.Validation.SkipValidationAttribute> to it. To opt out the whole endpoint, disable validation for the endpoint.
61+
62+
For more information, see <xref:fundamentals/validation>.

aspnetcore/diagnostics/asp0037.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
---
2+
title: "ASP0037: ValidatableType cannot be used in generated code"
3+
description: "Learn about analysis rule ASP0037: ValidatableType can't be used in generated code."
4+
monikerRange: '>= aspnetcore-11.0'
5+
ms.date: 08/25/2026
6+
author: snemeckayova
7+
ms.author: snemeckayova
8+
uid: diagnostics/asp0037
9+
---
10+
# ASP0037: `[ValidatableType]` cannot be used in generated code
11+
12+
| | Value |
13+
| - | - |
14+
| **Rule ID** | ASP0037 |
15+
| **Category** | Usage |
16+
| **Severity** | Warning |
17+
18+
## Cause
19+
20+
The <xref:Microsoft.Extensions.Validation.ValidatableTypeAttribute> attribute is applied to a type declared in generated code, such as code generated from a Razor (`.razor`) file.
21+
22+
## Rule description
23+
24+
The validation source generator can't inspect another source generator's output. Applying `[ValidatableType]` to a type in generated code has no effect, so the type isn't included in generated validation metadata.
25+
26+
For example, declaring the following model in a Razor component's `@code` block produces this diagnostic:
27+
28+
```razor
29+
@code {
30+
[ValidatableType]
31+
public class Order
32+
{
33+
[Required]
34+
public string? CustomerName { get; set; }
35+
}
36+
}
37+
```
38+
39+
## How to fix violations
40+
41+
Move the type to a regular C# (`.cs`) file and apply `[ValidatableType]` there:
42+
43+
```csharp
44+
[ValidatableType]
45+
public class Order
46+
{
47+
[Required]
48+
public string? CustomerName { get; set; }
49+
}
50+
```
51+
52+
For more information, see <xref:fundamentals/validation>.

aspnetcore/diagnostics/asp0038.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
---
2+
title: "ASP0038: ValidatableType should not be used without a call to AddValidation"
3+
description: "Learn about analysis rule ASP0038: ValidatableType shouldn't be used without a call to AddValidation."
4+
monikerRange: '>= aspnetcore-11.0'
5+
ms.date: 08/25/2026
6+
author: snemeckayova
7+
ms.author: snemeckayova
8+
uid: diagnostics/asp0038
9+
---
10+
# ASP0038: `[ValidatableType]` should not be used without a call to `AddValidation`
11+
12+
| | Value |
13+
| - | - |
14+
| **Rule ID** | ASP0038 |
15+
| **Category** | Usage |
16+
| **Severity** | Warning |
17+
18+
## Cause
19+
20+
The <xref:Microsoft.Extensions.Validation.ValidatableTypeAttribute> attribute is used in a project that doesn't call <xref:Microsoft.Extensions.DependencyInjection.ValidationServiceCollectionExtensions.AddValidation%2A>.
21+
22+
## Rule description
23+
24+
`[ValidatableType]` has no effect unless the current project calls `AddValidation`. The validation source generator only discovers types in the project that contains the `AddValidation` call.
25+
26+
## How to fix violations
27+
28+
Call `AddValidation` on the service collection in the project that declares the validatable type:
29+
30+
```csharp
31+
builder.Services.AddValidation();
32+
```
33+
34+
For a class library, provide a public service-registration method that calls `AddValidation` in the library project:
35+
36+
```csharp
37+
public static IServiceCollection AddOrderValidation(
38+
this IServiceCollection services)
39+
{
40+
return services.AddValidation();
41+
}
42+
```
43+
44+
For more information, see [Register validation in multi-assembly apps](xref:fundamentals/validation#register-validation-in-multi-assembly-apps).

aspnetcore/diagnostics/code-analysis.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: tdykstra
44
description: Review the list of diagnostic codes for ASP.NET Core and get details for specific diagnostic identifiers (IDs), such as ASP0007, BL0001, and MVC1006.
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: tdykstra
7-
ms.date: 04/22/2026
7+
ms.date: 08/25/2026
88
uid: diagnostics/code-analysis
99

1010
# customer intent: As an ASP.NET developer, I want to get details for specific diagnostic codes in ASP.NET Core, so I can understand the diagnostic messages in my apps.
@@ -53,6 +53,12 @@ The following table lists the diagnostics available for ASP.NET Core application
5353
| **[ASP0026](xref:diagnostics/asp0026)** | `[Authorize]` is overridden by `[AllowAnonymous]` from "farther away" |
5454
| **[ASP0027](xref:diagnostics/asp0027)** | Unnecessary `public Program` class declaration |
5555
| **[ASP0028](xref:diagnostics/asp0028)** | Consider using `IPAddress.IPv6Any` instead of `IPAddress.Any` |
56+
| **[ASP0033](xref:diagnostics/asp0033)** | `[ValidatableType]` is applied to an inaccessible type |
57+
| **[ASP0034](xref:diagnostics/asp0034)** | Endpoint parameter type is inaccessible from generated code |
58+
| **[ASP0035](xref:diagnostics/asp0035)** | Validatable property or its type on a `[ValidatableType]` is not accessible |
59+
| **[ASP0036](xref:diagnostics/asp0036)** | Validatable property or its type on an endpoint parameter type is not accessible |
60+
| **[ASP0037](xref:diagnostics/asp0037)** | `[ValidatableType]` cannot be used in generated code |
61+
| **[ASP0038](xref:diagnostics/asp0038)** | `[ValidatableType]` should not be used without a call to `AddValidation` |
5662
| **[BL0001](xref:diagnostics/bl0001)** | Component parameter should have public setters |
5763
| **[BL0002](xref:diagnostics/bl0002)** | Component has multiple `CaptureUnmatchedValues` parameters |
5864
| **[BL0003](xref:diagnostics/bl0003)** | Component parameter with `CaptureUnmatchedValues` has the wrong type |

aspnetcore/toc.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,6 +1795,18 @@ items:
17951795
uid: diagnostics/asp0027
17961796
- name: ASP0028
17971797
uid: diagnostics/asp0028
1798+
- name: ASP0033
1799+
uid: diagnostics/asp0033
1800+
- name: ASP0034
1801+
uid: diagnostics/asp0034
1802+
- name: ASP0035
1803+
uid: diagnostics/asp0035
1804+
- name: ASP0036
1805+
uid: diagnostics/asp0036
1806+
- name: ASP0037
1807+
uid: diagnostics/asp0037
1808+
- name: ASP0038
1809+
uid: diagnostics/asp0038
17981810
- name: BL0001
17991811
uid: diagnostics/bl0001
18001812
- name: BL0002

0 commit comments

Comments
 (0)