diff --git a/example/yup/schemas.ts b/example/yup/schemas.ts index 5051a100..240f426c 100644 --- a/example/yup/schemas.ts +++ b/example/yup/schemas.ts @@ -1,7 +1,7 @@ import * as yup from 'yup' import { Admin, AttributeInput, ButtonComponentType, ComponentInput, DropDownComponentInput, EventArgumentInput, EventInput, EventOptionType, Guest, HttpInput, HttpMethod, LayoutInput, PageInput, PageType, User, UserKind } from '../types' -function union(...schemas: ReadonlyArray>): yup.MixedSchema { +function union(...schemas: ReadonlyArray>): yup.MixedSchema { return yup.mixed().test({ test: (value) => schemas.some((schema) => schema.isValidSync(value)) }).defined() diff --git a/src/myzod/index.ts b/src/myzod/index.ts index 34b66324..09ff6c24 100644 --- a/src/myzod/index.ts +++ b/src/myzod/index.ts @@ -102,7 +102,16 @@ export const MyZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSche 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(', '); + const unionElements = node.types + ?.map(t => { + const element = tsVisitor.convertName(t.name.value); + const typ = schema.getType(t.name.value); + if (typ?.astNode?.kind === 'EnumTypeDefinition') { + return `${element}Schema`; + } + return `${element}Schema()`; + }) + .join(', '); const unionElementsCount = node.types?.length ?? 0; const union = diff --git a/src/yup/index.ts b/src/yup/index.ts index e4cdce47..1be7a597 100644 --- a/src/yup/index.ts +++ b/src/yup/index.ts @@ -36,7 +36,7 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema '\n' + new DeclarationBlock({}) .asKind('function') - .withName('union(...schemas: ReadonlyArray>): yup.MixedSchema') + .withName('union(...schemas: ReadonlyArray>): yup.MixedSchema') .withBlock( [ indent('return yup.mixed().test({'), @@ -129,7 +129,16 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema const unionName = tsVisitor.convertName(node.name.value); importTypes.push(unionName); - const unionElements = node.types?.map(t => `${tsVisitor.convertName(t.name.value)}Schema()`).join(', '); + const unionElements = node.types + ?.map(t => { + const element = tsVisitor.convertName(t.name.value); + const typ = schema.getType(t.name.value); + if (typ?.astNode?.kind === 'EnumTypeDefinition') { + return `${element}Schema`; + } + return `${element}Schema()`; + }) + .join(', '); const union = indent(`return union<${unionName}>(${unionElements})`); return new DeclarationBlock({}) diff --git a/src/zod/index.ts b/src/zod/index.ts index c540f8e7..dfe07362 100644 --- a/src/zod/index.ts +++ b/src/zod/index.ts @@ -117,7 +117,16 @@ export const ZodSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema 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(', '); + const unionElements = node.types + .map(t => { + const element = tsVisitor.convertName(t.name.value); + const typ = schema.getType(t.name.value); + if (typ?.astNode?.kind === 'EnumTypeDefinition') { + return `${element}Schema`; + } + return `${element}Schema()`; + }) + .join(', '); const unionElementsCount = node.types.length ?? 0; const union = diff --git a/tests/myzod.spec.ts b/tests/myzod.spec.ts index f03c2b3c..ccde0257 100644 --- a/tests/myzod.spec.ts +++ b/tests/myzod.spec.ts @@ -636,6 +636,41 @@ describe('myzod', () => { expect(result.content).toContain(wantContain); } }); + + it('generate enum union types', async () => { + const schema = buildSchema(/* GraphQL */ ` + enum PageType { + PUBLIC + BASIC_AUTH + } + + enum MethodType { + GET + POST + } + + union AnyType = PageType | MethodType + `); + + const result = await plugin( + schema, + [], + { + schema: 'myzod', + withObjectType: true, + }, + {} + ); + + const wantContains = [ + 'export function AnyTypeSchema() {', + 'return myzod.union([PageTypeSchema, MethodTypeSchema])', + '}', + ]; + for (const wantContain of wantContains) { + expect(result.content).toContain(wantContain); + } + }); }); it('properly generates custom directive values', async () => { diff --git a/tests/yup.spec.ts b/tests/yup.spec.ts index 13705bf0..11f9c47d 100644 --- a/tests/yup.spec.ts +++ b/tests/yup.spec.ts @@ -551,6 +551,41 @@ describe('yup', () => { expect(result.content).toContain(wantContain); } }); + + it('generate enum union types', async () => { + const schema = buildSchema(/* GraphQL */ ` + enum PageType { + PUBLIC + BASIC_AUTH + } + + enum MethodType { + GET + POST + } + + union AnyType = PageType | MethodType + `); + + const result = await plugin( + schema, + [], + { + schema: 'yup', + withObjectType: true, + }, + {} + ); + + const wantContains = [ + 'export function AnyTypeSchema(): yup.MixedSchema {', + 'union(PageTypeSchema, MethodTypeSchema)', + '}', + ]; + for (const wantContain of wantContains) { + expect(result.content).toContain(wantContain); + } + }); }); it('properly generates custom directive values', async () => { diff --git a/tests/zod.spec.ts b/tests/zod.spec.ts index c278ce00..07c749f4 100644 --- a/tests/zod.spec.ts +++ b/tests/zod.spec.ts @@ -320,6 +320,7 @@ describe('zod', () => { expect(result.prepend).toContain("import { SayI } from './types'"); expect(result.content).toContain('export function SayISchema(): z.ZodObject> {'); }); + describe('issues #19', () => { it('string field', async () => { const schema = buildSchema(/* GraphQL */ ` @@ -351,6 +352,7 @@ describe('zod', () => { expect(result.content).toContain(wantContain); } }); + it('not null field', async () => { const schema = buildSchema(/* GraphQL */ ` input UserCreateInput { @@ -381,6 +383,7 @@ describe('zod', () => { expect(result.content).toContain(wantContain); } }); + it('list field', async () => { const schema = buildSchema(/* GraphQL */ ` input UserCreateInput { @@ -412,6 +415,7 @@ describe('zod', () => { } }); }); + describe('PR #112', () => { it('with notAllowEmptyString', async () => { const schema = buildSchema(/* GraphQL */ ` @@ -476,6 +480,7 @@ describe('zod', () => { } }); }); + describe('with withObjectType', () => { const schema = buildSchema(/* GraphQL */ ` input ScalarsInput { @@ -732,6 +737,41 @@ describe('zod', () => { expect(result.content).toContain(wantContain); } }); + + it('generate enum union types', async () => { + const schema = buildSchema(/* GraphQL */ ` + enum PageType { + PUBLIC + BASIC_AUTH + } + + enum MethodType { + GET + POST + } + + union AnyType = PageType | MethodType + `); + + const result = await plugin( + schema, + [], + { + schema: 'zod', + withObjectType: true, + }, + {} + ); + + const wantContains = [ + 'export function AnyTypeSchema() {', + 'return z.union([PageTypeSchema, MethodTypeSchema])', + '}', + ]; + for (const wantContain of wantContains) { + expect(result.content).toContain(wantContain); + } + }); }); it('properly generates custom directive values', async () => {