-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathmetadata.ts
160 lines (135 loc) · 4.7 KB
/
metadata.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { ZodType, ZodTypeAny } from 'zod';
import { ZodTypes, isZodType } from './lib/zod-is-type';
import { ZodOpenAPIMetadata, ZodOpenApiFullMetadata } from './zod-extensions';
import { isNil, omit, omitBy } from './lib/lodash';
import { ParameterObject, ReferenceObject, SchemaObject } from './types';
export class Metadata {
static getMetadata<T extends any>(
zodSchema: ZodType<T>
): ZodOpenApiFullMetadata<T> | undefined {
const innerSchema = this.unwrapChained(zodSchema);
const metadata = zodSchema._def.openapi
? zodSchema._def.openapi
: innerSchema._def.openapi;
/**
* Every zod schema can receive a `description` by using the .describe method.
* That description should be used when generating an OpenApi schema.
* The `??` bellow makes sure we can handle both:
* - schema.describe('Test').optional()
* - schema.optional().describe('Test')
*/
const zodDescription = zodSchema.description ?? innerSchema.description;
// A description provided from .openapi() should be taken with higher precedence
return {
_internal: metadata?._internal,
metadata: {
description: zodDescription,
...metadata?.metadata,
},
};
}
static getInternalMetadata<T extends any>(zodSchema: ZodType<T>) {
const innerSchema = this.unwrapChained(zodSchema);
const openapi = zodSchema._def.openapi
? zodSchema._def.openapi
: innerSchema._def.openapi;
return openapi?._internal;
}
static getParamMetadata<T extends any>(
zodSchema: ZodType<T>
): ZodOpenApiFullMetadata<T> | undefined {
const innerSchema = this.unwrapChained(zodSchema);
const metadata = zodSchema._def.openapi
? zodSchema._def.openapi
: innerSchema._def.openapi;
/**
* Every zod schema can receive a `description` by using the .describe method.
* That description should be used when generating an OpenApi schema.
* The `??` bellow makes sure we can handle both:
* - schema.describe('Test').optional()
* - schema.optional().describe('Test')
*/
const zodDescription = zodSchema.description ?? innerSchema.description;
return {
_internal: metadata?._internal,
metadata: {
...metadata?.metadata,
// A description provided from .openapi() should be taken with higher precedence
param: {
description: zodDescription,
...metadata?.metadata?.param,
},
},
};
}
/**
* A method that omits all custom keys added to the regular OpenAPI
* metadata properties
*/
static buildSchemaMetadata(metadata: ZodOpenAPIMetadata) {
return omitBy(omit(metadata, ['param']), isNil);
}
static buildParameterMetadata(
metadata: Required<ZodOpenAPIMetadata>['param']
) {
return omitBy(metadata, isNil);
}
static applySchemaMetadata(
initialData: SchemaObject | ParameterObject | ReferenceObject,
metadata: Partial<ZodOpenAPIMetadata>
): SchemaObject | ReferenceObject {
return omitBy(
{
...initialData,
...this.buildSchemaMetadata(metadata),
},
isNil
);
}
static getRefId<T extends any>(zodSchema: ZodType<T>) {
return this.getInternalMetadata(zodSchema)?.refId;
}
static unwrapChained(schema: ZodType): ZodType {
return this.unwrapUntil(schema);
}
static getDefaultValue<T>(zodSchema: ZodTypeAny): T | undefined {
const unwrapped = this.unwrapUntil(zodSchema, 'ZodDefault');
return unwrapped?._def.defaultValue();
}
private static unwrapUntil(schema: ZodType): ZodType;
private static unwrapUntil<TypeName extends keyof ZodTypes>(
schema: ZodType,
typeName: TypeName | undefined
): ZodTypes[TypeName] | undefined;
private static unwrapUntil<TypeName extends keyof ZodTypes>(
schema: ZodType,
typeName?: TypeName
): ZodType | undefined {
if (typeName && isZodType(schema, typeName)) {
return schema;
}
if (
isZodType(schema, 'ZodOptional') ||
isZodType(schema, 'ZodNullable') ||
isZodType(schema, 'ZodBranded')
) {
return this.unwrapUntil(schema.unwrap(), typeName);
}
if (isZodType(schema, 'ZodDefault') || isZodType(schema, 'ZodReadonly')) {
return this.unwrapUntil(schema._def.innerType, typeName);
}
if (isZodType(schema, 'ZodEffects')) {
return this.unwrapUntil(schema._def.schema, typeName);
}
if (isZodType(schema, 'ZodPipeline')) {
return this.unwrapUntil(schema._def.in, typeName);
}
return typeName ? undefined : schema;
}
static isOptionalSchema(zodSchema: ZodTypeAny): boolean {
if (isZodType(zodSchema, 'ZodEffects')) {
return this.isOptionalSchema(zodSchema._def.schema);
}
return zodSchema.isOptional();
}
}