npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@bernierllc/zod-openapi-converter

v1.2.2

Published

Zod schema to OpenAPI conversion with runtime validation support

Readme

@bernierllc/zod-openapi-converter

Zod schema to OpenAPI conversion with runtime validation support

Installation

npm install @bernierllc/zod-openapi-converter

Peer Dependencies

This package requires zod as a peer dependency:

npm install zod

Usage

Basic Conversion

import { z } from 'zod';
import { ZodOpenAPIConverter } from '@bernierllc/zod-openapi-converter';

const converter = new ZodOpenAPIConverter();

// Define your Zod schema
const UserSchema = z.object({
  id: z.number().int().positive(),
  name: z.string().min(1).max(100),
  email: z.string().email(),
  age: z.number().int().min(0).max(150).optional()
});

// Convert to OpenAPI schema
const openApiSchema = converter.convertToSchema(UserSchema);

console.log(openApiSchema);
/*
{
  type: 'object',
  properties: {
    id: { type: 'integer', minimum: 0, exclusiveMinimum: 0 },
    name: { type: 'string', minLength: 1, maxLength: 100 },
    email: { type: 'string', format: 'email' },
    age: { type: 'integer', minimum: 0, maximum: 150 }
  },
  required: ['id', 'name', 'email']
}
*/

Full Conversion with Components and Examples

import { ZodOpenAPIConverter } from '@bernierllc/zod-openapi-converter';

const converter = new ZodOpenAPIConverter({
  includeExamples: true,
  preserveZodMeta: true
});

const UserSchema = z.object({
  id: z.number().int().positive().describe('Unique user identifier'),
  name: z.string().min(1).max(100).describe('User full name'),
  email: z.string().email().describe('User email address'),
  role: z.enum(['admin', 'user', 'guest']).default('user')
}).describe('User profile information');

// Get complete conversion with metadata
const result = converter.convert(UserSchema);

console.log(result.schema);      // OpenAPI schema object
console.log(result.components);  // Reusable component definitions
console.log(result.examples);    // Generated examples
console.log(result.zodMeta);     // Original Zod metadata

Complex Schema Types

Discriminated Unions

const ApiResponse = z.discriminatedUnion('status', [
  z.object({
    status: z.literal('success'),
    data: z.object({
      id: z.number(),
      message: z.string()
    })
  }),
  z.object({
    status: z.literal('error'),
    error: z.string(),
    code: z.number().int()
  })
]);

const schema = converter.convertToSchema(ApiResponse);
/*
{
  oneOf: [
    {
      type: 'object',
      properties: {
        status: { type: 'string', const: 'success' },
        data: { type: 'object', properties: {...} }
      },
      required: ['status', 'data']
    },
    {
      type: 'object',
      properties: {
        status: { type: 'string', const: 'error' },
        error: { type: 'string' },
        code: { type: 'integer' }
      },
      required: ['status', 'error', 'code']
    }
  ],
  discriminator: { propertyName: 'status' }
}
*/

Arrays and Tuples

// Array with constraints
const TagsSchema = z.array(z.string()).min(1).max(10);
const tagsOpenAPI = converter.convertToSchema(TagsSchema);
// { type: 'array', items: { type: 'string' }, minItems: 1, maxItems: 10 }

// Tuple with specific types
const CoordinatesSchema = z.tuple([z.number(), z.number()]);
const coordsOpenAPI = converter.convertToSchema(CoordinatesSchema);
// { type: 'array', prefixItems: [{ type: 'number' }, { type: 'number' }], minItems: 2, maxItems: 2 }

Records and Maps

// Record with dynamic keys
const MetadataSchema = z.record(z.string(), z.number());
const metadataOpenAPI = converter.convertToSchema(MetadataSchema);
// { type: 'object', additionalProperties: { type: 'number' } }

Runtime Validation

import { ZodValidationIntegrator } from '@bernierllc/zod-openapi-converter';

const integrator = new ZodValidationIntegrator();

// Create a validator
const validator = integrator.createValidator(UserSchema);

const validationResult = validator({
  id: 1,
  name: 'John Doe',
  email: '[email protected]'
});

if (validationResult.success) {
  console.log('Valid data:', validationResult.data);
} else {
  console.log('Validation errors:', validationResult.errors);
}

// Generate example data
const example = integrator.generateValidationExample(UserSchema);
console.log('Example:', example);
// { id: 42, name: 'example string', email: '[email protected]' }

