@@ -36,12 +36,12 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
36
36
'\n' +
37
37
new DeclarationBlock ( { } )
38
38
. 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>' )
40
40
. withBlock (
41
41
[
42
- indent ( 'return yup.mixed().test({' ) ,
42
+ indent ( 'return yup.mixed<T> ().test({' ) ,
43
43
indent ( 'test: (value) => schemas.some((schema) => schema.isValidSync(value))' , 2 ) ,
44
- indent ( '})' ) ,
44
+ indent ( '}).defined() ' ) ,
45
45
] . join ( '\n' )
46
46
) . string
47
47
) ;
@@ -52,13 +52,16 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
52
52
importTypes . push ( name ) ;
53
53
54
54
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
+ } )
56
59
. join ( ',\n' ) ;
57
60
58
61
return new DeclarationBlock ( { } )
59
62
. export ( )
60
63
. asKind ( 'function' )
61
- . withName ( `${ name } Schema(): yup.SchemaOf <${ name } >` )
64
+ . withName ( `${ name } Schema(): yup.ObjectSchema <${ name } >` )
62
65
. withBlock ( [ indent ( `return yup.object({` ) , shape , indent ( '})' ) ] . join ( '\n' ) ) . string ;
63
66
} ,
64
67
} ,
@@ -68,17 +71,20 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
68
71
importTypes . push ( name ) ;
69
72
70
73
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
+ } )
72
78
. join ( ',\n' ) ;
73
79
74
80
return new DeclarationBlock ( { } )
75
81
. export ( )
76
82
. asKind ( 'function' )
77
- . withName ( `${ name } Schema(): yup.SchemaOf <${ name } >` )
83
+ . withName ( `${ name } Schema(): yup.ObjectSchema <${ name } >` )
78
84
. withBlock (
79
85
[
80
86
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 ) ,
82
88
shape ,
83
89
indent ( '})' ) ,
84
90
] . join ( '\n' )
@@ -91,13 +97,13 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
91
97
importTypes . push ( enumname ) ;
92
98
93
99
if ( config . enumsAsTypes ) {
100
+ const enums = node . values ?. map ( enumOption => `'${ enumOption . name . value } '` ) ;
101
+
94
102
return new DeclarationBlock ( { } )
95
103
. export ( )
96
104
. asKind ( 'const' )
97
105
. 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 ;
101
107
}
102
108
103
109
const values = node . values
@@ -113,7 +119,7 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
113
119
. export ( )
114
120
. asKind ( 'const' )
115
121
. withName ( `${ enumname } Schema` )
116
- . withContent ( `yup.mixed ().oneOf([${ values } ])` ) . string ;
122
+ . withContent ( `yup.string< ${ enumname } > ().oneOf([${ values } ]).defined( )` ) . string ;
117
123
} ,
118
124
} ,
119
125
UnionTypeDefinition : {
@@ -129,7 +135,7 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
129
135
return new DeclarationBlock ( { } )
130
136
. export ( )
131
137
. asKind ( 'function' )
132
- . withName ( `${ unionName } Schema(): yup.BaseSchema <${ unionName } >` )
138
+ . withName ( `${ unionName } Schema(): yup.MixedSchema <${ unionName } >` )
133
139
. withBlock ( union ) . string ;
134
140
} ,
135
141
} ,
@@ -180,22 +186,28 @@ const generateFieldTypeYupSchema = (
180
186
if ( isListType ( type ) ) {
181
187
const gen = generateFieldTypeYupSchema ( config , tsVisitor , schema , type . type , type ) ;
182
188
if ( ! isNonNullType ( parentType ) ) {
183
- return `yup.array().of( ${ maybeLazy ( type . type , gen ) } ).optional ()` ;
189
+ return `yup.array(${ maybeLazy ( type . type , gen ) } ).defined().nullable ()` ;
184
190
}
185
- return `yup.array().of( ${ maybeLazy ( type . type , gen ) } )` ;
191
+ return `yup.array(${ maybeLazy ( type . type , gen ) } ).defined( )` ;
186
192
}
187
193
if ( isNonNullType ( type ) ) {
188
194
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 ) ;
191
196
}
192
197
if ( isNamedType ( type ) ) {
193
198
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
+ }
194
206
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 } ` ;
197
209
}
198
- return gen ;
210
+ return ` ${ gen } .nullable()` ;
199
211
}
200
212
console . warn ( 'unhandled type:' , type ) ;
201
213
return '' ;
@@ -236,40 +248,23 @@ const generateNameNodeYupSchema = (
236
248
const maybeLazy = ( type : TypeNode , schema : string ) : string => {
237
249
if ( isNamedType ( type ) && isInput ( type . name . value ) ) {
238
250
// https://github.com/jquense/yup/issues/1283#issuecomment-786559444
239
- return `yup.lazy(() => ${ schema } ) as never ` ;
251
+ return `yup.lazy(() => ${ schema } )` ;
240
252
}
241
253
return schema ;
242
254
} ;
243
255
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
-
261
256
const yup4Scalar = ( config : ValidationSchemaPluginConfig , tsVisitor : TsVisitor , scalarName : string ) : string => {
262
257
if ( config . scalarSchemas ?. [ scalarName ] ) {
263
- return config . scalarSchemas [ scalarName ] ;
258
+ return ` ${ config . scalarSchemas [ scalarName ] } .defined()` ;
264
259
}
265
260
const tsType = tsVisitor . scalars [ scalarName ] ;
266
261
switch ( tsType ) {
267
262
case 'string' :
268
- return `yup.string()` ;
263
+ return `yup.string().defined() ` ;
269
264
case 'number' :
270
- return `yup.number()` ;
265
+ return `yup.number().defined() ` ;
271
266
case 'boolean' :
272
- return `yup.boolean()` ;
267
+ return `yup.boolean().defined() ` ;
273
268
}
274
269
console . warn ( 'unhandled name:' , scalarName ) ;
275
270
return `yup.mixed()` ;
0 commit comments