Skip to content

fixed Union of enums: zod enum has no native call signature issue #352

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 1 commit into from
Apr 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion example/yup/schemas.ts
Original file line number Diff line number Diff line change
@@ -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<T extends {}>(...schemas: ReadonlyArray<yup.ObjectSchema<T>>): yup.MixedSchema<T> {
function union<T extends {}>(...schemas: ReadonlyArray<yup.Schema<T>>): yup.MixedSchema<T> {
return yup.mixed<T>().test({
test: (value) => schemas.some((schema) => schema.isValidSync(value))
}).defined()
Expand Down
11 changes: 10 additions & 1 deletion src/myzod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
13 changes: 11 additions & 2 deletions src/yup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const YupSchemaVisitor = (schema: GraphQLSchema, config: ValidationSchema
'\n' +
new DeclarationBlock({})
.asKind('function')
.withName('union<T extends {}>(...schemas: ReadonlyArray<yup.ObjectSchema<T>>): yup.MixedSchema<T>')
.withName('union<T extends {}>(...schemas: ReadonlyArray<yup.Schema<T>>): yup.MixedSchema<T>')
.withBlock(
[
indent('return yup.mixed<T>().test({'),
Expand Down Expand Up @@ -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({})
Expand Down
11 changes: 10 additions & 1 deletion src/zod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
35 changes: 35 additions & 0 deletions tests/myzod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
35 changes: 35 additions & 0 deletions tests/yup.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<AnyType> {',
'union<AnyType>(PageTypeSchema, MethodTypeSchema)',
'}',
];
for (const wantContain of wantContains) {
expect(result.content).toContain(wantContain);
}
});
});

it('properly generates custom directive values', async () => {
Expand Down
40 changes: 40 additions & 0 deletions tests/zod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ describe('zod', () => {
expect(result.prepend).toContain("import { SayI } from './types'");
expect(result.content).toContain('export function SayISchema(): z.ZodObject<Properties<SayI>> {');
});

describe('issues #19', () => {
it('string field', async () => {
const schema = buildSchema(/* GraphQL */ `
Expand Down Expand Up @@ -351,6 +352,7 @@ describe('zod', () => {
expect(result.content).toContain(wantContain);
}
});

it('not null field', async () => {
const schema = buildSchema(/* GraphQL */ `
input UserCreateInput {
Expand Down Expand Up @@ -381,6 +383,7 @@ describe('zod', () => {
expect(result.content).toContain(wantContain);
}
});

it('list field', async () => {
const schema = buildSchema(/* GraphQL */ `
input UserCreateInput {
Expand Down Expand Up @@ -412,6 +415,7 @@ describe('zod', () => {
}
});
});

describe('PR #112', () => {
it('with notAllowEmptyString', async () => {
const schema = buildSchema(/* GraphQL */ `
Expand Down Expand Up @@ -476,6 +480,7 @@ describe('zod', () => {
}
});
});

describe('with withObjectType', () => {
const schema = buildSchema(/* GraphQL */ `
input ScalarsInput {
Expand Down Expand Up @@ -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 () => {
Expand Down