-
Notifications
You must be signed in to change notification settings - Fork 10
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
issue with generators on the examples provided in the docs #95
Comments
Hey @alexgherman thanks for the kind words. The main issue is that the generator isn't registered/invoked because the ZodNumber schema can't be matched with zod's type. When you remove the schema property it should work. const totalVisitsGenerator = Generator({
// 👇 remove this
// schema: ZodNumber,
filter: ({ context }) => {
return context.path.at(-1) === 'totalVisits';
}, Do you also experience this issue outside of StackBlitz, and if so in what environment? |
ah I see! glad I'm not crazy, thank you @timdeschryver. Either removing the schema altogether or defining the schema separately and sharing it with the generator did it in stackblitz: However, I see the same issue in my vite project. Seeing the same output as in stackblitz
|
Thanks for the reproduction. Installing vitest in the referenced repository, and copying the example within to a test seems to work as expected. We probably have to change how we reference Zod's type within generators (https://github.com/timdeschryver/zod-fixture/blob/main/src/internal/zod.ts) to make this compatible with multiple runtimes. |
I was hoping to find some time to take a look at this but last weeks have been quite busy. |
I've looked a bit into the issue. And the problem lies in the function the condition: schema._def.typeName === target.name is not valid in compiled situations. as target.name === '_zodString' and schema._def.typeName === 'zodString' We could compare constructors instead possibly: Looking into that I see a bit of naughtyness around faking constructors. @timdeschryver Do you have some pointers in the right direction? I've pushed this, but it does not seem to work properly (tests pass, but I'm getting errors in my private codebase) |
@dxlbnl thanks for the investigation. |
If that's sufficient, why not. Yet it seems fragile. I did find this repo: https://github.com/lawvs/zod-compare might be a solution. |
@dxlbnl that seems to be a useful library as it will do most of the heavy lifting for us (and looking at the source code there are many "edge cases"). If you want you can try to incorporate it within this library. |
I'm having difficulty making it work properly. Largely due to the quirkyness regarding the zodTypes stringnames being cast as zodTypes. And also due to the schema types (ZodString, ... ) being tricky to match to schema instances. I'm not sure how to proceed, but maybe you'd be willing to drop in a call, and discuss it a bit. @timdeschryver |
@dxlbnl sorry, I missed this message - I also tried to implement a fix. If you have the time could you take a look at #97 and provide feedback? |
Somehow its now working in my repo, it seems to pick up the right generators. Maybe something has changed with how vite deals with it. |
Is it working with the current implementation or the implementation of the PR? |
There seems to be an issue with the generators, even the examples provided in the docs are not returning the expected values.
TL;DR: run the stackblitz here to see the issue:
https://stackblitz.com/edit/vitejs-vite-yg6ndm?file=src%2Fpreview.ts
Example 1:
Example 2:
What am I missing?
And thank you for a great project! =)
The text was updated successfully, but these errors were encountered: