Skip to content

Support zod/v3 from zod@4 #178

@finom

Description

@finom

Not sure if that's a duplicate for #173 but it doesn't mention the zod/v3 module.

It obviously doesn't make sense for zod-to-json-schema to support the new version because of the introduction of first-party support of JSON schema conversion, but there is a lot of legacy code that still relies on Zod 3, such as Prisma generators, AI tools etc. The authors of Zod made an interesting versioning trick, publishing both v4 and v3 at the same package of version 4 but using different modules.

import { z } from 'zod'; // Zod 4 from zod@4
import { z } from 'zod/v3'; // Zod 3 from zod@4

My work is heavily relying on JSON schema emission and support for both "zods" is crucial.

When I try to use zod/v3 from zod@4.0.9 I get nothing more than an empty schema definition:

import { z } from 'zod/v3';
import { zodToJsonSchema } from 'zod-to-json-schema';

console.log(
  zodToJsonSchema(
    z
      .object({
        name: z.string().describe('User full name'),
        age: z.number().min(0).max(120).describe('User age'),
        email: z.string().email().describe('User email'),
      })
      .describe('User object'),
    { errorMessages: true }
  )
);

The code logs { '$schema': 'http://json-schema.org/draft-07/schema#' }

run node index.js in the terminal of this demo https://stackblitz.com/edit/node-d1uqmtku?file=index.js

I think that's more like a bug report than a feature request since zod-to-json-schema was always a great tool for Zod 3, but now zodToJsonSchema isn't working with Zod 3 from zod@4/v3. I hope this is going to be fixed and zod-to-json-schema will live as long as Zod 3 is used.

I hope there is not that much confusion with versions in my message.

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions