timdeschryver / zod-fixture

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

Toggle Variants in a Deterministic Way #89

Closed THEtheChad closed 1 year ago

THEtheChad commented 1 year ago

Some schemas can be satisfied by more than one input (ie: optional, nullable, nullish). In many of these cases, we were only generating one of the potential variants. In a testing context, this meant that you could not produce certain variants of a fixture and in a mocking context (ie: apis) it meant that many of the potential code paths would never be explored.

This update produces psuedo random fixtures in a deterministic way via the seed.

Using this philosophy, I removed the alternating booleans. While useful in instances where a list is produced:

[ true, false, true, false ]

It made no sense when using nested values:

{
  isAdmin: true,
  config: {
    isDashboardEnabled: false // this would never be true
  }
}

If we really wanted to re-enable this, I could add an option to the config:

new Fixture({ boolean: 'alternate' })
vercel[bot] commented 1 year ago

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

Name Status Preview Updated (UTC)
zod-fixture ✅ Ready (Inspect) Visit Preview Aug 6, 2023 1:10pm
THEtheChad commented 1 year ago

I like the idea of alternating booleans for "Constrained Non-Determinism" but implementing it correctly feels tricky. In many cases, you need to know more about the problem domain to know whether alternating the booleans makes sense. I think that's an exercise best left to the end user.

To your point, I think we could package generators outside of the defaults that provide this level of functionality and the users can choose to plug them in where it makes sense.

github-actions[bot] commented 1 year ago

:tada: This PR is included in version 2.4.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket: