@@ -36,12 +36,12 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
3636 '\n' +
3737 new DeclarationBlock ( { } )
3838 . asKind ( 'function' )
39- . withName ( 'union<T>(...schemas: ReadonlyArray<yup.SchemaOf <T>>): yup.BaseSchema <T>' )
39+ . withName ( 'union<T extends {} >(...schemas: ReadonlyArray<yup.ObjectSchema <T>>): yup.MixedSchema <T>' )
4040 . withBlock (
4141 [
42- indent ( 'return yup.mixed().test({' ) ,
42+ indent ( 'return yup.mixed<T> ().test({' ) ,
4343 indent ( 'test: (value) => schemas.some((schema) => schema.isValidSync(value))' , 2 ) ,
44- indent ( '})' ) ,
44+ indent ( '}).defined() ' ) ,
4545 ] . join ( '\n' )
4646 ) . string
4747 ) ;
@@ -52,13 +52,16 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
5252 importTypes . push ( name ) ;
5353
5454 const shape = node . fields
55- ?. map ( field => generateFieldYupSchema ( config , tsVisitor , schema , field , 2 ) )
55+ ?. map ( field => {
56+ const fieldSchema = generateFieldYupSchema ( config , tsVisitor , schema , field , 2 ) ;
57+ return isNonNullType ( field . type ) ? fieldSchema : `${ fieldSchema } .optional()` ;
58+ } )
5659 . join ( ',\n' ) ;
5760
5861 return new DeclarationBlock ( { } )
5962 . export ( )
6063 . asKind ( 'function' )
61- . withName ( `${ name } Schema(): yup.SchemaOf <${ name } >` )
64+ . withName ( `${ name } Schema(): yup.ObjectSchema <${ name } >` )
6265 . withBlock ( [ indent ( `return yup.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string ;
6366 } ,
6467 } ,
@@ -68,17 +71,20 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
6871 importTypes . push ( name ) ;
6972
7073 const shape = node . fields
71- ?. map ( field => generateFieldYupSchema ( config , tsVisitor , schema , field , 2 ) )
74+ ?. map ( field => {
75+ const fieldSchema = generateFieldYupSchema ( config , tsVisitor , schema , field , 2 ) ;
76+ return isNonNullType ( field . type ) ? fieldSchema : `${ fieldSchema } .optional()` ;
77+ } )
7278 . join ( ',\n' ) ;
7379
7480 return new DeclarationBlock ( { } )
7581 . export ( )
7682 . asKind ( 'function' )
77- . withName ( `${ name } Schema(): yup.SchemaOf <${ name } >` )
83+ . withName ( `${ name } Schema(): yup.ObjectSchema <${ name } >` )
7884 . withBlock (
7985 [
8086 indent ( `return yup.object({` ) ,
81- indent ( `__typename: yup.mixed().oneOf([ '${ node . name . value } ', undefined] ),` , 2 ) ,
87+ indent ( `__typename: yup.string< '${ node . name . value } '>().optional( ),` , 2 ) ,
8288 shape ,
8389 indent ( '})' ) ,
8490 ] . join ( '\n' )
@@ -91,13 +97,13 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
9197 importTypes . push ( enumname ) ;
9298
9399 if ( config . enumsAsTypes ) {
100+ const enums = node . values ?. map ( enumOption => `'${ enumOption . name . value } '` ) ;
101+
94102 return new DeclarationBlock ( { } )
95103 . export ( )
96104 . asKind ( 'const' )
97105 . withName ( `${ enumname } Schema` )
98- . withContent (
99- `yup.mixed().oneOf([${ node . values ?. map ( enumOption => `'${ enumOption . name . value } '` ) . join ( ', ' ) } ])`
100- ) . string ;
106+ . withContent ( `yup.string().oneOf([${ enums ?. join ( ', ' ) } ]).defined()` ) . string ;
101107 }
102108
103109 const values = node . values
@@ -113,7 +119,7 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
113119 . export ( )
114120 . asKind ( 'const' )
115121 . withName ( `${ enumname } Schema` )
116- . withContent ( `yup.mixed ().oneOf([${ values } ])` ) . string ;
122+ . withContent ( `yup.string< ${ enumname } > ().oneOf([${ values } ]).defined( )` ) . string ;
117123 } ,
118124 } ,
119125 UnionTypeDefinition : {
@@ -129,7 +135,7 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
129135 return new DeclarationBlock ( { } )
130136 . export ( )
131137 . asKind ( 'function' )
132- . withName ( `${ unionName } Schema(): yup.BaseSchema <${ unionName } >` )
138+ . withName ( `${ unionName } Schema(): yup.MixedSchema <${ unionName } >` )
133139 . withBlock ( union ) . string ;
134140 } ,
135141 } ,
@@ -180,22 +186,28 @@ const generateFieldTypeYupSchema = (
180186 if ( isListType ( type ) ) {
181187 const gen = generateFieldTypeYupSchema ( config , tsVisitor , schema , type . type , type ) ;
182188 if ( ! isNonNullType ( parentType ) ) {
183- return `yup.array().of( ${ maybeLazy ( type . type , gen ) } ).optional ()` ;
189+ return `yup.array(${ maybeLazy ( type . type , gen ) } ).defined().nullable ()` ;
184190 }
185- return `yup.array().of( ${ maybeLazy ( type . type , gen ) } )` ;
191+ return `yup.array(${ maybeLazy ( type . type , gen ) } ).defined( )` ;
186192 }
187193 if ( isNonNullType ( type ) ) {
188194 const gen = generateFieldTypeYupSchema ( config , tsVisitor , schema , type . type , type ) ;
189- const nonNullGen = maybeNonEmptyString ( config , tsVisitor , gen , type . type ) ;
190- return maybeLazy ( type . type , nonNullGen ) ;
195+ return maybeLazy ( type . type , gen ) ;
191196 }
192197 if ( isNamedType ( type ) ) {
193198 const gen = generateNameNodeYupSchema ( config , tsVisitor , schema , type . name ) ;
199+ if ( isNonNullType ( parentType ) ) {
200+ if ( config . notAllowEmptyString === true ) {
201+ const tsType = tsVisitor . scalars [ type . name . value ] ;
202+ if ( tsType === 'string' ) return `${ gen } .required()` ;
203+ }
204+ return `${ gen } .nonNullable()` ;
205+ }
194206 const typ = schema . getType ( type . name . value ) ;
195- if ( typ ?. astNode ?. kind === 'ObjectTypeDefinition ' ) {
196- return `${ gen } .optional() ` ;
207+ if ( typ ?. astNode ?. kind === 'InputObjectTypeDefinition ' ) {
208+ return `${ gen } ` ;
197209 }
198- return gen ;
210+ return ` ${ gen } .nullable()` ;
199211 }
200212 console . warn ( 'unhandled type:' , type ) ;
201213 return '' ;
@@ -236,40 +248,23 @@ const generateNameNodeYupSchema = (
236248const maybeLazy = ( type : TypeNode , schema : string ) : string => {
237249 if ( isNamedType ( type ) && isInput ( type . name . value ) ) {
238250 // https://github.com/jquense/yup/issues/1283#issuecomment-786559444
239- return `yup.lazy(() => ${ schema } ) as never ` ;
251+ return `yup.lazy(() => ${ schema } )` ;
240252 }
241253 return schema ;
242254} ;
243255
244- const maybeNonEmptyString = (
245- config : ValidationSchemaPluginConfig ,
246- tsVisitor : TsVisitor ,
247- schema : string ,
248- childType : TypeNode
249- ) : string => {
250- if ( config . notAllowEmptyString === true && isNamedType ( childType ) ) {
251- const maybeScalarName = childType . name . value ;
252- const tsType = tsVisitor . scalars [ maybeScalarName ] ;
253- if ( tsType === 'string' ) {
254- return `${ schema } .required()` ;
255- }
256- }
257- // fallback
258- return `${ schema } .defined()` ;
259- } ;
260-
261256const yup4Scalar = ( config : ValidationSchemaPluginConfig , tsVisitor : TsVisitor , scalarName : string ) : string => {
262257 if ( config . scalarSchemas ?. [ scalarName ] ) {
263- return config . scalarSchemas [ scalarName ] ;
258+ return ` ${ config . scalarSchemas [ scalarName ] } .defined()` ;
264259 }
265260 const tsType = tsVisitor . scalars [ scalarName ] ;
266261 switch ( tsType ) {
267262 case 'string' :
268- return `yup.string()` ;
263+ return `yup.string().defined() ` ;
269264 case 'number' :
270- return `yup.number()` ;
265+ return `yup.number().defined() ` ;
271266 case 'boolean' :
272- return `yup.boolean()` ;
267+ return `yup.boolean().defined() ` ;
273268 }
274269 console . warn ( 'unhandled name:' , scalarName ) ;
275270 return `yup.mixed()` ;
0 commit comments