Engineering Guides6 min readUpdated: 2026-11-24

JSON Schema Draft 2020-12 to Zod v4, Pydantic v2 & TypeScript

Compile Draft 2020-12 and Draft-07 JSON Schemas with $defs, $ref, enums, and bounds into Zod v4, Pydantic v2, and TypeScript interfaces locally via AJV & IR.

Quick Answer

To compile JSON Schema Draft 2020-12 to Zod, Pydantic, and TypeScript without contract drift, validate payloads locally with AJV (Draft 2020-12 vs Draft-07), resolve #/$defs/ and #/definitions/ $ref pointers through DataTo's Unified Type IR, and emit Zod v4 validators, topologically ordered Pydantic v2 BaseModels, and TypeScript interfaces in browser RAM.

AJV Draft 2020-12 vs. Draft-07 and JSON Schema $ref codegen

Modern API contracts commonly target Draft 2020-12 ($defs and prefixItems tuples), while legacy OpenAPI 3.0 and backend specs still use Draft-07 (definitions and array items). Checking payloads upfront prevents subtle validation drift between TypeScript clients and Python services.

When you compile JSON Schema to code in DataTo, JSON Schema Validator automatically inspects $schema to select Ajv 2020-12 or Ajv Draft-07, checks email, uuid, uri, date, date-time, and ipv4 formats, and reports itemized JSON Pointer paths. Next, the Unified Type IR parser resolves #/$defs/ and #/definitions/ $ref pointers, allOf merges, anyOf/oneOf unions, and nullable flags into a single strongly typed AST.

Validate payloads with local AJV →

Exact mapping across Zod v4, Pydantic v2, and TypeScript

Because every target generator reads the same Unified Type IR, enums, numeric bounds, string length limits, regex patterns, and field descriptions translate consistently across TypeScript and Python.

JSON Schema ConstructZod v4 (.ts)Pydantic v2 (.py)TypeScript (.d.ts)
type: string, format: emailz.email()strstring
type: integer, minimum: 1, maximum: 100z.int().min(1).max(100)int = Field(ge=1, le=100)number
enum: ["open", "closed"]z.enum(["open", "closed"])Literal["open", "closed"]"open" | "closed"
additionalProperties: false.strict()Typed BaseModel fieldsinterface / type fields
description: "...".describe("...")Field(description="...")/** ... */ JSDoc comment

Compile JSON Schema to Zod v4 →

Resolving $defs, $ref, constraints, and aliases cleanly

In Pydantic v2 output, nested classes extracted from $defs or nested properties are ordered topologically before parent classes so Python never raises forward-reference NameErrors. Field names containing hyphens, dots, or Python keywords automatically receive safe identifiers paired with Field(alias=...) and ConfigDict(populate_by_name=True).

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$defs": {
    "Address": {
      "type": "object",
      "required": ["country"],
      "properties": {
        "country": { "type": "string", "minLength": 2, "maxLength": 2 }
      }
    }
  },
  "type": "object",
  "required": ["email", "tier", "retries", "address"],
  "properties": {
    "email": { "type": "string", "format": "email" },
    "tier": { "enum": ["free", "pro"] },
    "retries": { "type": "integer", "minimum": 0, "maximum": 5 },
    "address": { "$ref": "#/$defs/Address" }
  }
}

Compile JSON Schema to Pydantic v2 →

Emitting Zod v4, TypeScript .d.ts, or bootstrapping from sample JSON

JSON Schema to Zod emits Zod v4 primitives (z.email(), z.uuid(), z.url(), z.iso.datetime(), z.int()) alongside an optional z.infer type alias, while JSON Schema to TypeScript produces clean exported interfaces or type aliases with JSDoc descriptions. Need a starting schema first? Paste a sample payload into JSON to JSON Schema to infer a Draft 2020-12 or Draft-07 document locally.

import { z } from "zod";

export const AccountSchema = z.object({
  email: z.email(),
  tier: z.enum(["free", "pro"]),
  retries: z.int().min(0).max(5),
  address: z.object({
    country: z.string().min(2).max(2),
  }),
});

export type Account = z.infer<typeof AccountSchema>;

Compile JSON Schema to TypeScript →

Tools used in this guide

Schemas & MCP →

Related formats

Related Comparisons

Frequently asked questions

How does DataTo handle circular or recursive $ref pointers?

When the IR resolver detects a cyclic $ref, it emits a symbolic reference (rendered as z.lazy(() => ...) in Zod) and logs a non-fatal CYCLIC_REF diagnostic.

Does JSON Schema to Pydantic support Python Pydantic v1 as well as v2?

Yes. Switch Pydantic version target to Pydantic v1 (Legacy) to generate Optional[...], List[...], and inner Config classes instead of v2 unions and ConfigDict.

Are nested properties kept optional when omitted from required[]?

Yes. Properties absent from the object's required array map to .optional() in Zod, | None = None in Pydantic v2, and ?: in TypeScript.

Authoritative References

More engineering guides