Skip to content

Conversation

@AiYuZhen
Copy link
Collaborator

@AiYuZhen AiYuZhen commented Dec 30, 2024

fix(RequiredValidator):优化 ValidationResult 生成代码

Summary of the changes (Less than 80 chars)

简单描述你更改了什么, 不超过80个字符;如果有关联 Issue 请在下方填写相关编号

优化 public override void Validate(object? propertyValue, ValidationContext context, List<ValidationResult> results) 方法,只有在验证不通过时才执行创建 ValidationResult 的相关格式化代码。

fixes #4997

Regression?

  • Yes
  • No

[If yes, specify the version the behavior has regressed from]

[是否影响老版本]

Risk

  • High
  • Medium
  • Low

[Justify the selection above]

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

Bug Fixes:

  • Fix a crash in RequiredValidator when a custom ErrorMessage is used with invalid formatting.

Summary by Sourcery

Bug Fixes:

  • Optimize the Validate method to improve performance by generating ValidationResult only when necessary.

…lidator 在验证未出结果之前(结果必定为通过)本地化包含自定义内容的 ErrorMessage 且其内容中有不符合 string.Format 方法要求的内容(如:ErrorMessage = $"当前 {{{nameof(MenuType)}}} 的值必须要设置 {{0}} 。")时导致程序崩溃问题。

自定义验证方法会正确处理 {{{nameof(MenuType)}}} 自定义内容。
@sourcery-ai
Copy link
Contributor

sourcery-ai bot commented Dec 30, 2024

Reviewer's Guide by Sourcery

This pull request optimizes the RequiredValidator by generating ValidationResult objects only when validation fails. Previously, the code to format the error message and create the ValidationResult was executed regardless of the validation outcome. This change improves performance by avoiding unnecessary object creation and formatting when the validation succeeds.

Sequence diagram for optimized RequiredValidator validation flow

sequenceDiagram
    participant C as Client
    participant V as RequiredValidator
    participant VC as ValidationContext

    C->>+V: Validate(propertyValue, context, results)
    alt propertyValue is null
        V->>V: GetValidationResult(context)
        V->>VC: GetValidationResult(errorMessage)
        VC-->>V: ValidationResult
        V->>V: results.Add(ValidationResult)
    else propertyValue is empty string
        V->>V: GetValidationResult(context)
        V->>VC: GetValidationResult(errorMessage)
        VC-->>V: ValidationResult
        V->>V: results.Add(ValidationResult)
    else propertyValue is empty IEnumerable
        V->>V: GetValidationResult(context)
        V->>VC: GetValidationResult(errorMessage)
        VC-->>V: ValidationResult
        V->>V: results.Add(ValidationResult)
    end
    V-->>-C: void
Loading

File-Level Changes

Change Details Files
Optimize ValidationResult generation
  • Create a new GetValidationResult method in RequiredValidator to encapsulate the logic for creating ValidationResult objects.
  • Move the logic for formatting the error message and creating the ValidationResult into the GetValidationResult method.
  • Call the GetValidationResult method only when the validation fails.
  • Add a new extension method GetValidationResult to ValidationContextExtensions to create a ValidationResult with the given error message and member names.
  • Use the new GetValidationResult extension method in the RequiredValidator to simplify the code.
src/BootstrapBlazor/Validators/RequiredValidator.cs
src/BootstrapBlazor/Extensions/ValidateContextExtensions.cs
Update project file
  • Update the project file to reflect the changes made in the code.
src/BootstrapBlazor/BootstrapBlazor.csproj

Assessment against linked issues

Issue Objective Addressed Explanation
#4997 Handle custom RequiredAttribute with ErrorMessage containing custom placeholder {{Value}} without causing an exception during validation
#4997 Adjust RequiredValidator's localization code execution timing to prevent errors when formatting ErrorMessage
#4997 Ensure validation works correctly when using custom error messages with complex formatting

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.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a 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. You can also use
    this command to specify where the summary should be inserted.

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
Copy link

bb-auto bot commented Dec 30, 2024

Thanks for your PR, @AiYuZhen. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@bb-auto bb-auto bot requested a review from ArgoZhang December 30, 2024 03:46
sourcery-ai[bot]
sourcery-ai bot previously approved these changes Dec 30, 2024
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 @AiYuZhen - I've reviewed your changes - here's some feedback:

Overall Comments:

  • The System.Security.AccessControl using statement appears to be unused and can be removed.
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 Dec 30, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 100.00%. Comparing base (8080408) to head (2a3b559).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff            @@
##              main     #4996   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files          630       630           
  Lines        27922     27928    +6     
  Branches      3996      3996           
=========================================
+ Hits         27922     27928    +6     

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

@AiYuZhen AiYuZhen changed the title 修复自定义必填验证【RequiredAttribute】通过后(返回 ValidationResult 集合为零时),RequiredVa… AiYuZhen Contributor AiYuZhen commented 12 minutes ago • fix(RequiredValidator):调整错误结果生成时机 Dec 30, 2024
@AiYuZhen AiYuZhen changed the title AiYuZhen Contributor AiYuZhen commented 12 minutes ago • fix(RequiredValidator):调整错误结果生成时机 fix(RequiredValidator):调整错误结果生成时机 Dec 30, 2024
@bb-auto bb-auto bot added the bug Something isn't working label Dec 30, 2024
@bb-auto bb-auto bot added this to the v9.0.0 milestone Dec 30, 2024
@ArgoZhang ArgoZhang closed this Dec 31, 2024
@AiYuZhen AiYuZhen changed the title fix(RequiredValidator):调整错误结果生成时机 feat(RequiredValidator):优化 ValidationResult 生成代码 Dec 31, 2024
@ArgoZhang ArgoZhang reopened this Dec 31, 2024
ArgoZhang
ArgoZhang previously approved these changes Dec 31, 2024
# Conflicts:
#	src/BootstrapBlazor/BootstrapBlazor.csproj

Co-Authored-By: AiZhen <[email protected]>
ArgoZhang
ArgoZhang previously approved these changes Dec 31, 2024
@ArgoZhang
Copy link
Member

@sourcery-ai review

sourcery-ai[bot]
sourcery-ai bot previously approved these changes Dec 31, 2024
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 @AiYuZhen - I've reviewed your changes and they look great!

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.

@ArgoZhang ArgoZhang dismissed stale reviews from sourcery-ai[bot] and themself via 2a3b559 December 31, 2024 05:46
@ArgoZhang ArgoZhang changed the title feat(RequiredValidator):优化 ValidationResult 生成代码 feat(RequiredValidator): 优化 ValidationResult 生成代码 Dec 31, 2024
@ArgoZhang ArgoZhang changed the title feat(RequiredValidator): 优化 ValidationResult 生成代码 feat(RequiredValidator): call GetLocalizerErrorMessage after validate failed Dec 31, 2024
@ArgoZhang ArgoZhang changed the title feat(RequiredValidator): call GetLocalizerErrorMessage after validate failed perf(RequiredValidator): call GetLocalizerErrorMessage after validate failed Dec 31, 2024
@ArgoZhang ArgoZhang merged commit 2b1c587 into dotnetcore:main Dec 31, 2024
5 checks passed
@AiYuZhen AiYuZhen deleted the Feat_RequiredValidator branch April 29, 2025 00:10
@AiYuZhen AiYuZhen restored the Feat_RequiredValidator branch April 29, 2025 00:10
@AiYuZhen AiYuZhen deleted the Feat_RequiredValidator branch April 29, 2025 00:10
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.

perf(RequiredValidator): 数据合规性检查失败时调用 GetLocalizerErrorMessage 提高性能

2 participants