New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

zod-openapi

Package Overview
Dependencies
Maintainers
1
Versions
83
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

zod-openapi - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

lib-commonjs/openapi.js

16

lib-commonjs/create/components.js

@@ -6,4 +6,9 @@ "use strict";

const metadata_1 = require("./schema/metadata");
const getDefaultComponents = (componentsObject) => {
const defaultComponents = { schemas: {}, parameters: {}, headers: {} };
const getDefaultComponents = (componentsObject, openapi = '3.1.0') => {
const defaultComponents = {
schemas: {},
parameters: {},
headers: {},
openapi,
};
if (!componentsObject) {

@@ -23,8 +28,9 @@ return defaultComponents;

return Object.entries(schemas).forEach(([key, schema]) => {
const ref = schema instanceof zod_1.ZodType ? schema._def.openapi?.ref ?? key : key;
const component = components.schemas[key];
if (component) {
throw new Error(`schemaRef "${key}" is already registered`);
throw new Error(`schemaRef "${ref}" is already registered`);
}
if (schema instanceof zod_1.ZodType) {
components.schemas[key] = {
components.schemas[ref] = {
schemaObject: (0, metadata_1.createSchemaWithMetadata)(schema, components),

@@ -35,3 +41,3 @@ zodSchema: schema,

}
components.schemas[key] = {
components.schemas[ref] = {
schemaObject: schema,

@@ -38,0 +44,0 @@ };

@@ -9,3 +9,3 @@ "use strict";

const { schemas, parameters, headers, ...rest } = zodOpenApiObject.components ?? {};
const components = (0, components_1.getDefaultComponents)({ headers, schemas, parameters });
const components = (0, components_1.getDefaultComponents)({ headers, schemas, parameters }, zodOpenApiObject.openapi);
return {

@@ -12,0 +12,0 @@ ...zodOpenApiObject,

@@ -1,144 +0,127 @@

'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
exports.createSchemaOrRef =
exports.createRegisteredSchema =
exports.createSchema =
void 0;
const zod_1 = require('zod');
const components_1 = require('../components');
const array_1 = require('./array');
const boolean_1 = require('./boolean');
const date_1 = require('./date');
const default_1 = require('./default');
const discriminatedUnion_1 = require('./discriminatedUnion');
const effects_1 = require('./effects');
const enum_1 = require('./enum');
const intersection_1 = require('./intersection');
const literal_1 = require('./literal');
const metadata_1 = require('./metadata');
const nativeEnum_1 = require('./nativeEnum');
const null_1 = require('./null');
const nullable_1 = require('./nullable');
const number_1 = require('./number');
const object_1 = require('./object');
const optional_1 = require('./optional');
const record_1 = require('./record');
const string_1 = require('./string');
const tuple_1 = require('./tuple');
const union_1 = require('./union');
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createSchemaOrRef = exports.createRegisteredSchema = exports.createSchema = void 0;
const zod_1 = require("zod");
const components_1 = require("../components");
const array_1 = require("./array");
const boolean_1 = require("./boolean");
const date_1 = require("./date");
const default_1 = require("./default");
const discriminatedUnion_1 = require("./discriminatedUnion");
const effects_1 = require("./effects");
const enum_1 = require("./enum");
const intersection_1 = require("./intersection");
const literal_1 = require("./literal");
const metadata_1 = require("./metadata");
const nativeEnum_1 = require("./nativeEnum");
const null_1 = require("./null");
const nullable_1 = require("./nullable");
const number_1 = require("./number");
const object_1 = require("./object");
const optional_1 = require("./optional");
const record_1 = require("./record");
const string_1 = require("./string");
const tuple_1 = require("./tuple");
const union_1 = require("./union");
const createSchema = (zodSchema, components) => {
if (zodSchema instanceof zod_1.ZodString) {
return (0, string_1.createStringSchema)(zodSchema);
}
if (zodSchema instanceof zod_1.ZodNumber) {
return (0, number_1.createNumberSchema)(zodSchema);
}
if (zodSchema instanceof zod_1.ZodBoolean) {
return (0, boolean_1.createBooleanSchema)(zodSchema);
}
if (zodSchema instanceof zod_1.ZodEnum) {
return (0, enum_1.createEnumSchema)(zodSchema);
}
if (zodSchema instanceof zod_1.ZodLiteral) {
return (0, literal_1.createLiteralSchema)(zodSchema);
}
if (zodSchema instanceof zod_1.ZodNativeEnum) {
return (0, nativeEnum_1.createNativeEnumSchema)(zodSchema);
}
if (zodSchema instanceof zod_1.ZodArray) {
return (0, array_1.createArraySchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodObject) {
return (0, object_1.createObjectSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodUnion) {
return (0, union_1.createUnionSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodDiscriminatedUnion) {
return (0, discriminatedUnion_1.createDiscriminatedUnionSchema)(
zodSchema,
components,
);
}
if (zodSchema instanceof zod_1.ZodNull) {
return (0, null_1.createNullSchema)(zodSchema);
}
if (zodSchema instanceof zod_1.ZodNullable) {
return (0, nullable_1.createNullableSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodOptional) {
return (0, optional_1.createOptionalSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodDefault) {
return (0, default_1.createDefaultSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodRecord) {
return (0, record_1.createRecordSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodTuple) {
return (0, tuple_1.createTupleSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodDate) {
return (0, date_1.createDateSchema)(zodSchema);
}
if (
zodSchema._def.effect?.type === 'refinement' ||
zodSchema._def.effect?.type === 'preprocess'
) {
return (0, effects_1.createEffectsSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodNativeEnum) {
return (0, nativeEnum_1.createNativeEnumSchema)(zodSchema);
}
if (zodSchema instanceof zod_1.ZodIntersection) {
return (0, intersection_1.createIntersectionSchema)(zodSchema, components);
}
if (!zodSchema._def.openapi?.type) {
console.log(zodSchema);
throw new Error(
`Unknown schema ${zodSchema.toString()}. Please assign it a manual type`,
);
}
return {};
if (zodSchema instanceof zod_1.ZodString) {
return (0, string_1.createStringSchema)(zodSchema);
}
if (zodSchema instanceof zod_1.ZodNumber) {
return (0, number_1.createNumberSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodBoolean) {
return (0, boolean_1.createBooleanSchema)(zodSchema);
}
if (zodSchema instanceof zod_1.ZodEnum) {
return (0, enum_1.createEnumSchema)(zodSchema);
}
if (zodSchema instanceof zod_1.ZodLiteral) {
return (0, literal_1.createLiteralSchema)(zodSchema);
}
if (zodSchema instanceof zod_1.ZodNativeEnum) {
return (0, nativeEnum_1.createNativeEnumSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodArray) {
return (0, array_1.createArraySchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodObject) {
return (0, object_1.createObjectSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodUnion) {
return (0, union_1.createUnionSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodDiscriminatedUnion) {
return (0, discriminatedUnion_1.createDiscriminatedUnionSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodNull) {
return (0, null_1.createNullSchema)(zodSchema);
}
if (zodSchema instanceof zod_1.ZodNullable) {
return (0, nullable_1.createNullableSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodOptional) {
return (0, optional_1.createOptionalSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodDefault) {
return (0, default_1.createDefaultSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodRecord) {
return (0, record_1.createRecordSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodTuple) {
return (0, tuple_1.createTupleSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodDate) {
return (0, date_1.createDateSchema)(zodSchema);
}
if (zodSchema instanceof zod_1.ZodEffects &&
(zodSchema._def.effect.type === 'refinement' ||
zodSchema._def.effect.type === 'preprocess')) {
return (0, effects_1.createEffectsSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodNativeEnum) {
return (0, nativeEnum_1.createNativeEnumSchema)(zodSchema, components);
}
if (zodSchema instanceof zod_1.ZodIntersection) {
return (0, intersection_1.createIntersectionSchema)(zodSchema, components);
}
if (!zodSchema._def.openapi?.type) {
throw new Error(`Unknown schema ${zodSchema.toString()}. Please assign it a manual type`);
}
return {};
};
exports.createSchema = createSchema;
const createRegisteredSchema = (zodSchema, schemaRef, components) => {
const component = components.schemas[schemaRef];
if (component) {
if (component.zodSchema !== zodSchema) {
throw new Error(`schemaRef "${schemaRef}" is already registered`);
const component = components.schemas[schemaRef];
if (component) {
if (component.zodSchema !== zodSchema) {
throw new Error(`schemaRef "${schemaRef}" is already registered`);
}
return {
$ref: (0, components_1.createComponentSchemaRef)(schemaRef),
};
}
// Optional Objects can return a reference object
const schemaOrRef = (0, metadata_1.createSchemaWithMetadata)(zodSchema, components);
if ('$ref' in schemaOrRef) {
throw new Error('Unexpected Error: received a reference object');
}
components.schemas[schemaRef] = {
schemaObject: schemaOrRef,
zodSchema,
};
return {
$ref: (0, components_1.createComponentSchemaRef)(schemaRef),
$ref: (0, components_1.createComponentSchemaRef)(schemaRef),
};
}
// Optional Objects can return a reference object
const schemaOrRef = (0, metadata_1.createSchemaWithMetadata)(
zodSchema,
components,
);
if ('$ref' in schemaOrRef) {
throw new Error('Unexpected Error: received a reference object');
}
components.schemas[schemaRef] = {
schemaObject: schemaOrRef,
zodSchema,
};
return {
$ref: (0, components_1.createComponentSchemaRef)(schemaRef),
};
};
exports.createRegisteredSchema = createRegisteredSchema;
const createSchemaOrRef = (zodSchema, components) => {
const schemaRef = zodSchema._def.openapi?.ref;
if (schemaRef) {
return (0, exports.createRegisteredSchema)(
zodSchema,
schemaRef,
components,
);
}
return (0, metadata_1.createSchemaWithMetadata)(zodSchema, components);
const schemaRef = zodSchema._def.openapi?.ref;
if (schemaRef) {
return (0, exports.createRegisteredSchema)(zodSchema, schemaRef, components);
}
return (0, metadata_1.createSchemaWithMetadata)(zodSchema, components);
};
exports.createSchemaOrRef = createSchemaOrRef;
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.sortStringsAndNumbers = exports.getValidEnumValues = exports.createNativeEnumSchema = void 0;
const createNativeEnumSchema = (zodEnum) => {
const openapi_1 = require("../../openapi");
const createNativeEnumSchema = (zodEnum, components) => {
const enumValues = (0, exports.getValidEnumValues)(zodEnum._def.values);
const { numbers, strings } = (0, exports.sortStringsAndNumbers)(enumValues);
if (strings.length && numbers.length) {
if ((0, openapi_1.satisfiesVersion)(components.openapi, '3.1.0'))
return {
type: ['string', 'number'],
enum: [...strings, ...numbers],
};
return {
type: ['string', 'number'],
enum: [...strings, ...numbers],
oneOf: [
{ type: 'string', enum: strings },
{ type: 'number', enum: numbers },
],
};

@@ -12,0 +20,0 @@ }

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.mapNullType = exports.createNullableSchema = void 0;
exports.createNullableSchema = void 0;
const openapi_1 = require("../../openapi");
const _1 = require(".");
const createNullableSchema = (zodNullable, components) => {
const schemaOrReference = (0, _1.createSchemaOrRef)(zodNullable.unwrap(), components);
if ('$ref' in schemaOrReference) {
if ('$ref' in schemaOrReference || schemaOrReference.allOf) {
return {
oneOf: mapNullOf([schemaOrReference]),
oneOf: mapNullOf([schemaOrReference], components.openapi),
};

@@ -15,21 +16,23 @@ }

return {
oneOf: mapNullOf(oneOf),
oneOf: mapNullOf(oneOf, components.openapi),
...schema,
};
}
if (schemaOrReference.allOf) {
if (schemaOrReference.anyOf) {
const { anyOf, ...schema } = schemaOrReference;
return {
oneOf: [schemaOrReference, { type: 'null' }],
anyOf: mapNullOf(anyOf, components.openapi),
...schema,
};
}
if (schemaOrReference.anyOf) {
const { anyOf, ...schema } = schemaOrReference;
const { type, ...schema } = schemaOrReference;
if ((0, openapi_1.satisfiesVersion)(components.openapi, '3.1.0')) {
return {
anyOf: mapNullOf(anyOf),
type: mapNullType(type),
...schema,
};
}
const { type, ...schema } = schemaOrReference;
return {
type: (0, exports.mapNullType)(type),
type,
nullable: true,
...schema,

@@ -48,7 +51,8 @@ };

};
exports.mapNullType = mapNullType;
const mapNullOf = (ofSchema) => [
...ofSchema,
{ type: 'null' },
];
const mapNullOf = (ofSchema, openapi) => {
if ((0, openapi_1.satisfiesVersion)(openapi, '3.1.0')) {
return [...ofSchema, { type: 'null' }];
}
return [...ofSchema, { nullable: true }];
};
//# sourceMappingURL=nullable.js.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createNumberSchema = void 0;
const createNumberSchema = (zodNumber) => {
const openapi_1 = require("../../openapi");
const createNumberSchema = (zodNumber, components) => {
const zodNumberChecks = getZodNumberChecks(zodNumber);
return {
type: mapNumberType(zodNumberChecks),
...mapRanges(zodNumberChecks),
...mapRanges(zodNumberChecks, components.openapi), // union types are too pesky to drill through
};
};
exports.createNumberSchema = createNumberSchema;
const mapRanges = (zodNumberChecks) => {
const mapRanges = (zodNumberChecks, openapi) => {
const minimum = zodNumberChecks.min?.value;
const maximum = zodNumberChecks.max?.value;
const exclusiveMinimum = zodNumberChecks.min?.inclusive;
const exclusiveMaximum = zodNumberChecks.max?.inclusive;
return {
...(minimum !== undefined && { minimum }),
...(maximum !== undefined && { maximum }),
...(zodNumberChecks.min?.inclusive && { exclusiveMinimum: minimum }),
...(zodNumberChecks.max?.inclusive && { exclusiveMaximum: maximum }),
...(exclusiveMinimum && {
exclusiveMinimum: (0, openapi_1.satisfiesVersion)(openapi, '3.1.0') ? minimum : true,
}),
...(exclusiveMaximum && {
exclusiveMaximum: (0, openapi_1.satisfiesVersion)(openapi, '3.1.0') ? maximum : true,
}),
};

@@ -21,0 +28,0 @@ };

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createTupleSchema = void 0;
const openapi_1 = require("../../openapi");
const _1 = require(".");

@@ -10,3 +11,2 @@ const createTupleSchema = (zodTuple, components) => {

type: 'array',
...mapPrefixItems(items, components),
...mapItemProperties(items, rest, components),

@@ -16,6 +16,23 @@ };

exports.createTupleSchema = createTupleSchema;
const mapPrefixItems = (items, components) => items.length
? { prefixItems: items.map((item) => (0, _1.createSchemaOrRef)(item, components)) }
: undefined;
const mapPrefixItems = (items, components) => {
if (items.length) {
return items.map((item) => (0, _1.createSchemaOrRef)(item, components));
}
return undefined;
};
const mapItemProperties = (items, rest, components) => {
const prefixItems = mapPrefixItems(items, components);
if ((0, openapi_1.satisfiesVersion)(components.openapi, '3.1.0')) {
if (!rest) {
return {
maxItems: items.length,
minItems: items.length,
...(prefixItems && { prefixItems }),
};
}
return {
items: (0, _1.createSchemaOrRef)(rest, components),
...(prefixItems && { prefixItems }),
};
}
if (!rest) {

@@ -25,8 +42,11 @@ return {

minItems: items.length,
...(prefixItems && { items: { oneOf: prefixItems } }),
};
}
return {
items: (0, _1.createSchemaOrRef)(rest, components),
...(prefixItems && {
items: { oneOf: [...prefixItems, (0, _1.createSchemaOrRef)(rest, components)] },
}),
};
};
//# sourceMappingURL=tuple.js.map
import { ZodType } from 'zod';
import { createSchemaWithMetadata } from './schema/metadata';
export const getDefaultComponents = (componentsObject) => {
const defaultComponents = { schemas: {}, parameters: {}, headers: {} };
export const getDefaultComponents = (componentsObject, openapi = '3.1.0') => {
const defaultComponents = {
schemas: {},
parameters: {},
headers: {},
openapi,
};
if (!componentsObject) {

@@ -18,8 +23,9 @@ return defaultComponents;

return Object.entries(schemas).forEach(([key, schema]) => {
const ref = schema instanceof ZodType ? schema._def.openapi?.ref ?? key : key;
const component = components.schemas[key];
if (component) {
throw new Error(`schemaRef "${key}" is already registered`);
throw new Error(`schemaRef "${ref}" is already registered`);
}
if (schema instanceof ZodType) {
components.schemas[key] = {
components.schemas[ref] = {
schemaObject: createSchemaWithMetadata(schema, components),

@@ -30,3 +36,3 @@ zodSchema: schema,

}
components.schemas[key] = {
components.schemas[ref] = {
schemaObject: schema,

@@ -33,0 +39,0 @@ };

@@ -6,3 +6,3 @@ import { stringify } from 'yaml';

const { schemas, parameters, headers, ...rest } = zodOpenApiObject.components ?? {};
const components = getDefaultComponents({ headers, schemas, parameters });
const components = getDefaultComponents({ headers, schemas, parameters }, zodOpenApiObject.openapi);
return {

@@ -9,0 +9,0 @@ ...zodOpenApiObject,

@@ -28,3 +28,3 @@ import { ZodArray, ZodBoolean, ZodDate, ZodDefault, ZodDiscriminatedUnion, ZodEffects, ZodEnum, ZodIntersection, ZodLiteral, ZodNativeEnum, ZodNull, ZodNullable, ZodNumber, ZodObject, ZodOptional, ZodRecord, ZodString, ZodTuple, ZodUnion, } from 'zod';

if (zodSchema instanceof ZodNumber) {
return createNumberSchema(zodSchema);
return createNumberSchema(zodSchema, components);
}

@@ -41,3 +41,3 @@ if (zodSchema instanceof ZodBoolean) {

if (zodSchema instanceof ZodNativeEnum) {
return createNativeEnumSchema(zodSchema);
return createNativeEnumSchema(zodSchema, components);
}

@@ -83,3 +83,3 @@ if (zodSchema instanceof ZodArray) {

if (zodSchema instanceof ZodNativeEnum) {
return createNativeEnumSchema(zodSchema);
return createNativeEnumSchema(zodSchema, components);
}

@@ -86,0 +86,0 @@ if (zodSchema instanceof ZodIntersection) {

@@ -1,8 +0,16 @@

export const createNativeEnumSchema = (zodEnum) => {
import { satisfiesVersion } from '../../openapi';
export const createNativeEnumSchema = (zodEnum, components) => {
const enumValues = getValidEnumValues(zodEnum._def.values);
const { numbers, strings } = sortStringsAndNumbers(enumValues);
if (strings.length && numbers.length) {
if (satisfiesVersion(components.openapi, '3.1.0'))
return {
type: ['string', 'number'],
enum: [...strings, ...numbers],
};
return {
type: ['string', 'number'],
enum: [...strings, ...numbers],
oneOf: [
{ type: 'string', enum: strings },
{ type: 'number', enum: numbers },
],
};

@@ -9,0 +17,0 @@ }

@@ -0,7 +1,8 @@

import { satisfiesVersion } from '../../openapi';
import { createSchemaOrRef } from '.';
export const createNullableSchema = (zodNullable, components) => {
const schemaOrReference = createSchemaOrRef(zodNullable.unwrap(), components);
if ('$ref' in schemaOrReference) {
if ('$ref' in schemaOrReference || schemaOrReference.allOf) {
return {
oneOf: mapNullOf([schemaOrReference]),
oneOf: mapNullOf([schemaOrReference], components.openapi),
};

@@ -12,25 +13,27 @@ }

return {
oneOf: mapNullOf(oneOf),
oneOf: mapNullOf(oneOf, components.openapi),
...schema,
};
}
if (schemaOrReference.allOf) {
if (schemaOrReference.anyOf) {
const { anyOf, ...schema } = schemaOrReference;
return {
oneOf: [schemaOrReference, { type: 'null' }],
anyOf: mapNullOf(anyOf, components.openapi),
...schema,
};
}
if (schemaOrReference.anyOf) {
const { anyOf, ...schema } = schemaOrReference;
const { type, ...schema } = schemaOrReference;
if (satisfiesVersion(components.openapi, '3.1.0')) {
return {
anyOf: mapNullOf(anyOf),
type: mapNullType(type),
...schema,
};
}
const { type, ...schema } = schemaOrReference;
return {
type: mapNullType(type),
type,
nullable: true,
...schema,
};
};
export const mapNullType = (type) => {
const mapNullType = (type) => {
if (!type) {

@@ -44,6 +47,8 @@ return 'null';

};
const mapNullOf = (ofSchema) => [
...ofSchema,
{ type: 'null' },
];
const mapNullOf = (ofSchema, openapi) => {
if (satisfiesVersion(openapi, '3.1.0')) {
return [...ofSchema, { type: 'null' }];
}
return [...ofSchema, { nullable: true }];
};
//# sourceMappingURL=nullable.js.map

@@ -1,16 +0,23 @@

export const createNumberSchema = (zodNumber) => {
import { satisfiesVersion } from '../../openapi';
export const createNumberSchema = (zodNumber, components) => {
const zodNumberChecks = getZodNumberChecks(zodNumber);
return {
type: mapNumberType(zodNumberChecks),
...mapRanges(zodNumberChecks),
...mapRanges(zodNumberChecks, components.openapi), // union types are too pesky to drill through
};
};
const mapRanges = (zodNumberChecks) => {
const mapRanges = (zodNumberChecks, openapi) => {
const minimum = zodNumberChecks.min?.value;
const maximum = zodNumberChecks.max?.value;
const exclusiveMinimum = zodNumberChecks.min?.inclusive;
const exclusiveMaximum = zodNumberChecks.max?.inclusive;
return {
...(minimum !== undefined && { minimum }),
...(maximum !== undefined && { maximum }),
...(zodNumberChecks.min?.inclusive && { exclusiveMinimum: minimum }),
...(zodNumberChecks.max?.inclusive && { exclusiveMaximum: maximum }),
...(exclusiveMinimum && {
exclusiveMinimum: satisfiesVersion(openapi, '3.1.0') ? minimum : true,
}),
...(exclusiveMaximum && {
exclusiveMaximum: satisfiesVersion(openapi, '3.1.0') ? maximum : true,
}),
};

@@ -17,0 +24,0 @@ };

@@ -0,1 +1,2 @@

import { satisfiesVersion } from '../../openapi';
import { createSchemaOrRef } from '.';

@@ -7,10 +8,26 @@ export const createTupleSchema = (zodTuple, components) => {

type: 'array',
...mapPrefixItems(items, components),
...mapItemProperties(items, rest, components),
};
};
const mapPrefixItems = (items, components) => items.length
? { prefixItems: items.map((item) => createSchemaOrRef(item, components)) }
: undefined;
const mapPrefixItems = (items, components) => {
if (items.length) {
return items.map((item) => createSchemaOrRef(item, components));
}
return undefined;
};
const mapItemProperties = (items, rest, components) => {
const prefixItems = mapPrefixItems(items, components);
if (satisfiesVersion(components.openapi, '3.1.0')) {
if (!rest) {
return {
maxItems: items.length,
minItems: items.length,
...(prefixItems && { prefixItems }),
};
}
return {
items: createSchemaOrRef(rest, components),
...(prefixItems && { prefixItems }),
};
}
if (!rest) {

@@ -20,8 +37,11 @@ return {

minItems: items.length,
...(prefixItems && { items: { oneOf: prefixItems } }),
};
}
return {
items: createSchemaOrRef(rest, components),
...(prefixItems && {
items: { oneOf: [...prefixItems, createSchemaOrRef(rest, components)] },
}),
};
};
//# sourceMappingURL=tuple.js.map

@@ -1,7 +0,7 @@

import { oas31 } from 'openapi3-ts';
import { oas30, oas31 } from 'openapi3-ts';
import { ZodType } from 'zod';
import { ZodOpenApiComponentsObject } from './document';
import { ZodOpenAPIVersion, ZodOpenApiComponentsObject } from './document';
export interface Schema {
zodSchema?: ZodType;
schemaObject: oas31.SchemaObject | oas31.ReferenceObject;
schemaObject: oas31.SchemaObject | oas31.ReferenceObject | oas30.SchemaObject | oas30.ReferenceObject;
}

@@ -13,3 +13,3 @@ interface SchemaComponentObject {

zodSchema?: ZodType;
paramObject: oas31.ParameterObject | oas31.ReferenceObject;
paramObject: oas31.ParameterObject | oas31.ReferenceObject | oas30.ParameterObject | oas30.ReferenceObject;
}

@@ -21,3 +21,3 @@ interface ParametersComponentObject {

zodSchema?: ZodType;
headerObject: oas31.HeaderObject | oas31.ReferenceObject;
headerObject: oas31.HeaderObject | oas31.ReferenceObject | oas30.HeaderObject | oas30.ReferenceObject;
}

@@ -31,6 +31,7 @@ interface HeadersComponentObject {

headers: HeadersComponentObject;
openapi: ZodOpenAPIVersion;
}
export declare const getDefaultComponents: (componentsObject?: Pick<ZodOpenApiComponentsObject, 'schemas' | 'parameters' | 'headers'>) => ComponentsObject;
export declare const getDefaultComponents: (componentsObject?: Pick<ZodOpenApiComponentsObject, 'schemas' | 'parameters' | 'headers'>, openapi?: ZodOpenAPIVersion) => ComponentsObject;
export declare const createComponentSchemaRef: (schemaRef: string) => string;
export declare const createComponents: (componentsObject: Omit<ZodOpenApiComponentsObject, 'schemas' | 'parameters' | 'headers'> | undefined, components: ComponentsObject) => oas31.ComponentsObject | undefined;
export {};

@@ -1,5 +0,5 @@

import { oas31 } from 'openapi3-ts';
import { oas30, oas31 } from 'openapi3-ts';
import { stringify } from 'yaml';
import { AnyZodObject, ZodType } from 'zod';
export interface ZodOpenApiMediaTypeObject extends Omit<oas31.MediaTypeObject, 'schema'> {
export interface ZodOpenApiMediaTypeObject extends Omit<oas31.MediaTypeObject & oas30.MediaTypeObject, 'schema'> {
schema?: AnyZodObject | oas31.SchemaObject | oas31.ReferenceObject;

@@ -11,6 +11,6 @@ }

}
export interface ZodOpenApiRequestBodyObject extends Omit<oas31.RequestBodyObject, 'content'> {
export interface ZodOpenApiRequestBodyObject extends Omit<oas31.RequestBodyObject & oas30.RequestBodyObject, 'content'> {
content: ZodOpenApiContentObject;
}
export interface ZodOpenApiResponseObject extends Omit<oas31.ResponseObject, 'content'> {
export interface ZodOpenApiResponseObject extends Omit<oas31.ResponseObject & oas30.ResponseObject, 'content'> {
content?: ZodOpenApiContentObject;

@@ -20,9 +20,9 @@ responseHeaders?: AnyZodObject;

export interface ZodOpenApiResponsesObject extends oas31.ISpecificationExtension {
default?: ZodOpenApiResponseObject | oas31.ReferenceObject;
default?: ZodOpenApiResponseObject | oas31.ReferenceObject | oas30.ReferenceObject;
[statuscode: `${1 | 2 | 3 | 4 | 5}${string}`]: ZodOpenApiResponseObject | oas31.ReferenceObject;
}
export type ZodOpenApiParameters = {
[type in oas31.ParameterLocation]?: AnyZodObject;
[type in oas31.ParameterLocation & oas30.ParameterLocation]?: AnyZodObject;
};
export interface ZodOpenApiOperationObject extends Omit<oas31.OperationObject, 'requestBody' | 'responses'> {
export interface ZodOpenApiOperationObject extends Omit<oas31.OperationObject & oas30.OperationObject, 'requestBody' | 'responses'> {
requestBody?: ZodOpenApiRequestBodyObject;

@@ -32,3 +32,3 @@ requestParams?: ZodOpenApiParameters;

}
export interface ZodOpenApiPathItemObject extends Omit<oas31.PathItemObject, 'get' | 'put' | 'post' | 'delete' | 'options' | 'head' | 'patch' | 'trace'> {
export interface ZodOpenApiPathItemObject extends Omit<oas31.PathItemObject & oas30.PathItemObject, 'get' | 'put' | 'post' | 'delete' | 'options' | 'head' | 'patch' | 'trace'> {
get?: ZodOpenApiOperationObject;

@@ -46,9 +46,10 @@ put?: ZodOpenApiOperationObject;

}
export interface ZodOpenApiComponentsObject extends Omit<oas31.ComponentsObject, 'schemas'> {
export interface ZodOpenApiComponentsObject extends Omit<oas31.ComponentsObject & oas30.ComponentsObject, 'schemas'> {
schemas?: {
[schema: string]: ZodType | oas31.SchemaObject | oas31.ReferenceObject;
[schema: string]: ZodType | oas31.SchemaObject | oas31.ReferenceObject | oas30.SchemaObject | oas30.ReferenceObject;
};
}
export type ZodOpenAPIVersion = '3.0.0' | '3.0.1' | '3.0.2' | '3.0.3' | '3.1.0';
export interface ZodOpenApiObject extends Omit<oas31.OpenAPIObject, 'openapi' | 'paths' | 'webhooks' | 'components'> {
openapi: '3.1.0';
openapi: ZodOpenAPIVersion;
paths?: ZodOpenApiPathsObject;

@@ -55,0 +56,0 @@ webhooks?: ZodOpenApiPathsObject;

import { oas31 } from 'openapi3-ts';
import { EnumLike, ZodNativeEnum } from 'zod';
export declare const createNativeEnumSchema: <T extends EnumLike>(zodEnum: ZodNativeEnum<T>) => oas31.SchemaObject | oas31.ReferenceObject;
import { ComponentsObject } from '../components';
export declare const createNativeEnumSchema: <T extends EnumLike>(zodEnum: ZodNativeEnum<T>, components: ComponentsObject) => oas31.SchemaObject | oas31.ReferenceObject;
interface StringsAndNumbers {

@@ -5,0 +6,0 @@ strings: string[];

@@ -5,2 +5,1 @@ import { oas31 } from 'openapi3-ts';

export declare const createNullableSchema: (zodNullable: ZodNullable<any>, components: ComponentsObject) => oas31.SchemaObject;
export declare const mapNullType: (type: oas31.SchemaObject['type']) => oas31.SchemaObject['type'];
import { oas31 } from 'openapi3-ts';
import { ZodNumber } from 'zod';
export declare const createNumberSchema: (zodNumber: ZodNumber) => oas31.SchemaObject;
import { ComponentsObject } from '../components';
export declare const createNumberSchema: (zodNumber: ZodNumber, components: ComponentsObject) => oas31.SchemaObject;

@@ -1,4 +0,6 @@

import { oas31 } from 'openapi3-ts';
import { oas30, oas31 } from 'openapi3-ts';
import { UnknownKeysParam, ZodDate, ZodObject, ZodRawShape, ZodTypeAny, z } from 'zod';
interface ZodOpenApiMetadata<T extends ZodTypeAny, TInferred = z.infer<T>> extends Omit<oas31.SchemaObject, 'example'> {
type SchemaObject = oas30.SchemaObject & oas31.SchemaObject;
interface ZodOpenApiMetadata<T extends ZodTypeAny, TInferred = z.infer<T>> extends SchemaObject {
example?: TInferred;
examples?: [TInferred, ...TInferred[]];

@@ -16,3 +18,3 @@ default?: T extends ZodDate ? string : TInferred;

};
header?: Partial<oas31.HeaderObject> & {
header?: Partial<oas31.HeaderObject & oas30.HeaderObject> & {
ref?: string;

@@ -19,0 +21,0 @@ };

{
"name": "zod-openapi",
"version": "0.3.0",
"description": "A plugin to create full OpenAPI documents from your Zod types",
"version": "0.4.0",
"description": "A library to create full OpenAPI documents from your Zod types",
"homepage": "https://github.com/samchungy/zod-openapi#readme",

@@ -6,0 +6,0 @@ "bugs": {

# zod-openapi
A Typescript library to create full OpenAPI 3 documentation from [Zod](https://github.com/colinhacks/zod) Types.
[![npm version](https://img.shields.io/npm/v/zod-openapi)](https://www.npmjs.com/package/zod-openapi)
[![npm downloads](https://img.shields.io/npm/dm/zod-openapi)](https://www.npmjs.com/package/zod-openapi)
[![Powered by skuba](https://img.shields.io/badge/🤿%20skuba-powered-009DC4)](https://github.com/seek-oss/skuba)
A Typescript library to create full OpenAPI 3.1 documentation from Zod Schemas.
## Install
Install via npm:
```bash
npm install zod zod-openapi
```
## API

@@ -315,2 +325,10 @@

## Supported OpenAPI Versions
- '3.0.0'
- '3.0.1'
- '3.0.2'
- '3.0.3'
- '3.1.0'
## Supported Zod Schema

@@ -317,0 +335,0 @@

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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