diff --git a/package-lock.json b/package-lock.json index 4a407d8..e2f7815 100644 --- a/package-lock.json +++ b/package-lock.json @@ -17983,7 +17983,7 @@ }, "packages/zod-mock": { "name": "@anatine/zod-mock", - "version": "3.13.1", + "version": "3.13.2", "license": "MIT", "dependencies": { "randexp": "^0.5.3" @@ -17994,10 +17994,10 @@ }, "packages/zod-nestjs": { "name": "@anatine/zod-nestjs", - "version": "2.0.1", + "version": "2.0.3", "license": "MIT", "peerDependencies": { - "@anatine/zod-openapi": "^1.10.0", + "@anatine/zod-openapi": "^2.0.1", "@nestjs/common": "^7.0.0 || ^8.0.0 || ^9.0.0 || ^10.0.0", "@nestjs/swagger": "^6.0.0 || ^7.0.0", "openapi3-ts": "^4.1.2", @@ -18006,7 +18006,7 @@ }, "packages/zod-openapi": { "name": "@anatine/zod-openapi", - "version": "2.0.1", + "version": "2.1.0", "license": "MIT", "dependencies": { "ts-deepmerge": "^6.0.3" diff --git a/packages/zod-openapi/src/lib/zod-openapi.spec.ts b/packages/zod-openapi/src/lib/zod-openapi.spec.ts index 27c4587..c3dfe71 100644 --- a/packages/zod-openapi/src/lib/zod-openapi.spec.ts +++ b/packages/zod-openapi/src/lib/zod-openapi.spec.ts @@ -931,4 +931,34 @@ describe('zodOpenapi', () => { ] }); }) + + it('should work with ZodPipeline', () => { + expect( + generateSchema( + z + .string() + .regex(/^\d+$/) + .transform(Number) + .pipe(z.number().min(0).max(10)) + ) + ).toEqual({ + type: 'string', + pattern: '^\\d+$', + } satisfies SchemaObject); + + expect( + generateSchema( + z + .string() + .regex(/^\d+$/) + .transform(Number) + .pipe(z.number().min(0).max(10)), + true + ) + ).toEqual({ + type: 'number', + minimum: 0, + maximum: 10, + } satisfies SchemaObject); + }); }); diff --git a/packages/zod-openapi/src/lib/zod-openapi.ts b/packages/zod-openapi/src/lib/zod-openapi.ts index baaee16..c8689a2 100644 --- a/packages/zod-openapi/src/lib/zod-openapi.ts +++ b/packages/zod-openapi/src/lib/zod-openapi.ts @@ -511,6 +511,16 @@ function catchAllParser({ ); } +function parsePipeline({ + zodRef, + useOutput, +}: ParsingArgs>): SchemaObject { + if (useOutput) { + return generateSchema(zodRef._def.out, useOutput); + } + return generateSchema(zodRef._def.in, useOutput); +} + const workerMap = { ZodObject: parseObject, ZodRecord: parseRecord, @@ -545,6 +555,7 @@ const workerMap = { ZodAny: catchAllParser, ZodUnknown: catchAllParser, ZodVoid: catchAllParser, + ZodPipeline: parsePipeline, }; type WorkerKeys = keyof typeof workerMap;