Security News
Research
Supply Chain Attack on Rspack npm Packages Injects Cryptojacking Malware
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
@anatine/zod-mock
Advanced tools
@anatine/zod-mock is an npm package that provides utilities for generating mock data based on Zod schemas. It is particularly useful for testing and development purposes where you need to create consistent and realistic mock data that adheres to your Zod schema definitions.
Generate Mock Data
This feature allows you to generate mock data based on a Zod schema. In this example, a user schema is defined with an id, name, and age. The `mock` function generates a mock user object that adheres to this schema.
const { z } = require('zod');
const { mock } = require('@anatine/zod-mock');
const userSchema = z.object({
id: z.string().uuid(),
name: z.string(),
age: z.number().int().min(18).max(99)
});
const mockUser = mock(userSchema);
console.log(mockUser);
Custom Mock Generators
This feature allows you to set custom mock generators for specific Zod types. In this example, a custom UUID generator is set, and the `mock` function uses this custom generator when creating the mock user object.
const { z } = require('zod');
const { mock, setMock } = require('@anatine/zod-mock');
const userSchema = z.object({
id: z.string().uuid(),
name: z.string(),
age: z.number().int().min(18).max(99)
});
setMock(z.string().uuid(), () => 'custom-uuid');
const mockUser = mock(userSchema);
console.log(mockUser);
Nested Schemas
This feature supports generating mock data for schemas with nested objects. In this example, a user schema includes an address schema, and the `mock` function generates a mock user object with a nested address object.
const { z } = require('zod');
const { mock } = require('@anatine/zod-mock');
const addressSchema = z.object({
street: z.string(),
city: z.string(),
zipCode: z.string().length(5)
});
const userSchema = z.object({
id: z.string().uuid(),
name: z.string(),
age: z.number().int().min(18).max(99),
address: addressSchema
});
const mockUser = mock(userSchema);
console.log(mockUser);
Faker is a popular library for generating fake data. It provides a wide range of data types and is highly customizable. Unlike @anatine/zod-mock, Faker does not integrate directly with Zod schemas but offers more extensive data generation capabilities.
Chance is another library for generating random data. It is lightweight and easy to use, offering a variety of data types. Similar to Faker, Chance does not integrate with Zod schemas but is useful for generating random data for testing purposes.
Test Data Bot is a library for generating test data using a builder pattern. It allows for the creation of complex data structures and is highly customizable. While it does not integrate with Zod schemas, it provides a flexible way to generate test data.
Generates a mock data object using faker.js from a Zod schema.
Both openapi3-ts and zod are peer dependencies instead of dependant packages.
While zod
is necessary for operation, openapi3-ts
is for type-casting.
npm install @faker-js/faker zod @anatine/zod-mock
import { generateMock } from '@anatine/zod-mock';
const schema = z.object({
uid: z.string().nonempty(),
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 mockData = generateMock(schema);
// ...
This will generate mock data similar to:
{
"uid": "3f46b40e-95ed-43d0-9165-0b8730de8d14",
"theme": "light",
"email": "Alexandre99@hotmail.com",
"phoneNumber": "1-665-405-2226",
"avatar": "https://cdn.fakercloud.com/avatars/olaolusoga_128.jpg",
"jobTitle": "Lead Brand Facilitator",
"otherUserEmails": [
"Wyman58@example.net",
"Ignacio_Nader@example.org",
"Jorge_Bradtke@example.org",
"Elena.Torphy33@example.org",
"Kelli_Bartoletti@example.com"
],
"stringArrays": [
"quisquam",
"corrupti",
"atque",
"sunt",
"voluptatem"
],
"stringLength": 4,
"numberCount": "total value = 25430",
"age": 110
}
Sometimes there might be a reason to have a more specific mock for a string value.
You can supply an options field to generate specific mock data that will be triggered by the matching key.
const schema = z.object({
locked: z.string(),
email: z.string().email(),
primaryColor: z.string(),
});
const mockData = generateMock(schema, {
stringMap: {
locked: () => `this return set to the locked value`,
email: () => `not a email anymore`,
primaryColor: faker.internet.color,
},
});
zod-mock
tries to generate mock data from two sources.
{ firstName: z.string() }
)This will check the string name of the key against all the available faker
function names.
Upon a match, it uses that function to generate a mock value.
const something = z.string()
)In the case there is no key name (the schema doesn't contain an object) or there is no key name match,
zod-mock
will use the primitive type provided by zod
.
Some zod filter types (email, uuid, url, min, max, length) will also modify the results.
If zod-mock
does not yet support a Zod type used in your schema, you may provide a backup mock function to use for that particular type.
const schema = z.object({
anyVal: z.any()
});
const mockData = generateMock(schema, {
backupMocks: {
ZodAny: () => 'a value'
}
});
faker
, such as setting phone number formattingA great lib that provided some insights on dealing with various zod types.
This library is part of a nx monorepo @anatine/zod-plugins.
FAQs
Zod auto-mock object generator using Faker at @faker-js/faker
The npm package @anatine/zod-mock receives a total of 143,349 weekly downloads. As such, @anatine/zod-mock popularity was classified as popular.
We found that @anatine/zod-mock demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.
Security News
Sonar’s acquisition of Tidelift highlights a growing industry shift toward sustainable open source funding, addressing maintainer burnout and critical software dependencies.