dotnet / dotnet/aspnetcore

Let calling code populate `ValidationContext.Items` when using Blazor or Minimal APIs

Open
#66,995 3 comments 0 reactions 0 assignees View on GitHub
api-proposal api-suggestion area-blazor area-minimal feature-validation
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Is your feature request related to a problem? Please describe the problem.

When writing custom data-annotations or using the `[CustomValidation]` attribute, it is sometimes useful to provide additional context to the validators. For example, to vary validation rules using "rule sets" (similar to FluentValidation).

The base data-annotations already provide this functionality. This feature request is about exposing that functionality in Blazor/Minimal APIs/MVC. The base `Validator.TryValidateObject` API lets me initialize the `ValidationContext` with the `Items` property.

**A concrete use case**
I have to a POCO declaring the data contract of an API endpoint. Depending on a query-string parameter, the client may save the POCO as a draft instead of submitting it. When validating a draft, I would like to only validate things such as maximum string lengths, but defer requiredness or minimum length validations until the submission. Using the `ValidationContext`'s `Items`property, I can add a custom key-value pair `{ "IsDraft": true }`.

When using Blazor's built-in validation or using Microsoft.Extensions.Validation to validate Minimal API endpoints, this is not possible.

### Describe the solution you'd like

When invoking Blazor's `EditContext.Validate()` method, I would like to pass a dictionary of context items to the `ValidationContext`.

When using Minimal APIs integration with Microsoft.Extensions.Validation, I would like to populate the context items before the endpoint filter runs.

The following ares examples of an API shape to start a conversation:

````csharp
// Blazor
var validationContextItems = new Dictionary()
{
["IsDraft"] = true
};

var isValid = EditContext.Validate(validationContextItems);
````

````csharp
// Minimal APIs
var builder = WebApplication.CreateBuilder(args);

var app = builder.Build();

app.MapGet(...).WithValidationContextItems((httpContext, validationContext) =>
{
if (HttpContext.Request.Query.TryGetValue("isDraft", out var stringValues) && bool.TryParse(stringValues[0], out var isDraft))
{
validationContext.Items.Add("IsDraft", isDraft);
}
else
{
validationContext.Items.Add("IsDraft", false);
}
})
````

# Alternatives

Don't use Blazor's built-in `` component or disable automatic validation using `.DisableValidation()` for Minimal APIs. In both cases, I need to inject the `ValidationOptions` to call `TryGetValidatableTypeInfo()` and initialize the `ValidationContext` with the items.

### Additional context

_No response_

Contributor guide

Open the contributing guide

Research direction

Start by tracing Blazor's EditContext.Validate and DataAnnotationsValidator, then the Microsoft.Extensions.Validation integration for Minimal API endpoint filters. Compare how each initializes ValidationContext and determine a consistent way to supply Items. Done means the agreed API supports caller-provided context items in both validation paths, with behavior verified for custom validators.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
api, backend-api-design, frontend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.