Skip to content

fix(zod): apply propertyNames to string schemas in JSON Schema gen#217

Merged
dinwwwh merged 1 commit intomainfrom
fix/zod/property-names
Mar 10, 2025
Merged

fix(zod): apply propertyNames to string schemas in JSON Schema gen#217
dinwwwh merged 1 commit intomainfrom
fix/zod/property-names

Conversation

@dinwwwh
Copy link
Copy Markdown
Member

@dinwwwh dinwwwh commented Mar 10, 2025

Summary by CodeRabbit

  • Tests
    • Introduced an edge-case test to verify proper handling of schemas with date-formatted keys.
  • Refactor
    • Streamlined the logic for processing record key types, simplifying the conversion of schema definitions.

@vercel
Copy link
Copy Markdown

vercel Bot commented Mar 10, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
orpc ✅ Ready (Inspect) Visit Preview 💬 Add feedback Mar 10, 2025 0:57am

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Mar 10, 2025

Walkthrough

This pull request adds a new test case covering record schemas with date-formatted keys and refines the conversion logic in the ZodToJsonSchemaConverter. The test case in the edgeCases array ensures proper handling of keys when using z.record(z.string().date(), z.string()) and designates it to be ignored for JSON Schema conversion. Meanwhile, the converter now directly converts the key type and evaluates its structure rather than relying on a specific type name check.

Changes

File Summary
packages/zod/src/converter.test.ts Added a new test case in the edgeCases array using a schema with z.record(z.string().date(), z.string()), with defined input and the ignoreZodToJsonSchema flag set.
packages/zod/src/converter.ts Modified the ZodToJsonSchemaConverter to replace the type name check with a direct conversion of the key type and a structural evaluation of the resulting JSON representation.

Sequence Diagram(s)

sequenceDiagram
  participant TestSuite as Test Suite
  participant Converter as ZodToJsonSchemaConverter
  participant Schema as ZodRecord Key Schema

  TestSuite->>Converter: Invoke conversion for record schema
  Converter->>Schema: Convert key schema using convert()
  Schema-->>Converter: Return key JSON (keyJson)
  Converter->>Converter: Evaluate keyJson structure to determine type
  Converter-->>TestSuite: Return converted schema
Loading

Possibly related PRs

  • unnoq/orpc#211: Modifies the edgeCases array for a z.record schema by focusing on the handling of test cases with date-formatted keys, indicating a similar approach to schema testing.

Poem

I'm a happy code rabbit, hopping through the night,
New test cases bloom, giving date keys light.
I nibble on logic, with a carrot for cheer,
Converter changes make my path so clear.
In the garden of code, I dance with delight!
🥕🐰

Warning

There were issues while running some tools. Please review the errors and either fix the tool’s configuration or disable the tool if it’s a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

 ERR_PNPM_OPTIONAL_DEPS_REQUIRE_PROD_DEPS  Optional dependencies cannot be installed without production dependencies

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
  • @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.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 10, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

📢 Thoughts on this report? Let us know!

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Mar 10, 2025

Open in Stackblitz

More templates

@orpc/client

npm i https://pkg.pr.new/@orpc/client@217

@orpc/contract

npm i https://pkg.pr.new/@orpc/contract@217

@orpc/openapi-client

npm i https://pkg.pr.new/@orpc/openapi-client@217

@orpc/react-query

npm i https://pkg.pr.new/@orpc/react-query@217

@orpc/openapi

npm i https://pkg.pr.new/@orpc/openapi@217

@orpc/server

npm i https://pkg.pr.new/@orpc/server@217

@orpc/shared

npm i https://pkg.pr.new/@orpc/shared@217

@orpc/standard-server

npm i https://pkg.pr.new/@orpc/standard-server@217

@orpc/standard-server-fetch

npm i https://pkg.pr.new/@orpc/standard-server-fetch@217

@orpc/standard-server-node

npm i https://pkg.pr.new/@orpc/standard-server-node@217

@orpc/vue-colada

npm i https://pkg.pr.new/@orpc/vue-colada@217

@orpc/vue-query

npm i https://pkg.pr.new/@orpc/vue-query@217

@orpc/zod

npm i https://pkg.pr.new/@orpc/zod@217

commit: 8aee3fc

Copy link
Copy Markdown

@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)
packages/zod/src/converter.ts (1)

416-418: Improved key type handling for ZodRecord

The change improves the way record key types are processed by directly converting and examining the structure rather than relying on type name checks. This correctly handles string key types with additional constraints (like date formatting).

Consider adding a comment explaining the intention behind this check:

 const [__, keyJson] = this.convert(schema_._def.keyType, options, lazyDepth, false, false)
+// Apply propertyNames constraint if the key schema is anything more complex than a basic string type
 if (Object.entries(keyJson).some(([k, v]) => k !== 'type' || v !== 'string')) {
   json.propertyNames = keyJson
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 9adcd05 and 8aee3fc.

📒 Files selected for processing (2)
  • packages/zod/src/converter.test.ts (1 hunks)
  • packages/zod/src/converter.ts (1 hunks)
🔇 Additional comments (1)
packages/zod/src/converter.test.ts (1)

408-412: Good test case addition for date-formatted string keys

This test case correctly ensures that record schemas with date-formatted string keys have the appropriate propertyNames constraint in the generated JSON Schema.

@dinwwwh dinwwwh merged commit 3b1dac3 into main Mar 10, 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.

1 participant