Skip to content

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

Closed as not planned
@dearlordylord

Description

@dearlordylord

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions