From 6608e3e562a9ca0e1d83c100f8d0b25d8386dd6b Mon Sep 17 00:00:00 2001 From: InfiniteXyy Date: Thu, 16 May 2024 11:32:21 +0800 Subject: [PATCH] feat(zod-mock): support configure mock array length --- package-lock.json | 10 +++++----- packages/zod-mock/src/lib/zod-mock.ts | 10 +++++++++- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 4c1997a..5f3d776 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17807,7 +17807,7 @@ }, "packages/graphql-codegen-zod": { "name": "@anatine/graphql-codegen-zod", - "version": "0.4.0", + "version": "0.4.1", "license": "MIT", "dependencies": { "@graphql-tools/utils": "^8.8.0" @@ -17831,7 +17831,7 @@ }, "packages/graphql-zod-validation": { "name": "@anatine/graphql-zod-validation", - "version": "0.9.0", + "version": "0.9.1", "license": "MIT", "dependencies": { "@graphql-codegen/plugin-helpers": "^2.6.0", @@ -17983,7 +17983,7 @@ }, "packages/zod-mock": { "name": "@anatine/zod-mock", - "version": "3.13.3", + "version": "3.13.4", "license": "MIT", "dependencies": { "randexp": "^0.5.3" @@ -17995,7 +17995,7 @@ }, "packages/zod-nestjs": { "name": "@anatine/zod-nestjs", - "version": "2.0.3", + "version": "2.0.8", "license": "MIT", "peerDependencies": { "@anatine/zod-openapi": "^2.0.1", @@ -18007,7 +18007,7 @@ }, "packages/zod-openapi": { "name": "@anatine/zod-openapi", - "version": "2.2.2", + "version": "2.2.5", "license": "MIT", "dependencies": { "ts-deepmerge": "^6.0.3" diff --git a/packages/zod-mock/src/lib/zod-mock.ts b/packages/zod-mock/src/lib/zod-mock.ts index 31ebdeb..7d388aa 100644 --- a/packages/zod-mock/src/lib/zod-mock.ts +++ b/packages/zod-mock/src/lib/zod-mock.ts @@ -336,7 +336,10 @@ function parseArray(zodRef: z.ZodArray, 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(zodRef._def.type, options)); @@ -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` */