timdeschryver / zod-fixture

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

Pass schema to generator condition #17

Closed ALMaclaine closed 1 year ago

ALMaclaine commented 1 year ago

Would it be possible to pass the schema to the Customization condition?

I'm adding a setTag method to the zod schema, that allows me to add a symbol to the zod schema.

I then use this symbol in the condition of the Customization to allow me to determine which schema it is and generate custom fixtures.

I'm currently supporting this by patching zod-fixture.

I understand this isn't standard zod, but would seem it would be easy to support my use case by giving the schema to the condition.

If you have any advice on how to do this better, or if v2 will change this please let me know.

timdeschryver commented 1 year ago

Hi, sorry for my late response but I wanted v2 to be released first. v2 should pass the zod schema information to the generators (previously Customizations).

While some things can still change, you can test this out via 2.0.0-beta.1

ALMaclaine commented 1 year ago

Thank you so much, look forward to it.

THEtheChad commented 1 year ago

v2 already does this, @ALMaclaine. You can create a Generator (Customization) with a Zod instance and it will match only that instance. This was originally done to work with z.custom but will work for any Zod instance.

const yourInstance = z.string();

export const YourGenerator = Generator({
    schema: yourInstance,
    matches: () => true,
    output: () => 'like-magic',
});

Coincidentally, it also does this by attaching a symbol 😉