Skip to content
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

Fix flattening zod schema - chained calls, enum value, tsConfigFilePath #59

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

tinglei8
Copy link

@tinglei8 tinglei8 commented Jan 3, 2025

There are 3 fixes in this PR:

Fix 1: Flatten chained function calls.

An example:

import { z } from 'zod';

const TypeEnum = z
  .enum(['Normal', 'Unknown'])
  .describe('Type of the item');

const FindManyInput = z.object({
  options: z
    .object({
      userId: z.string().describe('ID of the current user'),
      type1: TypeEnum.optional().describe('Type 1 of the item')
    })
    .merge({
      z.object({
        type2: TypeEnum.optional().describe('Type 2 of the item')
      })
    })
    .describe('Options to find many items'),
});

Before the fix, references to TypeEnum are not flattened in FindManyInput because it's referenced as parameters of the object() and merge() calls instead of the last describe() call.

After the fix, The TypeEnum schema is properly flattened in all places that reference it.

Fix 2: add tsConfigFilePath to module config.

See issue: #18

Fix 3: Flatten enum name to literal values.

An example:

// types.ts
export enum TypeEnum { Normal = 'Normal', Unknown = 'Unknown' };
// test.ts
import { z } from 'zod';
import { TypeEnum } from './types';

const FindManyInput = z.object({
  options: z
    .object({
      userId: z.string().describe('ID of the current user'),
      type: z.literal(TypeEnum.Normal).describe('Type of the item')
    })
    .describe('Options to find many items'),
});

Before the fix, it generates: type: z.literal(export enum TypeEnum { Normal = 'Normal', Unknown = 'Unknown' }).describe('Type of the item')

After the fix, it generates: type: z.literal('Normal').describe('Type of the item')

Copy link

vercel bot commented Jan 3, 2025

@tinglei8 is attempting to deploy a commit to the Kevin's Projects Team on Vercel.

A member of the Team first needs to authorize it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant