Skip to content

fix(JSON): @default not working for @JSON types & Typed JSON lists generating invalid Zod schemas #2040

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

Merged
merged 2 commits into from
Mar 13, 2025

Conversation

diesal11
Copy link
Contributor

Fixes #2039

Copy link
Contributor

coderabbitai bot commented Mar 12, 2025

📝 Walkthrough

Walkthrough

The changes update type validation and schema transformation logic. In the attribute validator, a new check ensures that if a Typed JSON field’s parameter is marked as default, its resolved type must be a string. In the transformer, the schema generation now conditionally appends .array() to lazy-loaded schema strings when handling arrays. Additionally, a new regression test for issue 2039 has been added to verify the proper application of default values and the generation of Zod schemas.

Changes

File(s) Change Summary
packages/schema/.../validator/attribute-application-validator.ts Updated assignableToAttributeParam to require the resolved argument to be a string when the container is a Typed JSON field with a default parameter; formatting.
packages/schema/.../transformer.ts Modified generateObjectSchemaField to check if the field is an array (using isList) and conditionally append .array() to the lazy-loaded schema string.
tests/regression/.../issue-2039.test.ts Added a new regression test to validate issue 2039, ensuring that default values are correctly applied and the Zod schema validation functions as expected.

Sequence Diagram(s)

sequenceDiagram
  participant Caller
  participant Validator
  note over Validator: assignableToAttributeParam(...)
  Caller->>Validator: Call assignableToAttributeParam(arg, param)
  Validator->>Validator: Check if container is Typed JSON field
  Validator->>Validator: If parameter is marked as @default, verify resolved type is string
  Validator->>Caller: Return validation result
Loading
sequenceDiagram
  participant Transformer
  participant SchemaBuilder
  Transformer->>SchemaBuilder: generateObjectSchemaField(field)
  SchemaBuilder->>SchemaBuilder: Determine if field is an array (isList)
  alt isList true
    SchemaBuilder->>SchemaBuilder: Append .array() to lazy-loaded schema string
  else isList false
    SchemaBuilder->>SchemaBuilder: Use lazy-loaded schema string as is
  end
  SchemaBuilder->>Transformer: Return constructed alternatives
Loading

Tip

⚡🧪 Multi-step agentic review comment chat (experimental)
  • We're introducing multi-step agentic chat in review comments. This experimental feature enhances review discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments.
    - To enable this feature, set early_access to true under in the settings.

📜 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 81dec88 and 570512f.

📒 Files selected for processing (1)
  • packages/schema/src/language-server/validator/attribute-application-validator.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/schema/src/language-server/validator/attribute-application-validator.ts
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: OSSAR-Scan
  • GitHub Check: build-test (20.x)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: build-test (20.x)
  • GitHub Check: build-test (20.x)

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

‼️ IMPORTANT
Auto-reply has been disabled for this repository in the CodeRabbit settings. The CodeRabbit bot will not respond to your replies unless it is explicitly tagged.

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

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 d4fb5ab and 81dec88.

📒 Files selected for processing (3)
  • packages/schema/src/language-server/validator/attribute-application-validator.ts (2 hunks)
  • packages/schema/src/plugins/zod/transformer.ts (1 hunks)
  • tests/regression/tests/issue-2039.test.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: build-test (20.x)
  • GitHub Check: build-test (20.x)
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: build-test (20.x)
  • GitHub Check: OSSAR-Scan
🔇 Additional comments (3)
packages/schema/src/language-server/validator/attribute-application-validator.ts (2)

270-275: Good implementation for the Typed JSON field validation.

This addition properly handles the case where a field is Typed JSON with a @default parameter, ensuring that the argument must be a string. This is critical because default values for JSON fields need to be serialized as strings that can be parsed back into their object form.


336-337: Clean code separation with proper spacing.

The empty line added here improves readability by separating logical blocks of code.

tests/regression/tests/issue-2039.test.ts (1)

1-42: Well-structured regression test for issue #2039.

This test comprehensively validates both aspects of the fix:

  1. It verifies that default values are correctly set for both single JSON objects (foo) and arrays of JSON objects (fooList) when creating a new record
  2. It confirms that the generated Zod schemas correctly validate JSON input data

The test case is properly isolated with its own PostgreSQL database and uses realistic JSON values in the default attributes.

@ymc9 ymc9 changed the base branch from main to dev March 12, 2025 22:11
Copy link
Member

@ymc9 ymc9 left a comment

Choose a reason for hiding this comment

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

Thank you for making the fix @diesal11 ! I've added a minor comments. I'll make the adjustment and release a patch 😄.

@ymc9 ymc9 merged commit 7632f24 into zenstackhq:dev Mar 13, 2025
11 checks passed
@diesal11 diesal11 deleted the fix/issue-2039 branch March 13, 2025 02:26
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.

Strong typed JSON fields don't support @default and generate invalid Zod schemas when they are a List
2 participants