Skip to content

fixed to supress union type generation when withObjectType is false #280

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 15, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
fixed to supress union type generation when withObjectType is false
  • Loading branch information
Code-Hex committed Jan 15, 2023
commit 73f6bfcdd0abb425fd946a0d01c80c0fdedf1be9
2 changes: 1 addition & 1 deletion src/myzod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche
.withContent(`myzod.enum(${enumname})`).string;
},
UnionTypeDefinition: (node: UnionTypeDefinitionNode) => {
if (!node.types) return;
if (!node.types || !config.withObjectType) return;

const unionName = tsVisitor.convertName(node.name.value);
const unionElements = node.types?.map(t => `${tsVisitor.convertName(t.name.value)}Schema()`).join(', ');
Expand Down
30 changes: 17 additions & 13 deletions src/yup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,22 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
}
return [importYup];
},
initialEmit: (): string =>
'\n' +
new DeclarationBlock({})
.asKind('function')
.withName('union<T>(...schemas: ReadonlyArray<yup.SchemaOf<T>>): yup.BaseSchema<T>')
.withBlock(
[
indent('return yup.mixed().test({'),
indent('test: (value) => schemas.some((schema) => schema.isValidSync(value))', 2),
indent('})'),
].join('\n')
).string,
initialEmit: (): string => {
if (!config.withObjectType) return '';
return (
'\n' +
new DeclarationBlock({})
.asKind('function')
.withName('union<T>(...schemas: ReadonlyArray<yup.SchemaOf<T>>): yup.BaseSchema<T>')
.withBlock(
[
indent('return yup.mixed().test({'),
indent('test: (value) => schemas.some((schema) => schema.isValidSync(value))', 2),
indent('})'),
].join('\n')
).string
);
},
InputObjectTypeDefinition: (node: InputObjectTypeDefinitionNode) => {
const name = tsVisitor.convertName(node.name.value);
importTypes.push(name);
Expand Down Expand Up @@ -102,7 +106,7 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
.withContent(`yup.mixed().oneOf([${values}])`).string;
},
UnionTypeDefinition: (node: UnionTypeDefinitionNode) => {
if (!node.types) return;
if (!node.types || !config.withObjectType) return;

const unionName = tsVisitor.convertName(node.name.value);
importTypes.push(unionName);
Expand Down
2 changes: 1 addition & 1 deletion src/zod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
.withContent(`z.nativeEnum(${enumname})`).string;
},
UnionTypeDefinition: (node: UnionTypeDefinitionNode) => {
if (!node.types) return;
if (!node.types || !config.withObjectType) return;

const unionName = tsVisitor.convertName(node.name.value);
const unionElements = node.types.map(t => `${tsVisitor.convertName(t.name.value)}Schema()`).join(', ');
Expand Down