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

fix(json): false error when using typed json in multi-file schemas #1836

Merged
merged 1 commit into from
Nov 7, 2024

Conversation

ymc9
Copy link
Member

@ymc9 ymc9 commented Nov 7, 2024

fixes #1883

Copy link
Contributor

coderabbitai bot commented Nov 7, 2024

📝 Walkthrough

Walkthrough

This pull request includes updates to multiple files, primarily focusing on version increments and validation logic adjustments. The version of the JetBrains plugin in build.gradle.kts is updated from "2.8.0" to "2.8.1". In datamodel-validator.ts, validation logic related to data source provider restrictions for custom-typed fields is removed. The schema-generator.ts introduces a new method for validating type definition fields based on the data source provider and adds a constant for supported providers. Additionally, the test cases in validation.test.ts are modified to reflect changes in JSON field validation.

Changes

File Path Change Summary
packages/ide/jetbrains/build.gradle.kts Version updated from "2.8.0" to "2.8.1".
packages/schema/src/language-server/validator/datamodel-validator.ts Removed import for getDataSourceProvider and related validation logic for custom-typed fields.
packages/schema/src/plugins/prisma/schema-generator.ts Added method ensureSupportingTypeDefFields for validating type definition fields; introduced constant PROVIDERS_SUPPORTING_TYPEDEF_FIELDS. Updated generateModelField method to include new validation.
tests/integration/tests/enhancements/json/validation.test.ts Updated test cases to reflect broader validation of JSON fields across different data source providers.

Possibly related PRs

  • merge dev to main (v2.1.0) #1438: This PR updates the version in build.gradle.kts from "2.0.3" to "2.1.0", similar to the version bump in the main PR from "2.8.0" to "2.8.1".
  • chore: bump version #1459: This PR updates the version in build.gradle.kts from "2.1.1" to "2.1.2", reflecting a similar version increment as in the main PR.
  • chore: bump version #1481: This PR updates the version in build.gradle.kts from "2.1.2" to "2.2.0", continuing the pattern of version updates seen in the main PR.
  • chore: bump version #1729: This PR updates the version in build.gradle.kts from "2.5.2" to "2.6.0", which is another example of a version bump like the one in the main PR.
  • merge dev to main (v2.6.2) #1752: This PR updates the version in build.gradle.kts from "2.6.1" to "2.6.2", aligning with the version update in the main PR.
  • chore: bump version #1778: This PR updates the version in build.gradle.kts from "2.6.2" to "2.7.0", showing a similar version increment.
  • chore: bump version #1788: This PR updates the version in build.gradle.kts from "2.7.0" to "2.7.1", consistent with the version change in the main PR.
  • chore: bump version #1792: This PR updates the version in build.gradle.kts from "2.7.1" to "2.7.2", reflecting the same type of change as in the main PR.
  • merge dev to main (v2.7.2) #1793: This PR updates the version in build.gradle.kts from "2.7.1" to "2.7.2", similar to the version bump in the main PR.
  • merge dev to main (v2.7.3) #1800: This PR updates the version in build.gradle.kts from "2.7.2" to "2.7.3", which is another example of a version increment.
  • chore: bump version #1807: This PR updates the version in build.gradle.kts from "2.7.3" to "2.7.4", consistent with the version change in the main PR.
  • merge dev to main (v2.7.4) #1808: This PR updates the version in build.gradle.kts from "2.7.3" to "2.7.4", reflecting a similar version increment as in the main PR.

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:

‼️ 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 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: 0

🧹 Outside diff range and nitpick comments (3)
tests/integration/tests/enhancements/json/validation.test.ts (1)

4-4: Update test description to match the actual test

The test description "is only supported by postgres" is inconsistent with the actual test, which verifies that SQLite doesn't support JSON fields.

Consider updating to something like "is not supported by sqlite" or "validates datasource provider support".

packages/schema/src/plugins/prisma/schema-generator.ts (2)

85-85: Consider adding other databases that support JSON.

While PostgreSQL has excellent JSON support, other databases like MySQL and MariaDB also have native JSON capabilities. Consider expanding PROVIDERS_SUPPORTING_TYPEDEF_FIELDS to include these providers if they are compatible with your JSON field implementation.


852-857: LGTM! Clear and effective validation implementation.

The validation logic is well-implemented with a descriptive error message. However, since the method only uses the provider information, consider optimizing the parameter:

