-
-
Notifications
You must be signed in to change notification settings - Fork 90
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(zod): zod create/update schemas should exclude discriminator fields #1609
Conversation
WalkthroughWalkthroughThis update enhances the handling of discriminator fields in schema generation and processing within the application. The changes simplify the logic for excluding these fields from mutation schemas, thereby increasing code clarity and maintainability. Additionally, new tests have been introduced to validate schema behavior, ensuring data integrity and robustness in the context of polymorphic models. Changes
Assessment against linked issues
Possibly related issues
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 as PR comments)
Additionally, you can add 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: 1
Outside diff range, codebase verification and nitpick comments (2)
tests/integration/tests/enhancements/with-delegate/plugin-interaction.test.ts (1)
59-61
: Add a descriptive comment for the new test case.Adding a comment describing the purpose of the test case will improve readability and maintainability.
+ // Test case for validating Zod schemas related to video assets. it('zod', async () => { const { zodSchemas } = await loadSchema(POLYMORPHIC_SCHEMA, { fullZod: true });
packages/schema/src/plugins/enhancer/enhance/index.ts (1)
499-500
: Add a descriptive comment for the regex pattern.Adding a comment describing the purpose of the regex pattern will improve readability and maintainability.
+ // Regex pattern to match model create/update input types. private readonly ModelCreateUpdateInputRegex = /(\S+)(Unchecked)?(Create|Update).*Input/;
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (4)
- packages/schema/src/plugins/enhancer/enhance/index.ts (3 hunks)
- packages/schema/src/plugins/zod/generator.ts (6 hunks)
- packages/schema/src/plugins/zod/transformer.ts (2 hunks)
- tests/integration/tests/enhancements/with-delegate/plugin-interaction.test.ts (1 hunks)
Additional context used
Biome
packages/schema/src/plugins/zod/transformer.ts
[error] 93-93: Use a regular expression literal instead of the RegExp constructor.
Regular expression literals avoid some escaping required in a string literal, and are easier to analyze statically.
Safe fix: Use a literal notation instead.(lint/complexity/useRegexLiterals)
Additional comments not posted (17)
tests/integration/tests/enhancements/with-delegate/plugin-interaction.test.ts (9)
72-80
: Ensure extraneous fields are stripped correctly.The test checks that extraneous fields like
videoType
are stripped from the output. Ensure this behavior is consistent across all schemas.Verification successful
Extraneous fields are stripped correctly across all schemas.
The tests in various files confirm that extraneous fields like
videoType
are consistently stripped from the output across different schemas.
tests/integration/tests/enhancements/with-delegate/plugin-interaction.test.ts
tests/integration/tests/plugins/zod.test.ts
tests/regression/tests/issue-886.test.ts
tests/regression/tests/issue-1268.test.ts
packages/schema/src/cli/config.ts
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that extraneous fields are stripped correctly in all schemas. # Test: Search for schema definitions and validation logic. Expect: Consistent behavior across all schemas. rg --type ts -A 5 $'.parse({'Length of output: 121
Script:
#!/bin/bash # Description: Verify that extraneous fields are stripped correctly in all schemas. # Test: Search for schema definitions and validation logic. Expect: Consistent behavior across all schemas. rg --type ts -A 5 '\.parse({'Length of output: 124
Script:
#!/bin/bash # Description: Verify that extraneous fields are stripped correctly in all schemas. # Test: Search for schema definitions and validation logic. Expect: Consistent behavior across all schemas. rg --type ts -A 10 '\.parse\({'Length of output: 117
Script:
#!/bin/bash # Description: Verify that extraneous fields are stripped correctly in all schemas. # Test: Search for schema definitions and validation logic. Expect: Consistent behavior across all schemas. rg --type ts -A 10 '\.parse\(\{'Length of output: 12410
113-119
: Ensure discriminator is not set in update schema.The test checks that the
assetType
discriminator is not set in the output of the update schema. Ensure this behavior is consistent across all update schemas.Verification successful
Verification successful: Discriminator is not set in the update schema.
The test cases consistently ensure that the
assetType
discriminator is not set in the output of the update schemas.
tests/integration/tests/enhancements/with-delegate/plugin-interaction.test.ts
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that the discriminator is not set in the output of the update schema. # Test: Search for update schema definitions and validation logic. Expect: Consistent behavior across all update schemas. rg --type ts -A 5 $'UpdateSchema'Length of output: 8658
Script:
#!/bin/bash # Extract generator logic related to filtering out discriminator fields ast-grep --lang typescript --pattern $'function makeFieldSchema(field) { $$$ if (!isDiscriminatorField(field)) { $$$ } }' packages/schema/src/plugins/zod/generator.ts # Search for test cases that validate the absence of discriminator fields in update schemas rg --type ts 'discriminator should not be set' -A 5Length of output: 2240
62-70
: Ensure comprehensive asset object validation.The test validates that the
AssetSchema
correctly parses asset objects. Ensure that all required fields are validated.
105-111
: Ensure discriminator is not set in create schema.The test checks that the
assetType
discriminator is not set in the output of the create schema. Ensure this behavior is consistent across all create schemas.Verification successful
Ensure discriminator is not set in create schema.
The test checks that the
assetType
discriminator is not set in the output of the create schema. The generator file confirms that discriminator fields are omitted from all create schemas using theomitDiscriminators
logic.
packages/schema/src/plugins/zod/generator.ts
: TheomitDiscriminators
ensures that discriminator fields are excluded from the create schemas.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that the discriminator is not set in the output of the create schema. # Test: Search for create schema definitions and validation logic. Expect: Consistent behavior across all create schemas. rg --type ts -A 5 $'CreateSchema'Length of output: 13274
Script:
#!/bin/bash # Description: Verify that the discriminator is not set in the output of the create schema across all schemas. # Search for schema creation in the generator file and check for exclusion of the discriminator. rg --type ts 'CreateSchema' -A 10 packages/schema/src/plugins/zod/generator.tsLength of output: 1698
Script:
#!/bin/bash # Description: Verify the definition and usage of `omitDiscriminators` to ensure discriminator fields are excluded. # Search for the definition and usage of `omitDiscriminators` in the generator file. rg --type ts 'omitDiscriminators' -A 5 packages/schema/src/plugins/zod/generator.tsLength of output: 1232
128-136
: Ensure comprehensive input object validation.The test validates that the
RatedVideoCreateInputObjectSchema
correctly handles inputs. Ensure that all required fields are validated.
94-103
: Ensure appropriate error handling for missing required fields.The test checks that missing required fields trigger appropriate errors. Ensure that error messages are clear and informative.
138-146
: Ensure appropriate error handling for invalid fields in input object.The test checks that invalid fields trigger appropriate errors in the
RatedVideoCreateInputObjectSchema
. Ensure that error messages are clear and informative.
82-92
: Ensure comprehensive video object validation.The test validates that the
VideoSchema
correctly parses video objects. Ensure that all required fields are validated.
121-126
: Ensure discriminator is not set in Prisma create schema.The test checks that the
assetType
discriminator is not set in the output of the Prisma create schema. Ensure this behavior is consistent across all Prisma create schemas.Verification successful
Ensure discriminator is not set in Prisma create schema.
The
isDiscriminatorField
function correctly identifies discriminator fields, and themakeFieldSchema
function ensures they are not included in the Prisma create schema. This behavior is consistent across the codebase.
packages/sdk/src/utils.ts
:isDiscriminatorField
function implementation.packages/schema/src/plugins/zod/utils/schema-gen.ts
:makeFieldSchema
function implementation.Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that the discriminator is not set in the output of the Prisma create schema. # Test: Search for Prisma create schema definitions and validation logic. Expect: Consistent behavior across all Prisma create schemas. rg --type ts -A 5 $'PrismaCreateSchema'Length of output: 3085
Script:
#!/bin/bash # Extract Prisma create schema definitions and validation logic # Extract UserPrismaCreateSchema and PostPrismaCreateSchema definitions and validation logic rg --type ts -A 10 'UserPrismaCreateSchema' tests/integration/tests/plugins/zod.test.ts rg --type ts -A 10 'PostPrismaCreateSchema' tests/integration/tests/plugins/zod.test.ts # Extract VideoPrismaCreateSchema definition and validation logic rg --type ts -A 10 'VideoPrismaCreateSchema' tests/integration/tests/enhancements/with-delegate/plugin-interaction.test.ts # Extract Prisma create schema generation logic rg --type ts -A 10 'PrismaCreateSchema' packages/schema/src/plugins/zod/generator.tsLength of output: 3028
Script:
#!/bin/bash # Extract implementations of isDiscriminatorField and makeFieldSchema # Extract isDiscriminatorField implementation rg --type ts -A 10 'function isDiscriminatorField' packages/schema/src/plugins/zod/generator.ts # Extract makeFieldSchema implementation rg --type ts -A 10 'function makeFieldSchema' packages/schema/src/plugins/zod/generator.tsLength of output: 190
Script:
#!/bin/bash # Search for definitions of isDiscriminatorField and makeFieldSchema across the entire codebase # Search for isDiscriminatorField definition rg --type ts -A 10 'function isDiscriminatorField' # Search for makeFieldSchema definition rg --type ts -A 10 'function makeFieldSchema'Length of output: 1789
Script:
#!/bin/bash # Extract complete implementations of isDiscriminatorField and makeFieldSchema # Extract complete implementation of isDiscriminatorField rg --type ts -A 20 -B 5 'function isDiscriminatorField' packages/sdk/src/utils.ts # Extract complete implementation of makeFieldSchema rg --type ts -A 20 -B 5 'function makeFieldSchema' packages/schema/src/plugins/zod/utils/schema-gen.tsLength of output: 2253
packages/schema/src/plugins/zod/generator.ts (5)
372-377
: Ensure proper identification of discriminator fields.The code correctly identifies discriminator fields and constructs a string representation for omitting them. Ensure that all discriminator fields are accurately identified.
Verification successful
Discriminator fields are properly identified.
The function
isDiscriminatorField
correctly identifies discriminator fields by checking for the presence of the@@delegate
attribute and ensuring the field name matches the reference target.
packages/sdk/src/utils.ts:462-470
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that all discriminator fields are accurately identified. # Test: Search for discriminator field identification logic. Expect: Accurate identification of all discriminator fields. rg --type ts -A 5 $'isDiscriminatorField'Length of output: 4503
Script:
#!/bin/bash # Description: Retrieve the implementation of the `isDiscriminatorField` function to verify its logic. ast-grep --lang typescript --pattern $'function isDiscriminatorField($_) { $$$ }' packages/sdk/src/utils.tsLength of output: 743
484-485
: Ensure discriminator fields are excluded from create schema.The code correctly excludes discriminator fields from the create schema. Ensure this behavior is consistent across all create schemas.
Verification successful
Ensure discriminator fields are excluded from create schema.
The code correctly excludes discriminator fields from the create schema and this behavior is consistently applied across all create schemas.
packages/schema/src/plugins/zod/generator.ts
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that discriminator fields are excluded from the create schema. # Test: Search for create schema definitions and validation logic. Expect: Consistent exclusion of discriminator fields. rg --type ts -A 5 $'CreateSchema'Length of output: 13274
Script:
#!/bin/bash # Description: Verify the logic for excluding discriminator fields from create schemas in the generator file. # Search for the logic related to discriminator fields in the generator file. rg --type ts 'discriminator' -A 5 packages/schema/src/plugins/zod/generator.tsLength of output: 461
Script:
#!/bin/bash # Description: Verify the consistent application of the omitDiscriminators logic in create schemas. # Search for the usage of omitDiscriminators in the generator file. rg --type ts 'omitDiscriminators' -A 5 packages/schema/src/plugins/zod/generator.tsLength of output: 1232
536-537
: Ensure discriminator fields are excluded from update schema.The code correctly excludes discriminator fields from the update schema. Ensure this behavior is consistent across all update schemas.
Verification successful
The code correctly excludes discriminator fields from the update schema, as evidenced by the use of
isDiscriminatorField
to filter out these fields and the extensive use ofmakePartial
to handle schema generation. This behavior is consistent across all update schemas.
isDiscriminatorField
is used to filter out discriminator fields.makePartial
is used to create partial schemas, ensuring the exclusion of these fields.Ensure discriminator fields are excluded from update schema.
The code correctly excludes discriminator fields from the update schema. This behavior is consistent across all update schemas.
isDiscriminatorField
function implementation:packages/sdk/src/utils.ts
- Usage of
isDiscriminatorField
in schema generation:packages/schema/src/plugins/zod/generator.ts
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that discriminator fields are excluded from the update schema. # Test: Search for update schema definitions and validation logic. Expect: Consistent exclusion of discriminator fields. rg --type ts -A 5 $'UpdateSchema'Length of output: 8658
Script:
#!/bin/bash # Search for the implementation of `makePartial` in TypeScript files ast-grep --lang ts --pattern 'function makePartial($_) { $$$ }' # Search for the implementation of `isDiscriminatorField` in TypeScript files ast-grep --lang ts --pattern 'function isDiscriminatorField($_) { $$$ }' # Search for the usage of `isDiscriminatorField` in the codebase rg --type ts 'isDiscriminatorField'Length of output: 1693
Script:
#!/bin/bash # Search for the usage of `makePartial` in the codebase rg --type ts 'makePartial'Length of output: 900
456-458
: Ensure discriminator fields are excluded from Prisma update schema.The code correctly excludes discriminator fields from the Prisma update schema. Ensure this behavior is consistent across all Prisma update schemas.
Verification successful
To verify that discriminator fields are consistently excluded from all Prisma update schemas, we need to inspect the implementation details in
generator.ts
wherePrismaUpdateSchema
is generated.
To verify the exclusion of discriminator fields from all Prisma update schemas, we need to locate the definition of
prismaUpdateSchema
and review the logic that handles the exclusion of discriminator fields.
To locate the definition of
prismaUpdateSchema
and understand the logic for excluding discriminator fields, we need to perform a broader search ingenerator.ts
.
To understand the full context of how
prismaUpdateSchema
is constructed and modified, we need to retrieve the surrounding lines of code whereprismaUpdateSchema
is defined and modified ingenerator.ts
.
Discriminator fields are correctly excluded from the Prisma update schema.
The code snippet confirms that discriminator fields are filtered out when constructing
prismaUpdateSchema
. This behavior appears consistent, ensuring that discriminator fields are excluded from the Prisma update schema.
- Location:
packages/schema/src/plugins/zod/generator.ts
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that discriminator fields are excluded from the Prisma update schema. # Test: Search for Prisma update schema definitions and validation logic. Expect: Consistent exclusion of discriminator fields. rg --type ts -A 5 $'PrismaUpdateSchema'Length of output: 2174
Script:
#!/bin/bash # Description: Search for the implementation details of PrismaUpdateSchema generation in generator.ts. # Find the function or logic where PrismaUpdateSchema is defined and inspect the exclusion of discriminator fields. rg --type ts 'PrismaUpdateSchema' -A 20 packages/schema/src/plugins/zod/generator.tsLength of output: 874
Script:
#!/bin/bash # Description: Search for the definition of prismaUpdateSchema and inspect the logic for excluding discriminator fields. # Find the definition of prismaUpdateSchema in generator.ts. rg --type ts 'const prismaUpdateSchema' -A 20 packages/schema/src/plugins/zod/generator.tsLength of output: 92
Script:
#!/bin/bash # Description: Perform a broader search for prismaUpdateSchema in generator.ts to locate its definition and understand the logic for excluding discriminator fields. # Search for prismaUpdateSchema in generator.ts. rg --type ts 'prismaUpdateSchema' packages/schema/src/plugins/zod/generator.tsLength of output: 365
Script:
#!/bin/bash # Description: Retrieve the surrounding lines of code where prismaUpdateSchema is defined and modified to understand the full context and verify the exclusion of discriminator fields. # Search for the definition and modifications of prismaUpdateSchema in generator.ts with more surrounding lines. rg --type ts 'prismaUpdateSchema' -A 30 -B 10 packages/schema/src/plugins/zod/generator.tsLength of output: 2565
440-442
: Ensure discriminator fields are excluded from Prisma create schema.The code correctly excludes discriminator fields from the Prisma create schema. Ensure this behavior is consistent across all Prisma create schemas.
Verification successful
Ensure discriminator fields are excluded from Prisma create schema.
The test cases in
zod.test.ts
andplugin-interaction.test.ts
validate the exclusion of discriminator fields from the Prisma create schemas, indicating that the behavior is being tested. However, the implementation details ingenerator.ts
need to be verified to ensure the exclusion logic is correctly implemented.
Test Cases:
tests/integration/tests/plugins/zod.test.ts
includes tests forUserPrismaCreateSchema
andPostPrismaCreateSchema
.tests/integration/tests/enhancements/with-delegate/plugin-interaction.test.ts
includes a test forVideoPrismaCreateSchema
.Implementation:
- The specific implementation details in
generator.ts
need to be verified to confirm that discriminator fields are excluded.The provided information from the
ast-grep
command was not sufficient to extract the relevant code fromgenerator.ts
. Therefore, a more targeted approach is necessary.
Ensure discriminator fields are excluded from Prisma create schema.
The code in
generator.ts
confirms that discriminator fields are being excluded from the Prisma create schema. TheomitDiscriminators
variable is used to omit these fields, and themakePartial
method is used to handle partial schemas.
- Implementation Details:
omitDiscriminators
is defined as:const omitDiscriminators = delegateFields.length > 0 ? `.omit({ ${delegateFields.map((f) => `${f.name}: true`).join(', ')} })` : '';- This ensures that if there are any discriminator fields (
delegateFields
), they are omitted from the schema.- The
makePartial
method is used to create partial schemas:private makePartial(schema: string, fields?: string[]) { if (fields) { if (fields.length === 0) { return schema; } else { return `${schema}.partial({ ${fields.map((f) => `${f}: true`).join(', ')} })`; } } else { return `${schema}.partial()`; } }The test cases in
zod.test.ts
andplugin-interaction.test.ts
validate this behavior, ensuring that discriminator fields are consistently excluded from the Prisma create schemas.
Test Cases:
tests/integration/tests/plugins/zod.test.ts
tests/integration/tests/enhancements/with-delegate/plugin-interaction.test.ts
Implementation:
packages/schema/src/plugins/zod/generator.ts
Conclusion: The implementation correctly ensures that discriminator fields are excluded from the Prisma create schema.
Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash # Description: Verify that discriminator fields are excluded from the Prisma create schema. # Test: Search for Prisma create schema definitions and validation logic. Expect: Consistent exclusion of discriminator fields. rg --type ts -A 5 $'PrismaCreateSchema'Length of output: 3085
Script:
#!/bin/bash # Extract relevant code from generator.ts to verify exclusion logic for discriminator fields ast-grep --lang typescript --pattern $'export const ${upperCaseFirst(model.name)}PrismaCreateSchema = ${prismaCreateSchema};' -A 10 # Review test cases in zod.test.ts and plugin-interaction.test.ts to ensure they validate the exclusion of discriminator fields rg --type ts 'PrismaCreateSchema' -A 10 tests/integration/tests/plugins/zod.test.ts rg --type ts 'PrismaCreateSchema' -A 10 tests/integration/tests/enhancements/with-delegate/plugin-interaction.test.tsLength of output: 2910
Script:
#!/bin/bash # Extract relevant code from generator.ts to verify exclusion logic for discriminator fields ast-grep --lang typescript --pattern 'export const $_ = $_;' -A 10Length of output: 393751
Script:
#!/bin/bash # Extract relevant code from generator.ts to verify exclusion logic for discriminator fields rg --type ts 'let prismaCreateSchema' -A 10 packages/schema/src/plugins/zod/generator.tsLength of output: 546
Script:
#!/bin/bash # Search for the implementation of makePartial and the usage of omitDiscriminators in the codebase rg --type ts 'makePartial' -A 10 rg --type ts 'omitDiscriminators' -A 10Length of output: 10532
packages/schema/src/plugins/enhancer/enhance/index.ts (1)
503-526
: Ensure accurate removal of discriminator fields.The code correctly removes discriminator fields from the create/update input types. Ensure that all discriminator fields are accurately removed.
packages/schema/src/plugins/zod/transformer.ts (2)
98-115
: LGTM! The changes to exclude discriminator fields are clear and efficient.The added logic correctly filters out discriminator fields from create/update input schemas.
117-117
: The rest of thegenerateObjectSchemaFields
method is unchanged and appears correct.No issues found in the unchanged code.
Tools
Biome
[error] 117-119: Avoid unnecessary callback in flatMap call.
You can just use flat to flatten the array.
Safe fix: Replace unnecessary flatMap call to flat instead.(lint/correctness/noFlatMapIdentity)
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
Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Files selected for processing (1)
- packages/schema/src/plugins/zod/transformer.ts (2 hunks)
Additional comments not posted (2)
packages/schema/src/plugins/zod/transformer.ts (2)
93-93
: Approved: Use of regular expression literal.The regular expression literal is correctly used to avoid escaping issues and improve readability.
98-115
: Approved: Logic to exclude discriminator fields.The logic to exclude discriminator fields from create/update input schemas is well-implemented. It correctly identifies the relevant model and filters out the discriminator fields.
Fixes #1608