Skip to content

Resolved FIXME #2147

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

Closed
wants to merge 5 commits into from
Closed

Conversation

callumfrederiksen
Copy link

PR Draft Summary

The recent work includes two key tasks:

  1. Validate namespace server messages

    • Added checks in zodNamespace to reject server messages that define callbacks.
    • Introduced tests in zodNamespace.test.ts ensuring unsupported callbacks produce errors.
  2. Improve message parsing logic

    • Updated zodMessageHandler to reliably detect payloads using runtime validation.
    • Added zodMessageHandler.test.ts covering messages with and without explicit payload fields.

These updates strengthen server message validation and enhance message parsing reliability.

Copy link

changeset-bot bot commented Jun 3, 2025

⚠️ No Changeset found

Latest commit: 40492dd

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link
Contributor

coderabbitai bot commented Jun 3, 2025

Walkthrough

The changes introduce new unit tests for the ZodMessageHandler and ZodNamespace classes, focusing on message handling and schema validation logic. In zodMessageHandler.test.ts, tests are added to verify the correct processing of messages with and without a payload property, using event emitters and Zod schema validation. The zodMessageHandler.ts implementation is updated to use Zod's runtime validation for detecting payloads instead of a direct property check. In zodNamespace.ts, both compile-time and runtime checks are added to prevent server message schemas from including callbacks, with errors thrown if such schemas are detected. A corresponding test in zodNamespace.test.ts ensures this restriction is enforced. No changes are made to public APIs beyond additional type constraints and validation logic.


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 17e84d4 and 40492dd.

📒 Files selected for processing (1)
  • packages/core/src/v3/zodNamespace.ts (3 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/core/src/v3/zodNamespace.ts (1)
packages/core/src/v3/zodSocket.ts (2)
  • ZodSocketMessageCatalogSchema (8-17)
  • GetSocketMessagesWithCallback (244-248)
🔇 Additional comments (4)
packages/core/src/v3/zodNamespace.ts (4)

8-8: LGTM: Import addition supports new validation logic.

The import of GetSocketMessagesWithCallback is necessary for the compile-time validation and is correctly added.


15-19: Excellent compile-time validation with improved type safety.

The AssertNoCallbackSchemas type provides robust compile-time validation to prevent server message schemas with callbacks. I'm pleased to see that the previous feedback about using {} has been addressed by replacing it with Record<string, never>, which provides better type safety.

The conditional type logic correctly:

  • Returns an empty record when no callbacks are detected
  • Returns an error type containing the problematic message keys when callbacks are found

42-42: Well-implemented constraint application.

Extending the interface with AssertNoCallbackSchemas<TServerMessages> correctly applies the compile-time validation constraint, ensuring TypeScript will catch attempts to pass server message schemas with callbacks at development time.


118-126: Robust runtime validation complements compile-time checks.

The runtime validation provides excellent defense-in-depth by catching callback schemas that might bypass compile-time checks (e.g., through type assertions or dynamic construction). The implementation is well-designed:

  • Efficiently filters server messages to identify those with callback properties
  • Uses both property existence and truthiness checks to avoid false positives
  • Provides clear, actionable error messages listing the problematic message keys
  • Throws early in the constructor to fail fast

This runtime check works consistently with the compile-time validation and addresses the FIXME mentioned in the PR objectives.

✨ 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.

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: 1

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between eee0fb2 and 17e84d4.

📒 Files selected for processing (4)
  • packages/core/src/v3/zodMessageHandler.test.ts (1 hunks)
  • packages/core/src/v3/zodMessageHandler.ts (1 hunks)
  • packages/core/src/v3/zodNamespace.ts (3 hunks)
  • packages/core/test/zodNamespace.test.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
packages/core/src/v3/zodMessageHandler.test.ts (1)
packages/core/src/v3/zodMessageHandler.ts (1)
  • ZodMessageHandler (58-210)
packages/core/src/v3/zodNamespace.ts (1)
packages/core/src/v3/zodSocket.ts (2)
  • ZodSocketMessageCatalogSchema (8-17)
  • GetSocketMessagesWithCallback (244-248)
🪛 Biome (1.9.4)
packages/core/src/v3/zodNamespace.ts

[error] 18-18: Don't use '{}' as a type.

Prefer explicitly define the object shape. '{}' means "any non-nullable value".

(lint/complexity/noBannedTypes)

🔇 Additional comments (9)
packages/core/src/v3/zodNamespace.ts (3)

8-8: LGTM: Import addition supports the new validation logic.

The import of GetSocketMessagesWithCallback is correctly added to support the compile-time validation.


42-42: LGTM: Compile-time constraint properly applied.

The type constraint is correctly applied to the interface, providing compile-time safety against callback schemas in server messages.


118-126: LGTM: Runtime validation logic is robust and well-implemented.

The runtime validation correctly:

  • Filters server message entries for those with callback properties
  • Uses truthiness check to identify actual callback schemas (not just undefined properties)
  • Provides a clear, descriptive error message listing the problematic message keys

This complements the compile-time validation and ensures runtime safety.

packages/core/test/zodNamespace.test.ts (2)

6-14: LGTM: Clean stub server implementation.

The stub server provides the minimal interface needed for testing without unnecessary complexity. The chainable pattern for the namespace methods is well-implemented.


16-38: LGTM: Comprehensive test coverage for callback validation.

The test correctly:

  • Sets up a server message schema with a callback
  • Verifies that the ZodNamespace constructor throws an error
  • Uses appropriate regex matching for the error message

This test provides good coverage for the runtime validation added in zodNamespace.ts.

packages/core/src/v3/zodMessageHandler.test.ts (2)

6-21: LGTM: Comprehensive test for payload-wrapped messages.

The test correctly verifies the handling of messages with explicit payload fields, ensuring:

  • The handler receives the correct payload
  • The acknowledgment is properly returned
  • The message structure matches expected format

23-35: LGTM: Essential test for direct payload messages.

This test covers the important scenario where messages don't have a payload wrapper, ensuring backward compatibility and proper handling of messages from sources other than ZodMessageSender.

packages/core/src/v3/zodMessageHandler.ts (2)

182-186: LGTM: Enhanced payload detection using Zod validation.

The improvement from a simple property check to Zod schema validation is excellent. This approach:

  • Validates that the message is actually an object with a payload property
  • Uses passthrough() to allow additional properties
  • Provides more reliable detection than "payload" in message

This addresses potential edge cases where a message might have a payload property but not be a valid message structure.


188-195: LGTM: Robust message handling for both scenarios.

The conditional logic correctly handles both message types:

  • Messages with payload: spreads the entire message with the event type
  • Messages without payload: safely extracts version and constructs payload from remaining properties

The defensive programming with the messageObj initialization ensures safety even for non-object messages.

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
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.

1 participant