Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@anatine/zod-openapi

Package Overview
Dependencies
Maintainers
1
Versions
45
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@anatine/zod-openapi

Zod to OpenAPI converter

  • 1.12.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
151K
increased by9.13%
Maintainers
1
Weekly downloads
 
Created

What is @anatine/zod-openapi?

@anatine/zod-openapi is an npm package that bridges the gap between Zod, a TypeScript-first schema declaration and validation library, and OpenAPI, a standard for defining RESTful APIs. This package allows developers to generate OpenAPI documentation from Zod schemas, making it easier to maintain API documentation in sync with the actual code.

What are @anatine/zod-openapi's main functionalities?

Generate OpenAPI Schema from Zod Schema

This feature allows you to generate an OpenAPI schema from a Zod schema. The code sample demonstrates how to define a Zod schema for a User object and then convert it into an OpenAPI schema.

const { z } = require('zod');
const { openApi } = require('@anatine/zod-openapi');

const UserSchema = z.object({
  id: z.string(),
  name: z.string(),
  email: z.string().email()
});

const openApiSchema = openApi({
  title: 'User API',
  version: '1.0.0',
  schemas: {
    User: UserSchema
  }
});

console.log(JSON.stringify(openApiSchema, null, 2));

Add Metadata to Zod Schemas

This feature allows you to add OpenAPI metadata directly to Zod schemas. The code sample shows how to extend Zod with OpenAPI capabilities and add descriptions to the fields in a User schema.

const { z } = require('zod');
const { extendZodWithOpenApi } = require('@anatine/zod-openapi');

extendZodWithOpenApi(z);

const UserSchema = z.object({
  id: z.string().openapi({ description: 'User ID' }),
  name: z.string().openapi({ description: 'User Name' }),
  email: z.string().email().openapi({ description: 'User Email' })
});

console.log(UserSchema.openapi);

Generate OpenAPI Documentation

This feature allows you to generate a full OpenAPI documentation from Zod schemas. The code sample demonstrates how to define a Zod schema, convert it to an OpenAPI schema, and then generate the full OpenAPI documentation.

const { z } = require('zod');
const { openApi, generateOpenApiDocument } = require('@anatine/zod-openapi');

const UserSchema = z.object({
  id: z.string(),
  name: z.string(),
  email: z.string().email()
});

const openApiSchema = openApi({
  title: 'User API',
  version: '1.0.0',
  schemas: {
    User: UserSchema
  }
});

const openApiDocument = generateOpenApiDocument(openApiSchema);

console.log(JSON.stringify(openApiDocument, null, 2));

Other packages similar to @anatine/zod-openapi

Keywords

FAQs

Package last updated on 17 Jan 2023

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc