{"_id":"zodown","_rev":"2-d0dc63aa9c934cd7d156f3f44aa7fa5a","name":"zodown","dist-tags":{"latest":"0.2.1"},"versions":{"0.2.0":{"name":"zodown","version":"0.2.0","keywords":["zod","schema","validation","typescript","converter","compatibility"],"author":{"name":"Justin Schroeder"},"license":"MIT","_id":"zodown@0.2.0","maintainers":[{"name":"justin-schroeder","email":"justin@formkit.com"}],"homepage":"https://github.com/justinschroeder/zodown","bugs":{"url":"https://github.com/justinschroeder/zodown/issues"},"dist":{"shasum":"010fdeccd754742f850151318e165d579b51a6f3","tarball":"https://registry.npmjs.org/zodown/-/zodown-0.2.0.tgz","fileCount":9,"integrity":"sha512-BTXQQtphdfc/i+2Zfj+0MbF0TJBQscemPKJlLe5RXsFD9CFfGIr6tN63szTx3CJFBe1rWfFZ6W0tB0MQGOgwXQ==","signatures":[{"sig":"MEUCIQDPHqaHKjXG0kndM0cHWU6uZOGGCZyzsfyq6xKJRmBuRAIgJrhA+xNssmVicpluBcOm0sx12xKZrK0u7iukWXIWigU=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":131552},"main":"./dist/index.js","types":"./dist/index.d.ts","module":"./dist/index.mjs","engines":{"node":">=16"},"exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.js"}},"gitHead":"8c59204d0c80d934ac794177797957ac8ae0607e","scripts":{"dev":"tsup --watch","lint":"prettier --check .","test":"vitest","build":"tsup","format":"prettier --write .","release":"pnpm test:run && pnpm build && bumpp && npm publish --access public","docs:dev":"pnpm --filter docs dev","test:run":"vitest run","typecheck":"tsc --noEmit","docs:build":"pnpm --filter docs build","docs:preview":"pnpm --filter docs preview","test:coverage":"vitest run --coverage","prepublishOnly":"pnpm run build"},"_npmUser":{"name":"justin-schroeder","email":"justin@formkit.com"},"repository":{"url":"git+https://github.com/justinschroeder/zodown.git","type":"git"},"_npmVersion":"10.9.2","description":"Write Zod v4 schemas, run them in Zod v3 environments","directories":{},"_nodeVersion":"23.11.0","dependencies":{"zod":"^4.0.0"},"_hasShrinkwrap":false,"packageManager":"pnpm@10.14.0+sha512.ad27a79641b49c3e481a16a805baa71817a04bbe06a38d17e60e2eaee83f6a146c6a688125f5792e48dd5ba30e7da52a5cda4c3992b9ccf333f9ce223af84748","devDependencies":{"tsup":"^8.0.1","bumpp":"^9.2.1","vitest":"^1.2.1","prettier":"^3.2.4","typescript":"^5.3.3","@types/node":"^20.11.5","@vitest/coverage-v8":"^1.2.1"},"_npmOperationalInternal":{"tmp":"tmp/zodown_0.2.0_1757023754545_0.13151793347216767","host":"s3://npm-registry-packages-npm-production"}},"0.2.1":{"name":"zodown","version":"0.2.1","description":"Write Zod v4 schemas, run them in Zod v3 environments","author":{"name":"Justin Schroeder"},"license":"MIT","homepage":"https://github.com/formkit/zodown","repository":{"type":"git","url":"git+https://github.com/formkit/zodown.git"},"bugs":{"url":"https://github.com/formkit/zodown/issues"},"keywords":["zod","schema","validation","typescript","converter","compatibility"],"main":"./dist/index.js","module":"./dist/index.mjs","types":"./dist/index.d.ts","exports":{".":{"types":"./dist/index.d.ts","import":"./dist/index.mjs","require":"./dist/index.js"}},"scripts":{"build":"tsup","dev":"tsup --watch","test":"vitest","test:run":"vitest run","test:coverage":"vitest run --coverage","lint":"prettier --check .","format":"prettier --write .","typecheck":"tsc --noEmit","release":"pnpm test:run && pnpm build && bumpp && npm publish --access public","prepublishOnly":"pnpm run build","docs:dev":"pnpm --filter docs dev","docs:build":"pnpm --filter docs build","docs:preview":"pnpm --filter docs preview"},"dependencies":{"zod":"^4.0.0"},"devDependencies":{"@types/node":"^20.11.5","@vitest/coverage-v8":"^1.2.1","bumpp":"^9.2.1","prettier":"^3.2.4","tsup":"^8.0.1","typescript":"^5.3.3","vitest":"^1.2.1"},"engines":{"node":">=16"},"packageManager":"pnpm@10.14.0+sha512.ad27a79641b49c3e481a16a805baa71817a04bbe06a38d17e60e2eaee83f6a146c6a688125f5792e48dd5ba30e7da52a5cda4c3992b9ccf333f9ce223af84748","_id":"zodown@0.2.1","gitHead":"02c71a4726075338fb306cd2ffd8bbc545cb8522","_nodeVersion":"23.11.0","_npmVersion":"10.9.2","dist":{"integrity":"sha512-/oIwIaEphCYkNCaCL/SVVNVtqqiMGYD/wLyhCG1cFPA9kzi/H0/uJwp7CzgYPZ9gphBdkIMiDE5Q9NPmUorBEg==","shasum":"6c618d8f4b92f5b021f99efc2342b022eaefb791","tarball":"https://registry.npmjs.org/zodown/-/zodown-0.2.1.tgz","fileCount":9,"unpackedSize":131528,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEUCIQCPuL/ase2AF/mK/M2SVSkSX6WPahNzru91SCUBDAOzLwIgKXbOmFqgc0NNHLJK1BrUPf76bY8dXBwT1TxG7de80Nc="}]},"_npmUser":{"name":"justin-schroeder","email":"justin@formkit.com"},"directories":{},"maintainers":[{"name":"justin-schroeder","email":"justin@formkit.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/zodown_0.2.1_1757023878666_0.9543184061065626"},"_hasShrinkwrap":false}},"time":{"created":"2025-09-04T22:09:14.403Z","modified":"2025-09-04T22:11:19.022Z","0.2.0":"2025-09-04T22:09:14.735Z","0.2.1":"2025-09-04T22:11:18.847Z"},"bugs":{"url":"https://github.com/formkit/zodown/issues"},"author":{"name":"Justin Schroeder"},"license":"MIT","homepage":"https://github.com/formkit/zodown","keywords":["zod","schema","validation","typescript","converter","compatibility"],"repository":{"type":"git","url":"git+https://github.com/formkit/zodown.git"},"description":"Write Zod v4 schemas, run them in Zod v3 environments","maintainers":[{"name":"justin-schroeder","email":"justin@formkit.com"}],"readme":"# zodown\n\n<p align=\"center\">\n  <a href=\"https://www.npmjs.com/package/zodown\"><img src=\"https://img.shields.io/npm/v/zodown.svg?style=flat-square\" alt=\"npm version\"></a>\n  <a href=\"https://github.com/formkit/zodown/actions\"><img src=\"https://img.shields.io/github/actions/workflow/status/formkit/zodown/ci.yml?branch=main&style=flat-square\" alt=\"build status\"></a>\n  <a href=\"https://www.npmjs.com/package/zodown\"><img src=\"https://img.shields.io/npm/dm/zodown.svg?style=flat-square\" alt=\"npm downloads\"></a>\n  <a href=\"https://github.com/formkit/zodown/blob/main/LICENSE\"><img src=\"https://img.shields.io/npm/l/zodown.svg?style=flat-square\" alt=\"license\"></a>\n</p>\n\n> Write Zod v4 schemas, run them in Zod v3 environments\n\n## The Problem\n\nMany popular libraries still depend on Zod v3 (like `@modelcontextprotocol/sdk`), but you want to use the latest Zod v4 features and stay current with the ecosystem. Version conflicts force you to choose between modern schemas and library compatibility.\n\n## The Solution\n\n`zodown` converts Zod v4 schemas to functionally equivalent Zod v3 schemas at runtime, preserving all validations, refinements, and type safety. Write modern code, maintain compatibility.\n\n## Installation\n\n```bash\nnpm install zodown\n# or\npnpm add zodown\n# or\nyarn add zodown\n```\n\n**Note:** zodown includes Zod v4 built-in and handles Zod v3 conversion internally. You don't need to install Zod separately!\n\n## Usage\n\n```typescript\nimport { z, zodown } from 'zodown' // Includes Zod v4!\n\n// Write modern Zod v4 schemas\nconst UserSchema = z.object({\n  email: z.string().email(),\n  age: z.number().int().positive(),\n  role: z.enum(['admin', 'user']),\n  metadata: z.record(z.unknown()).optional(),\n})\n\n// Convert for Zod v3 compatibility\nconst v3Schema = zodown(UserSchema)\n\n// Use with libraries that require Zod v3\nimport { Client } from '@modelcontextprotocol/sdk'\nconst client = new Client({\n  schema: v3Schema, // Works perfectly!\n})\n```\n\n## Features\n\n✅ **Complete Type Support** - All Zod types including primitives, objects, arrays, unions, intersections, and more  \n✅ **Validation Preservation** - All refinements, transforms, and custom validations are maintained  \n✅ **Type Safety** - Full TypeScript support with proper type inference  \n✅ **Circular References** - Handles recursive schemas with WeakMap caching  \n✅ **Zero Configuration** - Just wrap your schema with `zodown()`  \n✅ **Lightweight** - ~1.3KB gzipped converter logic\n\n## Supported Types\n\n`zodown` supports all Zod v4 types:\n\n- **Primitives**: string, number, boolean, date, bigint, symbol, undefined, null, void, any, unknown, never\n- **Literals**: literal values, enums, native enums\n- **Strings**: with all refinements (email, url, uuid, regex, length constraints, etc.)\n- **Numbers**: with all refinements (int, positive, min/max, multipleOf, finite)\n- **Complex Types**: objects, arrays, tuples, records, maps, sets\n- **Compositions**: unions, intersections, optional, nullable, default\n- **Advanced**: lazy (recursive), promise, function, transform, refine, preprocess\n- **Effects**: Custom refinements and transformations\n\n## Examples\n\n### Basic Schema\n\n```typescript\nimport { z, zodown } from 'zodown'\n\nconst ProductSchema = z.object({\n  id: z.string().uuid(),\n  name: z.string().min(1).max(100),\n  price: z.number().positive(),\n  tags: z.array(z.string()),\n})\n\nconst v3Product = zodown(ProductSchema)\n```\n\n### With Refinements\n\n```typescript\nimport { z, zodown } from 'zodown'\n\nconst PasswordSchema = z\n  .string()\n  .min(8)\n  .regex(/[A-Z]/, 'Must contain uppercase')\n  .regex(/[0-9]/, 'Must contain number')\n  .refine((val) => !commonPasswords.includes(val), 'Too common')\n\nconst v3Password = zodown(PasswordSchema)\n```\n\n### Recursive Schema\n\n```typescript\nimport { z, zodown, ZodType } from 'zodown'\n\nconst CategorySchema: ZodType<Category> = z.lazy(() =>\n  z.object({\n    name: z.string(),\n    subcategories: z.array(CategorySchema).optional(),\n  })\n)\n\nconst v3Category = zodown(CategorySchema)\n```\n\n### With Transform\n\n```typescript\nimport { z, zodown } from 'zodown'\n\nconst DateSchema = z\n  .string()\n  .transform((str) => new Date(str))\n  .refine((date) => !isNaN(date.getTime()), 'Invalid date')\n\nconst v3Date = zodown(DateSchema)\n```\n\n## API\n\n### `zodown(schema)`\n\nConverts a Zod v4 schema to a Zod v3 schema.\n\n- **Parameters**: `schema` - Any Zod v4 schema\n- **Returns**: Equivalent Zod v3 schema\n- **Type Safety**: Preserves full type inference\n\n### `InferDowngraded<T>`\n\nType helper for extracting the inferred type from a downgraded schema.\n\n```typescript\nimport type { InferDowngraded } from 'zodown'\n\nconst Schema = z.object({ name: z.string() })\ntype User = InferDowngraded<typeof Schema> // { name: string }\n```\n\n## Limitations\n\nWhile `zodown` handles most Zod v4 features, there are some limitations due to architectural differences between v3 and v4:\n\n### Features That Cannot Be Converted\n\n1. **`.refine()` and `.superRefine()` on base types** - In Zod v4, these are compiled into internal check functions that cannot be reverse-engineered. The base schema is returned without the refinement.\n\n2. **Branded types** - Zod v4 implements branding as a compile-time TypeScript feature with no runtime representation, while v3 expects runtime `ZodBranded` instances. Branded schemas are returned as their base type.\n\n3. **Variable-length tuples with optional elements** - Zod v4 allows tuples like `[string, number?]` to have variable length, but v3 requires fixed length with `undefined` for optional elements.\n\n### Workarounds\n\nIf you need these features, consider:\n\n- Using transforms instead of refinements where possible\n- Implementing validation logic outside of Zod\n- Using v3-compatible patterns from the start\n\n### Example of Limitations\n\n```typescript\n// These v4 features have limitations:\n\n// ❌ Refinements are lost\nconst v4Refined = z.number().refine((n) => n % 2 === 0)\nconst v3Refined = zodown(v4Refined) // Returns ZodNumber without refinement\n\n// ❌ Brands become base types\nconst v4Branded = z.string().brand<'UserId'>()\nconst v3Branded = zodown(v4Branded) // Returns ZodString, not ZodBranded\n\n// ❌ Optional tuple elements need all positions\nconst v4Tuple = z.tuple([z.string(), z.number().optional()])\nconst v3Tuple = zodown(v4Tuple)\nv3Tuple.parse(['hi', undefined]) // ✅ Works\nv3Tuple.parse(['hi']) // ❌ Fails - v3 needs fixed length\n```\n\n## Use Cases\n\n- 📦 Using `@modelcontextprotocol/sdk` with modern Zod schemas\n- 🔄 Gradually migrating large codebases from Zod v3 to v4\n- 📚 Maintaining libraries that need to support both versions\n- 🛠️ Working with tools that haven't updated to Zod v4 yet\n\n## Contributing\n\nContributions are welcome! Please feel free to submit a Pull Request. See [CONTRIBUTING.md](./CONTRIBUTING.md) for details.\n\n## License\n\nMIT © Justin Schroeder\n\n## Acknowledgments\n\nBuilt with necessity for the [Model Context Protocol SDK](https://github.com/anthropics/model-context-protocol) and the many other libraries still on Zod v3.\n","readmeFilename":"README.md"}