Gathering detailed insights and metrics for @anatine/zod-mock
Gathering detailed insights and metrics for @anatine/zod-mock
Gathering detailed insights and metrics for @anatine/zod-mock
Gathering detailed insights and metrics for @anatine/zod-mock
npm install @anatine/zod-mock
Typescript
Module System
Node Version
NPM Version
zod-nestjs-2.0.12
Updated on Apr 05, 2025
zod-nestjs-2.0.11
Updated on Apr 04, 2025
zod-mock-3.14.0
Updated on Apr 04, 2025
zod-openapi-2.2.8
Updated on Apr 04, 2025
zod-mock-3.13.5
Updated on Jan 20, 2025
zod-nestjs-2.0.10
Updated on Jan 20, 2025
TypeScript (99.51%)
JavaScript (0.49%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
757 Stars
525 Commits
108 Forks
8 Watchers
12 Branches
53 Contributors
Updated on Jul 08, 2025
Latest Version
3.14.0
Package Id
@anatine/zod-mock@3.14.0
Unpacked Size
55.20 kB
Size
14.41 kB
File Count
13
NPM Version
10.8.2
Node Version
18.20.7
Published on
Apr 04, 2025
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
2
Generates a mock data object using faker.js from a Zod schema.
@faker-js/faker
is a peer dependency of @anatine/zod-mock
and is used for mock data generation.
1npm install zod @faker-js/faker @anatine/zod-mock
1import { generateMock } from '@anatine/zod-mock'; 2const schema = z.object({ 3 uid: z.string().nonempty(), 4 theme: z.enum([`light`, `dark`]), 5 email: z.string().email().optional(), 6 phoneNumber: z.string().min(10).optional(), 7 avatar: z.string().url().optional(), 8 jobTitle: z.string().optional(), 9 otherUserEmails: z.array(z.string().email()), 10 stringArrays: z.array(z.string()), 11 stringLength: z.string().transform((val) => val.length), 12 numberCount: z.number().transform((item) => `total value = ${item}`), 13 age: z.number().min(18).max(120), 14 }); 15const mockData = generateMock(schema); 16// ...
This will generate mock data similar to:
1{ 2 "uid": "3f46b40e-95ed-43d0-9165-0b8730de8d14", 3 "theme": "light", 4 "email": "Alexandre99@hotmail.com", 5 "phoneNumber": "1-665-405-2226", 6 "avatar": "https://cdn.fakercloud.com/avatars/olaolusoga_128.jpg", 7 "jobTitle": "Lead Brand Facilitator", 8 "otherUserEmails": [ 9 "Wyman58@example.net", 10 "Ignacio_Nader@example.org", 11 "Jorge_Bradtke@example.org", 12 "Elena.Torphy33@example.org", 13 "Kelli_Bartoletti@example.com" 14 ], 15 "stringArrays": [ 16 "quisquam", 17 "corrupti", 18 "atque", 19 "sunt", 20 "voluptatem" 21 ], 22 "stringLength": 4, 23 "numberCount": "total value = 25430", 24 "age": 110 25}
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.
1const schema = z.object({ 2 locked: z.string(), 3 email: z.string().email(), 4 primaryColor: z.string(), 5}); 6const mockData = generateMock(schema, { 7 stringMap: { 8 locked: () => `this return set to the locked value`, 9 email: () => `not a email anymore`, 10 primaryColor: () => faker.internet.color(), 11 }, 12});
For consistent testing, you can also add in a seed or seed array.
1const schema = z.object({ 2 name: z.string(), 3 age: z.number(), 4}); 5const seed = 123; 6const first = generateMock(schema, { seed }); 7const second = generateMock(schema, { seed }); 8expect(first).toEqual(second);
Once drilled down to deliver a string, number, boolean, or other primitive value a function with a matching name is searched for in faker.
You can add your own key/fn mapper in the options.
1 2export function mockeryMapper( 3 keyName: string, 4 fakerInstance: Faker 5): FakerFunction | undefined { 6 const keyToFnMap: Record<string, FakerFunction> = { 7 image: fakerInstance.image.url, 8 imageurl: fakerInstance.image.url, 9 number: fakerInstance.number.int, 10 float: fakerInstance.number.float, 11 hexadecimal: fakerInstance.number.hex, 12 uuid: fakerInstance.string.uuid, 13 boolean: fakerInstance.datatype.boolean, 14 // Email more guaranteed to be random for testing 15 email: () => fakerInstance.database.mongodbObjectId() + '@example.com' 16 }; 17 return keyName && keyName.toLowerCase() in keyToFnMap 18 ? keyToFnMap[keyName.toLowerCase() as never] 19 : undefined; 20} 21 22const schema = z.object({ 23 locked: z.string(), 24 email: z.string().email(), 25 primaryColor: z.string(), 26}); 27 28const result = generateMock(schema, { mockeryMapper }); 29
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.
1const schema = z.object({ 2 anyVal: z.any() 3}); 4const mockData = generateMock(schema, { 5 backupMocks: { 6 ZodAny: () => 'a value' 7 } 8});
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.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
Found 12/20 approved changesets -- score normalized to 6
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
license file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
11 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More