zod-openapi 5.0.0-beta.4 → 5.0.0-beta.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +208 -71
- package/api/index.d.ts +1 -0
- package/api/package.json +5 -0
- package/dist/api.cjs +8 -0
- package/dist/api.d.mts +3 -22
- package/dist/api.d.ts +3 -22
- package/dist/api.mjs +8 -4
- package/dist/components.chunk.cjs +2463 -0
- package/dist/components.chunk.mjs +2464 -0
- package/dist/create/components.d.ts +125 -0
- package/dist/create/content.d.ts +8 -0
- package/dist/create/document.d.ts +91 -0
- package/dist/create/parameters.d.ts +9 -0
- package/dist/create/schema/single.d.ts +33 -0
- package/dist/extend.cjs +4 -0
- package/dist/extend.d.mts +1 -0
- package/dist/extend.d.ts +1 -0
- package/dist/extend.mjs +3 -0
- package/dist/extendZod.chunk.cjs +95 -0
- package/dist/extendZod.chunk.mjs +96 -0
- package/dist/extendZod.d.ts +6 -0
- package/dist/extendZodSymbols.chunk.cjs +5 -0
- package/dist/extendZodSymbols.chunk.mjs +6 -0
- package/dist/extendZodSymbols.d.ts +4 -0
- package/dist/extendZodTypes.d.ts +91 -0
- package/dist/index.cjs +60 -0
- package/dist/index.d.mts +7 -23
- package/dist/index.d.ts +7 -23
- package/dist/index.mjs +59 -18
- package/dist/openapi.d.ts +5 -0
- package/dist/openapi3-ts/dist/model/oas-common.d.ts +16 -0
- package/dist/openapi3-ts/dist/model/openapi30.d.ts +291 -0
- package/dist/openapi3-ts/dist/model/openapi31.d.ts +298 -0
- package/dist/openapi3-ts/dist/model/specification-extension.d.ts +7 -0
- package/dist/openapi3-ts/dist/oas30.d.ts +3 -0
- package/dist/openapi3-ts/dist/oas31.d.ts +3 -0
- package/extend/index.d.ts +1 -0
- package/extend/package.json +5 -0
- package/package.json +30 -13
- package/dist/api.js +0 -8
- package/dist/components-5_CJdR73.d.ts +0 -543
- package/dist/components-CXjVnBr-.js +0 -782
- package/dist/components-CvutxtFV.mjs +0 -741
- package/dist/components-DAYTA1Um.d.mts +0 -543
- package/dist/create/componentsSideEffects.d.mts +0 -6
- package/dist/create/componentsSideEffects.d.ts +0 -6
- package/dist/create/componentsSideEffects.js +0 -48
- package/dist/create/componentsSideEffects.mjs +0 -48
- package/dist/index.js +0 -20
- package/dist/zod-BvA30wad.mjs +0 -5
- package/dist/zod-i2t01GF0.js +0 -40
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { ZodType } from 'zod';
|
|
2
|
+
import { SchemaObject as SchemaObject$1, ReferenceObject as ReferenceObject$1, ParameterObject as ParameterObject$1, HeaderObject as HeaderObject$1, RequestBodyObject as RequestBodyObject$1, ResponseObject as ResponseObject$1, CallbackObject as CallbackObject$1 } from '../openapi3-ts/dist/model/openapi30.js';
|
|
3
|
+
import { SchemaObject, ReferenceObject, ParameterLocation, ParameterObject, HeaderObject, RequestBodyObject, ResponseObject, CallbackObject, ComponentsObject as ComponentsObject$1 } from '../openapi3-ts/dist/model/openapi31.js';
|
|
4
|
+
import { ZodOpenApiComponentsObject, ZodOpenApiRequestBodyObject, ZodOpenApiResponseObject, ZodOpenApiCallbackObject, ZodOpenApiVersion, CreateDocumentOptions } from './document.js';
|
|
5
|
+
|
|
6
|
+
type CreationType = 'input' | 'output';
|
|
7
|
+
type BaseEffect = {
|
|
8
|
+
zodType: ZodType;
|
|
9
|
+
path: string[];
|
|
10
|
+
};
|
|
11
|
+
type ComponentEffect = BaseEffect & {
|
|
12
|
+
type: 'component';
|
|
13
|
+
};
|
|
14
|
+
type SchemaEffect = BaseEffect & {
|
|
15
|
+
type: 'schema';
|
|
16
|
+
creationType: CreationType;
|
|
17
|
+
};
|
|
18
|
+
type Effect = ComponentEffect | SchemaEffect;
|
|
19
|
+
type ResolvedEffect = {
|
|
20
|
+
creationType: CreationType;
|
|
21
|
+
path: string[];
|
|
22
|
+
zodType: ZodType;
|
|
23
|
+
component?: {
|
|
24
|
+
ref: string;
|
|
25
|
+
zodType: ZodType;
|
|
26
|
+
path: string[];
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
interface CompleteSchemaComponent extends BaseSchemaComponent {
|
|
30
|
+
type: 'complete';
|
|
31
|
+
schemaObject: SchemaObject | ReferenceObject | SchemaObject$1 | ReferenceObject$1;
|
|
32
|
+
/** Set when the created schemaObject is specific to a particular effect */
|
|
33
|
+
effects?: Effect[];
|
|
34
|
+
resolvedEffect?: ResolvedEffect;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
*
|
|
38
|
+
*/
|
|
39
|
+
interface ManualSchemaComponent extends BaseSchemaComponent {
|
|
40
|
+
type: 'manual';
|
|
41
|
+
}
|
|
42
|
+
interface InProgressSchemaComponent extends BaseSchemaComponent {
|
|
43
|
+
type: 'in-progress';
|
|
44
|
+
}
|
|
45
|
+
interface BaseSchemaComponent {
|
|
46
|
+
ref: string;
|
|
47
|
+
}
|
|
48
|
+
type SchemaComponent = CompleteSchemaComponent | ManualSchemaComponent | InProgressSchemaComponent;
|
|
49
|
+
type SchemaComponentMap = Map<ZodType, SchemaComponent>;
|
|
50
|
+
interface CompleteParameterComponent extends BaseParameterComponent {
|
|
51
|
+
type: 'complete';
|
|
52
|
+
paramObject: ParameterObject | ReferenceObject | ParameterObject$1 | ReferenceObject$1;
|
|
53
|
+
}
|
|
54
|
+
interface PartialParameterComponent extends BaseParameterComponent {
|
|
55
|
+
type: 'manual';
|
|
56
|
+
}
|
|
57
|
+
interface BaseParameterComponent {
|
|
58
|
+
ref: string;
|
|
59
|
+
in: ParameterLocation;
|
|
60
|
+
name: string;
|
|
61
|
+
}
|
|
62
|
+
type ParameterComponent = CompleteParameterComponent | PartialParameterComponent;
|
|
63
|
+
type ParameterComponentMap = Map<ZodType, ParameterComponent>;
|
|
64
|
+
interface CompleteHeaderComponent extends BaseHeaderComponent {
|
|
65
|
+
type: 'complete';
|
|
66
|
+
headerObject: HeaderObject | ReferenceObject | HeaderObject$1 | ReferenceObject$1;
|
|
67
|
+
}
|
|
68
|
+
interface PartialHeaderComponent extends BaseHeaderComponent {
|
|
69
|
+
type: 'manual';
|
|
70
|
+
}
|
|
71
|
+
interface BaseHeaderComponent {
|
|
72
|
+
ref: string;
|
|
73
|
+
}
|
|
74
|
+
type HeaderComponent = CompleteHeaderComponent | PartialHeaderComponent;
|
|
75
|
+
type HeaderComponentMap = Map<ZodType, HeaderComponent>;
|
|
76
|
+
interface BaseResponseComponent {
|
|
77
|
+
ref: string;
|
|
78
|
+
}
|
|
79
|
+
interface CompleteResponseComponent extends BaseResponseComponent {
|
|
80
|
+
type: 'complete';
|
|
81
|
+
responseObject: ResponseObject | ReferenceObject | ResponseObject$1 | ReferenceObject$1;
|
|
82
|
+
}
|
|
83
|
+
interface PartialResponseComponent extends BaseResponseComponent {
|
|
84
|
+
type: 'manual';
|
|
85
|
+
}
|
|
86
|
+
type ResponseComponent = CompleteResponseComponent | PartialResponseComponent;
|
|
87
|
+
type ResponseComponentMap = Map<ZodOpenApiResponseObject, ResponseComponent>;
|
|
88
|
+
interface BaseRequestBodyComponent {
|
|
89
|
+
ref: string;
|
|
90
|
+
}
|
|
91
|
+
interface CompleteRequestBodyComponent extends BaseRequestBodyComponent {
|
|
92
|
+
type: 'complete';
|
|
93
|
+
requestBodyObject: RequestBodyObject | ReferenceObject | RequestBodyObject$1 | ReferenceObject$1;
|
|
94
|
+
}
|
|
95
|
+
interface PartialRequestBodyComponent extends BaseRequestBodyComponent {
|
|
96
|
+
type: 'manual';
|
|
97
|
+
}
|
|
98
|
+
type RequestBodyComponent = CompleteRequestBodyComponent | PartialRequestBodyComponent;
|
|
99
|
+
type RequestBodyComponentMap = Map<ZodOpenApiRequestBodyObject, RequestBodyComponent>;
|
|
100
|
+
interface BaseCallbackComponent {
|
|
101
|
+
ref: string;
|
|
102
|
+
}
|
|
103
|
+
interface CompleteCallbackComponent extends BaseCallbackComponent {
|
|
104
|
+
type: 'complete';
|
|
105
|
+
callbackObject: ZodOpenApiCallbackObject | CallbackObject | CallbackObject$1;
|
|
106
|
+
}
|
|
107
|
+
interface PartialCallbackComponent extends BaseCallbackComponent {
|
|
108
|
+
type: 'manual';
|
|
109
|
+
}
|
|
110
|
+
type CallbackComponent = CompleteCallbackComponent | PartialCallbackComponent;
|
|
111
|
+
type CallbackComponentMap = Map<ZodOpenApiCallbackObject, CallbackComponent>;
|
|
112
|
+
interface ComponentsObject {
|
|
113
|
+
schemas: SchemaComponentMap;
|
|
114
|
+
parameters: ParameterComponentMap;
|
|
115
|
+
headers: HeaderComponentMap;
|
|
116
|
+
requestBodies: RequestBodyComponentMap;
|
|
117
|
+
responses: ResponseComponentMap;
|
|
118
|
+
callbacks: CallbackComponentMap;
|
|
119
|
+
openapi: ZodOpenApiVersion;
|
|
120
|
+
}
|
|
121
|
+
declare const getDefaultComponents: (componentsObject?: ZodOpenApiComponentsObject, openapi?: ZodOpenApiVersion) => ComponentsObject;
|
|
122
|
+
declare const createComponents: (componentsObject: ZodOpenApiComponentsObject, components: ComponentsObject, documentOptions?: CreateDocumentOptions) => ComponentsObject$1 | undefined;
|
|
123
|
+
|
|
124
|
+
export { createComponents, getDefaultComponents };
|
|
125
|
+
export type { BaseCallbackComponent, CallbackComponent, CallbackComponentMap, CompleteCallbackComponent, CompleteHeaderComponent, CompleteParameterComponent, CompleteRequestBodyComponent, CompleteResponseComponent, CompleteSchemaComponent, ComponentEffect, ComponentsObject, CreationType, Effect, HeaderComponent, HeaderComponentMap, InProgressSchemaComponent, ManualSchemaComponent, ParameterComponent, ParameterComponentMap, PartialCallbackComponent, PartialHeaderComponent, PartialParameterComponent, PartialRequestBodyComponent, PartialResponseComponent, RequestBodyComponent, RequestBodyComponentMap, ResolvedEffect, ResponseComponent, ResponseComponentMap, SchemaComponent, SchemaComponentMap, SchemaEffect };
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { ZodType } from 'zod';
|
|
2
|
+
import { SchemaObject, ReferenceObject } from '../openapi3-ts/dist/model/openapi31.js';
|
|
3
|
+
import { ComponentsObject, CreationType } from './components.js';
|
|
4
|
+
import { CreateDocumentOptions } from './document.js';
|
|
5
|
+
|
|
6
|
+
declare const createMediaTypeSchema: (schemaObject: ZodType | SchemaObject | ReferenceObject | undefined, components: ComponentsObject, type: CreationType, subpath: string[], documentOptions?: CreateDocumentOptions) => SchemaObject | ReferenceObject | undefined;
|
|
7
|
+
|
|
8
|
+
export { createMediaTypeSchema };
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { ZodType, AnyZodObject, ZodTypeDef } from 'zod';
|
|
2
|
+
import { OpenApiVersion } from '../openapi.js';
|
|
3
|
+
import { MediaTypeObject as MediaTypeObject$1, RequestBodyObject as RequestBodyObject$1, ResponseObject as ResponseObject$1, HeadersObject, ReferenceObject as ReferenceObject$1, ParameterLocation as ParameterLocation$1, PathItemObject as PathItemObject$1, OperationObject as OperationObject$1, ParameterObject as ParameterObject$1, ComponentsObject as ComponentsObject$1, SchemaObject as SchemaObject$1, HeaderObject as HeaderObject$1 } from '../openapi3-ts/dist/model/openapi30.js';
|
|
4
|
+
import { ISpecificationExtension } from '../openapi3-ts/dist/model/specification-extension.js';
|
|
5
|
+
import { MediaTypeObject, SchemaObject, ReferenceObject, RequestBodyObject, ResponseObject, HeadersObject as HeadersObject$1, ParameterLocation, PathItemObject, OperationObject, ParameterObject, ComponentsObject, HeaderObject, OpenAPIObject } from '../openapi3-ts/dist/model/openapi31.js';
|
|
6
|
+
|
|
7
|
+
interface ZodOpenApiMediaTypeObject extends Omit<MediaTypeObject & MediaTypeObject$1, 'schema'> {
|
|
8
|
+
schema?: ZodType | SchemaObject | ReferenceObject;
|
|
9
|
+
}
|
|
10
|
+
interface ZodOpenApiContentObject {
|
|
11
|
+
'application/json'?: ZodOpenApiMediaTypeObject;
|
|
12
|
+
[mediatype: string]: ZodOpenApiMediaTypeObject | undefined;
|
|
13
|
+
}
|
|
14
|
+
interface ZodOpenApiRequestBodyObject extends Omit<RequestBodyObject & RequestBodyObject$1, 'content'> {
|
|
15
|
+
content: ZodOpenApiContentObject;
|
|
16
|
+
/** Use this field to auto register this request body as a component */
|
|
17
|
+
ref?: string;
|
|
18
|
+
}
|
|
19
|
+
interface ZodOpenApiResponseObject extends Omit<ResponseObject & ResponseObject$1, 'content' | 'headers'> {
|
|
20
|
+
content?: ZodOpenApiContentObject;
|
|
21
|
+
headers?: AnyZodObject | HeadersObject | HeadersObject$1;
|
|
22
|
+
/** Use this field to auto register this response object as a component */
|
|
23
|
+
ref?: string;
|
|
24
|
+
}
|
|
25
|
+
interface ZodOpenApiResponsesObject extends ISpecificationExtension {
|
|
26
|
+
default?: ZodOpenApiResponseObject | ReferenceObject | ReferenceObject$1;
|
|
27
|
+
[statuscode: `${1 | 2 | 3 | 4 | 5}${string}`]: ZodOpenApiResponseObject | ReferenceObject;
|
|
28
|
+
}
|
|
29
|
+
type ZodOpenApiParameters = Partial<Record<ParameterLocation & ParameterLocation$1, ZodObjectInputType>>;
|
|
30
|
+
interface ZodOpenApiCallbacksObject extends ISpecificationExtension {
|
|
31
|
+
[name: string]: ZodOpenApiCallbackObject;
|
|
32
|
+
}
|
|
33
|
+
interface ZodOpenApiCallbackObject extends ISpecificationExtension {
|
|
34
|
+
/** Use this field to auto register this callback object as a component */
|
|
35
|
+
ref?: string;
|
|
36
|
+
[name: string]: ZodOpenApiPathItemObject | string | undefined;
|
|
37
|
+
}
|
|
38
|
+
interface ZodOpenApiOperationObject extends Omit<OperationObject & OperationObject$1, 'requestBody' | 'responses' | 'parameters' | 'callbacks'> {
|
|
39
|
+
parameters?: Array<ZodType | ParameterObject | ParameterObject$1 | ReferenceObject | ReferenceObject$1>;
|
|
40
|
+
requestBody?: ZodOpenApiRequestBodyObject;
|
|
41
|
+
requestParams?: ZodOpenApiParameters;
|
|
42
|
+
responses: ZodOpenApiResponsesObject;
|
|
43
|
+
callbacks?: ZodOpenApiCallbacksObject;
|
|
44
|
+
}
|
|
45
|
+
interface ZodOpenApiPathItemObject extends Omit<PathItemObject & PathItemObject$1, 'get' | 'put' | 'post' | 'delete' | 'options' | 'head' | 'patch' | 'trace'> {
|
|
46
|
+
get?: ZodOpenApiOperationObject;
|
|
47
|
+
put?: ZodOpenApiOperationObject;
|
|
48
|
+
post?: ZodOpenApiOperationObject;
|
|
49
|
+
delete?: ZodOpenApiOperationObject;
|
|
50
|
+
options?: ZodOpenApiOperationObject;
|
|
51
|
+
head?: ZodOpenApiOperationObject;
|
|
52
|
+
patch?: ZodOpenApiOperationObject;
|
|
53
|
+
trace?: ZodOpenApiOperationObject;
|
|
54
|
+
}
|
|
55
|
+
interface ZodOpenApiPathsObject extends ISpecificationExtension {
|
|
56
|
+
[path: string]: ZodOpenApiPathItemObject;
|
|
57
|
+
}
|
|
58
|
+
interface ZodOpenApiComponentsObject extends Omit<ComponentsObject & ComponentsObject$1, 'schemas' | 'responses' | 'requestBodies' | 'headers' | 'parameters'> {
|
|
59
|
+
parameters?: Record<string, ZodType | ParameterObject | ParameterObject$1 | ReferenceObject | ReferenceObject$1>;
|
|
60
|
+
schemas?: Record<string, ZodType | SchemaObject | ReferenceObject | SchemaObject$1 | ReferenceObject$1>;
|
|
61
|
+
requestBodies?: Record<string, ZodOpenApiRequestBodyObject>;
|
|
62
|
+
headers?: Record<string, ZodType | HeaderObject | HeaderObject$1 | ReferenceObject | ReferenceObject$1>;
|
|
63
|
+
responses?: Record<string, ZodOpenApiResponseObject>;
|
|
64
|
+
callbacks?: Record<string, ZodOpenApiCallbackObject>;
|
|
65
|
+
}
|
|
66
|
+
type ZodOpenApiVersion = OpenApiVersion;
|
|
67
|
+
interface ZodOpenApiObject extends Omit<OpenAPIObject, 'openapi' | 'paths' | 'webhooks' | 'components'> {
|
|
68
|
+
openapi: ZodOpenApiVersion;
|
|
69
|
+
paths?: ZodOpenApiPathsObject;
|
|
70
|
+
webhooks?: ZodOpenApiPathsObject;
|
|
71
|
+
components?: ZodOpenApiComponentsObject;
|
|
72
|
+
}
|
|
73
|
+
type ZodObjectInputType<Output = unknown, Def extends ZodTypeDef = ZodTypeDef, Input = Record<string, unknown>> = ZodType<Output, Def, Input>;
|
|
74
|
+
interface CreateDocumentOptions {
|
|
75
|
+
/**
|
|
76
|
+
* Used to throw an error if a Discriminated Union member is not registered as a component
|
|
77
|
+
*/
|
|
78
|
+
enforceDiscriminatedUnionComponents?: boolean;
|
|
79
|
+
/**
|
|
80
|
+
* Used to change the default Zod Date schema
|
|
81
|
+
*/
|
|
82
|
+
defaultDateSchema?: Pick<SchemaObject, 'type' | 'format'>;
|
|
83
|
+
/**
|
|
84
|
+
* Used to set the output of a ZodUnion to be `oneOf` instead of `anyOf`
|
|
85
|
+
*/
|
|
86
|
+
unionOneOf?: boolean;
|
|
87
|
+
}
|
|
88
|
+
declare const createDocument: (zodOpenApiObject: ZodOpenApiObject, documentOptions?: CreateDocumentOptions) => OpenAPIObject;
|
|
89
|
+
|
|
90
|
+
export { createDocument };
|
|
91
|
+
export type { CreateDocumentOptions, ZodObjectInputType, ZodOpenApiCallbackObject, ZodOpenApiCallbacksObject, ZodOpenApiComponentsObject, ZodOpenApiContentObject, ZodOpenApiMediaTypeObject, ZodOpenApiObject, ZodOpenApiOperationObject, ZodOpenApiParameters, ZodOpenApiPathItemObject, ZodOpenApiPathsObject, ZodOpenApiRequestBodyObject, ZodOpenApiResponseObject, ZodOpenApiResponsesObject, ZodOpenApiVersion };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { ZodType, AnyZodObject } from 'zod';
|
|
2
|
+
import { ParameterObject, ReferenceObject } from '../openapi3-ts/dist/model/openapi31.js';
|
|
3
|
+
import { ComponentsObject } from './components.js';
|
|
4
|
+
import { ZodOpenApiParameters, CreateDocumentOptions, ZodObjectInputType } from './document.js';
|
|
5
|
+
|
|
6
|
+
declare const createParamOrRef: (zodSchema: ZodType, components: ComponentsObject, subpath: string[], type?: keyof ZodOpenApiParameters, name?: string, documentOptions?: CreateDocumentOptions) => ParameterObject | ReferenceObject;
|
|
7
|
+
declare const getZodObject: (schema: ZodObjectInputType, type: "input" | "output") => AnyZodObject;
|
|
8
|
+
|
|
9
|
+
export { createParamOrRef, getZodObject };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { ZodType } from 'zod';
|
|
2
|
+
import { OpenApiVersion } from '../../openapi.js';
|
|
3
|
+
import { SchemaObject } from '../../openapi3-ts/dist/model/openapi30.js';
|
|
4
|
+
import { SchemaObject as SchemaObject$1, ReferenceObject } from '../../openapi3-ts/dist/model/openapi31.js';
|
|
5
|
+
import { CreationType } from '../components.js';
|
|
6
|
+
import { CreateDocumentOptions } from '../document.js';
|
|
7
|
+
|
|
8
|
+
interface SchemaResult {
|
|
9
|
+
schema: SchemaObject | SchemaObject$1 | ReferenceObject;
|
|
10
|
+
components?: Record<string, SchemaObject | SchemaObject$1 | ReferenceObject> | undefined;
|
|
11
|
+
}
|
|
12
|
+
interface CreateSchemaOptions extends CreateDocumentOptions {
|
|
13
|
+
/**
|
|
14
|
+
* This controls whether this should be rendered as a request (`input`) or response (`output`). Defaults to `output`
|
|
15
|
+
*/
|
|
16
|
+
schemaType?: CreationType;
|
|
17
|
+
/**
|
|
18
|
+
* OpenAPI version to use, defaults to `'3.1.0'`
|
|
19
|
+
*/
|
|
20
|
+
openapi?: OpenApiVersion;
|
|
21
|
+
/**
|
|
22
|
+
* Additional components to use and create while rendering the schema
|
|
23
|
+
*/
|
|
24
|
+
components?: Record<string, ZodType>;
|
|
25
|
+
/**
|
|
26
|
+
* The $ref path to use for the component. Defaults to `#/components/schemas/`
|
|
27
|
+
*/
|
|
28
|
+
componentRefPath?: string;
|
|
29
|
+
}
|
|
30
|
+
declare const createSchema: (zodType: ZodType, opts?: CreateSchemaOptions) => SchemaResult;
|
|
31
|
+
|
|
32
|
+
export { createSchema };
|
|
33
|
+
export type { CreateSchemaOptions, SchemaResult };
|
package/dist/extend.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import './extendZodTypes.js';
|
package/dist/extend.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import './extendZodTypes.js';
|
package/dist/extend.mjs
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const extendZodSymbols = require("./extendZodSymbols.chunk.cjs");
|
|
3
|
+
const mergeOpenApi = (openapi, {
|
|
4
|
+
ref: _ref,
|
|
5
|
+
refType: _refType,
|
|
6
|
+
param: _param,
|
|
7
|
+
header: _header,
|
|
8
|
+
...rest
|
|
9
|
+
} = {}) => ({
|
|
10
|
+
...rest,
|
|
11
|
+
...openapi
|
|
12
|
+
});
|
|
13
|
+
function extendZodWithOpenApi(zod) {
|
|
14
|
+
if (typeof zod.ZodType.prototype.openapi !== "undefined") {
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
zod.ZodType.prototype.openapi = function(openapi) {
|
|
18
|
+
const { zodOpenApi, ...rest } = this._def;
|
|
19
|
+
const result = new this.constructor({
|
|
20
|
+
...rest,
|
|
21
|
+
zodOpenApi: {
|
|
22
|
+
openapi: mergeOpenApi(
|
|
23
|
+
openapi,
|
|
24
|
+
zodOpenApi == null ? void 0 : zodOpenApi.openapi
|
|
25
|
+
)
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
result._def.zodOpenApi[extendZodSymbols.currentSymbol] = result;
|
|
29
|
+
if (zodOpenApi) {
|
|
30
|
+
result._def.zodOpenApi[extendZodSymbols.previousSymbol] = this;
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
const zodDescribe = zod.ZodType.prototype.describe;
|
|
35
|
+
zod.ZodType.prototype.describe = function(...args) {
|
|
36
|
+
const result = zodDescribe.apply(this, args);
|
|
37
|
+
const def = result._def;
|
|
38
|
+
if (def.zodOpenApi) {
|
|
39
|
+
const cloned = { ...def.zodOpenApi };
|
|
40
|
+
cloned.openapi = mergeOpenApi({ description: args[0] }, cloned.openapi);
|
|
41
|
+
cloned[extendZodSymbols.previousSymbol] = this;
|
|
42
|
+
cloned[extendZodSymbols.currentSymbol] = result;
|
|
43
|
+
def.zodOpenApi = cloned;
|
|
44
|
+
} else {
|
|
45
|
+
def.zodOpenApi = {
|
|
46
|
+
openapi: { description: args[0] },
|
|
47
|
+
[extendZodSymbols.currentSymbol]: result
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
return result;
|
|
51
|
+
};
|
|
52
|
+
const zodObjectExtend = zod.ZodObject.prototype.extend;
|
|
53
|
+
zod.ZodObject.prototype.extend = function(...args) {
|
|
54
|
+
const extendResult = zodObjectExtend.apply(this, args);
|
|
55
|
+
const zodOpenApi = extendResult._def.zodOpenApi;
|
|
56
|
+
if (zodOpenApi) {
|
|
57
|
+
const cloned = { ...zodOpenApi };
|
|
58
|
+
cloned.openapi = mergeOpenApi({}, cloned.openapi);
|
|
59
|
+
cloned[extendZodSymbols.previousSymbol] = this;
|
|
60
|
+
extendResult._def.zodOpenApi = cloned;
|
|
61
|
+
} else {
|
|
62
|
+
extendResult._def.zodOpenApi = {
|
|
63
|
+
[extendZodSymbols.previousSymbol]: this
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
return extendResult;
|
|
67
|
+
};
|
|
68
|
+
const zodObjectOmit = zod.ZodObject.prototype.omit;
|
|
69
|
+
zod.ZodObject.prototype.omit = function(...args) {
|
|
70
|
+
const omitResult = zodObjectOmit.apply(this, args);
|
|
71
|
+
const zodOpenApi = omitResult._def.zodOpenApi;
|
|
72
|
+
if (zodOpenApi) {
|
|
73
|
+
const cloned = { ...zodOpenApi };
|
|
74
|
+
cloned.openapi = mergeOpenApi({}, cloned.openapi);
|
|
75
|
+
delete cloned[extendZodSymbols.previousSymbol];
|
|
76
|
+
delete cloned[extendZodSymbols.currentSymbol];
|
|
77
|
+
omitResult._def.zodOpenApi = cloned;
|
|
78
|
+
}
|
|
79
|
+
return omitResult;
|
|
80
|
+
};
|
|
81
|
+
const zodObjectPick = zod.ZodObject.prototype.pick;
|
|
82
|
+
zod.ZodObject.prototype.pick = function(...args) {
|
|
83
|
+
const pickResult = zodObjectPick.apply(this, args);
|
|
84
|
+
const zodOpenApi = pickResult._def.zodOpenApi;
|
|
85
|
+
if (zodOpenApi) {
|
|
86
|
+
const cloned = { ...zodOpenApi };
|
|
87
|
+
cloned.openapi = mergeOpenApi({}, cloned.openapi);
|
|
88
|
+
delete cloned[extendZodSymbols.previousSymbol];
|
|
89
|
+
delete cloned[extendZodSymbols.currentSymbol];
|
|
90
|
+
pickResult._def.zodOpenApi = cloned;
|
|
91
|
+
}
|
|
92
|
+
return pickResult;
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
exports.extendZodWithOpenApi = extendZodWithOpenApi;
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { currentSymbol, previousSymbol } from "./extendZodSymbols.chunk.mjs";
|
|
2
|
+
const mergeOpenApi = (openapi, {
|
|
3
|
+
ref: _ref,
|
|
4
|
+
refType: _refType,
|
|
5
|
+
param: _param,
|
|
6
|
+
header: _header,
|
|
7
|
+
...rest
|
|
8
|
+
} = {}) => ({
|
|
9
|
+
...rest,
|
|
10
|
+
...openapi
|
|
11
|
+
});
|
|
12
|
+
function extendZodWithOpenApi(zod) {
|
|
13
|
+
if (typeof zod.ZodType.prototype.openapi !== "undefined") {
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
zod.ZodType.prototype.openapi = function(openapi) {
|
|
17
|
+
const { zodOpenApi, ...rest } = this._def;
|
|
18
|
+
const result = new this.constructor({
|
|
19
|
+
...rest,
|
|
20
|
+
zodOpenApi: {
|
|
21
|
+
openapi: mergeOpenApi(
|
|
22
|
+
openapi,
|
|
23
|
+
zodOpenApi == null ? void 0 : zodOpenApi.openapi
|
|
24
|
+
)
|
|
25
|
+
}
|
|
26
|
+
});
|
|
27
|
+
result._def.zodOpenApi[currentSymbol] = result;
|
|
28
|
+
if (zodOpenApi) {
|
|
29
|
+
result._def.zodOpenApi[previousSymbol] = this;
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
};
|
|
33
|
+
const zodDescribe = zod.ZodType.prototype.describe;
|
|
34
|
+
zod.ZodType.prototype.describe = function(...args) {
|
|
35
|
+
const result = zodDescribe.apply(this, args);
|
|
36
|
+
const def = result._def;
|
|
37
|
+
if (def.zodOpenApi) {
|
|
38
|
+
const cloned = { ...def.zodOpenApi };
|
|
39
|
+
cloned.openapi = mergeOpenApi({ description: args[0] }, cloned.openapi);
|
|
40
|
+
cloned[previousSymbol] = this;
|
|
41
|
+
cloned[currentSymbol] = result;
|
|
42
|
+
def.zodOpenApi = cloned;
|
|
43
|
+
} else {
|
|
44
|
+
def.zodOpenApi = {
|
|
45
|
+
openapi: { description: args[0] },
|
|
46
|
+
[currentSymbol]: result
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
return result;
|
|
50
|
+
};
|
|
51
|
+
const zodObjectExtend = zod.ZodObject.prototype.extend;
|
|
52
|
+
zod.ZodObject.prototype.extend = function(...args) {
|
|
53
|
+
const extendResult = zodObjectExtend.apply(this, args);
|
|
54
|
+
const zodOpenApi = extendResult._def.zodOpenApi;
|
|
55
|
+
if (zodOpenApi) {
|
|
56
|
+
const cloned = { ...zodOpenApi };
|
|
57
|
+
cloned.openapi = mergeOpenApi({}, cloned.openapi);
|
|
58
|
+
cloned[previousSymbol] = this;
|
|
59
|
+
extendResult._def.zodOpenApi = cloned;
|
|
60
|
+
} else {
|
|
61
|
+
extendResult._def.zodOpenApi = {
|
|
62
|
+
[previousSymbol]: this
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return extendResult;
|
|
66
|
+
};
|
|
67
|
+
const zodObjectOmit = zod.ZodObject.prototype.omit;
|
|
68
|
+
zod.ZodObject.prototype.omit = function(...args) {
|
|
69
|
+
const omitResult = zodObjectOmit.apply(this, args);
|
|
70
|
+
const zodOpenApi = omitResult._def.zodOpenApi;
|
|
71
|
+
if (zodOpenApi) {
|
|
72
|
+
const cloned = { ...zodOpenApi };
|
|
73
|
+
cloned.openapi = mergeOpenApi({}, cloned.openapi);
|
|
74
|
+
delete cloned[previousSymbol];
|
|
75
|
+
delete cloned[currentSymbol];
|
|
76
|
+
omitResult._def.zodOpenApi = cloned;
|
|
77
|
+
}
|
|
78
|
+
return omitResult;
|
|
79
|
+
};
|
|
80
|
+
const zodObjectPick = zod.ZodObject.prototype.pick;
|
|
81
|
+
zod.ZodObject.prototype.pick = function(...args) {
|
|
82
|
+
const pickResult = zodObjectPick.apply(this, args);
|
|
83
|
+
const zodOpenApi = pickResult._def.zodOpenApi;
|
|
84
|
+
if (zodOpenApi) {
|
|
85
|
+
const cloned = { ...zodOpenApi };
|
|
86
|
+
cloned.openapi = mergeOpenApi({}, cloned.openapi);
|
|
87
|
+
delete cloned[previousSymbol];
|
|
88
|
+
delete cloned[currentSymbol];
|
|
89
|
+
pickResult._def.zodOpenApi = cloned;
|
|
90
|
+
}
|
|
91
|
+
return pickResult;
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
export {
|
|
95
|
+
extendZodWithOpenApi
|
|
96
|
+
};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { ZodTypeAny, z, ZodObject } from 'zod';
|
|
2
|
+
import { CreationType } from './create/components.js';
|
|
3
|
+
import { currentSymbol, previousSymbol } from './extendZodSymbols.js';
|
|
4
|
+
import { SchemaObject as SchemaObject$1, HeaderObject as HeaderObject$1 } from './openapi3-ts/dist/model/openapi30.js';
|
|
5
|
+
import { SchemaObject as SchemaObject$2, ParameterObject, ExampleObject, ReferenceObject, HeaderObject } from './openapi3-ts/dist/model/openapi31.js';
|
|
6
|
+
|
|
7
|
+
type SchemaObject = SchemaObject$1 & SchemaObject$2;
|
|
8
|
+
type ReplaceDate<T> = T extends Date ? Date | string : T;
|
|
9
|
+
/**
|
|
10
|
+
* zod-openapi metadata
|
|
11
|
+
*/
|
|
12
|
+
interface ZodOpenApiMetadata<T extends ZodTypeAny, TInferred = Exclude<ReplaceDate<z.input<T> | z.output<T>>, undefined>> extends SchemaObject {
|
|
13
|
+
example?: TInferred;
|
|
14
|
+
examples?: [TInferred, ...TInferred[]];
|
|
15
|
+
default?: TInferred;
|
|
16
|
+
/**
|
|
17
|
+
* Used to set the output of a ZodUnion to be `oneOf` instead of `allOf`
|
|
18
|
+
*/
|
|
19
|
+
unionOneOf?: boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Used to output this Zod Schema in the components schemas section. Any usage of this Zod Schema will then be transformed into a $ref.
|
|
22
|
+
*/
|
|
23
|
+
ref?: string;
|
|
24
|
+
/**
|
|
25
|
+
* 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`
|
|
26
|
+
*/
|
|
27
|
+
refType?: CreationType;
|
|
28
|
+
/**
|
|
29
|
+
* Used to set the created type of an effect.
|
|
30
|
+
* If this was previously set to `same` and this is throwing an error, your effect is no longer returning the same type.
|
|
31
|
+
*/
|
|
32
|
+
effectType?: CreationType | (z.input<T> extends z.output<T> ? z.output<T> extends z.input<T> ? 'same' : never : never);
|
|
33
|
+
/**
|
|
34
|
+
* Used to set metadata for a parameter, request header or cookie
|
|
35
|
+
*/
|
|
36
|
+
param?: Partial<ParameterObject> & {
|
|
37
|
+
example?: TInferred;
|
|
38
|
+
examples?: Record<string, (ExampleObject & {
|
|
39
|
+
value: TInferred;
|
|
40
|
+
}) | ReferenceObject>;
|
|
41
|
+
/**
|
|
42
|
+
* Used to output this Zod Schema in the components parameters section. Any usage of this Zod Schema will then be transformed into a $ref.
|
|
43
|
+
*/
|
|
44
|
+
ref?: string;
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* Used to set data for a response header
|
|
48
|
+
*/
|
|
49
|
+
header?: Partial<HeaderObject & HeaderObject$1> & {
|
|
50
|
+
/**
|
|
51
|
+
* Used to output this Zod Schema in the components headers section. Any usage of this Zod Schema will then be transformed into a $ref.
|
|
52
|
+
*/
|
|
53
|
+
ref?: string;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Used to override the generated type. If this is provided no metadata will be generated.
|
|
57
|
+
*/
|
|
58
|
+
type?: SchemaObject['type'];
|
|
59
|
+
}
|
|
60
|
+
interface ZodOpenApiMetadataDef {
|
|
61
|
+
/**
|
|
62
|
+
* Up to date OpenAPI metadata
|
|
63
|
+
*/
|
|
64
|
+
openapi?: ZodOpenApiMetadata<ZodTypeAny>;
|
|
65
|
+
/**
|
|
66
|
+
* Used to keep track of the Zod Schema had `.openapi` called on it
|
|
67
|
+
*/
|
|
68
|
+
[currentSymbol]?: ZodTypeAny;
|
|
69
|
+
/**
|
|
70
|
+
* Used to keep track of the previous Zod Schema that had `.openapi` called on it if another `.openapi` is called.
|
|
71
|
+
* This can also be present when .extend is called on an object.
|
|
72
|
+
*/
|
|
73
|
+
[previousSymbol]?: ZodTypeAny;
|
|
74
|
+
}
|
|
75
|
+
interface ZodOpenApiExtendMetadata {
|
|
76
|
+
extends: ZodObject<any, any, any, any, any>;
|
|
77
|
+
}
|
|
78
|
+
declare module 'zod' {
|
|
79
|
+
interface ZodType {
|
|
80
|
+
/**
|
|
81
|
+
* Add OpenAPI metadata to a Zod Type
|
|
82
|
+
*/
|
|
83
|
+
openapi<T extends ZodTypeAny>(this: T, metadata: ZodOpenApiMetadata<T>): T;
|
|
84
|
+
}
|
|
85
|
+
interface ZodTypeDef {
|
|
86
|
+
zodOpenApi?: ZodOpenApiMetadataDef;
|
|
87
|
+
}
|
|
88
|
+
interface ZodObjectDef {
|
|
89
|
+
extendMetadata?: ZodOpenApiExtendMetadata;
|
|
90
|
+
}
|
|
91
|
+
}
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
+
const components = require("./components.chunk.cjs");
|
|
4
|
+
const extendZod = require("./extendZod.chunk.cjs");
|
|
5
|
+
const createDocument = (zodOpenApiObject, documentOptions) => {
|
|
6
|
+
const { paths, webhooks, components: components$1 = {}, ...rest } = zodOpenApiObject;
|
|
7
|
+
const defaultComponents = components.getDefaultComponents(
|
|
8
|
+
components$1,
|
|
9
|
+
zodOpenApiObject.openapi
|
|
10
|
+
);
|
|
11
|
+
const createdPaths = components.createPaths(paths, defaultComponents, documentOptions);
|
|
12
|
+
const createdWebhooks = components.createPaths(
|
|
13
|
+
webhooks,
|
|
14
|
+
defaultComponents,
|
|
15
|
+
documentOptions
|
|
16
|
+
);
|
|
17
|
+
const createdComponents = components.createComponents(
|
|
18
|
+
components$1,
|
|
19
|
+
defaultComponents,
|
|
20
|
+
documentOptions
|
|
21
|
+
);
|
|
22
|
+
return {
|
|
23
|
+
...rest,
|
|
24
|
+
...createdPaths && { paths: createdPaths },
|
|
25
|
+
...createdWebhooks && { webhooks: createdWebhooks },
|
|
26
|
+
...createdComponents && { components: createdComponents }
|
|
27
|
+
};
|
|
28
|
+
};
|
|
29
|
+
const createSchema = (zodType, opts) => {
|
|
30
|
+
const components$1 = components.getDefaultComponents(
|
|
31
|
+
{
|
|
32
|
+
schemas: opts == null ? void 0 : opts.components
|
|
33
|
+
},
|
|
34
|
+
opts == null ? void 0 : opts.openapi
|
|
35
|
+
);
|
|
36
|
+
const state = {
|
|
37
|
+
components: components$1,
|
|
38
|
+
type: (opts == null ? void 0 : opts.schemaType) ?? "output",
|
|
39
|
+
path: [],
|
|
40
|
+
visited: /* @__PURE__ */ new Set(),
|
|
41
|
+
documentOptions: opts
|
|
42
|
+
};
|
|
43
|
+
const schema = components.createSchema(zodType, state, ["createSchema"]);
|
|
44
|
+
const schemaComponents = components.createSchemaComponents({}, components$1);
|
|
45
|
+
return {
|
|
46
|
+
schema,
|
|
47
|
+
components: schemaComponents
|
|
48
|
+
};
|
|
49
|
+
};
|
|
50
|
+
const oas30 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
51
|
+
__proto__: null
|
|
52
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
53
|
+
const oas31 = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
|
|
54
|
+
__proto__: null
|
|
55
|
+
}, Symbol.toStringTag, { value: "Module" }));
|
|
56
|
+
exports.extendZodWithOpenApi = extendZod.extendZodWithOpenApi;
|
|
57
|
+
exports.createDocument = createDocument;
|
|
58
|
+
exports.createSchema = createSchema;
|
|
59
|
+
exports.oas30 = oas30;
|
|
60
|
+
exports.oas31 = oas31;
|