
Product
Introducing Repository Access Permissions and Custom Roles
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.
openapi-to-zod-schema
Advanced tools
This package provides a CLI and library to convert OpenAPI schemas to Zod schemas and generate schema code.
This package provides a CLI and library to convert OpenAPI schemas to Zod schemas and generate schema code.
allOf, oneOf, and anyOf$ref) in OpenAPI schemasYou can install the package from NPM:
# install
npm i -D openapi-to-zod-schema
# or run it directly
npx openapi-to-zod-schema https://raw.githubusercontent.com/openai/openai-openapi/refs/heads/manual_spec/openapi.yaml
npx openapi-to-zod-schema .openapi.yaml -o openai-schemas.ts
You can use the library in your project like this:
import { convertOpenAPISpecToZodSchemas, codegen } from "openapi-to-zod-schema";
const openAPISpec = {
components: {
// Your OpenAPI spec's components here
},
};
const zodSchemas = convertOpenAPISpecToZodSchemas(openAPISpec);
console.log(zodSchemas); // { items: [], map: Record<name, {}> }
// Generate TypeScript code
const code = codegen(openAPISpec);
console.log(code);
You can use the CLI wrapper to convert OpenAPI schemas to Zod schemas like this:
npx openapi-to-zod-schema ./path/to/your/openapi-spec.yaml
Or for remote files:
npx openapi-to-zod-schema https://example.com/path/to/openapi-spec.yaml
By default, the CLI tool will output the generated Zod schema to the console. If you want to save it to a file, you can use the -o option:
npx openapi-to-zod-schema ./path/to/your/openapi-spec.yaml -o ./output-schema.ts
convertOpenAPISpecToZodSchemas(spec: OpenAPISpec): { map: Record<string, z.ZodTypeAny>, items: Array<{ name: string, schema: z.ZodTypeAny }> }Converts an OpenAPI specification to Zod schemas.
spec: The OpenAPI specification object.map: A record of schema names to their corresponding Zod schemas.items: An array of objects, each containing the schema name and the Zod schema.Example:
const { map, items } = convertOpenAPISpecToZodSchemas(openAPISpec);
console.log(map.UserSchema); // Zod schema for User
console.log(items[0]); // { name: 'User', schema: ZodSchema }
codegen(spec: OpenAPISpec): stringGenerates TypeScript code for the Zod schemas based on the OpenAPI specification.
spec: The OpenAPI specification object.Example:
const code = codegen(openAPISpec);
console.log(code);
// Output: TypeScript code defining Zod schemas
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License.
FAQs
This package provides a CLI and library to convert OpenAPI schemas to Zod schemas and generate schema code.
We found that openapi-to-zod-schema demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.

Product
Socket Firewall blocks malicious VS Code and Open VSX extensions before install, protecting developers from compromised editor marketplaces.