Skip to content

Commit 66388e8

Browse files
committed
fixed generator for union type again
1 parent d9dbd35 commit 66388e8

File tree

3 files changed

+15
-19
lines changed

3 files changed

+15
-19
lines changed

src/myzod/index.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -97,15 +97,11 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
9797
const unionElements = node.types?.map(t => `${tsVisitor.convertName(t.name.value)}Schema()`).join(', ');
9898
const unionElementsCount = node.types?.length ?? 0;
9999

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

102-
const result = new DeclarationBlock({})
103-
.export()
104-
.asKind('const')
105-
.withName(`${unionName}Schema`)
106-
.withContent(union);
107-
108-
return result.string;
103+
return new DeclarationBlock({}).export().asKind('function').withName(`${unionName}Schema()`).withBlock(union)
104+
.string;
109105
},
110106
};
111107
};
@@ -201,7 +197,7 @@ const generateNameNodeMyZodSchema = (
201197

202198
if (typ?.astNode?.kind === 'UnionTypeDefinition') {
203199
const enumName = tsVisitor.convertName(typ.astNode.name.value);
204-
return `${enumName}Schema`;
200+
return `${enumName}Schema()`;
205201
}
206202

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

src/yup/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,15 +108,13 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
108108
importTypes.push(unionName);
109109

110110
const unionElements = node.types?.map(t => `${tsVisitor.convertName(t.name.value)}Schema()`).join(', ');
111-
const unionElementsCount = node.types?.length ?? 0;
112-
113-
const union = unionElementsCount > 1 ? `union<${unionName}>(${unionElements})` : unionElements;
111+
const union = indent(`return union<${unionName}>(${unionElements})`);
114112

115113
return new DeclarationBlock({})
116114
.export()
117-
.asKind('const')
118-
.withName(`${unionName}Schema: yup.BaseSchema<${unionName}>`)
119-
.withContent(union).string;
115+
.asKind('function')
116+
.withName(`${unionName}Schema(): yup.BaseSchema<${unionName}>`)
117+
.withBlock(union).string;
120118
},
121119
// ScalarTypeDefinition: (node) => {
122120
// const decl = new DeclarationBlock({})
@@ -211,7 +209,7 @@ const generateNameNodeYupSchema = (
211209

212210
if (typ?.astNode?.kind === 'UnionTypeDefinition') {
213211
const enumName = tsVisitor.convertName(typ.astNode.name.value);
214-
return `${enumName}Schema`;
212+
return `${enumName}Schema()`;
215213
}
216214

217215
const primitive = yup4Scalar(config, tsVisitor, node.value);

src/zod/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,11 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
108108
const unionElements = node.types.map(t => `${tsVisitor.convertName(t.name.value)}Schema()`).join(', ');
109109
const unionElementsCount = node.types.length ?? 0;
110110

111-
const union = unionElementsCount > 1 ? `z.union([${unionElements}])` : unionElements;
111+
const union =
112+
unionElementsCount > 1 ? indent(`return z.union([${unionElements}])`) : indent(`return ${unionElements}`);
112113

113-
return new DeclarationBlock({}).export().asKind('const').withName(`${unionName}Schema`).withContent(union).string;
114+
return new DeclarationBlock({}).export().asKind('function').withName(`${unionName}Schema()`).withBlock(union)
115+
.string;
114116
},
115117
};
116118
};
@@ -206,7 +208,7 @@ const generateNameNodeZodSchema = (
206208

207209
if (typ?.astNode?.kind === 'UnionTypeDefinition') {
208210
const enumName = tsVisitor.convertName(typ.astNode.name.value);
209-
return `${enumName}Schema`;
211+
return `${enumName}Schema()`;
210212
}
211213

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

0 commit comments

Comments
 (0)