zod-openapi
Version:
Convert Zod Schemas to OpenAPI v3.x documentation
92 lines (90 loc) • 3.56 kB
TypeScript
import { ZodTypeAny, z, ZodObject } from 'zod';
import { CreationType } from './create/components.js';
import { currentSymbol, previousSymbol } from './extendZodSymbols.js';
import { HeaderObject as HeaderObject$1, SchemaObject as SchemaObject$1 } from './openapi3-ts/dist/model/openapi30.js';
import { ParameterObject, ExampleObject, ReferenceObject, HeaderObject, SchemaObject as SchemaObject$2 } from './openapi3-ts/dist/model/openapi31.js';
type SchemaObject = SchemaObject$1 & SchemaObject$2;
type ReplaceDate<T> = T extends Date ? Date | string : T;
/**
* zod-openapi metadata
*/
interface ZodOpenApiMetadata<T extends ZodTypeAny, TInferred = Exclude<ReplaceDate<z.input<T> | z.output<T>>, undefined>> extends SchemaObject {
example?: TInferred;
examples?: [TInferred, ...TInferred[]];
default?: TInferred;
/**
* Used to set the output of a ZodUnion to be `oneOf` instead of `allOf`
*/
unionOneOf?: boolean;
/**
* Used to output this Zod Schema in the components schemas section. Any usage of this Zod Schema will then be transformed into a $ref.
*/
ref?: string;
/**
* Used when you are manually adding a Zod Schema to the components section. This controls whether this should be rendered as a request (`input`) or response (`output`). Defaults to `output`
*/
refType?: CreationType;
/**
* Used to set the created type of an effect.
* If this was previously set to `same` and this is throwing an error, your effect is no longer returning the same type.
*/
effectType?: CreationType | (z.input<T> extends z.output<T> ? z.output<T> extends z.input<T> ? 'same' : never : never);
/**
* Used to set metadata for a parameter, request header or cookie
*/
param?: Partial<ParameterObject> & {
example?: TInferred;
examples?: Record<string, (ExampleObject & {
value: TInferred;
}) | ReferenceObject>;
/**
* Used to output this Zod Schema in the components parameters section. Any usage of this Zod Schema will then be transformed into a $ref.
*/
ref?: string;
};
/**
* Used to set data for a response header
*/
header?: Partial<HeaderObject & HeaderObject$1> & {
/**
* Used to output this Zod Schema in the components headers section. Any usage of this Zod Schema will then be transformed into a $ref.
*/
ref?: string;
};
/**
* Used to override the generated type. If this is provided no metadata will be generated.
*/
type?: SchemaObject['type'];
}
interface ZodOpenApiMetadataDef {
/**
* Up to date OpenAPI metadata
*/
openapi?: ZodOpenApiMetadata<ZodTypeAny>;
/**
* Used to keep track of the Zod Schema had `.openapi` called on it
*/
[currentSymbol]?: ZodTypeAny;
/**
* Used to keep track of the previous Zod Schema that had `.openapi` called on it if another `.openapi` is called.
* This can also be present when .extend is called on an object.
*/
[previousSymbol]?: ZodTypeAny;
}
interface ZodOpenApiExtendMetadata {
extends: ZodObject<any, any, any, any, any>;
}
declare module 'zod' {
interface ZodType {
/**
* Add OpenAPI metadata to a Zod Type
*/
openapi<T extends ZodTypeAny>(this: T, metadata: ZodOpenApiMetadata<T>): T;
}
interface ZodTypeDef {
zodOpenApi?: ZodOpenApiMetadataDef;
}
interface ZodObjectDef {
extendMetadata?: ZodOpenApiExtendMetadata;
}
}