Skip to main content

Usage with zod-mock

info

zod-mock is a Zod plugin that generates a mock data object from a Zod schema (using faker.js)

import { mocq } from "mocq";
import { z } from 'zod';
import { generateMock } from '@anatine/zod-mock';

const schema = z.object({
uid: z.string(),
theme: z.enum([ 'light', 'dark' ]),
email: z.string().email().optional(),
phoneNumber: z.string().min(10).optional(),
avatar: z.string().url().optional(),
jobTitle: z.string().optional(),
otherUserEmails: z.array(z.string().email()),
stringArrays: z.array(z.string()),
stringLength: z.string().transform((val) => val.length),
numberCount: z.number().transform((item) => `total value = ${item}`),
age: z.number().min(18).max(120),
});

const { generate } = mocq({
users: {
generator: () => generateMock(schema),
count: 100,
}
});

const { data: { users } } = generate();

Sample Resolved Data

mocks:

[
{
uid: "e004b4a2-a70c-4de9-99e4-90b61efe969a",
theme: "dark",
email: "Alfonzo.Johnston82@example.net",
phoneNumber: "1-807-669-5499",
avatar: "https://parched-baritone.com/",
jobTitle: "Regional Interactions Representative",
otherUserEmails: [ "Ted_Kozey@example.org" ],
stringArrays: [ "arbustum", "venustas", "commemoro" ],
stringLength: 3,
numberCount: "total value = 2330509388546048",
age: 109
},
{
uid: "4f590e3d-d021-4a41-86ed-f680a0d9ab35",
theme: "dark",
email: "Gideon_Hamill@example.net",
phoneNumber: "1-978-320-7665",
avatar: "https://rough-something.net/",
jobTitle: "Senior Operations Engineer",
otherUserEmails: [ "Karine_Collins40@example.net", "Makenna47@example.com", "Marge23@example.org"
],
stringArrays: [ "aperiam", "vespillo", "antiquus" ],
stringLength: 5,
numberCount: "total value = 5509735363117056",
age: 81
},
// ... 98 more entries
]
using TypeScript but not Zod?

use ts-to-zod to generate Zod schemas from TypeScript types/interfaces