Schema Optimization

import { SchemaOptimizer } from '@bernierllc/zod-openapi-converter';

const optimizer = new SchemaOptimizer();

// Optimize a schema
const schema = {
  type: 'object',
  properties: { name: { type: 'string' } },
  required: []  // Empty required array
};

const optimized = optimizer.optimizeSchema(schema);
// required field is removed

// Simplify unions
const unionSchema = {
  anyOf: [{ type: 'string' }, { type: 'number' }, { type: 'string' }]
};

const simplified = optimizer.simplifyUnions(unionSchema);
// Duplicates removed: { anyOf: [{ type: 'string' }, { type: 'number' }] }

API Reference

ZodOpenAPIConverter

Main converter class for transforming Zod schemas to OpenAPI format.

Constructor Options

interface ZodOpenAPIConverterOptions {
  strictMode?: boolean;              // Strict OpenAPI compliance (default: true)
  includeExamples?: boolean;         // Generate examples from defaults (default: true)
  preserveZodMeta?: boolean;         // Keep Zod metadata in extensions (default: false)
  customTransforms?: ZodTransformMap; // Custom schema transformations
  componentPrefix?: string;          // Prefix for component names (default: '')
  deduplicateComponents?: boolean;   // Deduplicate identical components (default: true)
}

Methods

  • convert(zodSchema) - Complete conversion with components and metadata
  • convertToSchema(zodSchema) - Convert to OpenAPI schema only
  • convertWithComponents(zodSchema) - Convert with component extraction
  • convertObject(schema) - Convert ZodObject
  • convertArray(schema) - Convert ZodArray
  • convertUnion(schema) - Convert ZodUnion
  • convertEnum(schema) - Convert ZodEnum
  • extractValidationRules(schema) - Extract validation constraints

ZodValidationIntegrator

Runtime validation integration with Zod schemas.

Methods

  • validateRequest(schema, data) - Validate data against schema
  • createValidator(schema) - Create reusable validator function
  • createValidationMiddleware(schema) - Create middleware function
  • generateValidationExample(schema) - Generate example data
  • createAsyncValidator(schema) - Create async validator

SchemaOptimizer

Schema optimization utilities for cleaner OpenAPI output.

Methods

  • optimizeSchema(schema) - Remove redundant properties
  • deduplicateSchemas(schemas) - Remove duplicate schemas
  • extractCommonPatterns(schemas) - Find reusable patterns
  • simplifyUnions(schema) - Simplify union types
  • mergeAllOf(schema) - Merge compatible allOf schemas

Supported Zod Types

  • ✅ Primitives: string, number, boolean, date, bigint
  • ✅ Special: null, undefined, any, unknown, never, void
  • ✅ Objects: object, record, map
  • ✅ Arrays: array, tuple, set
  • ✅ Unions: union, discriminated union, intersection
  • ✅ Enums: enum, native enum, literal
  • ✅ Modifiers: optional, nullable, default
  • ✅ Effects: refinements, transforms (with metadata preservation)
  • ✅ Advanced: lazy, promise, branded, pipeline

String Validation Support

All Zod string validations are converted to appropriate OpenAPI constraints:

  • min/maxminLength/maxLength
  • lengthminLength and maxLength
  • emailformat: 'email'
  • urlformat: 'uri'
  • uuidformat: 'uuid'
  • regexpattern
  • startsWith/endsWith/includespattern

Number Validation Support

  • min/maxminimum/maximum or exclusiveMinimum/exclusiveMaximum
  • inttype: 'integer'
  • positive/negative → appropriate min/max constraints
  • multipleOfmultipleOf

Integration Status

  • Logger integration: not-applicable - Pure schema conversion utility with no runtime logging requirements. Operations are synchronous transformations without side effects requiring observation. No @bernierllc/logger dependency needed.
  • Docs-Suite: ready - Complete API documentation with TypeDoc comments and comprehensive usage examples in markdown format.
  • NeverHub integration: not-applicable - Core utility package focused on schema transformation. No service discovery, event bus, or dynamic configuration needs. Designed as a pure functional library for build-time and runtime schema conversion. No @bernierllc/neverhub-adapter dependency needed.

See Also

License

Copyright (c) 2025 Bernier LLC. All rights reserved.

This file is licensed to the client under a limited-use license. The client may use and modify this code only within the scope of the project it was delivered for. Redistribution or use in other products or commercial offerings is not permitted without written consent from Bernier LLC.