Skip to content

Commit

Permalink
feat(zod-mock): support configure mock array length
Browse files Browse the repository at this point in the history
  • Loading branch information
InfiniteXyy committed May 16, 2024
1 parent 132e472 commit 6608e3e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion packages/zod-mock/src/lib/zod-mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,10 @@ function parseArray(zodRef: z.ZodArray<never>, options?: GenerateMockOptions) {
if (min > max) {
min = max;
}
const targetLength = fakerInstance.number.int({ min, max });
// if arrayItemsLength is already configured in options, use that
const targetLength =
options?.mapArrayItemsLength ?? fakerInstance.number.int({ min, max });

const results: ZodTypeAny[] = [];
for (let index = 0; index < targetLength; index++) {
results.push(generateMock<ZodTypeAny>(zodRef._def.type, options));
Expand Down Expand Up @@ -571,6 +574,11 @@ export interface GenerateMockOptions {
*/
mapEntriesLength?: number;

/**
* How items to create for Arrays
*/
mapArrayItemsLength?: number;

/**
* Set to true to throw an exception instead of returning undefined when encountering an unknown `ZodType`
*/
Expand Down

0 comments on commit 6608e3e

Please sign in to comment.