Skip to content

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

@Trivivium

Description

@Trivivium

Is there an existing issue for this?

  • 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 Itemsproperty, 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:

// Blazor
var validationContextItems = new Dictionary<object, object?>()
{
    ["IsDraft"] = true
};

var isValid = EditContext.Validate(validationContextItems);
// 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 <DataAnnotationsValidator> 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-proposalapi-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-blazorIncludes: Blazor, Razor Componentsarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-validationIssues related to model validation in minimal and controller-based APIs
    No fields configured for Feature.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions