Registry / devops / openapi-to-zod-schema

openapi-to-zod-schema

JSON →
library1.3.1jsnpmunverified

Converts OpenAPI specifications (YAML or JSON) to Zod schemas with code generation. v1.3.1 supports complex structures (allOf, oneOf, anyOf), $ref resolution, and a CLI for local/remote files. Differentiators: dual library/CLI, TypeScript-first output, and handles advanced OpenAPI patterns. Peer dependency on zod ^3.21.4.

npm install openapi-to-zod-schema
INSTALL
IMPORT
SIG · OPENAPI-TO-ZOD-SCH
O
openapi-to-zod-schema
devopsjavascriptv1.3.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

convertOpenAPISpecToZodSchemas
import { convertOpenAPISpecToZodSchemas } from 'openapi-to-zod-schema'
const convertOpenAPISpecToZodSchemas = require('openapi-to-zod-schema')
ESM only; CJS require would fail (package.json type: module)
codegen
import { codegen } from 'openapi-to-zod-schema'
import codegen from 'openapi-to-zod-schema'
Named export, not default.

Demonstrates converting an OpenAPI spec with a User schema to Zod schemas and generating TypeScript code.

import { convertOpenAPISpecToZodSchemas, codegen } from 'openapi-to-zod-schema'; const openAPISpec = { openapi: '3.0.0', info: { title: 'Example', version: '1.0.0' }, components: { schemas: { User: { type: 'object', properties: { id: { type: 'integer' }, name: { type: 'string' }, email: { type: 'string', format: 'email' } }, required: ['id', 'name'] } } } }; const { map, items } = convertOpenAPISpecToZodSchemas(openAPISpec); console.log(map.User); const code = codegen(openAPISpec); console.log(code);
Debug
Known issues
breakingPackage is ESM-only (type: module). Cannot require() in CommonJS projects without dynamic import.
fix
Use import syntax or dynamic import() in CJS.
affects: >=1.1.0
gotchaconvertOpenAPISpecToZodSchemas returns { map: Record<string, z.ZodTypeAny>, items: ... }. Properties may be undefined if spec is malformed.
fix
Validate spec before passing; handle undefined schemas in map.
affects: all
deprecatedCLI method naming may change in future; check latest docs.
fix
Prefer using codegen programmatically.
affects: >=1.2.0
gotcha$ref resolution only works within the same spec; external $ref not supported.
fix
Resolve references manually before passing to convertOpenAPISpecToZodSchemas.
affects: all
Errors
Common errors & fixes
ERR_REQUIRE_ESM: require() of ES Module
Using require('openapi-to-zod-schema') in a CommonJS file.
fix
Switch to import { ... } from 'openapi-to-zod-schema' or use dynamic import().
TypeError: Cannot read properties of undefined (reading 'properties')
Passing an incomplete or malformed OpenAPI spec object.
fix
Ensure spec has valid 'components.schemas' and is a proper OpenAPI document.
error: unknown option '-o'
Older version of CLI or using -o before other flags.
fix
Use 'npx openapi-to-zod-schema input.yaml -o output.ts' with correct order.
Upgrade
Version history
1.3.1latest on npm
Audit
Dependencies
zodrequiredRequired runtime peer dependency for schema definition types
Agent activity
7 hits · last 30 days
node
6
Resources
openapi-to-zod-schema — npm install openapi-to-zod-schema · libregistry