Overview
Zod provides bidirectional conversion between Zod schemas and JSON Schema, enabling interoperability with tools and systems that use JSON Schema.Converting to JSON Schema
Basic Conversion
Usez.toJSONSchema() to convert Zod schemas to JSON Schema:
Primitive Types
Special Types
String Formats
JSON Schema Versions
JSON Schema conversion defaults to Draft 2020-12, but you can target different versions:OpenAPI 3.0 Compatibility
Validate that generated JSON Schema is compatible with OpenAPI 3.0:Converting from JSON Schema
Basic Conversion
Usez.fromJSONSchema() to convert JSON Schema to Zod schemas:
Object Schemas
Array Schemas
Schema References
Conversion Options
Target Version
Specify the JSON Schema version being converted from:Custom Registry
Use a custom registry for schema resolution:Supported JSON Schema Features
Zod’s JSON Schema conversion supports:Type Keywords
type: All JSON types (string, number, boolean, null, object, array, integer)enum: Enumerated valuesconst: Constant values
Composition Keywords
anyOf: Union typesoneOf: Discriminated unionsallOf: Intersection typesnot: Negation
Object Keywords
properties: Object propertiesrequired: Required fieldsadditionalProperties: Extra propertiespatternProperties: Pattern-based propertiesminProperties,maxProperties: Property count constraints
Array Keywords
items: Array item schemaprefixItems: Tuple validationminItems,maxItems: Length constraintsuniqueItems: Uniqueness constraintcontains: Contains validation
String Keywords
minLength,maxLength: Length constraintspattern: Regex patternformat: Format validation (email, url, uuid, etc.)
Number Keywords
minimum,maximum: Range constraintsexclusiveMinimum,exclusiveMaximum: Exclusive boundsmultipleOf: Multiple constraint
Metadata
description: Schema descriptiondefault: Default values$schema,$id,$comment: Schema metadata
Unsupported Features
Some JSON Schema features are not supported:unevaluatedItemsunevaluatedPropertiesif/then/elseconditionalsdependentSchemasdependentRequired
Round-Trip Conversion
You can convert back and forth between Zod and JSON Schema:While round-trip conversion is possible, some Zod features (like custom refinements) cannot be represented in JSON Schema and will be lost in conversion.
Best Practices
- Use standard JSON Schema features - Stick to widely supported keywords for better compatibility
- Validate OpenAPI compatibility - If targeting OpenAPI, validate the generated schema
- Handle unsupported features - Catch errors when converting JSON Schema with unsupported features
- Preserve metadata - Use
descriptionand other metadata fields for documentation - Test round-trip conversion - Verify that converting to/from JSON Schema preserves validation behavior
- **Use defs` and reference them