Zod, mocking fake data that works with refine?

Does anyone have some experience with zod mocking libaries? Docs list like six of them but my most important feature would be to get it working with refine(). I understand it's not possible to automate refine(), but I need it to get that working without huge hassle.
4 Replies
Sturlen
Sturlen3y ago
what exactly is it you're trying to do? is to generate fake data based on a zod schema?
Massukka
MassukkaOP3y ago
Yes
Froxx
Froxx3y ago
What I do is defining a generate function for each model using faker for data population like so:
import { z } from "zod";
import { faker } from "@faker-js/faker";

const Person = z.object({
firstName: z.string(),
lastName: z.string(),
});

export const generatePerson = (): z.infer<typeof Person> => {
return {
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
};
};
import { z } from "zod";
import { faker } from "@faker-js/faker";

const Person = z.object({
firstName: z.string(),
lastName: z.string(),
});

export const generatePerson = (): z.infer<typeof Person> => {
return {
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
};
};
Massukka
MassukkaOP3y ago
Seems like it's best to just do it manually like you said with faker.

Did you find this page helpful?