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

zodToJsonSchema tries to evaluate .preprocess with undefined passed in #23

Closed
dearlordylord opened this issue Oct 4, 2022 · 2 comments

Comments

@dearlordylord
Copy link

To reproduce, use https://github.com/Firfi/zod-to-json-schema-preprocess-bug

For such code as

const idParser = z.preprocess(
  (uuid) => {
    if (typeof uuid !== 'string') throw new Error(`Expected string, got ${typeof uuid}: ${uuid}`);
    const [type, id] = uuid.split('SEPARATOR');
    return { type, id };
  }, z.object({ type: z.string(), id: z.string() }));

zodToJsonSchema(idParser) will be evaluated all right, however

if I do

const idParser2 = z.object({
  id: idParser
})

zodToJsonSchema(idParser2) starts passing undefined into preprocess (as my uuid var)

I wonder if that's expected behaviour and why it happens only when a parser is used inside z.object

@StefanTerdell
Copy link
Owner

Hello @Firfi, thanks for logging an issue. This looks like an internal Zod bug. I've logged an issue here: colinhacks/zod#1460

@StefanTerdell
Copy link
Owner

I've added a note about this issue in the readme. Closing this for lack of activity in the meantime but let's keep an eye on the issue in Zod

@StefanTerdell StefanTerdell closed this as not planned Won't fix, can't repro, duplicate, stale Oct 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants