diff --git a/src/extendZod.test.ts b/src/extendZod.test.ts index faea140..00501dc 100644 --- a/src/extendZod.test.ts +++ b/src/extendZod.test.ts @@ -89,4 +89,15 @@ describe('extendZodWithOpenApi', () => { expect(barString._def.zodOpenApi?.openapi?.effectType).toBe('input'); }); + + it('makes a date input accept strings', () => { + const fooString = z.union([z.date().optional(), z.string(), z.null()]); + + const barString = fooString.openapi({ + description: 'foo', + examples: [null, '2021-01-01'], + }); + + expect(barString._def.zodOpenApi?.openapi?.effectType).toBe('input'); + }); }); diff --git a/src/extendZodTypes.ts b/src/extendZodTypes.ts index af6e16b..b4a69a0 100644 --- a/src/extendZodTypes.ts +++ b/src/extendZodTypes.ts @@ -1,20 +1,22 @@ -import type { ZodDate, ZodObject, ZodTypeAny, z } from 'zod'; +import type { ZodObject, ZodTypeAny, z } from 'zod'; import type { CreationType } from './create/components'; import type { oas30, oas31 } from './openapi3-ts/dist'; type SchemaObject = oas30.SchemaObject & oas31.SchemaObject; +type ReplaceDate = T extends Date ? Exclude | string : T; + /** * zod-openapi metadata */ interface ZodOpenApiMetadata< T extends ZodTypeAny, - TInferred = z.input | z.output, + TInferred = Exclude | z.output>, undefined>, > extends SchemaObject { example?: TInferred; examples?: [TInferred, ...TInferred[]]; - default?: T extends ZodDate ? string : TInferred; + default?: TInferred; /** * Used to set the output of a ZodUnion to be `oneOf` instead of `allOf` */