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(zod): add coercion call when generating schema for DateTime field #1068

Merged
merged 1 commit into from
Mar 1, 2024

Conversation

ymc9
Copy link
Member

@ymc9 ymc9 commented Mar 1, 2024

Fixes #1067

Summary by CodeRabbit

  • New Features
    • Added a new publishedAt field of type DateTime to the Post entity to enhance date handling and validation.
  • Tests
    • Enhanced REST server tests to include scenarios for creating and updating items with the publishedAt field.
    • Added tests for handling data validation errors using the Zod plugin, including date coercion and format validation.

Copy link
Contributor

coderabbitai bot commented Mar 1, 2024

Walkthrough

Walkthrough

The updates focus on enhancing date handling and validation within the REST server tests and Zod plugin, specifically by introducing a publishedAt DateTime field in the Post entity and model, and by testing date coercion and Zod schema violations. These changes aim to improve the functionality related to date validation and model-level validation, addressing issues with date serialization and Zod validation conflicts.

Changes

Files Changes
.../rest.test.ts
.../utils.ts
Added publishedAt DateTime field to Post entity/model and updated tests for date handling and validation.
.../zod.test.ts Added test for date coercion in the Zod plugin.

Assessment against linked issues

Objective Addressed Explanation
Enable model-level validation rules and utilize Zod for validation [#1067]
Address error 403 with Zod validation messages for DateTime fields [#1067]
Ensure CRUD API compatibility with RESTful handler post-validation implementation [#1067]
Handle DateTime data types appropriately during serialization/validation [#1067]
Validate interaction between Zod validation, model-level validation, and field serialization [#1067]

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?

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 comments 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 from git 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: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between a01065c and d42c415.
Files selected for processing (4)
  • packages/schema/src/plugins/zod/utils/schema-gen.ts (1 hunks)
  • packages/server/tests/api/rest.test.ts (3 hunks)
  • packages/server/tests/utils.ts (1 hunks)
  • tests/integration/tests/plugins/zod.test.ts (1 hunks)
Additional comments: 8
packages/server/tests/utils.ts (1)
  • 23-23: The addition of the publishedAt field of type DateTime to the Post model aligns with the PR objectives and enhances the model's capabilities for handling date and time information.
packages/schema/src/plugins/zod/utils/schema-gen.ts (1)
  • 177-177: Updating the schema generation for 'DateTime' fields to use z.coerce.date() is a critical improvement. It enhances the flexibility in handling date inputs, addressing the core issue with DateTime field serialization and validation in model-level validation.
tests/integration/tests/plugins/zod.test.ts (1)
  • 506-548: The addition of the 'does date coercion' test case is commendable. It effectively validates the handling of different date formats for a model field, ensuring that the changes made to DateTime field handling work as expected across various scenarios.
packages/server/tests/api/rest.test.ts (5)
  • 43-47: Adding a new publishedAt field of type DateTime to the Post model is a significant change. Ensure that all related CRUD operations, especially those involving date handling, are properly updated to accommodate this new field.
  • 1297-1317: The test for creating an item with date coercion is crucial for ensuring that the system correctly handles DateTime fields. It's important to verify that the publishedAt field accepts and correctly processes ISO 8601 date strings. Additionally, consider adding assertions to check the actual stored value in the database to ensure the coercion happened as expected.
  • 1319-1338: This test case for creating an item with a Zod violation is essential for validating the robustness of the input validation mechanism. It's good practice to include more detailed assertions about the error message to ensure it provides meaningful feedback to the client. This helps in debugging and improves the API's usability.
  • 1633-1653: The test for updating an item with date coercion is important for ensuring consistency in handling DateTime fields across different operations. Similar to the creation test, it would be beneficial to add assertions that verify the new value of the publishedAt field in the database, ensuring the update and coercion were successful.
  • 1655-1675: Testing for Zod validation errors during an update operation is crucial for maintaining data integrity. This test case should also include more detailed assertions regarding the error message to ensure it accurately reflects the nature of the violation, aiding in troubleshooting and enhancing API usability.

@ymc9 ymc9 merged commit b60627c into dev Mar 1, 2024
3 checks passed
@ymc9 ymc9 deleted the fix/issue-1067 branch March 1, 2024 18:32
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