Skip to content

Commit c080771

Browse files
committed
fixed a few miss code
1 parent efe8628 commit c080771

File tree

3 files changed

+29
-28
lines changed

3 files changed

+29
-28
lines changed

src/myzod/index.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,18 +91,19 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
9191
.withContent(`myzod.enum(${enumname})`).string;
9292
},
9393
UnionTypeDefinition: (node: UnionTypeDefinitionNode) => {
94+
if (!node.types) return;
95+
9496
const unionName = tsVisitor.convertName(node.name.value);
9597
const unionElements = node.types?.map(t => `${tsVisitor.convertName(t.name.value)}Schema()`).join(', ');
9698
const unionElementsCount = node.types?.length ?? 0;
9799

98-
const union =
99-
unionElementsCount > 1 ? indent(`return myzod.union([${unionElements}])`) : indent(`return ${unionElements}`);
100+
const union = unionElementsCount > 1 ? `myzod.union([${unionElements}])` : unionElements;
100101

101102
const result = new DeclarationBlock({})
102103
.export()
103-
.asKind('function')
104-
.withName(`${unionName}Schema()`)
105-
.withBlock(union);
104+
.asKind('const')
105+
.withName(`${unionName}Schema`)
106+
.withContent(union);
106107

107108
return result.string;
108109
},
@@ -200,7 +201,7 @@ const generateNameNodeMyZodSchema = (
200201

201202
if (typ?.astNode?.kind === 'UnionTypeDefinition') {
202203
const enumName = tsVisitor.convertName(typ.astNode.name.value);
203-
return `${enumName}Schema()`;
204+
return `${enumName}Schema`;
204205
}
205206

206207
return myzod4Scalar(config, tsVisitor, node.value);

src/yup/index.ts

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
3030
return [importYup];
3131
},
3232
initialEmit: (): string =>
33+
'\n' +
3334
new DeclarationBlock({})
3435
.asKind('function')
3536
.withName('union<T>(...schemas: ReadonlyArray<yup.SchemaOf<T>>): yup.BaseSchema<T>')
@@ -101,22 +102,21 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
101102
.withContent(`yup.mixed().oneOf([${values}])`).string;
102103
},
103104
UnionTypeDefinition: (node: UnionTypeDefinitionNode) => {
105+
if (!node.types) return;
106+
104107
const unionName = tsVisitor.convertName(node.name.value);
108+
importTypes.push(unionName);
109+
105110
const unionElements = node.types?.map(t => `${tsVisitor.convertName(t.name.value)}Schema()`).join(', ');
106111
const unionElementsCount = node.types?.length ?? 0;
107112

108-
const union =
109-
unionElementsCount > 1
110-
? indent(`return union<${unionName}>(${unionElements})`)
111-
: indent(`return ${unionElements}`);
113+
const union = unionElementsCount > 1 ? `union<${unionName}>(${unionElements})` : unionElements;
112114

113-
const result = new DeclarationBlock({})
115+
return new DeclarationBlock({})
114116
.export()
115-
.asKind('function')
116-
.withName(`${unionName}Schema(): yup.BaseSchema<${unionName}>`)
117-
.withBlock(union);
118-
119-
return result.string;
117+
.asKind('const')
118+
.withName(`${unionName}Schema: yup.BaseSchema<${unionName}>`)
119+
.withContent(union).string;
120120
},
121121
// ScalarTypeDefinition: (node) => {
122122
// const decl = new DeclarationBlock({})
@@ -209,6 +209,11 @@ const generateNameNodeYupSchema = (
209209
return `${enumName}Schema`;
210210
}
211211

212+
if (typ?.astNode?.kind === 'UnionTypeDefinition') {
213+
const enumName = tsVisitor.convertName(typ.astNode.name.value);
214+
return `${enumName}Schema`;
215+
}
216+
212217
const primitive = yup4Scalar(config, tsVisitor, node.value);
213218
return primitive;
214219
};

src/zod/index.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,15 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
102102
.withContent(`z.nativeEnum(${enumname})`).string;
103103
},
104104
UnionTypeDefinition: (node: UnionTypeDefinitionNode) => {
105-
const unionName = tsVisitor.convertName(node.name.value);
106-
const unionElements = node.types?.map(t => `${tsVisitor.convertName(t.name.value)}Schema()`).join(', ');
107-
const unionElementsCount = node.types?.length ?? 0;
105+
if (!node.types) return;
108106

109-
const union =
110-
unionElementsCount > 1 ? indent(`return z.union([${unionElements}])`) : indent(`return ${unionElements}`);
107+
const unionName = tsVisitor.convertName(node.name.value);
108+
const unionElements = node.types.map(t => `${tsVisitor.convertName(t.name.value)}Schema()`).join(', ');
109+
const unionElementsCount = node.types.length ?? 0;
111110

112-
const result = new DeclarationBlock({})
113-
.export()
114-
.asKind('function')
115-
.withName(`${unionName}Schema()`)
116-
.withBlock(union);
111+
const union = unionElementsCount > 1 ? `z.union([${unionElements}])` : unionElements;
117112

118-
return result.string;
113+
return new DeclarationBlock({}).export().asKind('const').withName(`${unionName}Schema`).withContent(union).string;
119114
},
120115
};
121116
};
@@ -211,7 +206,7 @@ const generateNameNodeZodSchema = (
211206

212207
if (typ?.astNode?.kind === 'UnionTypeDefinition') {
213208
const enumName = tsVisitor.convertName(typ.astNode.name.value);
214-
return `${enumName}Schema()`;
209+
return `${enumName}Schema`;
215210
}
216211

217212
return zod4Scalar(config, tsVisitor, node.value);

0 commit comments

Comments
 (0)