Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Support addExtensions for validated-dto. #839

Merged
merged 5 commits into from
Feb 14, 2025

Conversation

xuanyanwow
Copy link
Collaborator

@xuanyanwow xuanyanwow commented Feb 14, 2025

protected function initValidator(ValidatorInterface $validator): void
{
    if ($validator instanceof Validator) {
        $validator->addExtensions([
            'abc' => function ($attribute, $value, $parameters, $validator) {
                return false;
            },
        ]);
    }
    parent::initValidator($validator);
}

Summary by CodeRabbit

  • 新功能
    • 改进了数据验证流程,新增了扩展入口,为未来提供更多灵活定制的数据验证选项。此次更新确保现有功能保持稳定,同时为后续个性化优化奠定基础。

@xuanyanwow xuanyanwow changed the title feature: Support handle init event for validation-dto. feature: Support handle init event for validated-dto. Feb 14, 2025
Copy link

coderabbitai bot commented Feb 14, 2025

Warning

Rate limit exceeded

@huangdijia has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 13 minutes and 56 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between fd068ad and 79f1a75.

📒 Files selected for processing (1)
  • src/validated-dto/src/ValidatedDTO.php (4 hunks)

"""

Walkthrough

此次变更在 ValidatedDTO 类中新增了一个受保护方法 extensions,该方法返回一个空数组,旨在定义自定义验证扩展。此方法在 isValidDatavalidationPasses 方法中被调用,检查验证器是否具有 addExtensions 方法。如果条件成立,并且 extensions 方法返回任何扩展,则将这些扩展添加到验证器中。这一修改增强了验证过程的灵活性,允许未来自定义验证行为。

Changes

文件路径 变化描述
src/validated-dto/.../ValidatedDTO.php 新增受保护方法 extensions(),在 isValidDatavalidationPasses 中调用

Sequence Diagram(s)

sequenceDiagram
    participant Client as 客户端
    participant DTO as ValidatedDTO
    participant Validator as Validator

    Client->>DTO: 调用 isValidData / validationPasses
    DTO->>Validator: 创建验证器实例
    DTO->>DTO: 调用 extensions()
    DTO->>Validator: 执行验证操作
    Validator-->>DTO: 返回验证结果
    DTO-->>Client: 返回验证结果
Loading

Poem

我是一只快乐的小兔,
在代码大地上轻轻跳跃;
新增的扩展悄然闪烁,
为验证注入无限可能性;
键盘的敲击如同心跳,
欢庆每一行充满奇思妙想的代码!
"""


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
src/validated-dto/src/ValidatedDTO.php (1)

147-163: 建议重构重复的验证逻辑

isValidDatavalidationPasses 方法中存在大量重复代码,包括验证器的创建和初始化逻辑。建议将这些共同的逻辑提取到一个私有方法中。

建议应用以下重构:

+    private function createAndInitValidator(): ValidatorInterface 
+    {
+        $container = ApplicationContext::getContainer();
+        $validator = $container->get(ValidatorFactoryInterface::class)
+            ->make(
+                $this->dtoData,
+                $this->rulesList(),
+                $this->messagesList(),
+                $this->attributes()
+            );
+
+        $this->initValidator($validator);
+        $validator->after(fn ($validator) => $this->after($validator));
+
+        return $validator;
+    }
+
     protected function isValidData(): bool
     {
-        $container = ApplicationContext::getContainer();
-        $this->validator = $container->get(ValidatorFactoryInterface::class)
-            ->make(
-                $this->dtoData,
-                $this->rulesList(),
-                $this->messagesList(),
-                $this->attributes()
-            );
-
-        $this->initValidator($this->validator);
-
-        $this->validator->after(fn ($validator) => $this->after($validator));
+        $this->validator = $this->createAndInitValidator();
         return ! $this->validator->fails();
     }

     private function validationPasses(): bool
     {
-        $container = ApplicationContext::getContainer();
-        $this->validator = $container->get(ValidatorFactoryInterface::class)
-            ->make(
-                $this->dtoData,
-                $this->rulesList(),
-                $this->messagesList(),
-                $this->attributes()
-            );
-
-        $this->initValidator($this->validator);
-
-        $this->validator->after(fn (ValidatorInterface $validator) => $this->after($validator));
+        $this->validator = $this->createAndInitValidator();
         return ! $this->validator->fails();
     }

Also applies to: 196-212

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c0a9762 and c1a17f2.

📒 Files selected for processing (1)
  • src/validated-dto/src/ValidatedDTO.php (3 hunks)
🔇 Additional comments (1)
src/validated-dto/src/ValidatedDTO.php (1)

64-67: 新增的 initValidator 方法提供了良好的扩展点

该方法为验证器的初始化提供了一个清晰的扩展点,符合开闭原则。子类可以通过重写此方法来自定义验证器的初始化行为。

@xuanyanwow xuanyanwow changed the title feature: Support handle init event for validated-dto. feature: Support addExtensions for validated-dto. Feb 14, 2025
@huangdijia huangdijia merged commit aa04a2d into friendsofhyperf:main Feb 14, 2025
8 checks passed
huangdijia added a commit that referenced this pull request Feb 14, 2025
* Update ValidatedDTO.php

* Update ValidatedDTO.php

* refactor: rename addExtensions to extensions and update validator extension logic

* refactor: improve validator extension logic in ValidatedDTO

* refactor: replace extensions method with afterValidatorResolving for improved validator handling

---------

Co-authored-by: Deeka Wong <[email protected]>
@xuanyanwow xuanyanwow deleted the siam/upgrade-validator branch February 14, 2025 05:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants