timdeschryver / zod-fixture

Creating fixtures based on zod schemas
https://zod-fixture.timdeschryver.dev/
MIT License
121 stars 10 forks source link

fix: don't mangle names #46

Closed timdeschryver closed 1 year ago

timdeschryver commented 1 year ago

Using this library outside of this repo results that no generators can be found, see https://stackblitz.com/edit/typescript-duufnz?file=index.ts for an example.

This fix removes the Symbols, and uses the type names to filter schemas

vercel[bot] commented 1 year ago

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
zod-fixture ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 7, 2023 3:52pm
THEtheChad commented 1 year ago

Hrmmmm..... so looking into this, is it the use of Symbols or the fact that we're packaging Zod with our generators instead of using the Zod instance that's included as a peer dependency? I think it's the latter.

We can't really use the def.names because we need an identifier on the instance (not just the class). Specifically for matching with z.custom.

This is definitely an interesting problem.

I'm going to explore other possible solutions.

If we DO resolve the issue with the PR, we'll need a way for people to create custom generators for zod instances.

THEtheChad commented 1 year ago

The issue with the generators is that they were constructed using a bundled version of zod. This meant that the symbols were placed on the zod constructors in the bundle but not on the constructors being used outside the library to define the schemas. You were correct to match the typeName of the instance to the name of the constructor. However, we still need symbols for zod instances (which aren't prebundled).

I also realized that the bundle size was being inflated by including zod when the only thing we need is the typeNames. I stubbed out zod constructors for us internally (so the API is consistent internally and externally). Still working on tweaking a couple things but here's the PR https://github.com/timdeschryver/zod-fixture/pull/47.

The bundle size was reduced from 91kb to 14kb.