-
-
Notifications
You must be signed in to change notification settings - Fork 127
fix(zod): avoid importing Prisma enum, recognize enum fields with default #2307
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
Conversation
📝 WalkthroughWalkthroughRemoved the Changes
Sequence Diagram(s)sequenceDiagram
participant Generator as ZodGenerator
participant Prelude as addPreludeAndImports
participant Writer as CodeBlockWriter
participant SchemaGen as getFieldSchemaDefault
participant Transformer as TypeScriptExpressionTransformer
rect `#eef2ff`
Generator->>Prelude: addPreludeAndImports(decl, writer)
note right of Prelude `#f0fff4`: no `output` arg\nno conditional `@prisma/client` enum import
Prelude-->>Writer: write prelude & imports
end
rect `#fff7ed`
Generator->>SchemaGen: getFieldSchemaDefault(field)
alt default is enum ref
SchemaGen->>Transformer: transform enum reference (useLiteralEnum: true)
Transformer-->>Generator: return JSON string literal (e.g. "Ex1")
else other literals
SchemaGen-->>Generator: return literal/default expression
end
Generator->>Writer: emit field default using returned value
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes
Possibly related PRs
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this 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
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
packages/schema/src/plugins/zod/generator.ts (1)
389-398: Remove dead code:importEnumscollection is no longer used.This code collects enum references but never uses the
importEnumsSet. Since the fix avoids importing enums from@prisma/client(usinguseLiteralEnuminstead), this entire block can be removed.Apply this diff to remove the dead code:
- // import user-defined enums from Prisma as they might be referenced in the expressions - const importEnums = new Set<string>(); - for (const node of streamAllContents(decl)) { - if (isEnumFieldReference(node)) { - const field = node.target.ref as EnumField; - if (!isFromStdlib(field.$container)) { - importEnums.add(field.$container.name); - } - } - } - // import enum schemas
🧹 Nitpick comments (1)
packages/schema/src/plugins/zod/generator.ts (1)
729-729: Minor: Consider moving the closing brace to a new line for readability.The closing brace and semicolon are now on the same line as the template literal. Placing them on a new line would improve readability:
- export function ${refineFuncName}<T>(schema: z.ZodType<T>) { return schema${refinements.join('\n')}; + export function ${refineFuncName}<T>(schema: z.ZodType<T>) { + return schema${refinements.join('\n')}; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
packages/schema/src/plugins/zod/generator.ts(5 hunks)packages/sdk/src/typescript-expression-transformer.ts(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
packages/schema/src/plugins/zod/generator.ts (1)
packages/language/src/ast.ts (1)
DataModel(56-66)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
- GitHub Check: build-test (20.x)
- GitHub Check: build-test (20.x)
- GitHub Check: dependency-review
- GitHub Check: build-test (20.x)
🔇 Additional comments (5)
packages/sdk/src/typescript-expression-transformer.ts (2)
43-43: LGTM: Clean addition of useLiteralEnum option.The new optional boolean follows the existing pattern and enables the enum-to-literal transformation needed to fix issue #2291.
395-398: LGTM: Correct implementation of enum literal transformation.The conditional logic properly returns the enum name as a JSON string literal when
useLiteralEnumis true, avoiding the need for external enum imports.packages/schema/src/plugins/zod/generator.ts (3)
386-386: LGTM: Unused parameter correctly removed.The
outputparameter was not used in the method body. Removing it simplifies the signature.
343-343: LGTM: Call sites correctly updated.Both invocations of
addPreludeAndImportsproperly reflect the updated signature.Also applies to: 447-447
763-763: LGTM: Correct usage of useLiteralEnum for validation rules.Setting
useLiteralEnum: trueensures enum references in@@validateexpressions are rendered as string literals, eliminating the need for@prisma/clientimports.
fixes #2291