Skip to content

Commit

Permalink
update example types
Browse files Browse the repository at this point in the history
  • Loading branch information
samchungy committed Nov 29, 2024
1 parent c9fd572 commit 8efeb59
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
11 changes: 11 additions & 0 deletions src/extendZod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
8 changes: 5 additions & 3 deletions src/extendZodTypes.ts
Original file line number Diff line number Diff line change
@@ -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> = T extends Date ? Exclude<T, Date> | string : T;

/**
* zod-openapi metadata
*/
interface ZodOpenApiMetadata<
T extends ZodTypeAny,
TInferred = z.input<T> | z.output<T>,
TInferred = Exclude<ReplaceDate<z.input<T> | z.output<T>>, 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`
*/
Expand Down

0 comments on commit 8efeb59

Please sign in to comment.