json-zodify
v1.0.2
Published
Convert JSON Schema to Zod schemas at runtime - Supports Zod v3 and v4
Maintainers
Readme
Zodify
Convert JSON Schema to Zod schemas at runtime.
A lightweight TypeScript library that dynamically generates Zod schemas from JSON Schema definitions. Perfect for scenarios where schemas are stored in databases or need to be validated at runtime.
Features
- 🚀 Dynamic Schema Generation - Convert JSON Schema to Zod on the fly
- 🔄 Zod v3 & v4 Compatible - Uses your project's Zod version (peer dependency)
- 📦 Zero Version Conflicts - No bundled Zod, avoids type mismatches
- 🎯 Full Type Support - String, number, boolean, array, object, null
- ✨ Rich Validations - Formats, patterns, min/max, enums, and more
- 🔗 Combinators - Supports oneOf, anyOf, allOf
- 📝 $ref Resolution - Basic support for local references
Installation
npm install zodify
# or
pnpm add zodify
# or
yarn add zodifyNote: Zod is a peer dependency. Make sure you have it installed:
npm install zodUsage
Basic Conversion
import { jsonSchemaToZod } from 'zodify'
const schema = jsonSchemaToZod({
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number', minimum: 0 },
},
required: ['name', 'age'],
})
// Validate data
const result = schema.parse({ name: 'John', age: 30 })
console.log(result) // { name: 'John', age: 30 }Using the Class API
import { JSONSchemaToZod } from 'zodify'
const zodSchema = JSONSchemaToZod.convert({
type: 'string',
format: 'email',
minLength: 5,
})
zodSchema.parse('[email protected]') // ✅ Valid
zodSchema.parse('invalid') // ❌ Throws ZodErrorComplex Objects
const userSchema = jsonSchemaToZod({
type: 'object',
properties: {
id: { type: 'string', format: 'uuid' },
email: { type: 'string', format: 'email' },
profile: {
type: 'object',
properties: {
firstName: { type: 'string' },
lastName: { type: 'string' },
age: { type: 'integer', minimum: 0, maximum: 150 },
},
required: ['firstName'],
},
tags: {
type: 'array',
items: { type: 'string' },
minItems: 1,
},
status: {
type: 'string',
enum: ['active', 'inactive', 'pending'],
},
},
required: ['id', 'email'],
})Using Combinators
// oneOf - Value must match exactly one schema
const oneOfSchema = jsonSchemaToZod({
oneOf: [
{ type: 'string' },
{ type: 'number' },
],
})
oneOfSchema.parse('hello') // ✅
oneOfSchema.parse(42) // ✅
// allOf - Value must match all schemas (intersection)
const allOfSchema = jsonSchemaToZod({
allOf: [
{ type: 'object', properties: { a: { type: 'string' } }, required: ['a'] },
{ type: 'object', properties: { b: { type: 'number' } }, required: ['b'] },
],
})
allOfSchema.parse({ a: 'hello', b: 123 }) // ✅Using $ref
const schemaWithRefs = jsonSchemaToZod({
type: 'object',
properties: {
user: { $ref: '#/$defs/User' },
},
$defs: {
User: {
type: 'object',
properties: {
name: { type: 'string' },
email: { type: 'string', format: 'email' },
},
required: ['name', 'email'],
},
},
})Options
const schema = jsonSchemaToZod(jsonSchema, {
// Make all properties optional
allOptional: true,
// Allow unknown keys (passthrough)
passthrough: true,
// Provide external definitions for $ref resolution
definitions: {
MyType: { type: 'string' },
},
})Supported JSON Schema Features
Types
string- With formats: email, uri, uuid, date-time, date, time, ipv4, ipv6number/integer- With min, max, multipleOfbooleanarray- With items, minItems, maxItems, tuplesobject- With properties, required, additionalPropertiesnull
Validations
- String:
minLength,maxLength,pattern,format - Number:
minimum,maximum,exclusiveMinimum,exclusiveMaximum,multipleOf - Array:
minItems,maxItems, tuple support - Object:
required,additionalProperties
Keywords
enum/constnullabledefaultdescriptiononeOf/anyOf/allOf$ref(local references)
Why Zodify?
Other JSON Schema to Zod converters either:
- Generate code strings that require
eval()(security risk) - Bundle their own Zod version causing type conflicts
- Only work at build time, not runtime
Zodify uses your project's Zod instance as a peer dependency, ensuring:
- ✅ No version conflicts
- ✅ Full TypeScript support
- ✅ Runtime conversion
- ✅ No
eval()needed
License
MIT
