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

@asteasolutions/zod-to-openapi

Package Overview
Dependencies
Maintainers
3
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@asteasolutions/zod-to-openapi

Builds OpenAPI schemas from Zod schemas

  • 6.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
110K
decreased by-56.56%
Maintainers
3
Weekly downloads
 
Created

What is @asteasolutions/zod-to-openapi?

@asteasolutions/zod-to-openapi is an npm package that allows you to convert Zod schemas to OpenAPI (Swagger) definitions. This is particularly useful for generating API documentation from your Zod validation schemas, ensuring that your API documentation stays in sync with your validation logic.

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

Convert Zod Schema to OpenAPI Schema

This feature allows you to convert a Zod schema into an OpenAPI 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('@asteasolutions/zod-to-openapi');

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

const openApiSchema = openApi({
  title: 'User',
  version: '1.0.0',
  schema: userSchema
});

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

Generate OpenAPI Documentation

This feature allows you to generate a complete OpenAPI document from Zod schemas. The code sample demonstrates how to define a Zod schema for a user object, convert it into an OpenAPI schema, and then generate an OpenAPI document that includes an endpoint for retrieving users.

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

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

const openApiSchema = openApi({
  title: 'User',
  version: '1.0.0',
  schema: userSchema
});

const openApiDocument = generateOpenApiDocument({
  openapi: '3.0.0',
  info: {
    title: 'My API',
    version: '1.0.0'
  },
  paths: {
    '/users': {
      get: {
        summary: 'Get Users',
        responses: {
          '200': {
            description: 'A list of users',
            content: {
              'application/json': {
                schema: openApiSchema
              }
            }
          }
        }
      }
    }
  }
});

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

Other packages similar to @asteasolutions/zod-to-openapi

Keywords

FAQs

Package last updated on 01 Mar 2024

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