Open alexgherman opened 8 months ago
Hey @alexgherman thanks for the kind words. It seems like you're right and that there is something off within the StackBlitz, at first I thought it was an outdated version but even after updating to the latest version the issue still occurs.
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? The reason I ask this is because when I copy paste the example to a local project, it just works.
ah I see! glad I'm not crazy, thank you @timdeschryver. I went down a rabbit hole trying different version combinations of zod and zod-fixture with the hope to find a combination that works :)
Either removing the schema altogether or defining the schema separately and sharing it with the generator did it in stackblitz: https://stackblitz.com/edit/vitejs-vite-mjxbz7?file=src%2Fpreview.ts
However, I see the same issue in my vite project. here's a reproducible fork, hopefully it will shed some more light on the issue: https://github.com/alexgherman/zod-fixture-issue-95
Seeing the same output as in stackblitz
[
{
"name": "yxyzyskryqofekd",
"birthday": "1952-01-21T17:32:42.094Z",
"address": {
"street": "cvhvlrnsxroqpum",
"city": "sdnlwozmxaigobr",
"state": "zc-lmtvotjcevmy"
},
"pets": [
{
"name": "ifbgglityarecl-",
"breed": "ydevqfcctdx-lin"
},
{
"name": "fmylchvprjdgelk",
"breed": "livrplyhts-yypa"
},
{
"name": "jrcrrkytqrdmzaj",
"breed": "em-rfot-rvbqmlc"
}
],
"totalVisits": 75
},
{
"name": "gvil-tm-io",
"resolution": {
"height": "100px",
"width": "100px"
}
}
]
Thanks for the reproduction. It seems as it's a runtime problem.
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. Feel free to take a look at this issue if you want @alexgherman
I've looked a bit into the issue. And the problem lies in the function isType
,
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: schema.constructor === target
(But this fails a bunch of tests)
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) https://github.com/dxlbnl/zod-fixture/commit/587e67e64c1b2d0f76bea93ffa40aae778746d98
@dxlbnl thanks for the investigation.
It seems like this is the root cause of this issue.
It's probably not the cleanest solution, but what if we remove/add the _
when we check the types?
If that's sufficient, why not. Yet it seems fragile. I'm not sure how to fix it properly though how to compares the schemas properly.
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. I tried zod-compare, but I quickly gave up 😅
If you have the time could you take a look at #97 and provide feedback? I like that the implementation is simpler, but it might be not so clear to implement a custom generator (I assume not many use this feature). The fix gives the desired output within the repo with the reproduction.
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.
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! =)