-
-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathzod_shared.ts
More file actions
283 lines (231 loc) · 11.2 KB
/
zod_shared.ts
File metadata and controls
283 lines (231 loc) · 11.2 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// Shared utilities used by both ZodSchemaVisitor (zod) and ZodV4SchemaVisitor (zodv4).
// These functions are identical across both implementations and are extracted here to
// eliminate duplication.
import type {
ConstValueNode,
FieldDefinitionNode,
InputObjectTypeDefinitionNode,
InputObjectTypeExtensionNode,
InputValueDefinitionNode,
NameNode,
TypeNode,
} from 'graphql';
import type { ValidationSchemaPluginConfig } from './config.js';
import type { Visitor } from './visitor.js';
import { resolveExternalModuleAndFn } from '@graphql-codegen/plugin-helpers';
import { convertNameParts, indent } from '@graphql-codegen/visitor-plugin-common';
import {
isInputObjectType,
Kind,
valueFromASTUntyped,
} from 'graphql';
import { buildApi, formatDirectiveConfig } from './directive.js';
import {
escapeGraphQLCharacters,
isListType,
isNamedType,
isNonNullType,
} from './graphql.js';
import { buildMaybeLazy } from './lazy.js';
import { buildScalarSchema } from './scalar.js';
export const anySchema = `definedNonNullAnySchema`;
export function generateFieldZodSchema(config: ValidationSchemaPluginConfig, visitor: Visitor, field: InputValueDefinitionNode | FieldDefinitionNode, indentCount: number, depthVariable?: string): string {
const gen = generateFieldTypeZodSchema(config, visitor, field, field.type, undefined, true, false, depthVariable);
return indent(`${field.name.value}: ${withDescription(config, field, maybeLazy(visitor, field.type, gen))}`, indentCount);
}
export function generateFieldTypeZodSchema(config: ValidationSchemaPluginConfig, visitor: Visitor, field: InputValueDefinitionNode | FieldDefinitionNode, type: TypeNode, parentType?: TypeNode, isRoot = true, forceRequired = false, depthVariable?: string): string {
if (isListType(type)) {
const gen = generateFieldTypeZodSchema(config, visitor, field, type.type, type, false, false, depthVariable);
const arrayGen = `z.array(${maybeLazy(visitor, type.type, gen)})`;
const maybeDirectivesGen = isRoot ? applyDirectives(config, field, arrayGen) : arrayGen;
const maybeDefaultGen = hasNullDefault(field) ? maybeDirectivesGen : applyDefaultValue(config, visitor, field, type, maybeDirectivesGen);
if (!isNonNullType(parentType) && !forceRequired) {
if (hasNullDefault(field))
return withNullDefault(config, maybeDirectivesGen);
return `${maybeDefaultGen}.${zodOptionalType(config)}()`;
}
return maybeDefaultGen;
}
if (isNonNullType(type)) {
const gen = generateFieldTypeZodSchema(config, visitor, field, type.type, type, isRoot, forceRequired, depthVariable);
return maybeLazy(visitor, type.type, gen);
}
if (isNamedType(type)) {
const gen = generateNameNodeZodSchema(config, visitor, type.name, depthVariable);
if (isListType(parentType))
return `${gen}.nullable()`;
const appliedDirectivesGen = isRoot
? hasNullDefault(field)
? applyDirectives(config, field, gen)
: applyDefaultValue(config, visitor, field, type, applyDirectives(config, field, gen))
: gen;
if (isNonNullType(parentType)) {
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value))
return `${appliedDirectivesGen}.min(1)`;
return appliedDirectivesGen;
}
if (isListType(parentType))
return `${appliedDirectivesGen}.nullable()`;
if (forceRequired)
return appliedDirectivesGen;
return hasNullDefault(field)
? withNullDefault(config, appliedDirectivesGen)
: `${appliedDirectivesGen}.${zodOptionalType(config)}()`;
}
console.warn('unhandled type:', type);
return '';
}
export function isOneOfInputObject(node: InputObjectTypeDefinitionNode): boolean {
return node.directives?.some(directive => directive.name.value === 'oneOf') === true;
}
export function buildObjectExpression(config: ValidationSchemaPluginConfig, shape: string | undefined): string {
return ['z.object({', shape, `})${strictObjectSuffix(config)}`].join('\n');
}
export function buildObjectReturn(config: ValidationSchemaPluginConfig, shape: string | undefined): string {
return [indent('return z.object({'), shape, indent(`})${strictObjectSuffix(config)}`)].join('\n');
}
export function strictObjectSuffix(config: ValidationSchemaPluginConfig): string {
return config.strictObjectSchemas === true ? '.strict()' : '';
}
export function zodOptionalType(config: ValidationSchemaPluginConfig): string {
return config.nullishBehavior ?? config.zodOptionalType ?? 'nullish';
}
export function withNullDefault(config: ValidationSchemaPluginConfig, gen: string): string {
if (zodOptionalType(config) === 'optional')
return `${gen}.nullable().optional().default(null)`;
return `${gen}.${zodOptionalType(config)}().default(null)`;
}
export function schemaDepthVariable(config: ValidationSchemaPluginConfig): string | undefined {
return typeof config.maxDepth === 'number' && config.validationSchemaExportType !== 'const'
? 'depth'
: undefined;
}
export function schemaDepthParameter(config: ValidationSchemaPluginConfig): string {
return schemaDepthVariable(config) ? 'depth = 0' : '';
}
export function withDescription(config: ValidationSchemaPluginConfig, field: InputValueDefinitionNode | FieldDefinitionNode, gen: string): string {
if (config.withDescriptions !== true || !field.description?.value)
return gen;
return `${gen}.describe(${JSON.stringify(field.description.value)})`;
}
export function applyDefaultValue(config: ValidationSchemaPluginConfig, visitor: Visitor, field: InputValueDefinitionNode | FieldDefinitionNode, type: TypeNode, gen: string): string {
if (field.kind !== Kind.INPUT_VALUE_DEFINITION || !field.defaultValue)
return gen;
return `${gen}.default(${defaultValueExpression(config, visitor, type, field.defaultValue)})`;
}
export function defaultValueExpression(config: ValidationSchemaPluginConfig, visitor: Visitor, type: TypeNode, value: ConstValueNode): string {
if (value.kind === Kind.NULL)
return 'null';
if (isNonNullType(type))
return defaultValueExpression(config, visitor, type.type, value);
if (isListType(type)) {
if (value.kind === Kind.LIST)
return `[${value.values.map(item => defaultValueExpression(config, visitor, type.type, item)).join(', ')}]`;
return `[${defaultValueExpression(config, visitor, type.type, value)}]`;
}
if (isNamedType(type) && visitor.getType(type.name.value)?.astNode?.kind === 'EnumTypeDefinition' && value.kind === Kind.ENUM) {
if (!config.enumsAsTypes)
return `${enumDefaultTypeName(visitor, type)}.${enumDefaultValueName(config, value.value)}`;
return JSON.stringify(value.value);
}
if (isNamedType(type) && value.kind === Kind.OBJECT) {
const graphQLType = visitor.getType(type.name.value);
const astNode = graphQLType?.astNode;
if (astNode?.kind === Kind.INPUT_OBJECT_TYPE_DEFINITION && isInputObjectType(graphQLType)) {
const explicitFields = new Map(value.fields.map(field => [field.name.value, field.value]));
const fields = inputObjectFields(astNode, graphQLType.extensionASTNodes).flatMap((field) => {
const fieldValue = explicitFields.get(field.name.value) ?? field.defaultValue;
if (!fieldValue)
return [];
return `${field.name.value}: ${defaultValueExpression(config, visitor, field.type, fieldValue)}`;
});
return `{ ${fields.join(', ')} }`;
}
}
if (value.kind === Kind.INT || value.kind === Kind.FLOAT || value.kind === Kind.BOOLEAN)
return `${value.value}`;
if (value.kind === Kind.STRING)
return `"${escapeGraphQLCharacters(value.value)}"`;
return JSON.stringify(valueFromASTUntyped(value));
}
export function hasNullDefault(field: InputValueDefinitionNode | FieldDefinitionNode): boolean {
return field.kind === Kind.INPUT_VALUE_DEFINITION && field.defaultValue?.kind === Kind.NULL;
}
export function inputObjectFields(
astNode: InputObjectTypeDefinitionNode,
extensionASTNodes: readonly InputObjectTypeExtensionNode[] | undefined,
): InputValueDefinitionNode[] {
return [
...(astNode.fields ?? []),
...(extensionASTNodes?.flatMap(extension => extension.fields ?? []) ?? []),
];
}
export function enumDefaultTypeName(visitor: Visitor, type: TypeNode): string {
if (isNonNullType(type))
return enumDefaultTypeName(visitor, type.type);
if (isNamedType(type))
return visitor.prefixTypeNamespace(visitor.convertSchemaName(type.name.value, visitor.getType(type.name.value)?.astNode?.kind));
return '';
}
export function enumDefaultValueName(config: ValidationSchemaPluginConfig, value: string): string {
let enumValue = convertNameParts(value, resolveExternalModuleAndFn('change-case-all#pascalCase'), config.namingConvention?.transformUnderscore);
if (config.namingConvention?.enumValues)
enumValue = convertNameParts(value, resolveExternalModuleAndFn(config.namingConvention?.enumValues), config.namingConvention?.transformUnderscore);
return enumValue;
}
export function applyDirectives(config: ValidationSchemaPluginConfig, field: InputValueDefinitionNode | FieldDefinitionNode, gen: string): string {
if (config.directives && field.directives) {
const formatted = formatDirectiveConfig(config.directives);
return gen + buildApi(formatted, field.directives);
}
return gen;
}
export function generateNameNodeZodSchema(config: ValidationSchemaPluginConfig, visitor: Visitor, node: NameNode, depthVariable?: string): string {
const converter = visitor.getNameNodeConverter(node);
switch (converter?.targetKind) {
case 'InterfaceTypeDefinition':
case 'InputObjectTypeDefinition':
case 'ObjectTypeDefinition':
case 'UnionTypeDefinition':
// using switch-case rather than if-else to allow for future expansion
switch (config.validationSchemaExportType) {
case 'const':
return `${converter.convertName()}Schema`;
case 'function':
default:
if (
depthVariable
&& (
converter.targetKind === 'InterfaceTypeDefinition'
|| converter.targetKind === 'ObjectTypeDefinition'
|| converter.targetKind === 'UnionTypeDefinition'
)
) {
return `${depthVariable} >= ${config.maxDepth} ? ${anySchema} : ${converter.convertName()}Schema(${depthVariable} + 1)`;
}
return `${converter.convertName()}Schema()`;
}
case 'EnumTypeDefinition':
return `${converter.convertName()}Schema`;
case 'ScalarTypeDefinition':
return zod4Scalar(config, visitor, node.value);
default:
if (converter?.targetKind)
console.warn('Unknown targetKind', converter?.targetKind);
return zod4Scalar(config, visitor, node.value);
}
}
export function maybeLazy(visitor: Visitor, type: TypeNode, schema: string): string {
return buildMaybeLazy(visitor, type, schema, s => `z.lazy(() => ${s})`);
}
export function zod4Scalar(config: ValidationSchemaPluginConfig, visitor: Visitor, scalarName: string): string {
return buildScalarSchema(config, visitor, scalarName, {
typeMap: { string: 'z.string()', number: 'z.number()', boolean: 'z.boolean()' },
fallback: anySchema,
});
}
export function unionLiterals(values: string[]): string {
if (values.length === 0)
return 'never';
return values.map(value => JSON.stringify(value)).join(' | ');
}