🚀 Socket Launch Week Day 5:Introducing Repository Access Permissions and Custom Roles.Learn more
Sign In

openapi-to-zod-schema

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openapi-to-zod-schema

This package provides a CLI and library to convert OpenAPI schemas to Zod schemas and generate schema code.

latest
Source
npmnpm
Version
1.3.1
Version published
Maintainers
1
Created
Source

OpenAPI to Zod Schema

This package provides a CLI and library to convert OpenAPI schemas to Zod schemas and generate schema code.

Features

  • Convert OpenAPI schemas (YAML or JSON) to Zod schemas
  • Support for complex schema structures including allOf, oneOf, and anyOf
  • Handle references ($ref) in OpenAPI schemas
  • CLI tool for easy conversion from the command line
  • Support for both local files and remote URLs as input

Installation

You 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

Usage

As a Library

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);

As a CLI Tool

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

API Reference

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.
  • Returns: An object containing:
    • 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): string

Generates TypeScript code for the Zod schemas based on the OpenAPI specification.

  • spec: The OpenAPI specification object.
  • Returns: A string containing the generated TypeScript code.

Example:

const code = codegen(openAPISpec);
console.log(code);
// Output: TypeScript code defining Zod schemas

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License.

FAQs

Package last updated on 23 Aug 2025

Did you know?

Socket

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.

Install

Related posts