-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
326 lines (300 loc) · 10.9 KB
/
index.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import { DeclarationBlock, indent } from '@graphql-codegen/visitor-plugin-common';
import {
EnumTypeDefinitionNode,
FieldDefinitionNode,
GraphQLSchema,
InputObjectTypeDefinitionNode,
InputValueDefinitionNode,
NameNode,
ObjectTypeDefinitionNode,
TypeNode,
UnionTypeDefinitionNode,
} from 'graphql';
import { ValidationSchemaPluginConfig } from '../config';
import { buildApi, formatDirectiveConfig } from '../directive';
import { BaseSchemaVisitor } from '../schema_visitor';
import { Visitor } from '../visitor';
import { isInput, isListType, isNamedType, isNonNullType, ObjectTypeDefinitionBuilder } from './../graphql';
export class YupSchemaVisitor extends BaseSchemaVisitor {
constructor(schema: GraphQLSchema, config: ValidationSchemaPluginConfig) {
super(schema, config);
}
importValidationSchema(): string {
return `import * as yup from 'yup'`;
}
initialEmit(): string {
if (!this.config.withObjectType) return '\n' + this.enumDeclarations.join('\n');
return (
'\n' +
this.enumDeclarations.join('\n') +
'\n' +
new DeclarationBlock({})
.asKind('function')
.withName('union<T extends {}>(...schemas: ReadonlyArray<yup.Schema<T>>): yup.MixedSchema<T>')
.withBlock(
[
indent('return yup.mixed<T>().test({'),
indent('test: (value) => schemas.some((schema) => schema.isValidSync(value))', 2),
indent('}).defined()'),
].join('\n')
).string
);
}
get InputObjectTypeDefinition() {
return {
leave: (node: InputObjectTypeDefinitionNode) => {
const visitor = this.createVisitor('input');
const name = visitor.convertName(node.name.value);
this.importTypes.push(name);
return this.buildInputFields(node.fields ?? [], visitor, name);
},
};
}
get ObjectTypeDefinition() {
return {
leave: ObjectTypeDefinitionBuilder(this.config.withObjectType, (node: ObjectTypeDefinitionNode) => {
const visitor = this.createVisitor('output');
const name = visitor.convertName(node.name.value);
this.importTypes.push(name);
// Building schema for field arguments.
const argumentBlocks = this.buildObjectTypeDefinitionArguments(node, visitor);
const appendArguments = argumentBlocks ? '\n' + argumentBlocks : '';
// Building schema for fields.
const shape = node.fields
?.map(field => {
const fieldSchema = generateFieldYupSchema(this.config, visitor, field, 2);
return isNonNullType(field.type) ? fieldSchema : `${fieldSchema}.optional()`;
})
.join(',\n');
switch (this.config.validationSchemaExportType) {
case 'const':
return (
new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${name}Schema: yup.ObjectSchema<${name}>`)
.withContent(
[
`yup.object({`,
indent(`__typename: yup.string<'${node.name.value}'>().optional(),`, 2),
shape,
'})',
].join('\n')
).string + appendArguments
);
case 'function':
default:
return (
new DeclarationBlock({})
.export()
.asKind('function')
.withName(`${name}Schema(): yup.ObjectSchema<${name}>`)
.withBlock(
[
indent(`return yup.object({`),
indent(`__typename: yup.string<'${node.name.value}'>().optional(),`, 2),
shape,
indent('})'),
].join('\n')
).string + appendArguments
);
}
}),
};
}
get EnumTypeDefinition() {
return {
leave: (node: EnumTypeDefinitionNode) => {
const visitor = this.createVisitor('both');
const enumname = visitor.convertName(node.name.value);
this.importTypes.push(enumname);
// hoise enum declarations
if (this.config.enumsAsTypes) {
const enums = node.values?.map(enumOption => `'${enumOption.name.value}'`);
this.enumDeclarations.push(
new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${enumname}Schema`)
.withContent(`yup.string().oneOf([${enums?.join(', ')}]).defined()`).string
);
} else {
const values = node.values
?.map(
enumOption =>
`${enumname}.${visitor.convertName(enumOption.name, {
useTypesPrefix: false,
transformUnderscore: true,
})}`
)
.join(', ');
this.enumDeclarations.push(
new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${enumname}Schema`)
.withContent(`yup.string<${enumname}>().oneOf([${values}]).defined()`).string
);
}
},
};
}
get UnionTypeDefinition() {
return {
leave: (node: UnionTypeDefinitionNode) => {
if (!node.types || !this.config.withObjectType) return;
const visitor = this.createVisitor('output');
const unionName = visitor.convertName(node.name.value);
this.importTypes.push(unionName);
const unionElements = node.types
?.map(t => {
const element = visitor.convertName(t.name.value);
const typ = visitor.getType(t.name.value);
if (typ?.astNode?.kind === 'EnumTypeDefinition') {
return `${element}Schema`;
}
switch (this.config.validationSchemaExportType) {
case 'const':
return `${element}Schema`;
case 'function':
default:
return `${element}Schema()`;
}
})
.join(', ');
switch (this.config.validationSchemaExportType) {
case 'const':
return new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${unionName}Schema: yup.MixedSchema<${unionName}>`)
.withContent(`union<${unionName}>(${unionElements})`).string;
case 'function':
default:
return new DeclarationBlock({})
.export()
.asKind('function')
.withName(`${unionName}Schema(): yup.MixedSchema<${unionName}>`)
.withBlock(indent(`return union<${unionName}>(${unionElements})`)).string;
}
},
};
}
protected buildInputFields(
fields: readonly (FieldDefinitionNode | InputValueDefinitionNode)[],
visitor: Visitor,
name: string
) {
const shape = fields
?.map(field => {
const fieldSchema = generateFieldYupSchema(this.config, visitor, field, 2);
return isNonNullType(field.type) ? fieldSchema : `${fieldSchema}.optional()`;
})
.join(',\n');
switch (this.config.validationSchemaExportType) {
case 'const':
return new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${name}Schema: yup.ObjectSchema<${name}>`)
.withContent(['yup.object({', shape, '})'].join('\n')).string;
case 'function':
default:
return new DeclarationBlock({})
.export()
.asKind('function')
.withName(`${name}Schema(): yup.ObjectSchema<${name}>`)
.withBlock([indent(`return yup.object({`), shape, indent('})')].join('\n')).string;
}
}
}
const generateFieldYupSchema = (
config: ValidationSchemaPluginConfig,
visitor: Visitor,
field: InputValueDefinitionNode | FieldDefinitionNode,
indentCount: number
): string => {
let gen = generateFieldTypeYupSchema(config, visitor, field.type);
if (config.directives && field.directives) {
const formatted = formatDirectiveConfig(config.directives);
gen += buildApi(formatted, field.directives);
}
return indent(`${field.name.value}: ${maybeLazy(field.type, gen)}`, indentCount);
};
const generateFieldTypeYupSchema = (
config: ValidationSchemaPluginConfig,
visitor: Visitor,
type: TypeNode,
parentType?: TypeNode
): string => {
if (isListType(type)) {
const gen = generateFieldTypeYupSchema(config, visitor, type.type, type);
if (!isNonNullType(parentType)) {
return `yup.array(${maybeLazy(type.type, gen)}).defined().${config.maybeSchemaValue ? config.maybeSchemaValue : 'nullable'}()`;
}
return `yup.array(${maybeLazy(type.type, gen)}).defined()`;
}
if (isNonNullType(type)) {
const gen = generateFieldTypeYupSchema(config, visitor, type.type, type);
return maybeLazy(type.type, gen);
}
if (isNamedType(type)) {
const gen = generateNameNodeYupSchema(config, visitor, type.name);
if (isNonNullType(parentType)) {
if (visitor.shouldEmitAsNotAllowEmptyString(type.name.value)) {
return `${gen}.required()`;
}
return `${gen}.nonNullable()`;
}
const typ = visitor.getType(type.name.value);
if (typ?.astNode?.kind === 'InputObjectTypeDefinition') {
return `${gen}`;
}
return `${gen}.${config.maybeSchemaValue ? config.maybeSchemaValue : 'nullable'}()`;
}
console.warn('unhandled type:', type);
return '';
};
const generateNameNodeYupSchema = (config: ValidationSchemaPluginConfig, visitor: Visitor, node: NameNode): string => {
const converter = visitor.getNameNodeConverter(node);
switch (converter?.targetKind) {
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:
return `${converter.convertName()}Schema()`;
}
case 'EnumTypeDefinition':
return `${converter.convertName()}Schema`;
default:
return yup4Scalar(config, visitor, node.value);
}
};
const maybeLazy = (type: TypeNode, schema: string): string => {
if (isNamedType(type) && isInput(type.name.value)) {
// https://github.com/jquense/yup/issues/1283#issuecomment-786559444
return `yup.lazy(() => ${schema})`;
}
return schema;
};
const yup4Scalar = (config: ValidationSchemaPluginConfig, visitor: Visitor, scalarName: string): string => {
if (config.scalarSchemas?.[scalarName]) {
return `${config.scalarSchemas[scalarName]}.defined()`;
}
const tsType = visitor.getScalarType(scalarName);
switch (tsType) {
case 'string':
return `yup.string().defined()`;
case 'number':
return `yup.number().defined()`;
case 'boolean':
return `yup.boolean().defined()`;
}
console.warn('unhandled name:', scalarName);
return `yup.mixed()`;
};