Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@Ren0503
Copy link
Contributor

@Ren0503 Ren0503 commented Jul 19, 2025

No description provided.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 19, 2025

Summary by CodeRabbit

  • New Features

    • Added support for custom validation in microservice configurations, allowing users to specify their own validation logic.
    • Introduced a new method for context objects to perform custom validation on incoming data.
  • Refactor

    • Improved configuration merging to support multiple input configurations and ensure all relevant fields are combined correctly.
  • Tests

    • Updated test setup to utilize the new custom validation feature in microservice server configurations.

Walkthrough

The changes introduce a customizable validation mechanism into the microservices module. A new CustomValidation field is added to the configuration, context objects gain a Scan method that uses this field, and the pipe middleware is updated to invoke validation via the context. Configuration merging logic is also enhanced to support multiple configs.

Changes

File(s) Change Summary
microservices/config.go Added CustomValidation to Config, updated DefaultConfig and ParseConfig for merging and initialization.
microservices/ctx.go Added Scan(val any) error to Ctx interface and implemented it in DefaultCtx using CustomValidation.
microservices/pipe.go Replaced direct validator import/call with ctx.Scan(payload) in PipeMiddleware.
microservices/pipe_test.go Set up custom validation in test server config via CustomValidation: validator.Scanner.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant PipeMiddleware
    participant Ctx
    participant Config
    participant CustomValidator

    Client->>PipeMiddleware: Send request with payload
    PipeMiddleware->>Ctx: ctx.Scan(payload)
    Ctx->>Config: Get CustomValidation
    Ctx->>CustomValidator: CustomValidation(payload)
    CustomValidator-->>Ctx: Validation result (error or nil)
    Ctx-->>PipeMiddleware: Return validation result
    PipeMiddleware-->>Client: Continue or return error
Loading

Possibly related issues

Possibly related PRs

Poem

In the meadow of code, where microservices play,
A rabbit hops in, with validation today.
Configs merge together, context learns to scan,
Custom checks abound, as only rabbits can!
Now errors are caught, before they take flight—
Hop, hop, hooray! The code feels just right.
🐇✨

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @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.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

@Ren0503 Ren0503 linked an issue Jul 19, 2025 that may be closed by this pull request
@codecov
Copy link

codecov bot commented Jul 19, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

Copy link
Contributor

@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: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 33c1293 and 35dd02b.

📒 Files selected for processing (4)
  • microservices/config.go (2 hunks)
  • microservices/ctx.go (2 hunks)
  • microservices/pipe.go (1 hunks)
  • microservices/pipe_test.go (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
microservices/pipe_test.go (2)
microservices/config.go (1)
  • Config (17-26)
dto/validator/scanner.go (1)
  • Scanner (14-180)
microservices/ctx.go (1)
microservices/config.go (1)
  • Config (17-26)
🔇 Additional comments (10)
microservices/pipe.go (1)

19-19: LGTM! Good architectural improvement with dependency inversion.

The change from direct validator.Scanner(payload) to ctx.Scan(payload) nicely abstracts the validation logic behind the context interface, making validation customizable through configuration while maintaining the same error handling flow.

microservices/pipe_test.go (2)

14-14: LGTM! Import added for explicit validation configuration.

The validator import is needed since the test now explicitly configures the CustomValidation field.


79-81: LGTM! Test properly exercises the new validation mechanism.

The explicit configuration of CustomValidation: validator.Scanner ensures that the test exercises the new context-based validation approach while maintaining backward compatibility with the existing validation behavior.

microservices/ctx.go (2)

19-19: LGTM! Interface extension enables configurable validation.

The addition of Scan(val any) error to the Ctx interface provides a clean abstraction for validation operations.


90-92: LGTM! Clean delegation to configured validation function.

The implementation correctly delegates to the CustomValidation function from the service configuration, enabling flexible validation strategies while maintaining a consistent interface.

microservices/config.go (5)

8-8: LGTM! Import added for improved config merging.

The common package import is needed for the MergeStruct function used in the updated ParseConfig.


11-11: LGTM! Import needed for default validation configuration.

The validator package import is required since DefaultConfig now sets CustomValidation to validator.Scanner.


25-25: LGTM! Config struct extended with validation field.

The addition of CustomValidation core.PipeFnc field enables configurable validation logic throughout the microservices framework.


41-41: LGTM! Sensible default maintains backward compatibility.

Setting CustomValidation: validator.Scanner as the default ensures existing code continues to work without changes.


82-84: LGTM! CustomValidation field properly handled in merge logic.

The nil check and assignment for CustomValidation follows the same pattern as other configurable fields.

Comment on lines 57 to 61
if len(cfg[0].Header) > 0 {
for k, v := range cfg[0].Header {
for k, v := range mergeConfig.Header {
defaultConfig.Header[k] = v
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Fix inconsistent merge logic for Header field.

The Header field handling is inconsistent with other fields - it uses cfg[0].Header instead of mergeConfig.Header like other fields.

-		if len(cfg[0].Header) > 0 {
-			for k, v := range mergeConfig.Header {
+		if len(mergeConfig.Header) > 0 {
+			for k, v := range mergeConfig.Header {
				defaultConfig.Header[k] = v
			}
		}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if len(cfg[0].Header) > 0 {
for k, v := range cfg[0].Header {
for k, v := range mergeConfig.Header {
defaultConfig.Header[k] = v
}
}
if len(mergeConfig.Header) > 0 {
for k, v := range mergeConfig.Header {
defaultConfig.Header[k] = v
}
}
🤖 Prompt for AI Agents
In microservices/config.go around lines 57 to 61, the merge logic for the Header
field incorrectly checks the length of cfg[0].Header instead of
mergeConfig.Header, causing inconsistency with other fields. Update the
condition to check the length of mergeConfig.Header and ensure the loop iterates
over mergeConfig.Header to maintain consistent merge behavior.

Comment on lines +69 to 71
if mergeConfig.ErrorHandler == nil {
defaultConfig.ErrorHandler = DefaultErrorHandler(cfg[0].Logger)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Fix inconsistent reference in ErrorHandler logic.

The code uses cfg[0].Logger instead of mergeConfig.Logger when creating the default error handler, which is inconsistent with the merge approach.

		if mergeConfig.Logger != nil {
			defaultConfig.Logger = mergeConfig.Logger
			if mergeConfig.ErrorHandler == nil {
-				defaultConfig.ErrorHandler = DefaultErrorHandler(cfg[0].Logger)
+				defaultConfig.ErrorHandler = DefaultErrorHandler(mergeConfig.Logger)
			}
		}
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if mergeConfig.ErrorHandler == nil {
defaultConfig.ErrorHandler = DefaultErrorHandler(cfg[0].Logger)
}
if mergeConfig.Logger != nil {
defaultConfig.Logger = mergeConfig.Logger
if mergeConfig.ErrorHandler == nil {
defaultConfig.ErrorHandler = DefaultErrorHandler(mergeConfig.Logger)
}
}
🤖 Prompt for AI Agents
In microservices/config.go around lines 69 to 71, the code incorrectly uses
cfg[0].Logger instead of mergeConfig.Logger when assigning the default error
handler. Update the code to use mergeConfig.Logger to maintain consistency with
the merged configuration approach.

@Ren0503 Ren0503 merged commit 46559e9 into master Jul 19, 2025
8 checks passed
@Ren0503 Ren0503 added this to the Microservice v1.1.0 milestone Jul 19, 2025
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.

Microservices: Add option custom validation pipe on microservice

2 participants