Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/zod/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,13 @@ export class ZodSchemaVisitor extends BaseSchemaVisitor {
? new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${enumname}Schema`)
.withName(`${enumname}Schema: z.ZodEnum<{ ${node.values?.map(enumOption => `${enumOption.name.value}: "${enumOption.name.value}"`).join('; ')} }>`)
.withContent(`z.enum([${node.values?.map(enumOption => `'${enumOption.name.value}'`).join(', ')}])`)
.string
: new DeclarationBlock({})
.export()
.asKind('const')
.withName(`${enumname}Schema`)
.withName(`${enumname}Schema: z.ZodEnum<typeof ${enumTypeName}>`)
.withContent(`z.nativeEnum(${enumTypeName})`)
.string,
);
Expand Down
24 changes: 12 additions & 12 deletions tests/zod.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ describe('zod', () => {
const result = await plugin(schema, [], { schema: 'zod', scalars }, {});
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
"
export const PageTypeSchema = z.nativeEnum(PageType);
export const PageTypeSchema: z.ZodEnum<typeof PageType> = z.nativeEnum(PageType);

export function PageInputSchema(): z.ZodObject<Properties<PageInput>> {
return z.object({
Expand All @@ -252,7 +252,7 @@ describe('zod', () => {
const result = await plugin(schema, [], { schema: 'zod', scalars, importFrom: './', schemaNamespacedImportName: 't' }, {});
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
"
export const PageTypeSchema = z.nativeEnum(t.PageType);
export const PageTypeSchema: z.ZodEnum<typeof t.PageType> = z.nativeEnum(t.PageType);

export function PageInputSchema(): z.ZodObject<Properties<t.PageInput>> {
return z.object({
Expand Down Expand Up @@ -281,7 +281,7 @@ describe('zod', () => {
const result = await plugin(schema, [], { schema: 'zod', scalars }, {});
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
"
export const HttpMethodSchema = z.nativeEnum(HttpMethod);
export const HttpMethodSchema: z.ZodEnum<typeof HttpMethod> = z.nativeEnum(HttpMethod);

export function HttpInputSchema(): z.ZodObject<Properties<HttpInput>> {
return z.object({
Expand Down Expand Up @@ -443,7 +443,7 @@ describe('zod', () => {
);
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
"
export const PageTypeSchema = z.enum(['PUBLIC', 'BASIC_AUTH']);
export const PageTypeSchema: z.ZodEnum<{ PUBLIC: "PUBLIC"; BASIC_AUTH: "BASIC_AUTH" }> = z.enum(['PUBLIC', 'BASIC_AUTH']);
"
`)
});
Expand All @@ -468,7 +468,7 @@ describe('zod', () => {
);
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
"
export const PageTypeSchema = z.enum(['PUBLIC', 'BASIC_AUTH']);
export const PageTypeSchema: z.ZodEnum<{ PUBLIC: "PUBLIC"; BASIC_AUTH: "BASIC_AUTH" }> = z.enum(['PUBLIC', 'BASIC_AUTH']);
"
`)
});
Expand Down Expand Up @@ -711,7 +711,7 @@ describe('zod', () => {
},
);

expect(result.content).toContain('export const PageTypeSchema = z.nativeEnum(PageType)');
expect(result.content).toContain('export const PageTypeSchema: z.ZodEnum<typeof PageType> = z.nativeEnum(PageType)');
expect(result.content).toContain('export function PageInputSchema(): z.ZodObject<Properties<PageInput>>');

expect(result.content).toContain('pageType: PageTypeSchema.default(PageType.Public)');
Expand Down Expand Up @@ -747,7 +747,7 @@ describe('zod', () => {
},
);

expect(result.content).toContain('export const PageTypeSchema = z.nativeEnum(PageType)');
expect(result.content).toContain('export const PageTypeSchema: z.ZodEnum<typeof PageType> = z.nativeEnum(PageType)');
expect(result.content).toContain('export function PageInputSchema(): z.ZodObject<Properties<PageInput>>');

expect(result.content).toContain('pageType: PageTypeSchema.default(PageType.Basic_Auth)');
Expand Down Expand Up @@ -786,7 +786,7 @@ describe('zod', () => {
},
);

expect(result.content).toContain('export const PageTypeSchema = z.nativeEnum(PageType)');
expect(result.content).toContain('export const PageTypeSchema: z.ZodEnum<typeof PageType> = z.nativeEnum(PageType)');
expect(result.content).toContain('export function PageInputSchema(): z.ZodObject<Properties<PageInput>>');

expect(result.content).toContain('pageType: PageTypeSchema.default(PageType.BasicAuth)');
Expand Down Expand Up @@ -823,7 +823,7 @@ describe('zod', () => {

expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
"
export const PageTypeSchema = z.nativeEnum(PageType);
export const PageTypeSchema: z.ZodEnum<typeof PageType> = z.nativeEnum(PageType);

export function PageInputSchema(): z.ZodObject<Properties<PageInput>> {
return z.object({
Expand Down Expand Up @@ -1369,9 +1369,9 @@ describe('zod', () => {

expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
"
export const PageTypeSchema = z.nativeEnum(PageType);
export const PageTypeSchema: z.ZodEnum<typeof PageType> = z.nativeEnum(PageType);

export const MethodTypeSchema = z.nativeEnum(MethodType);
export const MethodTypeSchema: z.ZodEnum<typeof MethodType> = z.nativeEnum(MethodType);

export function AnyTypeSchema() {
return z.union([PageTypeSchema, MethodTypeSchema])
Expand Down Expand Up @@ -1787,7 +1787,7 @@ describe('zod', () => {
);
expect(removedInitialEmitValue(result.content)).toMatchInlineSnapshot(`
"
export const TestSchema = z.nativeEnum(Test);
export const TestSchema: z.ZodEnum<typeof Test> = z.nativeEnum(Test);

export function QueryInputSchema(): z.ZodObject<Properties<QueryInput>> {
return z.object({
Expand Down