-
Notifications
You must be signed in to change notification settings - Fork 14
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
[BUG] TypeError: keyValidator._parse is not a function #12
Comments
Thanks for the bug report and clean reproduction. I stepped through the code and this line is causing the problem: const finalSchema = schema instanceof ZodType ? schema : z.object(schema); The @colinhacks is there a way to check if the schema is a |
I think the actually issues is missing something on the zodix package, but I'm not sure what it's missing, because if I'm copy the whole package to my source file it's works |
That makes sense because the dependency resolution would be less error prone that way. Regardless, I just tested your repro with the |
hey @rileytomasek, when using |
I'm running into this issue in my project. Removing the following check seems to fix it
We're using esbuild so it could be because of that. I haven't dug in too deep but without esbuild it seems to work. It would be good to have a fix. Edit: A suitable workaround (or maybe actually the intended way to use this library) is to pass a zod schema instead of a zod object. eg from the docs
instead of
|
This is still a problem for me when testing my remix action using vitest. Using latest @ZeevG's solution works fine but doesn't work if you need to infer from the schema to pass around. I previously have defined: export type SnipFormInput = z.infer<typeof snipFormInput>
export const snipFormInput = z.object({
// ...
}) But because I kept getting this error when running tests, I needed to create an intermediate object. export type SnipFormInput = z.infer<typeof snipFormInput>
export const snipFormInputObject = {
// ...
}
export const snipFormInput = z.object({ ...snipFormInputObject }) It's a bit hacky, but it lets me run my tests. It's likely a vite issue but if we find a workaround, we should document it so others don't need spend too much time on it. |
I also get this error when passing in a After looking into this a bit deeper, this likely is an issue with In other words - |
@tatemz I was able to reproduce the duplicate I'm open to PRs if someone finds a way to do that check without using |
If only there was a library that could help us check if an object matched a certain schema structure 🤔 😆 Joking aside, from the looks of it, the only thing that part of the code cares about is that the object has a Maybe a typeguard could help? const isZodType = (input: ZodRawShape | ZodTypeAny): input is ZodType => typeof raw.parse === "function" |
Could we please re-open this issue? Like @toddheslin, I am also using Vitest and the workaround at the moment seems to be storing the schema shape in a separate variable.
|
I agree, this needs to be reopened. Whilst my little trick worked for simple input objects, similar to @tatemz it breaks down when I have a union (or in my case a discriminated union) type: export const formSubmissionInput = z
.discriminatedUnion('snipMedium', [
// refactored out into plain objects, hoping it would help
z.object({ ...formSubmissionInputObjectEmail }),
z.object({ ...formSubmissionInputObjectSms }),
])
.refine(
(val) => {
if (val.snipMedium === 'sms') {
return validatePhoneNumber({
country: val.phoneCountry,
number: val.phoneNumber,
})
}
return true
},
() => ({ message: 'Phone number is invalid', path: ['phoneNumber'] })
) @rileytomasek can we reopen this so it's clear a PR is needed? |
I'm reopening this in case one of you want to take a shot at a PR. Given that it's actually a Vite issue, I won't be able to prioritize fixing it any time soon. |
Seeing this with RSBuild/RSPack, not using Vite at all. |
using separate zod schema doen't work
here the repo for reproduce: zodix-rr-reproduce
The text was updated successfully, but these errors were encountered: