Skip to content

issue with generators on the examples provided in the docs #95

@alexgherman

Description

@alexgherman

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:

  const totalVisitsGenerator = Generator({
    schema: ZodNumber,
    filter: ({ context }) => context.path.at(-1) === 'totalVisits',
    /**
     * The `context` provides a path to the current field
     *
     * {
     *   totalVisits: ...,
     *   nested: {
     *     totalVisits: ...,
     *   }
     * }
     *
     * Would match twice with the following paths:
     *   ['totalVisits']
     *   ['nested', 'totalVisits']
     */

    // returns a more realistic number of visits.
    output: ({ transform }) => transform.utils.random.int({ min: 0, max: 25 }),
  });

  const addressGenerator = Generator({
    schema: ZodObject,
    filter: ({ context }) => context.path.at(-1) === 'address',
    // returns a custom address object
    output: () => ({
      street: 'My Street',
      city: 'My City',
      state: 'My State',
    }),
  });

  const personSchema = z.object({
    name: z.string(),
    birthday: z.date(),
    address: z.object({
      street: z.string(),
      city: z.string(),
      state: z.string(),
    }),
    pets: z.array(z.object({ name: z.string(), breed: z.string() })),
    totalVisits: z.number().int().positive(),
  });

  const fixture = new Fixture({ seed: 38 }).extend([
    addressGenerator,
    totalVisitsGenerator,
  ]);
  const person = fixture.fromSchema(personSchema);
  console.log(person);
  
  // Expected:
// {
// 	address: {
// 		city: 'My City',
// 		state: 'My State',
// 		street: 'My Street',
// 	},
// 	birthday: new Date('1952-01-21T17:32:42.094Z'),
// 	name: 'yxyzyskryqofekd',
// 	pets: [
// 		{
// 			breed: 'dnlwozmxaigobrz',
// 			name: 'vhvlrnsxroqpuma',
// 		},
// 		{
// 			breed: 'ifbgglityarecl-',
// 			name: 'c-lmtvotjcevmyi',
// 		},
// 		{
// 			breed: 'fmylchvprjdgelk',
// 			name: 'ydevqfcctdx-lin',
// 		},
// 	],
// 	totalVisits: 15,
// },

  // Actual:
  // {
  //   "name": "sdnlwozmxaigobr",
  //   "birthday": "2091-09-17T10:54:58.574Z",
  //   "address": { // incorrect address remains untouched
  //     "street": "c-lmtvotjcevmyi",
  //     "city": "ifbgglityarecl-",
  //     "state": "ydevqfcctdx-lin"
  //   },
  //   "pets": [
  //     {
  //       "name": "mylchvprjdgelkq",
  //       "breed": "ivrplyhts-yypas"
  //     },
  //     {
  //       "name": "rcrrkytqrdmzajo",
  //       "breed": "m-rfot-rvbqmlcu"
  //     },
  //     {
  //       "name": "adahtinsiooiwrj",
  //       "breed": "xdxkgurszvshnvg"
  //     }
  //   ],
  //   "totalVisits": 7
  // }

Example 2:

  const pxSchema = z.custom<`${number}px`>((val) => {
    return /^\d+px$/.test(val as string);
  });

  const StringGenerator = Generator({
    schema: ZodString,
    output: () => 'John Doe',
  });

  const PixelGenerator = Generator({
    schema: pxSchema,
    output: () => '100px',
  });

  const developerSchema = z.object({
    name: z.string().max(10),
    resolution: z.object({
      height: pxSchema,
      width: pxSchema,
    }),
  });

  const fixture = new Fixture({ seed: 7 }).extend([
    PixelGenerator,
    StringGenerator,
  ]);
  const developer = fixture.fromSchema(developerSchema);
  console.log(developer);

// Expected:
// {
// 	name: 'John Doe',
// 	resolution: {
// 		height: '100px',
// 		width: '100px',
// 	},
// }

// Actual:
// {
//   "name": "mzzhauqgzw", // incorrect
//   "resolution": {
//     "height": "100px", // correct
//     "width": "100px" // correct
//   }
// }


What am I missing?
And thank you for a great project! =)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions