Introducing The Sweep: OX’s New Live Security Research Series
Register Now
Breaking: The 2026 Vulnpocalypse Hits PHP: Four Critical RCEs, but How Dangerous Are They?
Read the Report
Are you exposed? Connect your apps in minutes.
Scan your apps free

zod-openapi

Latest version: v6.0.3

Overview

Last scanned
24 Nov 2026
Published
24 Nov 2026
npm package Publisher: samchungy
657 Stars
36 Forks
4 Versions
8.9M Downloads
TypeScript Repo language

Scan summary

zod-openapi was last scanned on 24 Nov 2026 at version 6.0.3. Risk score 0 of 100. No malicious behavior was detected.

  • Releases: 4 versions; the latest was published on 24 Nov 2026.
  • Findings: No rules fired in version 6.0.3.
  • Downloads: 8,904,871 downloads.

Versions

4 versions published
Versions and the rules that fired in each
VersionPublishedStatus
6.0.3 LATEST 24 Nov 2026 No rules fired
6.0.2 31 Aug 2026 No rules fired
6.0.1 13 Aug 2026 No rules fired
0.0.0-fix-types-20260813043632 13 Aug 2026 Not shown

No rules fired in 6.0.3

Findings

Findings for v6.0.3. The score is a capped sum of the weights of the rules that fired — not CVSS, not exploitability, and not a probability. It is not comparable across packages with different scan coverage.

The score describes 6.0.3; other versions have their own rules, not their own score.

README

The publisher’s own README, as collected from the registry.

Installation

Install via npm, yarn, or pnpm:

npm install zod zod-openapi
# or
yarn add zod zod-openapi
# or
pnpm install zod zod-openapi

Usage

.meta()

Use Zod's native .meta() method to add OpenAPI metadata to your schemas. This library leverages Zod's built-in metadata functionality - no monkey patching or additional setup is required. Simply call .meta() on any Zod schema and it accepts an object with the following options:

Note: For full TypeScript support of OpenAPI properties in .meta(), simply import zod-openapi in your project or add /// <reference types="zod-openapi" /> anywhere in your project. This provides compile-time type definitions for properties like id, param, header, etc.

Option Description
id Registers a schema as a reusable OpenAPI component.
header Adds metadata for response headers.
param Adds metadata for request parameters.
override Allows you to override the rendered OpenAPI schema. This takes either an object or a function.
outputId Allows you to set a different ID for the output schema. This is useful when the input and output schemas differ.
unusedIO Allows you to set the io for an unused schema added to the components section. Defaults to output.

You can also set standard OpenAPI properties directly in Zod's native .meta() method, such as:

z.string().meta({
  description: 'A text field',
  example: 'Example value',
});

You can set additional metadata to the rendered schema in a few different ways. Zod's native .meta() method allows you to directly set OpenAPI properties to the schema. However, if you wanted to override any field that Zod generates, this may not work as expected. In this case, you can use the override option to customize the rendered OpenAPI schema.

eg.

z.string().datetime().meta({
  description: 'A date field',
  format: 'MY-FORMAT',
  'x-custom-field': 'custom value',
});

Would render the following OpenAPI schema. Note that format is not overridden with our custom format value.

{
  "type": "string",
  "format": "date-time",
  "description": "A date field",
  "x-custom-field": "custom value"
}

In order to override fields which are generated by Zod or this library, you can use the override option in Zod's .meta() method. This allows you to customize the rendered OpenAPI schema after we have both processed it.

override object

This does a naive merge of the override object with the rendered OpenAPI schema. This is useful for simple overrides where you want to add or modify properties without complex logic.

z.string.datetime().meta({
  description: 'A date field',
  override: {
    format: 'MY-FORMAT',
  },
});
override function

The override function allows you to deeply customize the rendered OpenAPI schema. Eg. if you wanted to render a Zod union as a oneOf instead of anyOf for a single schema, you can do so by using the override function. View the documentation for override in the CreateDocumentOptions section for more information.

z.union([z.string(), z.number()]).meta({
  description: 'A union of string and number',
  override: ({ jsonSchema }) => {
    jsonSchema.anyOf = jsonSchema.oneOf;
    delete jsonSchema.oneOf;
  },
});
createDocument

Generates an OpenAPI documentation object.

import * as z from 'zod/v4';
// or import * as z from 'zod'; if using Zod 4.0.0+

import { createDocument } from 'zod-openapi';

const jobId = z.string().meta({
  description: 'A unique identifier for a job',
  example: '12345',
  id: 'jobId',
});

const title = z.string().meta({
  description: 'Job title',
  example: 'My job',
});

const document = createDocument({
  openapi: '3.1.0',
  info: {
    title: 'My API',
    version: '1.0.0',
  },
  paths: {
    '/jobs/{jobId}': {
      put: {
        requestParams: { path: z.object({ jobId }) },
        requestBody: {
          content: {
            'application/json': { schema: z.object({ title }) },
          },
        },
        responses: {
          '200': {
            description: '200 OK',
            content: {
              'application/json': { schema: z.object({ jobId, title }) },
            },
          },
        },
      },
    },
  },
});
{
  "openapi": "3.1.0",
  "info": {
    "title": "My API",
    "version": "1.0.0"
  },
  "paths": {
    "/jobs/{jobId}": {
      "put": {
        "parameters": [
          {
            "in": "path",
            "name": "jobId",
            "description": "A unique identifier for a job",
            "schema": {
              "$ref": "#/components/schemas/jobId"
            }
          }
        ],
        "requestBody": {
          "content": {
            "application/json": {
              "schema": {
                "type": "object",
                "properties": {
                  "title": {
                    "type": "string",
                    "description": "Job title",
                    "example": "My job"
                  }
                },
                "required": ["title"]
              }
            }
          }
        },
        "responses": {
          "200": {
            "description": "200 OK",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "jobId": {
                      "$ref": "#/components/schemas/jobId"
                    },
                    "title": {
                      "type": "string",
                      "description": "Job title",
                      "example": "My job"
                    }
                  },
                  "required": ["jobId", "title"]
                }
              }
            }
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "jobId": {
        "type": "string",
        "description": "A unique identifier for a job",
        "example": "12345"
      }
    }
  }
}

createDocument takes an optional options argument which can be used to modify how the document is created

