@@ -51,30 +51,7 @@ export class YupSchemaVisitor extends BaseSchemaVisitor {
5151 const visitor = this . createVisitor ( 'input' ) ;
5252 const name = visitor . convertName ( node . name . value ) ;
5353 this . importTypes . push ( name ) ;
54-
55- const shape = node . fields
56- ?. map ( field => {
57- const fieldSchema = generateFieldYupSchema ( this . config , visitor , field , 2 ) ;
58- return isNonNullType ( field . type ) ? fieldSchema : `${ fieldSchema } .optional()` ;
59- } )
60- . join ( ',\n' ) ;
61-
62- switch ( this . config . validationSchemaExportType ) {
63- case 'const' :
64- return new DeclarationBlock ( { } )
65- . export ( )
66- . asKind ( 'const' )
67- . withName ( `${ name } Schema: yup.ObjectSchema<${ name } >` )
68- . withContent ( [ 'yup.object({' , shape , '})' ] . join ( '\n' ) ) . string ;
69-
70- case 'function' :
71- default :
72- return new DeclarationBlock ( { } )
73- . export ( )
74- . asKind ( 'function' )
75- . withName ( `${ name } Schema(): yup.ObjectSchema<${ name } >` )
76- . withBlock ( [ indent ( `return yup.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string ;
77- }
54+ return this . buildInputFields ( node . fields ?? [ ] , visitor , name ) ;
7855 } ,
7956 } ;
8057 }
@@ -89,24 +66,7 @@ export class YupSchemaVisitor extends BaseSchemaVisitor {
8966 // Building schema for field arguments.
9067 const argumentBlocks = visitor . buildArgumentsSchemaBlock ( node , ( typeName , field ) => {
9168 this . importTypes . push ( typeName ) ;
92- const args = field . arguments ?? [ ] ;
93- const shape = args . map ( field => generateFieldYupSchema ( this . config , visitor , field , 2 ) ) . join ( ',\n' ) ;
94- switch ( this . config . validationSchemaExportType ) {
95- case 'const' :
96- return new DeclarationBlock ( { } )
97- . export ( )
98- . asKind ( 'const' )
99- . withName ( `${ typeName } Schema: yup.ObjectSchema<${ typeName } >` )
100- . withContent ( [ `yup.object({` , shape , '})' ] . join ( '\n' ) ) . string ;
101-
102- case 'function' :
103- default :
104- return new DeclarationBlock ( { } )
105- . export ( )
106- . asKind ( 'function' )
107- . withName ( `${ typeName } Schema(): yup.ObjectSchema<${ typeName } >` )
108- . withBlock ( [ indent ( `return yup.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string ;
109- }
69+ return this . buildInputFields ( field . arguments ?? [ ] , visitor , typeName ) ;
11070 } ) ;
11171 const appendArguments = argumentBlocks ? '\n' + argumentBlocks : '' ;
11272
@@ -240,6 +200,36 @@ export class YupSchemaVisitor extends BaseSchemaVisitor {
240200 } ,
241201 } ;
242202 }
203+
204+ private buildInputFields (
205+ fields : readonly ( FieldDefinitionNode | InputValueDefinitionNode ) [ ] ,
206+ visitor : Visitor ,
207+ name : string
208+ ) {
209+ const shape = fields
210+ ?. map ( field => {
211+ const fieldSchema = generateFieldYupSchema ( this . config , visitor , field , 2 ) ;
212+ return isNonNullType ( field . type ) ? fieldSchema : `${ fieldSchema } .optional()` ;
213+ } )
214+ . join ( ',\n' ) ;
215+
216+ switch ( this . config . validationSchemaExportType ) {
217+ case 'const' :
218+ return new DeclarationBlock ( { } )
219+ . export ( )
220+ . asKind ( 'const' )
221+ . withName ( `${ name } Schema: yup.ObjectSchema<${ name } >` )
222+ . withContent ( [ 'yup.object({' , shape , '})' ] . join ( '\n' ) ) . string ;
223+
224+ case 'function' :
225+ default :
226+ return new DeclarationBlock ( { } )
227+ . export ( )
228+ . asKind ( 'function' )
229+ . withName ( `${ name } Schema(): yup.ObjectSchema<${ name } >` )
230+ . withBlock ( [ indent ( `return yup.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string ;
231+ }
232+ }
243233}
244234
245235const generateFieldYupSchema = (
0 commit comments