An open API service indexing awesome lists of open source software.

https://github.com/omermecitoglu/json-schema-to-zod

Zod schema generator from JSON schema specs
https://github.com/omermecitoglu/json-schema-to-zod

generator json-schema zod

Last synced: 6 months ago
JSON representation

Zod schema generator from JSON schema specs

Awesome Lists containing this project

README

          

# json-schema-to-zod

[![npm version](https://img.shields.io/npm/v/@omer-x/json-schema-to-zod?logo=npm&logoColor=CB3837&color=CB3837)](https://www.npmjs.com/package/@omer-x/json-schema-to-zod)
[![npm downloads](https://img.shields.io/npm/dm/@omer-x/json-schema-to-zod?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1MTIgNTEyIj48IS0tIUZvbnQgQXdlc29tZSBGcmVlIDYuNi4wIGJ5IEBmb250YXdlc29tZSAtIGh0dHBzOi8vZm9udGF3ZXNvbWUuY29tIExpY2Vuc2UgLSBodHRwczovL2ZvbnRhd2Vzb21lLmNvbS9saWNlbnNlL2ZyZWUgQ29weXJpZ2h0IDIwMjQgRm9udGljb25zLCBJbmMuLS0+PHBhdGggZD0iTTI4OCAzMmMwLTE3LjctMTQuMy0zMi0zMi0zMnMtMzIgMTQuMy0zMiAzMmwwIDI0Mi43LTczLjQtNzMuNGMtMTIuNS0xMi41LTMyLjgtMTIuNS00NS4zIDBzLTEyLjUgMzIuOCAwIDQ1LjNsMTI4IDEyOGMxMi41IDEyLjUgMzIuOCAxMi41IDQ1LjMgMGwxMjgtMTI4YzEyLjUtMTIuNSAxMi41LTMyLjggMC00NS4zcy0zMi44LTEyLjUtNDUuMyAwTDI4OCAyNzQuNyAyODggMzJ6TTY0IDM1MmMtMzUuMyAwLTY0IDI4LjctNjQgNjRsMCAzMmMwIDM1LjMgMjguNyA2NCA2NCA2NGwzODQgMGMzNS4zIDAgNjQtMjguNyA2NC02NGwwLTMyYzAtMzUuMy0yOC43LTY0LTY0LTY0bC0xMDEuNSAwLTQ1LjMgNDUuM2MtMjUgMjUtNjUuNSAyNS05MC41IDBMMTY1LjUgMzUyIDY0IDM1MnptMzY4IDU2YTI0IDI0IDAgMSAxIDAgNDggMjQgMjQgMCAxIDEgMC00OHoiIGZpbGw9IiMwMDc4MjAiIC8+PC9zdmc+&color=007820)](https://www.npmjs.com/package/@omer-x/json-schema-to-zod)
[![codecov](https://codecov.io/gh/omermecitoglu/json-schema-to-zod/branch/main/graph/badge.svg)](https://codecov.io/gh/omermecitoglu/json-schema-to-zod)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg?logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2NDAgNTEyIj48IS0tIUZvbnQgQXdlc29tZSBGcmVlIDYuNi4wIGJ5IEBmb250YXdlc29tZSAtIGh0dHBzOi8vZm9udGF3ZXNvbWUuY29tIExpY2Vuc2UgLSBodHRwczovL2ZvbnRhd2Vzb21lLmNvbS9saWNlbnNlL2ZyZWUgQ29weXJpZ2h0IDIwMjQgRm9udGljb25zLCBJbmMuLS0+PHBhdGggZD0iTTM4NCAzMmwxMjggMGMxNy43IDAgMzIgMTQuMyAzMiAzMnMtMTQuMyAzMi0zMiAzMkwzOTguNCA5NmMtNS4yIDI1LjgtMjIuOSA0Ny4xLTQ2LjQgNTcuM0wzNTIgNDQ4bDE2MCAwYzE3LjcgMCAzMiAxNC4zIDMyIDMycy0xNC4zIDMyLTMyIDMybC0xOTIgMC0xOTIgMGMtMTcuNyAwLTMyLTE0LjMtMzItMzJzMTQuMy0zMiAzMi0zMmwxNjAgMCAwLTI5NC43Yy0yMy41LTEwLjMtNDEuMi0zMS42LTQ2LjQtNTcuM0wxMjggOTZjLTE3LjcgMC0zMi0xNC4zLTMyLTMyczE0LjMtMzIgMzItMzJsMTI4IDBjMTQuNi0xOS40IDM3LjgtMzIgNjQtMzJzNDkuNCAxMi42IDY0IDMyem01NS42IDI4OGwxNDQuOSAwTDUxMiAxOTUuOCA0MzkuNiAzMjB6TTUxMiA0MTZjLTYyLjkgMC0xMTUuMi0zNC0xMjYtNzguOWMtMi42LTExIDEtMjIuMyA2LjctMzIuMWw5NS4yLTE2My4yYzUtOC42IDE0LjItMTMuOCAyNC4xLTEzLjhzMTkuMSA1LjMgMjQuMSAxMy44bDk1LjIgMTYzLjJjNS43IDkuOCA5LjMgMjEuMSA2LjcgMzIuMUM2MjcuMiAzODIgNTc0LjkgNDE2IDUxMiA0MTZ6TTEyNi44IDE5NS44TDU0LjQgMzIwbDE0NC45IDBMMTI2LjggMTk1Ljh6TS45IDMzNy4xYy0yLjYtMTEgMS0yMi4zIDYuNy0zMi4xbDk1LjItMTYzLjJjNS04LjYgMTQuMi0xMy44IDI0LjEtMTMuOHMxOS4xIDUuMyAyNC4xIDEzLjhsOTUuMiAxNjMuMmM1LjcgOS44IDkuMyAyMS4xIDYuNyAzMi4xQzI0MiAzODIgMTg5LjcgNDE2IDEyNi44IDQxNlMxMS43IDM4MiAuOSAzMzcuMXoiIGZpbGw9IiNEMEE4MUMiIC8+PC9zdmc+)](https://opensource.org/licenses/MIT)
[![GitHub last commit](https://img.shields.io/github/last-commit/omermecitoglu/json-schema-to-zod?color=c977be&logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA2NDAgNTEyIj48IS0tIUZvbnQgQXdlc29tZSBGcmVlIDYuNi4wIGJ5IEBmb250YXdlc29tZSAtIGh0dHBzOi8vZm9udGF3ZXNvbWUuY29tIExpY2Vuc2UgLSBodHRwczovL2ZvbnRhd2Vzb21lLmNvbS9saWNlbnNlL2ZyZWUgQ29weXJpZ2h0IDIwMjQgRm9udGljb25zLCBJbmMuLS0+PHBhdGggZD0iTTMyMCAzMzZhODAgODAgMCAxIDAgMC0xNjAgODAgODAgMCAxIDAgMCAxNjB6bTE1Ni44LTQ4QzQ2MiAzNjEgMzk3LjQgNDE2IDMyMCA0MTZzLTE0Mi01NS0xNTYuOC0xMjhMMzIgMjg4Yy0xNy43IDAtMzItMTQuMy0zMi0zMnMxNC4zLTMyIDMyLTMybDEzMS4yIDBDMTc4IDE1MSAyNDIuNiA5NiAzMjAgOTZzMTQyIDU1IDE1Ni44IDEyOEw2MDggMjI0YzE3LjcgMCAzMiAxNC4zIDMyIDMycy0xNC4zIDMyLTMyIDMybC0xMzEuMiAweiIgZmlsbD0iI0M5NzdCRSIgLz48L3N2Zz4=)](https://github.com/omermecitoglu/json-schema-to-zod/commits/main/)
[![GitHub issues](https://img.shields.io/github/issues/omermecitoglu/json-schema-to-zod?color=a38eed&logo=data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAxNiAxNiIgd2lkdGg9IjE2IiBoZWlnaHQ9IjE2Ij4KICA8cGF0aCBkPSJNOCA5LjVhMS41IDEuNSAwIDEgMCAwLTMgMS41IDEuNSAwIDAgMCAwIDNaIiBmaWxsPSIjQTM4RUVEIj48L3BhdGg+CiAgPHBhdGggZD0iTTggMGE4IDggMCAxIDEgMCAxNkE4IDggMCAwIDEgOCAwWk0xLjUgOGE2LjUgNi41IDAgMSAwIDEzIDAgNi41IDYuNSAwIDAgMC0xMyAwWiIgZmlsbD0iI0EzOEVFRCI+PC9wYXRoPgo8L3N2Zz4=)](https://github.com/omermecitoglu/json-schema-to-zod/issues)
[![GitHub stars](https://img.shields.io/github/stars/omermecitoglu/json-schema-to-zod?style=social)](https://github.com/omermecitoglu/json-schema-to-zod)

## Overview

`json-schema-to-zod` is a TypeScript library that generates Zod schema code from JSON Schema specifications. It supports OpenAPI 3.x schemas and provides both runtime schema generation and TypeScript type definitions.

## Features

- Generate Zod schemas from JSON Schema objects
- Support for OpenAPI 3.x specifications
- Handles complex types: objects, arrays, unions, references
- Type-safe generation with TypeScript
- Lightweight and dependency-free (except peer dependencies)

## Installation

Install the package using npm:

```sh
npm install @omer-x/json-schema-to-zod
```

### Peer Dependencies

This package requires the following peer dependencies:

- `zod`: ^4.0.0
- `@omer-x/openapi-types`: ^1.0.0

Install them if not already present:

```sh
npm install zod @omer-x/openapi-types
```

## Usage

### Basic Example

```typescript
import { generateZodSchema } from '@omer-x/json-schema-to-zod';

const jsonSchema = {
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'integer', minimum: 0 }
},
required: ['name']
};

const result = generateZodSchema(jsonSchema);
console.log(result.body); // "z.object({ name: z.string(), age: z.int().min(0) })"
```

### Using with References

```typescript
import { generateZodSchema } from '@omer-x/json-schema-to-zod';

const schemaWithRef = {
$ref: '#/components/schemas/User'
};

const result = generateZodSchema(schemaWithRef);
console.log(result.body); // User
console.log(result.dependencies); // ['User']
```

### Generating Type Definitions

```typescript
import { generateZodType } from '@omer-x/json-schema-to-zod';

const jsonSchema = { type: 'string' };
const result = generateZodType(jsonSchema);
console.log(result.body); // "z.ZodString"
```

## API Reference

### `generateZodSchema(jsonSchema: SchemaObject | ReferenceObject): Definition`

Generates a Zod schema string from a JSON Schema object.

- **Parameters:**
- `jsonSchema`: A JSON Schema object or reference object
- **Returns:** A `Definition` object with:
- `body`: The generated Zod schema code
- `dependencies`: Array of referenced component names

### `generateZodType(jsonSchema: SchemaObject): Definition`

Generates a Zod type definition string from a JSON Schema object.

- **Parameters:**
- `jsonSchema`: A JSON Schema object
- **Returns:** A `Definition` object with:
- `body`: The generated Zod type code
- `dependencies`: Array of referenced component names

## Supported JSON Schema Features

- Primitive types: `boolean`, `null`, `integer`, `number`, `string`
- Objects with properties and required fields
- Arrays with item schemas
- Unions (`anyOf`, `oneOf`)
- References (`$ref`)
- String formats and constraints
- Numeric constraints (min, max, etc.)

## Contributing

Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

### Development

1. Clone the repository
2. Install dependencies: `npm install`
3. Run tests: `npm test`
4. Build the project: `npm run build`

## License

This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.