zod icon indicating copy to clipboard operation
zod copied to clipboard

[v4] Feature Request: Add `fromJSONSchema` method to construct Zod schemas from JSON Schema

Open atralvarez opened this issue 3 months ago • 8 comments

Motivation

Currently, Zod provides a toJSONSchema() method to export Zod schemas to JSON Schema. However, there is no native support to go in the opposite direction: constructing a Zod schema directly from a JSON Schema definition.

This would be especially useful in scenarios where JSON Schemas are stored in a database or received from external sources, and developers want to leverage Zod's runtime validation and TypeScript inference without relying on third-party libraries.

Proposed Solution

Introduce a method such as z.fromJSONSchema(jsonSchema: JSONSchema) that returns a fully typed Zod schema equivalent to the provided JSON Schema. Ideally, it should support:

  • Basic types: string, number, boolean, array, object.
  • required properties.
  • Nested objects and arrays.
  • Optionally, enum and pattern validations.

This would complement the existing toJSONSchema() functionality, creating a two-way conversion between Zod and JSON Schema.

Example

const jsonSchema = {
  type: "object",
  properties: {
    name: { type: "string" },
    email: { type: "string" },
    age: { type: "number" }
  },
  required: ["name", "email"]
};

const schema = z.fromJSONSchema(jsonSchema);

// Now `schema` can be used with `.parse()` or `.safeParse()` like any Zod schema

Additional Context

This feature would reduce the need for third-party libraries that perform JSON Schema to Zod conversion and streamline workflows where JSON Schema is the source of truth for validation rules.

I would be happy to contribute or collaborate on implementing this feature if the maintainers think it aligns with Zod's roadmap.

atralvarez avatar Sep 13 '25 11:09 atralvarez

Zod (v4) provides a toJSONSchema() method for converting Zod schemas to JSON Schema, but there is no documented fromJSONSchema or similar method for constructing Zod schemas from JSON Schema definitions. The documentation only describes exporting to JSON Schema, not importing or converting the other way(1)(2)(3)(4)(5)(6).

📚 Sources:

  1. JSON Schema
  2. Zod v4 LLMs-Full
  3. Release notes
  4. Zod v4 LLMs-Full
  5. Zod v4 LLMs-Full
  6. JSON Schema

Have another question? Just tag @inkeep.

I think this should be reviewed by a contributor @inkeep

atralvarez avatar Sep 13 '25 15:09 atralvarez

+1

fmhall avatar Nov 19 '25 17:11 fmhall

+1

kecbigmt avatar Dec 05 '25 10:12 kecbigmt

+1

tomhaerter avatar Dec 08 '25 08:12 tomhaerter

+1

eyecan-ai avatar Dec 08 '25 22:12 eyecan-ai

+1

ovjectibity avatar Dec 09 '25 04:12 ovjectibity

+1

maxim-shilov-deel avatar Dec 10 '25 20:12 maxim-shilov-deel

This has landed in Zod 4.2: https://zod.dev/json-schema?id=zfromjsonschema

import * as z from "zod";
 
const jsonSchema = {
  type: "object",
  properties: {
    name: { type: "string" },
    age: { type: "number" },
  },
  required: ["name", "age"],
};
 
const zodSchema = z.fromJSONSchema(jsonSchema);

colinhacks avatar Dec 16 '25 16:12 colinhacks