8000 fix #3907: allow custom error message on invalid_date issue code by DavidSemke · Pull Request #4064 · colinhacks/zod · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix #3907: allow custom error message on invalid_date issue code #4064

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

DavidSemke
Copy link
@DavidSemke DavidSemke commented Apr 1, 2025

Overview

This PR adds support for custom error messages when the issue code is "invalid_date".

Why is this change non-breaking?

  • The behavior remains unchanged for all existing validation cases.
  • "invalid_date" validation now allows custom error messages.
  • If no custom message is provided, the default "Invalid date" message is still used.

Example Usage

The following code now correctly applies a custom error message:

const invalidDate = new Date("I am not a date");
const customMsg = "I am custom";
const dateWithMsg = z.date({ message: customMsg });
dateWithMsg.parse(invalidDate); // Throws ZodError with "I am custom"

Summary by CodeRabbit

  • Tests

    • Added new test cases to verify that when an invalid date input is provided, a custom error message is properly displayed.
  • Bug Fixes

    • Enhanced error handling to uniformly process invalid date inputs, ensuring consistent error messaging.

Copy link
Contributor
coderabbitai bot commented Apr 1, 2025

Walkthrough

This pull request adds new test cases for the z.date() function that confirm a custom error message is produced when an invalid date is passed. Additionally, it updates the error handling logic in both the Deno and src type files by modifying the processCreateParams function. The new logic now treats "invalid_date" issues the same as "invalid_type" ones when deciding whether to return the default error message. No changes have been made to the exported or public entities.

Changes

File(s) Change Summary
deno/lib/__tests__/date.test.ts, src/__tests__/date.test.ts Added a test case titled "custom error message on invalid_date" to verify that z.date() throws an error with a custom message and the code "invalid_date".
deno/lib/types.ts, src/types.ts Updated the processCreateParams function to include a check for iss.code !== "invalid_date" alongside iss.code !== "invalid_type", altering error handling.

Sequence Diagram(s)

sequenceDiagram
    participant T as Test Case
    participant Z as z.date() Parser
    participant E as Error Handler
    T->>Z: Call z.date() with invalid date & custom error message
    Z-->>T: Throw error { code: "invalid_date", message: custom message }
    T->>T: Catch and assert error properties
Loading
sequenceDiagram
    participant C as Caller
    participant P as processCreateParams
    participant D as Default Error (ctx.defaultError)
    C->>P: Provide RawCreateParams with issue code
    P->>P: Check if iss.code is "invalid_type" or "invalid_date"
    alt Code is "invalid_type" or "invalid_date"
        P-->>C: Process using custom logic
    else
        P->>D: Retrieve default error message
        D-->>P: Return default error message
        P-->>C: Return processed result
    end
Loading

Poem

I'm a bunny with a skip in my hop,
Testing dates until the errors stop.
Custom messages now sing with cheer,
"Invalid_date" rings loud and clear.
Code gardens bloom in each new line,
Hoppy changes make our tests shine! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4c7d8e5 and cf219f0.

📒 Files selected for processing (4)
  • deno/lib/__tests__/date.test.ts (1 hunks)
  • deno/lib/types.ts (1 hunks)
  • src/__tests__/date.test.ts (1 hunks)
  • src/types.ts (1 hunks)
🔇 Additional comments (4)
src/__tests__/date.test.ts (1)

36-47: Good test implementation for custom error messages.

This test correctly verifies the new functionality allowing custom error messages for invalid dates. The test effectively:

  1. Creates an invalid date instance
  2. Configures a date schema with a custom message
  3. Verifies b 8000 oth the error code and custom message

The test case aligns perfectly with the PR objective of allowing users to specify custom error messages for the "invalid_date" issue code.

deno/lib/__tests__/date.test.ts (1)

37-48: Good test implementation for custom error messages in Deno.

This test ensures the custom error message functionality works consistently in the Deno environment, mirroring the Node.js test. It correctly validates that:

  1. The error code is "invalid_date"
  2. The custom message is properly applied

Maintaining parallel tests across both environments is a good practice for ensuring consistent behavior.

deno/lib/types.ts (1)

148-149: Enhance error handling to support custom error messages for invalid dates.

This change extends the condition to treat "invalid_date" issues the same way as "invalid_type" issues when determining whether to use a custom error message or the default one. This enhancement allows users to specify custom error messages for invalid date validations, making the API more consistent.

src/types.ts (1)

148-149: Approve: Adding support for custom error messages on invalid_date issue code.

This change extends the error handling to allow users to provide custom error messages for invalid dates, just like they can already do for invalid types. The implementation is clean and consistent with the library's pattern.

The modification ensures that when the issue code is "invalid_date" or "invalid_type", the provided custom message (if any) will be used instead of the default error message, which fulfills the PR objective.

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • 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 testing code 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 gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • 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/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
netlify bot commented Apr 1, 2025

Deploy Preview for guileless-rolypoly-866f8a ready!

Built without sensitive environment variables

Name Link
🔨 Latest commit cf219f0
🔍 Latest deploy log https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/67ebb6360efad500084d593a
😎 Deploy Preview https://deploy-preview-4064--guileless-rolypoly-866f8a.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site configuration.

@DavidSemke DavidSemke marked this pull request as ready for review April 1, 2025 09:51
Copy link
vercel bot commented Apr 1, 2025

Deployment failed with the following error:

Creating the Deployment Timed Out.

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
0