https://github.com/3rd/openapi-to-zod-schema
https://github.com/3rd/openapi-to-zod-schema
Last synced: 3 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/3rd/openapi-to-zod-schema
- Owner: 3rd
- License: mit
- Created: 2024-06-23T21:47:06.000Z (almost 2 years ago)
- Default Branch: master
- Last Pushed: 2024-06-26T21:30:39.000Z (almost 2 years ago)
- Last Synced: 2025-05-17T07:09:11.875Z (12 months ago)
- Language: TypeScript
- Size: 104 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# 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](https://www.npmjs.com/openapi-to-zod-schema):
```bash
# 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:
```typescript
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 }
// 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:
```bash
npx openapi-to-zod-schema ./path/to/your/openapi-spec.yaml
```
Or for remote files:
```bash
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:
```bash
npx openapi-to-zod-schema ./path/to/your/openapi-spec.yaml -o ./output-schema.ts
```
## API Reference
### `convertOpenAPISpecToZodSchemas(spec: OpenAPISpec): { map: Record, 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:
```typescript
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:
```typescript
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.