-    private ensureSupportingTypeDefFields(zmodel: Model) {
-        const dsProvider = getDataSourceProvider(zmodel);
+    private ensureSupportingTypeDefFields(provider: string | undefined = getDataSourceProvider(this.zmodel)) {
         if (dsProvider && !PROVIDERS_SUPPORTING_TYPEDEF_FIELDS.includes(dsProvider)) {
             throw new PluginError(name, `Datasource provider "${dsProvider}" does not support "@json" fields`);
         }
     }

This change would:

  1. Make the method more flexible by accepting a provider directly
  2. Default to the current behavior when no provider is passed
  3. Reduce the coupling with the Model type
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 13f95d2 and e7d2be4.

⛔ Files ignored due to path filters (13)
  • package.json is excluded by !**/*.json
  • packages/ide/jetbrains/package.json is excluded by !**/*.json
  • packages/language/package.json is excluded by !**/*.json
  • packages/misc/redwood/package.json is excluded by !**/*.json
  • packages/plugins/openapi/package.json is excluded by !**/*.json
  • packages/plugins/swr/package.json is excluded by !**/*.json
  • packages/plugins/tanstack-query/package.json is excluded by !**/*.json
  • packages/plugins/trpc/package.json is excluded by !**/*.json
  • packages/runtime/package.json is excluded by !**/*.json
  • packages/schema/package.json is excluded by !**/*.json
  • packages/sdk/package.json is excluded by !**/*.json
  • packages/server/package.json is excluded by !**/*.json
  • packages/testtools/package.json is excluded by !**/*.json
📒 Files selected for processing (4)
  • packages/ide/jetbrains/build.gradle.kts (1 hunks)
  • packages/schema/src/language-server/validator/datamodel-validator.ts (0 hunks)
  • packages/schema/src/plugins/prisma/schema-generator.ts (4 hunks)
  • tests/integration/tests/enhancements/json/validation.test.ts (2 hunks)
💤 Files with no reviewable changes (1)
  • packages/schema/src/language-server/validator/datamodel-validator.ts
✅ Files skipped from review due to trivial changes (1)
  • packages/ide/jetbrains/build.gradle.kts
🔇 Additional comments (5)
tests/integration/tests/enhancements/json/validation.test.ts (3)

1-1: LGTM: Import statement updated correctly

The addition of loadSchema import is appropriate for the new test implementation.


Line range hint 21-37: Verify if second test case needs similar updates

While this test case remains unchanged and appears correct, let's verify if it should also use loadSchema for consistency with the first test.

#!/bin/bash
# Check for similar validation patterns in other test files
rg -A 5 'loadModelWithError.*@json' tests/

Line range hint 6-19: LGTM: Appropriate validation approach

The switch to loadSchema and the updated error message correctly validate the database provider support for JSON fields.

Let's verify if this is the only test affected by this change:

✅ Verification successful

Based on the analysis of the test files, I can now provide a final response:

The JSON field validation is correctly implemented for SQLite provider

The review comment is correct and no other test files need similar updates because:

  1. The JSON field validation test in validation.test.ts specifically tests SQLite's lack of support for JSON fields
  2. Other JSON-related tests in typing.test.ts and crud.test.ts are intentionally using PostgreSQL as the provider since they test the actual JSON functionality
  3. The zod.test.ts file uses PostgreSQL for different plugin-related tests and doesn't involve JSON field validation
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for other tests that might need similar updates
rg -l 'only supported by postgres' tests/

Length of output: 44


Script:

#!/bin/bash
# Search for tests involving JSON fields and error messages
rg -A 5 '@json.*provider' tests/
rg -A 5 'provider.*@json' tests/

# Search for any test files containing JSON field tests
rg -l '@json' tests/

Length of output: 890


Script:

#!/bin/bash
# Let's check the content of these files to see if they need similar provider validation
rg -A 5 'loadSchema.*@json|loadModelWithError.*@json' tests/integration/tests/plugins/zod.test.ts tests/integration/tests/enhancements/json/typing.test.ts tests/integration/tests/enhancements/json/crud.test.ts

Length of output: 211


Script:

#!/bin/bash
# Let's check for any JSON field tests with different patterns
rg -B 2 -A 5 '@json.*{' tests/integration/tests/plugins/zod.test.ts tests/integration/tests/enhancements/json/typing.test.ts tests/integration/tests/enhancements/json/crud.test.ts

# Also check for any provider-related tests in these files
rg -B 2 -A 5 'provider|sqlite|postgres' tests/integration/tests/plugins/zod.test.ts tests/integration/tests/enhancements/json/typing.test.ts tests/integration/tests/enhancements/json/crud.test.ts

Length of output: 27895

packages/schema/src/plugins/prisma/schema-generator.ts (2)

799-802: LGTM! Well-structured JSON field type handling.

The implementation correctly validates provider support before setting the field type to 'Json', ensuring type safety and compatibility.


852-857: Verify type definition usage across providers.

Let's ensure there are no existing type definitions being used with unsupported providers.

✅ Verification successful

Let me try a different approach to gather the necessary information.

The previous pattern matching with ast-grep didn't yield results, possibly due to the syntax. Let's try with ripgrep to find actual usage patterns in the codebase. This will help us verify:

  1. Where @JSON attributes are being used
  2. What providers are configured
  3. Any type definitions that might be affected

Based on the search results, I can now make a definitive assessment. The code shows:

  1. The @json attribute is used in test files with proper validation that checks for supported providers
  2. There's a specific test that verifies the error "Datasource provider 'sqlite' does not support '@JSON' fields"
  3. The code in question is part of the validation logic that ensures JSON fields are only used with supported providers

The changes are properly validated through tests and the error handling is consistent with the codebase's requirements. There's no evidence of incorrect usage of type definitions with unsupported providers in the actual codebase, only in controlled test scenarios.

The validation for JSON field support across providers is correctly implemented and tested.

  • The error message is properly tested in tests/integration/tests/enhancements/json/validation.test.ts
  • Provider validation is consistently applied across the codebase
  • Test cases cover both supported and unsupported provider scenarios
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Find all type definitions and their usage across different providers

# Find all type definitions
echo "Looking for type definition declarations..."
ast-grep --pattern 'type $name {
  $$$
}'

# Find all data source declarations with their providers
echo -e "\nChecking datasource providers..."
ast-grep --pattern 'datasource $name {
  provider = $provider
  $$$
}'

# Find all fields using type definitions
echo -e "\nChecking type definition usage in models..."
ast-grep --pattern 'model $model {
  $$$
  $field: $type
  $$$
}'

Length of output: 465


Script:

#!/bin/bash
# Search for @json attribute usage and type definitions in schema files
echo "Searching for @json attribute usage..."
rg "@json" -A 2 -B 2

echo -e "\nSearching for datasource provider declarations..."
rg "provider\s*=" -A 2 -B 2

echo -e "\nSearching for type definitions..."
rg "^type\s+\w+" -A 5

Length of output: 142989

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