-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Let calling code populate ValidationContext.Items when using Blazor or Minimal APIs #66995
Copy link
Copy link
Open
Open
Copy link
Labels
api-proposalapi-suggestionEarly API idea and discussion, it is NOT ready for implementationEarly API idea and discussion, it is NOT ready for implementationarea-blazorIncludes: Blazor, Razor ComponentsIncludes: Blazor, Razor Componentsarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-validationIssues related to model validation in minimal and controller-based APIsIssues related to model validation in minimal and controller-based APIs
Milestone
Metadata
Metadata
Assignees
Labels
api-proposalapi-suggestionEarly API idea and discussion, it is NOT ready for implementationEarly API idea and discussion, it is NOT ready for implementationarea-blazorIncludes: Blazor, Razor ComponentsIncludes: Blazor, Razor Componentsarea-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-validationIssues related to model validation in minimal and controller-based APIsIssues related to model validation in minimal and controller-based APIs
Type
Fields
Give feedbackNo fields configured for Feature.
Is there an existing issue for this?
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.TryValidateObjectAPI lets me initialize theValidationContextwith theItemsproperty.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'sItemsproperty, 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 theValidationContext.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:
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 theValidationOptionsto callTryGetValidatableTypeInfo()and initialize theValidationContextwith the items.Additional context
No response