Skip to content

feat(ValidateForm): unify async validation APIs across frameworks - #8441

Open
ArgoZhang wants to merge 5 commits into
mainfrom
refactor-validate
Open

feat(ValidateForm): unify async validation APIs across frameworks#8441
ArgoZhang wants to merge 5 commits into
mainfrom
refactor-validate

Conversation

@ArgoZhang

@ArgoZhang ArgoZhang commented Sep 5, 2026

Copy link
Copy Markdown
Member

Link issues

fixes #8440

Summary By Copilot

  • Add pre-.NET 11 async form/field validation extensions and state queries.
  • Simplify ValidateForm handlers and centralize cancellation and task tracking.
  • Expand exception and branch coverage; remove redundant null guards.

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Changes async task lifecycles. Older frameworks now reject synchronous Validate() calls that register async validators.

Verification

  • Manual (required)
  • Automated

The .NET 10 unit-test project builds successfully. Unit tests were not run by Copilot.

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

  • Doc is updated/provided or not needed
  • Demo is updated/provided or not needed
  • Merge the latest code from the main branch

Summary by Sourcery

Unify asynchronous form and field validation behavior across supported Blazor frameworks.

New Features:

  • Add cross-framework asynchronous EditContext validation APIs for form and field validation, including cancellation, pending-state, and fault-state queries.

Bug Fixes:

  • Ensure asynchronous validation cancellation, failures, stale operations, and validation messages are handled consistently across supported frameworks.

Enhancements:

  • Simplify ValidateForm validation handling by delegating async lifecycle management to shared EditContext extensions and deprecate synchronous Validate usage.

Tests:

  • Expand validation coverage for asynchronous form and field lifecycles, cancellation, fault reporting, concurrency, reentrancy, and invalid registrations.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @ArgoZhang, you've used your own review budget of 250,000 diff characters for the last 7 days.

You can request another review in 22 hours and 55 minutes by commenting @sourcery-ai review. Upgrade to get a review now.

@bb-auto bb-auto Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto approved by bb-auto

@sourcery-ai

sourcery-ai Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Reviewer's Guide

Unifies async form and field validation across frameworks by adding pre-.NET 11 EditContext compatibility extensions, centralizing task and cancellation lifecycles, exposing pending/fault state, simplifying ValidateForm integration, and adding broad lifecycle and exception coverage.

Sequence diagram for unified async form validation

sequenceDiagram
    participant Caller
    participant EditContext
    participant ValidateForm as ValidateForm
    participant Validator as AsyncValidator

    Caller->>EditContext: ValidateAsync(cancellationToken)
    EditContext->>EditContext: CancelAsyncFieldValidations()
    EditContext->>ValidateForm: ValidateModelAsync(cancellationToken)
    ValidateForm->>EditContext: AddAsyncValidator(validator)
    EditContext->>Validator: validator(cancellationToken)
    Validator-->>EditContext: Task
    EditContext->>EditContext: await task
    EditContext-->>Caller: bool result
Loading

State diagram for async validation lifecycle

stateDiagram-v2
    [*] --> Idle
    Idle --> Pending: ValidateAsync()
    Pending --> Idle: tasks complete successfully
    Pending --> Faulted: task fault or cancellation
    Faulted --> Idle: next validation pass
    Idle --> FieldPending: RegisterAsyncFieldValidator()
    FieldPending --> Idle: field task completes
    FieldPending --> FieldPending: RegisterAsyncFieldValidator() / cancel previous task
    FieldPending --> FieldFaulted: field task faults
    FieldFaulted --> Idle: next field validation
Loading

File-Level Changes

Change Details Files
Introduces a compatibility layer that provides unified asynchronous form and field validation APIs on pre-.NET 11 frameworks.
  • Adds EditContext.ValidateAsync with serialized validation passes, cancellation, task aggregation, reentrancy protection, and pending/fault state tracking.
  • Adds async field-validator registration with replacement cancellation, lifecycle cleanup, stale-task protection, and field pending/fault queries.
  • Adds ValidationRequestedEventArgs.AddAsyncValidator and updates the data-annotations validator to use the shared APIs.
  • Keeps the compatibility implementation excluded on .NET 11 and delegates to the framework APIs there.
src/BootstrapBlazor/Extensions/EditContextExtensions.cs
src/BootstrapBlazor/Extensions/ValidationRequestedEventArgsExtensions.cs
src/BootstrapBlazor/Components/ValidateForm/BootstrapBlazorDataAnnotationsValidator.cs
Simplifies validator exception handling and adjusts validation result flow for the centralized async lifecycle.
  • Removes the redundant validation-context null guard.
  • Changes model validation to report through exceptions and validation state rather than returning a boolean.
  • Logs field-validation exceptions before rethrowing and routes disposal cancellation through EditContext state management.
src/BootstrapBlazor/Attributes/AsyncValidationAttribute.cs
src/BootstrapBlazor/Components/ValidateForm/BootstrapBlazorDataAnnotationsValidator.cs
Expands automated coverage for asynchronous validation behavior and cross-framework compatibility.
  • Tests form and field pending/fault states, cancellation, replacement operations, serialization, reentrancy, nested scopes, handler/delegate failures, and message clearing.
  • Updates ValidateForm tests to call ValidateAsync consistently and verifies logging and cancellation behavior.
  • Removes the obsolete null-context attribute test.
test/UnitTest/Extensions/AsyncFieldValidationTest.cs
test/UnitTest/Extensions/EditContextExtensionsTest.cs
test/UnitTest/Components/ValidateFormTest.cs
test/UnitTest/Attributes/AsyncValidationAttributeTest.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#8440 Provide compatibility extensions across older frameworks for asynchronous form and field validation, including ValidateAsync, AddAsyncValidator, RegisterAsyncFieldValidator, and validation pending/fault state queries.
#8440 Unify ValidateForm's asynchronous validation implementation across framework versions by removing duplicated fallback logic and centralizing cancellation, serialization, task tracking, exception handling, and validation-scope isolation.
#8440 Ensure asynchronous validators are executed through ValidateAsync, while synchronous Validate() rejects registrations of asynchronous validators, with coverage for cancellation, failures, state cleanup, and independent validation scopes.

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@bb-auto bb-auto Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto approved by bb-auto

@ArgoZhang ArgoZhang self-assigned this Sep 5, 2026
@ArgoZhang ArgoZhang added the enhancement New feature or request label Sep 5, 2026
@ArgoZhang ArgoZhang added this to the v10.10.0 milestone Sep 5, 2026
@codecov

codecov Bot commented Sep 5, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (95776d1) to head (dfc0d9c).

Additional details and impacted files
@@            Coverage Diff             @@
##              main     #8441    +/-   ##
==========================================
  Coverage   100.00%   100.00%            
==========================================
  Files          772       774     +2     
  Lines        34798     34940   +142     
==========================================
+ Hits         34798     34940   +142     
Flag Coverage Δ
BB 100.00% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@bb-auto bb-auto Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Auto approved by bb-auto

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(ValidateForm): unify async validation APIs across frameworks

1 participant