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

Instanceof issue fatal #1131

Merged
merged 3 commits into from
May 20, 2022
Merged

Conversation

Stouffi
Copy link
Contributor

@Stouffi Stouffi commented May 12, 2022

closes #1001

Here is a codesandbox to illustrates the problem encountered: https://codesandbox.io/s/youthful-sunset-6jn810?file=/src/index.test.ts

An issue with instanceof schema should be fatal in order to prevent running refinement on a value which might be incompatible with the provided instance type.

This PR introduces an additional optional parameter in the custom signature to specify wether the issue should be fatal.

declare const custom: <T>(
  check?: (data: unknown) => any,
  params?: Parameters<ZodTypeAny["refine"]>[1],
  fatal?: boolean
) => ZodType<T>

However it would be better to specify it as an object to specify more options in the future.

interface CustomOptions {
  fatal: boolean
}

declare const custom: <T>(
  check?: (data: unknown) => any,
  params?: Parameters<ZodTypeAny["refine"]>[1],
  opts?: Partial<CustomOptions>
) => ZodType<T>

Let me know if this PR needs improvements to meet the guidelines.

@netlify
Copy link

netlify bot commented May 12, 2022

👷 Deploy request for guileless-rolypoly-866f8a pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit e0661a4

@Stouffi Stouffi marked this pull request as ready for review May 13, 2022 10:04
@colinhacks
Copy link
Owner

Immaculate PR, thanks!

@colinhacks colinhacks merged commit 7836900 into colinhacks:master May 20, 2022
MrAwesome pushed a commit to MrAwesome/zod that referenced this pull request May 20, 2022
* fix: instanceOfType issue is now fatal

* tests instanceof issue as fatal

* Fix lint

Co-authored-by: Colin McDonnell <colinmcd@alum.mit.edu>
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.

New refine behaviour issues with z.custom (and z.instanceof)
2 participants