@@ -13,94 +13,58 @@ import {
13
13
14
14
import { ValidationSchemaPluginConfig } from '../config' ;
15
15
import { buildApi , formatDirectiveConfig } from '../directive' ;
16
- import { SchemaVisitor } from '../types ' ;
16
+ import { BaseSchemaVisitor } from '../schema_visitor ' ;
17
17
import { Visitor } from '../visitor' ;
18
18
import { isInput , isListType , isNamedType , isNonNullType , ObjectTypeDefinitionBuilder } from './../graphql' ;
19
19
20
- const importZod = `import * as myzod from 'myzod'` ;
21
20
const anySchema = `definedNonNullAnySchema` ;
22
21
23
- export const MyZodSchemaVisitor = ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) : SchemaVisitor => {
24
- const importTypes : string [ ] = [ ] ;
25
- const enumDeclarations : string [ ] = [ ] ;
22
+ export class MyZodSchemaVisitor extends BaseSchemaVisitor {
23
+ constructor ( schema : GraphQLSchema , config : ValidationSchemaPluginConfig ) {
24
+ super ( schema , config ) ;
25
+ }
26
26
27
- return {
28
- buildImports : ( ) : string [ ] => {
29
- if ( config . importFrom && importTypes . length > 0 ) {
30
- return [
31
- importZod ,
32
- `import ${ config . useTypeImports ? 'type ' : '' } { ${ importTypes . join ( ', ' ) } } from '${ config . importFrom } '` ,
33
- ] ;
34
- }
35
- return [ importZod ] ;
36
- } ,
37
- initialEmit : ( ) : string =>
27
+ importValidationSchema ( ) : string {
28
+ return `import * as myzod from 'myzod'` ;
29
+ }
30
+
31
+ initialEmit ( ) : string {
32
+ return (
38
33
'\n' +
39
34
[
40
35
new DeclarationBlock ( { } ) . export ( ) . asKind ( 'const' ) . withName ( `${ anySchema } ` ) . withContent ( `myzod.object({})` )
41
36
. string ,
42
- ...enumDeclarations ,
43
- ] . join ( '\n' ) ,
44
- InputObjectTypeDefinition : {
37
+ ...this . enumDeclarations ,
38
+ ] . join ( '\n' )
39
+ ) ;
40
+ }
41
+
42
+ get InputObjectTypeDefinition ( ) {
43
+ return {
45
44
leave : ( node : InputObjectTypeDefinitionNode ) => {
46
- const visitor = new Visitor ( 'input' , schema , config ) ;
45
+ const visitor = this . createVisitor ( 'input' ) ;
47
46
const name = visitor . convertName ( node . name . value ) ;
48
- importTypes . push ( name ) ;
49
-
50
- const shape = node . fields ?. map ( field => generateFieldMyZodSchema ( config , visitor , field , 2 ) ) . join ( ',\n' ) ;
51
-
52
- switch ( config . validationSchemaExportType ) {
53
- case 'const' :
54
- return new DeclarationBlock ( { } )
55
- . export ( )
56
- . asKind ( 'const' )
57
- . withName ( `${ name } Schema: myzod.Type<${ name } >` )
58
- . withContent ( [ 'myzod.object({' , shape , '})' ] . join ( '\n' ) ) . string ;
59
-
60
- case 'function' :
61
- default :
62
- return new DeclarationBlock ( { } )
63
- . export ( )
64
- . asKind ( 'function' )
65
- . withName ( `${ name } Schema(): myzod.Type<${ name } >` )
66
- . withBlock ( [ indent ( `return myzod.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string ;
67
- }
47
+ this . importTypes . push ( name ) ;
48
+ return this . buildInputFields ( node . fields ?? [ ] , visitor , name ) ;
68
49
} ,
69
- } ,
70
- ObjectTypeDefinition : {
71
- leave : ObjectTypeDefinitionBuilder ( config . withObjectType , ( node : ObjectTypeDefinitionNode ) => {
72
- const visitor = new Visitor ( 'output' , schema , config ) ;
50
+ } ;
51
+ }
52
+
53
+ get ObjectTypeDefinition ( ) {
54
+ return {
55
+ leave : ObjectTypeDefinitionBuilder ( this . config . withObjectType , ( node : ObjectTypeDefinitionNode ) => {
56
+ const visitor = this . createVisitor ( 'output' ) ;
73
57
const name = visitor . convertName ( node . name . value ) ;
74
- importTypes . push ( name ) ;
58
+ this . importTypes . push ( name ) ;
75
59
76
60
// Building schema for field arguments.
77
- const argumentBlocks = visitor . buildArgumentsSchemaBlock ( node , ( typeName , field ) => {
78
- importTypes . push ( typeName ) ;
79
- const args = field . arguments ?? [ ] ;
80
- const shape = args . map ( field => generateFieldMyZodSchema ( config , visitor , field , 2 ) ) . join ( ',\n' ) ;
81
- switch ( config . validationSchemaExportType ) {
82
- case 'const' :
83
- return new DeclarationBlock ( { } )
84
- . export ( )
85
- . asKind ( 'const' )
86
- . withName ( `${ typeName } Schema: myzod.Type<${ typeName } >` )
87
- . withContent ( [ `myzod.object({` , shape , '})' ] . join ( '\n' ) ) . string ;
88
-
89
- case 'function' :
90
- default :
91
- return new DeclarationBlock ( { } )
92
- . export ( )
93
- . asKind ( 'function' )
94
- . withName ( `${ typeName } Schema(): myzod.Type<${ typeName } >` )
95
- . withBlock ( [ indent ( `return myzod.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string ;
96
- }
97
- } ) ;
61
+ const argumentBlocks = this . buildObjectTypeDefinitionArguments ( node , visitor ) ;
98
62
const appendArguments = argumentBlocks ? '\n' + argumentBlocks : '' ;
99
63
100
64
// Building schema for fields.
101
- const shape = node . fields ?. map ( field => generateFieldMyZodSchema ( config , visitor , field , 2 ) ) . join ( ',\n' ) ;
65
+ const shape = node . fields ?. map ( field => generateFieldMyZodSchema ( this . config , visitor , field , 2 ) ) . join ( ',\n' ) ;
102
66
103
- switch ( config . validationSchemaExportType ) {
67
+ switch ( this . config . validationSchemaExportType ) {
104
68
case 'const' :
105
69
return (
106
70
new DeclarationBlock ( { } )
@@ -135,16 +99,19 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
135
99
) ;
136
100
}
137
101
} ) ,
138
- } ,
139
- EnumTypeDefinition : {
102
+ } ;
103
+ }
104
+
105
+ get EnumTypeDefinition ( ) {
106
+ return {
140
107
leave : ( node : EnumTypeDefinitionNode ) => {
141
- const visitor = new Visitor ( 'both' , schema , config ) ;
108
+ const visitor = this . createVisitor ( 'both' ) ;
142
109
const enumname = visitor . convertName ( node . name . value ) ;
143
- importTypes . push ( enumname ) ;
110
+ this . importTypes . push ( enumname ) ;
144
111
// z.enum are basically myzod.literals
145
112
// hoist enum declarations
146
- enumDeclarations . push (
147
- config . enumsAsTypes
113
+ this . enumDeclarations . push (
114
+ this . config . enumsAsTypes
148
115
? new DeclarationBlock ( { } )
149
116
. export ( )
150
117
. asKind ( 'type' )
@@ -159,12 +126,15 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
159
126
. withContent ( `myzod.enum(${ enumname } )` ) . string
160
127
) ;
161
128
} ,
162
- } ,
163
- UnionTypeDefinition : {
129
+ } ;
130
+ }
131
+
132
+ get UnionTypeDefinition ( ) {
133
+ return {
164
134
leave : ( node : UnionTypeDefinitionNode ) => {
165
- if ( ! node . types || ! config . withObjectType ) return ;
135
+ if ( ! node . types || ! this . config . withObjectType ) return ;
166
136
167
- const visitor = new Visitor ( 'output' , schema , config ) ;
137
+ const visitor = this . createVisitor ( 'output' ) ;
168
138
169
139
const unionName = visitor . convertName ( node . name . value ) ;
170
140
const unionElements = node . types
@@ -174,7 +144,7 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
174
144
if ( typ ?. astNode ?. kind === 'EnumTypeDefinition' ) {
175
145
return `${ element } Schema` ;
176
146
}
177
- switch ( config . validationSchemaExportType ) {
147
+ switch ( this . config . validationSchemaExportType ) {
178
148
case 'const' :
179
149
return `${ element } Schema` ;
180
150
case 'function' :
@@ -187,7 +157,7 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
187
157
188
158
const union = unionElementsCount > 1 ? `myzod.union([${ unionElements } ])` : unionElements ;
189
159
190
- switch ( config . validationSchemaExportType ) {
160
+ switch ( this . config . validationSchemaExportType ) {
191
161
case 'const' :
192
162
return new DeclarationBlock ( { } ) . export ( ) . asKind ( 'const' ) . withName ( `${ unionName } Schema` ) . withContent ( union )
193
163
. string ;
@@ -200,9 +170,34 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
200
170
. withBlock ( indent ( `return ${ union } ` ) ) . string ;
201
171
}
202
172
} ,
203
- } ,
204
- } ;
205
- } ;
173
+ } ;
174
+ }
175
+
176
+ protected buildInputFields (
177
+ fields : readonly ( FieldDefinitionNode | InputValueDefinitionNode ) [ ] ,
178
+ visitor : Visitor ,
179
+ name : string
180
+ ) {
181
+ const shape = fields . map ( field => generateFieldMyZodSchema ( this . config , visitor , field , 2 ) ) . join ( ',\n' ) ;
182
+
183
+ switch ( this . config . validationSchemaExportType ) {
184
+ case 'const' :
185
+ return new DeclarationBlock ( { } )
186
+ . export ( )
187
+ . asKind ( 'const' )
188
+ . withName ( `${ name } Schema: myzod.Type<${ name } >` )
189
+ . withContent ( [ 'myzod.object({' , shape , '})' ] . join ( '\n' ) ) . string ;
190
+
191
+ case 'function' :
192
+ default :
193
+ return new DeclarationBlock ( { } )
194
+ . export ( )
195
+ . asKind ( 'function' )
196
+ . withName ( `${ name } Schema(): myzod.Type<${ name } >` )
197
+ . withBlock ( [ indent ( `return myzod.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string ;
198
+ }
199
+ }
200
+ }
206
201
207
202
const generateFieldMyZodSchema = (
208
203
config : ValidationSchemaPluginConfig ,
0 commit comments