[v4] Feature Request: Add `fromJSONSchema` method to construct Zod schemas from JSON Schema
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.
requiredproperties.- Nested objects and arrays.
- Optionally,
enumand 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.
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:
Have another question? Just tag @inkeep.
I think this should be reviewed by a contributor @inkeep
+1
+1
+1
+1
+1
+1
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);