Skip to content

Conversation

@ArgoZhang
Copy link
Member

@ArgoZhang ArgoZhang commented Mar 31, 2025

Link issues

fixes #5745

Summary By Copilot

This pull request includes changes to the ValidateForm component and its unit tests in order to improve validation logic and add new test coverage. The most important changes include refactoring the ValidateDataAnnotations method, adding a new test method, and introducing a custom validation rule.

Improvements to validation logic:

Enhancements to unit tests:

Code cleanup:

Regression?

  • Yes
  • No

Risk

  • High
  • Medium
  • Low

Verification

  • Manual (required)
  • Automated

Packaging changes reviewed?

  • Yes
  • No
  • N/A

☑️ Self Check before Merge

⚠️ Please check all items below before review. ⚠️

  • 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

Improve validation logic in ValidateForm to support custom validation classes that do not end with 'Attribute'

Bug Fixes:

  • Fixed localization logic for validation error messages to handle custom validation classes without the 'Attribute' suffix

Enhancements:

  • Refactored validation error message retrieval to be more flexible with custom validation rule naming
  • Improved support for localization of validation error messages

Tests:

  • Added test case to verify custom validation rules without using the conventional 'Attribute' suffix
  • Introduced a new test validation rule to demonstrate custom validation support

Chores:

  • Changed visibility of an error constant in a test attribute class from public to private

@bb-auto bb-auto bot added the need-repro-project Need repro project label Mar 31, 2025
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Mar 31, 2025

Reviewer's Guide by Sourcery

This pull request improves the ValidateForm component by refactoring the ValidateDataAnnotations method for better error message localization and adding a new test case for custom validation rules that do not end with 'Attribute'. It also includes a minor code cleanup by changing the visibility of a constant in a test class.

Updated class diagram for ValidateForm

classDiagram
  class ValidateForm {
    -LocalizerFactory: IStringLocalizerFactory
    +ValidateForm()
    +OnInitialized()
    +OnParametersSet()
    +ValidateModel(): bool
    +Reset()
    -ValidateDataAnnotations(object? value, ValidationContext context, List<ValidationResult> results, ValidationAttribute rule, string memberName, ReadOnlySpan<char> attributeSpan)
  }
  note for ValidateForm "Refactored ValidateDataAnnotations method to streamline error message localization and improve readability."
Loading

File-Level Changes

Change Details Files
Refactor the ValidateDataAnnotations method to streamline error message localization and improve readability.
  • Refactor the ValidateDataAnnotations method to improve the logic for finding localized error messages.
  • The method now attempts to find error messages using the ErrorMessage property of the validation attribute, the attribute type, and a combination of the member name and rule name.
  • The logic now handles cases where custom validation rules do not end with the conventional Attribute suffix.
src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs
Add a new test method to verify custom validation rules without using the conventional Attribute suffix.
  • Add a new test method TestService_Ok to verify custom validation rules.
  • Introduce a new private class TestValidateRule for custom validation logic.
  • Update the HasService class to include a property using this new rule.
test/UnitTest/Components/ValidateFormTest.cs
Change the visibility of the Error constant in the HasServiceAttribute class from public to private.
  • Change the visibility of the Error constant to private to encapsulate it within the class.
test/UnitTest/Components/ValidateFormTest.cs

Assessment against linked issues

Issue Objective Addressed Explanation
#5745 Fix the bug where custom validation rules that do not end with 'Attribute' cause an out-of-bounds exception in ValidateForm.razor.cs.
#5745 Add a check to ensure that the index is greater than 0 before accessing the rule name substring.
#5745 Ensure custom validation rules without the 'Attribute' suffix work correctly.

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!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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 added this to the v9.5.0 milestone Mar 31, 2025
@ArgoZhang ArgoZhang requested a review from Copilot March 31, 2025 05:07
@ArgoZhang ArgoZhang added bug Something isn't working and removed need-repro-project Need repro project labels Mar 31, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR fixes issue #5745 by enhancing the ValidateForm component to support custom validation classes that do not end with "Attribute". The changes include refactoring error message localization in the ValidateDataAnnotations method, adding a new test case, and introducing a custom validation rule.

  • Refactored error message retrieval logic in ValidateForm.
  • Added a new test method and custom validation rule in the unit tests.
  • Changed the visibility of an error constant in a test attribute class.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
test/UnitTest/Components/ValidateFormTest.cs Added tests for custom validation rules and updated constant visibility
src/BootstrapBlazor/Components/ValidateForm/ValidateForm.razor.cs Refactored error message localization logic to support custom validation classes

@ArgoZhang ArgoZhang enabled auto-merge (squash) March 31, 2025 05:07
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @ArgoZhang - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider extracting the validation logic into a separate class for better reusability.
  • The changes to ValidateDataAnnotations look good, but could use a comment explaining the order in which the localizers are checked.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@codecov
Copy link

codecov bot commented Mar 31, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (35a7511) to head (356c9bc).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #5752   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          657       657           
  Lines        29923     29926    +3     
  Branches      4245      4248    +3     
=========================================
+ Hits         29923     29926    +3     

☔ View full report in Codecov by Sentry.
📢 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.

@ArgoZhang ArgoZhang merged commit 49a4da0 into main Mar 31, 2025
6 checks passed
@ArgoZhang ArgoZhang deleted the refactor-validate branch March 31, 2025 05:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(ValidateForm): 非 Attribute 结尾的自定义验证规则报错

2 participants