createDocument(doc, {
  override: ({ jsonSchema, zodSchema, io }) => {
    const def = zodSchema._zod.def;
    if (def.type === 'date' && io === 'output') {
      jsonSchema.type = 'string';
      jsonSchema.format = 'date-time';
    }
  },
  allowEmptySchema: {
    custom: true,
  },
});
CreateDocumentOptions
Option Type Description
override Function Override rendered schema with a function``
outputIdSuffix string Suffix for output schema IDs when the schema is used in both a request and response. Defaults to ``'Output'`
allowEmptySchema Object Control whether empty schemas are allowed for specific Zod schema types
cycles 'ref' \| 'throw' How to handle cycles in schemas.- 'ref' — Break cycles using $defs- 'throw' — Error on cycles. Defaults to 'ref'
reused 'ref' \| 'inline' How to handle reused schemas.- 'ref' — Reused schemas as references- 'inline' — Inline reused schemas. Defaults to 'inline'
override

The override function allows you to customize the rendered OpenAPI schema. It receives an object with the following properties:

Property Type Description
jsonSchema Object The OpenAPI schema object being generated. You can modify this object to change the rendered schema.
zodSchema ZodType The original Zod schema being converted to OpenAPI. You can use this to access Zod-specific properties.
io 'input' \| 'output' The context in which the schema is being rendered. 'input' for request bodies/params, 'output' for responses.

This allows you to transform the schema based on your own rules or requirements. For example, if you

  1. Wanted to change how dates are rendered in the output context because a typical JSON serializer will transform them to a string
  2. Change all union outputs to be oneOf instead of anyOf
createDocument(doc, {
  override: ({ jsonSchema, zodSchema, io }) => {
    const def = zodSchema._zod.def;
    if (def.type === 'date' && io === 'output') {
      jsonSchema.type = 'string';
      jsonSchema.format = 'date-time';
      return;
    }
    if (def.type === 'union') {
      ctx.jsonSchema.oneOf ??= ctx.jsonSchema.anyOf;
      delete ctx.jsonSchema.anyOf;
      return;
    }
  },
});
outputIdSuffix

The outputIdSuffix option allows you to set a suffix for output schema IDs when the schema is used in both a request and response context. This is useful for distinguishing between input and output schemas.

allowEmptySchema

The allowEmptySchema option allows you to control whether or not this library will throw when it encounters an empty schema. By default this library will not throw if it encounters an z.any() or z.unknown() schema.

This allows you to customize how this library handles unrepresentable Zod schemas.

eg. If you wanted to allow z.custom() to be rendered as an empty schema, you can set the allowEmptySchema option as follows:

createDocument(doc, {
  allowEmptySchema: {
    custom: true, // Allow empty schemas for `z.custom()` in all contexts
    set: { output: true }, // Allow empty schemas for `z.set()` only in an output context
  },
});
cycles and reused

These options are exposed directly from the Zod. Read the documentation for more information on how to use these options.

createSchema

Creates an OpenAPI Schema Object along with any registered components. OpenAPI 3.1.0 Schema Objects are fully compatible with JSON Schema.

import * as z from 'zod/v4';
import { createSchema } from 'zod-openapi';

const jobId = z.string().meta({
  description: 'A unique identifier for a job',
  example: '12345',
  id: 'jobId',
});

const title = z.string().meta({
  description: 'Job title',
  example: 'My job',
});

const job = z.object({
  jobId,
  title,
});

const { schema, components } = createSchema(job);
{
  "schema": {
    "type": "object",
    "properties": {
      "jobId": {
        "$ref": "#/components/schemas/jobId"
      },
      "title": {
        "type": "string",
        "description": "Job title",
        "example": "My job"
      }
    },
    "required": ["jobId", "title"]
  },
  "components": {
    "jobId": {
      "type": "string",
      "description": "A unique identifier for a job",
      "example": "12345"
    }
  }
}
CreateSchemaOptions

createSchema takes an optional CreateSchemaOptions parameter which includes all options from CreateDocumentOptions plus the following:

const { schema, components } = createSchema(job, {
  // Input/Output context - controls how schemas are generated
  io: 'input', // 'input' for request bodies/params, 'output' for responses
  // Component handling
  schemaComponents: { jobId: z.string() }, // Pre-defined components to use
  schemaComponentRefPath: '#/definitions/', // Custom path prefix for component references
  opts: {}, // Create Document Options,
});
Request Parameters

Query, Path, Header & Cookie parameters can be created using the requestParams key under the method key as follows:

createDocument({
  paths: {
    '/jobs/{a}': {
      put: {
        requestParams: {
          path: z.object({ a: z.string() }),
          query: z.object({ b: z.string() }),
          cookie: z.object({ cookie: z.string() }),
          header: z.object({ 'custom-header': z.string() }),
        },
      },
    },
  },
});

If you would like to declare parameters in a more traditional way you may also declare them using the parameters key. The definitions will then all be combined.

createDocument({
  paths: {
    '/jobs/{a}': {
      put: {
        parameters: [
          z.string().meta({
            param: {
              name: 'job-header',
              in: 'header',
            },
          }),
        ],
      },
    },
  },
});
Request Body

Where you would normally declare the media type, set the schema as your Zod Schema as follows.

createDocument({
  paths: {
    '/jobs': {
      get: {
        requestBody: {
          content: {
            'application/json': { schema: z.object({ a: z.string() }) },
          },
        },
      },
    },
  },
});

If you wish to use OpenAPI syntax for your schemas, simply add an OpenAPI schema to the schema field instead.

Responses

Similarly to the Request Body, simply set the schema as your Zod Schema as follows. You can set the response headers using the headers key.

createDocument({
  paths: {
    '/jobs': {
      get: {
        responses: {
          200: {
            description: '200 OK',
            content: {
              'application/json': { schema: z.object({ a: z.string() }) },
            },
            headers: z.object({
              'header-key': z.string(),
            }),
          },
        },
      },
    },
  },
});
Callbacks
createDocument({
  paths: {
    '/jobs': {
      get: {
        callbacks: {
          onData: {
            '{$request.query.callbackUrl}/data': {
              post: {
                requestBody: {
                  content: {
                    'application/json': { schema: z.object({ a: z.string() }) },
                  },
                },
                responses: {
                  200: {
                    description: '200 OK',
                    content: {
                      'application/json': {
                        schema: z.object({ a: z.string() }),
                      },
                    },
                  },
                },
              },
            },
          },
        },
      },
    },
  },
});
Creating Components

OpenAPI allows you to define reusable components and this library allows you to replicate that in two separate ways.

  1. Auto registering schema
  2. Manually registering schema
Schema

If we take the example in createDocument and instead create title as follows

Auto Registering Schema
const title = z.string().meta({
  description: 'Job title',
  example: 'My job',
  id: 'jobTitle', // <- new field
});

OX Radar and Response Center

Find threats in your stack.

OX Radar can check high-priority threats, like critical package CVEs, against your context.

Each incident is marked Detected or Not Detected with a remediation plan and proof all tracked in the Response Center.

Where it’s listed

The probe returned only these fields for this server — auth requirement, protocol and tool list weren’t observed, so they’re not shown (never guessed).

  • npm zod-openapi

References

The publisher’s own links, as the registry reports them. These are not sources for the assessment on this page.

Scan your whole stack

Group 1261154050 1
Group 1261154050
 

From OX Research Newsroom

Microsoft's Official agent framework Hijacked
AI & VibeSec

BREAKING: Hijacked Releases of Microsoft’s Official agent-framework Steals Azure and CI Credentials

Cry2Wolf: How to Tell a Real Bug from a Nothingburger
AI & VibeSec

Cry2Wolf: Not Every Branded Bug Deserves a Fire Drill

CVE

Java4Shell (CVE-2026-114938): Easy to Crash, Hard to Exploit — Less Than 0.12% of JVMs Estimated at Risk of RCE

Frame 2085668439 (1)
Group 1261154229