timdeschryver / zod-fixture

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

Isolated Seeding and Constrained Defaults for Deterministic and Constrained-Non-Deterministic Fixtures #53

Closed THEtheChad closed 1 year ago

THEtheChad commented 1 year ago

This PR adds the ability to configure string defaults, per-schema seeds, and has opinionated Constrained and Unconstrained Transformers.

import { ConstrainedFixture, UnconstrainedFixture, Fixture } from 'zod-fixture'

// Fixture is an alias for ConstrainedFixture
const constrainedFixture = new Fixture()
const output = constrainedFixture.fromSchema(z.string().array())

/**
 * output = [
 *   'a1od0ihseasd1kj',
 *   'pnwc82ceadsf2f3',
 *   'apa-sduh1sdf11d'
 * ]
 */

const constrainedFromCharacterSetFixture = new Fixture({
  string: {
    characterSet: 'ab'
  }
})
const output = constrainedFromCharactersFixture.fromSchema(z.string().array())

/**
 * output = [
 *   'ababaabbbaaabaa',
 *   'bbbaababbbaabab',
 *   'ababbbbabbbbbbb'
 * ]
 */

const customCharacterFixture = new Fixture({
  string: {
    min: 2,
    max: 2,
    characterSet: 'ab'
  }
})
const output = customCharacterFixture.fromSchema(z.string().array())

/**
 * output = [
 *   'ab',
 *   'bb',
 *   'aa'
 * ]
 */

const fixture = new Fixture()
const output1 = fixture.fromSchema(z.string().array(), { seed: 22 })
const output2 = fixture.fromSchema(z.string().array(), { seed: 22 })
const output3 = fixture.fromSchema(z.string().array(), { seed: 22 })

/**
 * output1 = [
 *   'a1od0ihseasd1kj',
 *   'pnwc82ceadsf2f3',
 *   'apa-sduh1sdf11d'
 * ]
 *
 * output2 = [
 *   'a1od0ihseasd1kj',
 *   'pnwc82ceadsf2f3',
 *   'apa-sduh1sdf11d'
 * ]
 *
 * output3 = [
 *   'a1od0ihseasd1kj',
 *   'pnwc82ceadsf2f3',
 *   'apa-sduh1sdf11d'
 * ]
 */
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 13, 2023 1:17pm
github-actions[bot] commented 1 year ago

:tada: This PR is included in version 2.0.0-beta.13 :tada:

The release is available on:

Your semantic-release bot :package::rocket: