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

refactor(zod): clean up optionality of generated schema fields #1139

Merged
merged 3 commits into from
Mar 14, 2024

Conversation

ymc9
Copy link
Member

@ymc9 ymc9 commented Mar 14, 2024

Fixes #898

Summary by CodeRabbit

  • New Features
    • Enhanced schema generation logic to support more flexible and extensible create and update operations.
    • Introduced optional fields and validation for model properties, improving data integrity and user input validation.
  • Tests
    • Updated integration tests to cover new schema functionalities and validation rules, ensuring reliability and robustness of the schema changes.

Copy link
Contributor

coderabbitai bot commented Mar 14, 2024

Walkthrough

Walkthrough

The recent update refines the schema generation logic in a comprehensive manner, focusing on enhancing the flexibility and accuracy of schema definitions for models, particularly concerning Prisma create and update operations. It introduces selective handling of relations, schema refinement functions, and the export of both refined and unrefined schemas, alongside specific enhancements for user model validation.

Changes

Files Change Summary
.../zod/generator.ts Refined schema generation logic, introduced selective handling of relations, schema refinement functions, and exported both refined and unrefined schemas.
.../tests/plugins/zod.test.ts Updated the User model by adding an optional age field and password length validation. Introduced new schemas for user creation and update with refined and scalar versions. Updated tests accordingly.
.../regression/issue-886.test.ts Updated input object passed to ModelSchema.parse() with an id field value of 1 and adjusted expectation for the id property in the test case to check for value 1.

Assessment against linked issues

Objective Addressed Explanation
Understand the reason behind all model properties being marked as optional in the updated version. (#898) The provided information does not directly address the reason for all model properties being marked as optional.
Investigate the change in optional field definitions from version 1.2.0 to 1.5.0. (#898) The details in the summary do not explicitly mention an investigation into the change in optional field definitions.
Determine if the change to optional fields was intentional or resulted in breaking types. (#898) The summary lacks specific information on whether the change to optional fields was intentional or impacted type stability.
Assess if the schema design aligns with the intended design after the version upgrade. (#898) The summary does not provide clear details on assessing the alignment of the schema design post-upgrade with the intended design.
Identify the specific version where the optional field behavior changed. (#898) There is no mention in the summary of identifying the specific version where the change in optional field behavior occurred.

The assessment indicates that while the PR introduces significant improvements to schema generation and model validation, it lacks clear evidence on addressing the specific objectives related to the optional field behavior in the AccountSchema as outlined in issue #898. Further investigation into the implementation details would be necessary to conclusively determine the extent to which these objectives have been met.

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:

Note: Auto-reply has been disabled for this repository by the repository owner. 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 tests 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit tests.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • The JSON schema for the configuration file is available here.
  • 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/coderabbit-overrides.v2.json

CodeRabbit Discord Community

Join our Discord Community to get help, request features, and share feedback.

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.

Review Status

Actionable comments generated: 4

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between bbea0a9 and 818ada2.
Files selected for processing (2)
  • packages/schema/src/plugins/zod/generator.ts (6 hunks)
  • tests/integration/tests/plugins/zod.test.ts (6 hunks)
Additional comments: 11
packages/schema/src/plugins/zod/generator.ts (10)
  • 314-314: The introduction of baseSchema using makeFieldSchema for scalar fields with optionality is a good approach. However, ensure that makeFieldSchema properly handles all scalar types and their nuances, especially custom types or those requiring special validation.
  • 328-332: The handling of relation fields using makeFieldSchema without the optionality flag is appropriate. It's important to verify that relation fields are correctly identified and that their schemas are generated accurately, considering the potential complexity of nested relations.
  • 383-390: Exporting a schema with only scalar fields as [Model]ScalarSchema is a useful feature for scenarios requiring simplified validation. Ensure that this schema accurately reflects all scalar fields and their validation rules without including relations or foreign keys.
  • 398-399: Merging relation fields into the model schema and making them all optional is a logical step for flexibility. However, it's important to ensure that this does not lead to unintended consequences, such as bypassing necessary validations. Consider documenting this behavior clearly for users of the generated schemas.

Consider adding documentation or comments explaining the decision to make all relation fields optional in the merged schema.

  • 405-412: Exporting a schema without refinement for extensibility is thoughtful, allowing users to extend the schema further if needed. It's essential to document this feature prominently to ensure users are aware of its existence and how to use it effectively.

Recommend adding more detailed documentation or examples on how to use the [Model]WithoutRefineSchema for further customizations.

  • 416-422: The final model schema export, including all fields and validations, is the culmination of the schema generation process. Ensure that this schema accurately represents the model, including all scalar, foreign key, and relation fields, along with any applied refinements.
  • 433-439: The schema used for validating Prisma create input is marked for internal use only. It's important to ensure that this schema is correctly generated and that it aligns with the intended use cases for Prisma create operations.
  • 458-466: Similarly, the schema for validating Prisma update input should be thoroughly tested to ensure it correctly handles all potential update scenarios, including atomic operations on numeric fields.
  • 471-517: The creation of separate schemas for create operations, including handling fields with defaults as optional, is a nuanced approach. Ensure that this logic correctly identifies fields with defaults and applies optionality as intended.
  • 522-560: For update operations, making all fields optional is standard, but special attention should be paid to the handling of foreign key fields and the application of refinements. Verify that the update schema behaves as expected in various update scenarios.
tests/integration/tests/plugins/zod.test.ts (1)
  • 48-50: Adding an optional age field to the User model and a validation rule for password length is a good practice for enhancing the model's robustness. Ensure that these additions are reflected in the corresponding schemas and that the tests adequately cover these new aspects.

packages/schema/src/plugins/zod/generator.ts Show resolved Hide resolved
tests/integration/tests/plugins/zod.test.ts Show resolved Hide resolved
tests/integration/tests/plugins/zod.test.ts Show resolved Hide resolved
tests/integration/tests/plugins/zod.test.ts Show resolved Hide resolved
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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 818ada2 and e81d8db.
Files selected for processing (1)
  • packages/schema/src/plugins/zod/generator.ts (7 hunks)
Files skipped from review as they are similar to previous changes (1)
  • packages/schema/src/plugins/zod/generator.ts

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.

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between e81d8db and 2e79573.
Files selected for processing (1)
  • tests/integration/tests/regression/issue-886.test.ts (1 hunks)
Additional comments: 1
tests/integration/tests/regression/issue-886.test.ts (1)
  • 16-20: The test case has been updated to include an explicit id field with a value of 1 in the input object for ModelSchema.parse(), and the expectation for the id property has been adjusted accordingly. This change effectively tests the handling of explicit id values in the schema, which is relevant to the regression issue being addressed. Ensure that this test case aligns with the intended behavior of the schema regarding id fields and default values.

@ymc9 ymc9 merged commit 8099793 into v2 Mar 14, 2024
12 checks passed
@ymc9 ymc9 deleted the refactor/zod-optionality branch March 14, 2024 22:30
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