-
-
Notifications
You must be signed in to change notification settings - Fork 95
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(delegate): clean up generated zod schemas for delegate auxiliary fields #2003
Conversation
📝 WalkthroughWalkthroughThis pull request refines the schema generation process by enhancing the filtering logic for delegate auxiliary models. In the Zod schema generator, filtering now excludes any types or fields that include the Changes
Sequence Diagram(s)sequenceDiagram
participant Client as Schema Generation Process
participant Generator as ZodSchemaGenerator
participant Transformer as Transformer
Client->>Generator: Call generate()
Generator->>Generator: Filter out types with DELEGATE_AUX_RELATION_PREFIX
Generator->>Generator: Process object schemas and filter delegate auxiliary fields
Generator->>Transformer: Invoke generateEnumSchemas()
Transformer->>Transformer: Filter enum values with DELEGATE_AUX_RELATION_PREFIX
Transformer->>Transformer: Call mapDelegateInputType() if input type requires transformation
Transformer-->>Generator: Return transformed enum schema
Generator-->>Client: Return complete schema
Possibly related PRs
✨ Finishing Touches
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? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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
🧹 Nitpick comments (2)
tests/regression/tests/issue-1993.test.ts (1)
1-46
: Regression test addition.
This new test suite neatly verifies schema handling for delegate auxiliary fields. Consider including an explicit assertion that generated code excludes delegate-prefixed types, ensuring thorough coverage.packages/schema/src/plugins/zod/generator.ts (1)
246-247
: Field-level exclusion withstartsWith()
.
Filtering fields bystartsWith(DELEGATE_AUX_RELATION_PREFIX)
complements the.includes()
checks above. Although slightly different from the earlierincludes()
, it suits prefix-based naming conventions for fields.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
packages/schema/src/plugins/zod/generator.ts
(3 hunks)packages/schema/src/plugins/zod/transformer.ts
(4 hunks)tests/regression/tests/issue-1993.test.ts
(1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (6)
- GitHub Check: dependency-review
- 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 (8)
packages/schema/src/plugins/zod/transformer.ts (5)
2-2
: New constant import looks correct.
No issues with importingDELEGATE_AUX_RELATION_PREFIX
from'@zenstackhq/runtime'
; this aligns well with the later filtering logic.
7-7
: Addition ofisDelegateModel
import.
Good addition that supports delegate-model detection.
72-76
: Enum value filtering with delegate prefix.
Excluding enum values starting withDELEGATE_AUX_RELATION_PREFIX
avoids polluting user-facing schemas with internal fields. Implementation is direct and maintains clarity.
252-262
: Reduced input types for delegate references.
Filtering and remapping delegate-specific input types is appropriate. Ensures only relevant names are imported and avoids generating extraneous schemas.
302-327
: NewmapDelegateInputType
method.
Regex approach and the conditional remapping logic are clear and well-handled. Gracefully returns early if patterns are not matched. This is a solid addition for delegate input type transformation.packages/schema/src/plugins/zod/generator.ts (3)
1-1
: New constant import.
Pulling inDELEGATE_AUX_RELATION_PREFIX
is consistent with the intended filtering logic.
92-98
: Exclusion of delegate aux input object types.
Excluding input object types that match the prefix prevents generating unwanted auxiliary schemas. The.includes()
usage is appropriate for internal partial matches.
100-104
: Exclusion of delegate aux output object types.
Mirrors the input object logic to prevent extraneous output object generation. This symmetry is well implemented.
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
🧹 Nitpick comments (1)
packages/schema/src/plugins/zod/transformer.ts (1)
306-344
: Add error handling and documentation to the delegate input type mapping.While the implementation is logically sound, consider these improvements:
Add JSDoc documentation to explain:
- The purpose of the method
- The format of input type names it expects to handle
- The transformation logic
- Return value description
Add error handling for edge cases:
- When regex match fails
- When
oppositeRelationField
is not foundHere's a suggested improvement:
+/** + * Maps delegate model input types to their base types. + * E.g.: "UserCreateNestedOneWithoutDelegate_aux_PostInput" => "UserCreateWithoutAssetInput" + * + * @param inputType - The input type reference to transform + * @param contextDataModel - The data model providing context + * @param contextFieldName - The field name in the context model + * @returns The transformed input type reference or the original if no transformation needed + */ private mapDelegateInputType( inputType: PrismaDMMF.InputTypeRef, contextDataModel: DataModel, contextFieldName: string ) { const contextField = contextDataModel.fields.find((f) => f.name === contextFieldName); if (!contextField || !isDataModel(contextField.type.reference?.ref)) { return inputType; } if (!contextField.$inheritedFrom || !isDelegateModel(contextField.$inheritedFrom)) { return inputType; } let processedInputType = inputType; const match = inputType.type.match(/^(\S+?)((NestedOne)?WithoutDelegate_aux\S+?)((Nested)?Input)$/); if (match) { let mappedInputTypeName = match[1]; if (contextDataModel) { const oppositeRelationField = getRelationBackLink(contextField); + // Return early if we can't find the opposite relation + if (!oppositeRelationField) { + return inputType; + } mappedInputTypeName += `Without${upperCaseFirst(oppositeRelationField.name)}`; } mappedInputTypeName += match[4]; processedInputType = { ...inputType, type: mappedInputTypeName }; } return processedInputType; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
packages/schema/src/plugins/zod/transformer.ts
(4 hunks)packages/sdk/src/model-meta-generator.ts
(2 hunks)packages/sdk/src/utils.ts
(1 hunks)tests/regression/tests/issue-1993.test.ts
(1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- tests/regression/tests/issue-1993.test.ts
⏰ Context from checks skipped due to timeout of 90000ms (6)
- GitHub Check: build-test (20.x)
- GitHub Check: build-test (20.x)
- GitHub Check: dependency-review
- GitHub Check: build-test (20.x)
- GitHub Check: Analyze (javascript-typescript)
- GitHub Check: OSSAR-Scan
🔇 Additional comments (6)
packages/sdk/src/model-meta-generator.ts (2)
32-32
: LGTM!The import of
getRelationBackLink
is correctly added to replace the removedgetBackLink
function.
291-291
: LGTM!The usage of
getRelationBackLink
is correctly implemented to replace the removedgetBackLink
function.packages/sdk/src/utils.ts (2)
636-674
: LGTM! ThegetRelationBackLink
function is well-implemented.The function correctly handles:
- Type reference validation
- Source model determination based on inheritance
- Relation name matching for backlinks
- Edge cases like self-references
676-685
: LGTM! ThegetRelationName
function is concise and focused.The function efficiently extracts the relation name from the
@relation
attribute.packages/schema/src/plugins/zod/transformer.ts (2)
2-2
: LGTM! Required imports added for delegate auxiliary field handling.The new imports are correctly added and necessary for the enhanced delegate model handling functionality.
Also applies to: 5-5, 8-8
73-77
: LGTM! Proper filtering of delegate auxiliary fields from enum schemas.The implementation correctly filters out enum values that start with
DELEGATE_AUX_RELATION_PREFIX
before generating the schema, which aligns with the PR's objective of cleaning up generated Zod schemas for delegate auxiliary fields.
fixes #1993