# @ibabkin/openapi-to-zod

> Generates zod from openapi

Latest version **1.2.15** (published 2024-03-31) · ISC license · 0 weekly downloads

## Install

```sh
npm install @ibabkin/openapi-to-zod
pnpm add @ibabkin/openapi-to-zod
yarn add @ibabkin/openapi-to-zod
bun add @ibabkin/openapi-to-zod
```

Provides the command `openapi-to-zod`.

## Health

**Score 40/100 (D)** — status: abandoned.

Positive: has types; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 1.2.15 |
| Published | 2024-03-31 |
| First published | 2023-06-13 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 4 |
| Unpacked size | 28.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 17 |
| Author | ibabkin |
| Maintainers | igorbabkin |
| Keywords | openapi, swagger, zod, typescript |

## Links

- npm: https://www.npmjs.com/package/@ibabkin/openapi-to-zod
- Repository: https://github.com/IgorBabkin/ts-ioc-container
- Homepage: https://github.com/IgorBabkin/ts-ioc-container/tree/master/packages/openapi-to-zod
- Issues: https://github.com/IgorBabkin/ts-ioc-container/issues
- npm.io page: https://npm.io/package/@ibabkin/openapi-to-zod

## Dependencies (4)

- [args](https://npm.io/package/args.md) ^5.0.3
- [js-yaml](https://npm.io/package/js-yaml.md) ^4.1.0
- [handlebars](https://npm.io/package/handlebars.md) ^4.7.7
- [yaml-import](https://npm.io/package/yaml-import.md) ^2.0.0

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.2.15 (latest) — 2024-03-31
- 1.2.14 — 2024-03-28
- 1.2.13 — 2023-09-09
- 1.2.12 — 2023-08-26
- 1.2.11 — 2023-07-16
- 1.2.10 — 2023-07-09
- 1.2.9 — 2023-06-19
- 1.2.8 — 2023-06-18
- 1.2.7 — 2023-06-18
- 1.2.6 — 2023-06-18
- 1.2.5 — 2023-06-18
- 1.2.4 — 2023-06-18
- 1.2.3 — 2023-06-17
- 1.2.2 — 2023-06-17
- 1.2.1 — 2023-06-17
- … 12 more at https://npm.io/package/@ibabkin/openapi-to-zod/versions

## README

![NPM version:latest](https://img.shields.io/npm/v/@ibabkin/openapi-to-zod/latest.svg?style=flat-square)
![npm downloads](https://img.shields.io/npm/dt/@ibabkin/openapi-to-zod.svg?style=flat-square)
[![Coverage Status](https://coveralls.io/repos/github/IgorBabkin/ts-ioc-container/badge.svg?branch=master)](https://coveralls.io/github/IgorBabkin/ts-ioc-container?branch=master)
![License](https://img.shields.io/npm/l/@ibabkin/openapi-to-zod)

## Description
Generates validators for operation payloads from OpenAPI 3.0 specification using Zod.

## Usage

- supported version of OpenAPI: 3.0.0
- supports `yaml-import`

```bash
openapi-to-zod --input ./swagger.yaml --output ./validators.ts
```

```typescript
import { renderDocument } from '@ibabkin/openapi-to-zod';
fs.writeFileSync('.generated/output.ts', renderDocument(api as OpenAPIV3.Document));
```

Input example:

```yaml
openapi: 3.0.0
info:
  title: Simple Inventory API
  description: This is a simple API
  contact:
    email: you@your-company.com
  license:
    name: Apache 2.0
    url: http://www.apache.org/licenses/LICENSE-2.0.html
  version: 1.0.0
servers:
  - url: https://virtserver.swaggerhub.com/DOSAKANTIVENKATESH/smp/1.0.0
    description: SwaggerHub API Auto Mocking
tags:
  - name: admins
    description: Secured Admin-only calls
  - name: developers
    description: Operations available to regular developers
paths:
  !!import/merge
    - paths.yaml

components:
  schemas:
    !!import/merge
      - components.yaml
```

Output example:

```typescript
import { z } from "zod";

const zNumber = z.string().regex(/^\d+$/).transform(Number);

const zDate = z.preprocess((arg) => {
  if (typeof arg === "string" || typeof arg === "number") {
    return new Date(arg);
  }

  return arg;
}, z.date());

export const PAYLOADS = {
  searchInventory: z.object({
    query: z.object({
      searchString: z.string().optional(),
      skip: zNumber,
      limit: zNumber
    })
  }),
  addInventory: z.object({
    body: z.object({
      id: z.string(),
      name: z.string(),
      releaseDate: zDate
    })
  })
};
```

---
_Source: https://npm.io/package/@ibabkin/openapi-to-zod · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
