From 3b6100abdd857878cadbb3c2424d78807825ff8f Mon Sep 17 00:00:00 2001 From: Gamote Date: Tue, 10 Dec 2024 09:26:06 +0100 Subject: [PATCH] fix: mark the `ZodDate` as `string` type in the OpenAPI schema --- .../nestjs-zod/src/openapi/zod-to-openapi.test.ts | 11 ++++++++++- packages/nestjs-zod/src/openapi/zod-to-openapi.ts | 4 ++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/nestjs-zod/src/openapi/zod-to-openapi.test.ts b/packages/nestjs-zod/src/openapi/zod-to-openapi.test.ts index ae0c916..1a056af 100644 --- a/packages/nestjs-zod/src/openapi/zod-to-openapi.test.ts +++ b/packages/nestjs-zod/src/openapi/zod-to-openapi.test.ts @@ -94,7 +94,16 @@ describe.each([ expect(openApiObject).toMatchSnapshot() }) - + + it('should serialize dates', () => { + const schema = z.date() + const openApiObject = zodToOpenAPI(schema) + + expect(openApiObject).toEqual({ + type: 'string', + }) + }); + it('should serialize objects', () => { const schema = z.object({ prop1: z.string(), diff --git a/packages/nestjs-zod/src/openapi/zod-to-openapi.ts b/packages/nestjs-zod/src/openapi/zod-to-openapi.ts index 66e8d66..1ad056f 100644 --- a/packages/nestjs-zod/src/openapi/zod-to-openapi.ts +++ b/packages/nestjs-zod/src/openapi/zod-to-openapi.ts @@ -51,6 +51,10 @@ export function zodToOpenAPI( } } + if (is(zodType, z.ZodDate)) { + object.type = 'string' + } + if (is(zodType, z.ZodPassword)) { const { checks } = zodType._def const regex = zodType.buildFullRegExp()