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.8.0 to 0.9.0

154

lib-commonjs/create/components.js

@@ -5,2 +5,3 @@ "use strict";

const zod_1 = require("zod");
const parameters_1 = require("./parameters");
const metadata_1 = require("./schema/metadata");

@@ -10,4 +11,4 @@ const getDefaultComponents = (componentsObject, openapi = '3.1.0') => {

schemas: new Map(),
parameters: {},
headers: {},
parameters: new Map(),
headers: new Map(),
openapi,

@@ -19,4 +20,4 @@ };

createSchemas(componentsObject.schemas, defaultComponents);
createParameters(componentsObject.parameters, defaultComponents);
createHeaders(componentsObject.headers, defaultComponents);
createParameters(componentsObject.requestParams, defaultComponents);
createHeaders(componentsObject.responseHeaders, defaultComponents);
return defaultComponents;

@@ -32,3 +33,3 @@ };

if (components.schemas.has(schema)) {
throw new Error(`schema ${JSON.stringify(schema._def)} is already registered`);
throw new Error(`Schema ${JSON.stringify(schema._def)} is already registered`);
}

@@ -42,43 +43,76 @@ const ref = schema._def.openapi?.ref ?? key;

});
return Array.from(components.schemas).forEach(([schema, { ref }]) => {
const state = {
components,
type: schema._def.openapi?.refType ?? 'output',
};
const schemaObject = (0, metadata_1.createSchemaWithMetadata)(schema, state);
components.schemas.set(schema, {
type: 'complete',
ref,
schemaObject,
creationType: state.effectType,
});
return Array.from(components.schemas).forEach(([schema, { ref, type }]) => {
if (type === 'partial') {
const state = {
components,
type: schema._def.openapi?.refType ?? 'output',
};
const schemaObject = (0, metadata_1.createSchemaWithMetadata)(schema, state);
components.schemas.set(schema, {
type: 'complete',
ref,
schemaObject,
...(state.effectType && { creationType: state.effectType }),
});
}
});
};
const createParameters = (parameters, components) => {
if (!parameters) {
const createParameters = (requestParams, components) => {
if (!requestParams) {
return;
}
return Object.entries(parameters).forEach(([key, schema]) => {
const component = components.parameters[key];
if (component) {
throw new Error(`parameter "${key}" is already registered`);
Object.entries(requestParams).forEach(([paramType, zodObject]) => {
Object.entries(zodObject._def.shape()).forEach(([key, schema]) => {
if (schema instanceof zod_1.ZodType) {
if (components.parameters.has(schema)) {
throw new Error(`Parameter ${JSON.stringify(schema._def)} is already registered`);
}
const ref = schema._def.openapi?.param?.ref ?? key;
components.parameters.set(schema, {
type: 'partial',
ref,
in: paramType,
});
}
});
});
return Array.from(components.parameters).forEach(([schema, component]) => {
if (component.type === 'partial') {
const parameter = (0, parameters_1.createBaseParameter)(schema, components);
components.parameters.set(schema, {
type: 'complete',
ref: component.ref,
paramObject: {
in: component.in,
name: component.ref,
...parameter,
},
});
}
components.parameters[key] = {
paramObject: schema,
};
});
};
const createHeaders = (headers, components) => {
if (!headers) {
const createHeaders = (responseHeaders, components) => {
if (!responseHeaders) {
return;
}
return Object.entries(headers).forEach(([key, schema]) => {
const component = components.headers[key];
if (component) {
throw new Error(`header "${key}" is already registered`);
Object.entries(responseHeaders._def.shape()).forEach(([key, schema]) => {
if (components.parameters.has(schema)) {
throw new Error(`Header ${JSON.stringify(schema._def)} is already registered`);
}
components.headers[key] = {
headerObject: schema,
};
const ref = schema._def.openapi?.param?.ref ?? key;
components.headers.set(schema, {
type: 'partial',
ref,
});
});
return Array.from(components.headers).forEach(([schema, component]) => {
if (component.type === 'partial') {
const header = (0, parameters_1.createBaseParameter)(schema, components);
components.headers.set(schema, {
type: 'complete',
ref: component.ref,
headerObject: header,
});
}
});
};

@@ -89,4 +123,4 @@ const createComponentSchemaRef = (schemaRef) => `#/components/schemas/${schemaRef}`;

const combinedSchemas = createSchemaComponents(componentsObject, components.schemas);
const combinedParameters = createParamComponents(components.parameters);
const combinedHeaders = createHeaderComponents(components.headers);
const combinedParameters = createParamComponents(componentsObject, components.parameters);
const combinedHeaders = createHeaderComponents(componentsObject, components.headers);
const { schemas, parameters, headers, ...rest } = componentsObject;

@@ -113,8 +147,8 @@ const finalComponents = {

}, {});
const components = Array.from(componentMap).reduce((acc, [_zodType, value]) => {
if (value.type === 'complete') {
if (acc[value.ref]) {
throw new Error(`Schema "${value.ref}" is already registered`);
const components = Array.from(componentMap).reduce((acc, [_zodType, component]) => {
if (component.type === 'complete') {
if (acc[component.ref]) {
throw new Error(`Schema "${component.ref}" is already registered`);
}
acc[value.ref] = value.schemaObject;
acc[component.ref] = component.schemaObject;
}

@@ -125,20 +159,40 @@ return acc;

};
const createParamComponents = (component) => {
const components = Object.entries(component).reduce((acc, [key, value]) => {
if (value) {
acc[key] = value.paramObject;
const createParamComponents = (componentsObject, componentMap) => {
const customComponents = Object.entries(componentsObject.parameters ?? {}).reduce((acc, [key, value]) => {
if (acc[key]) {
throw new Error(`Parameter "${key}" is already registered`);
}
acc[key] = value;
return acc;
}, {});
const components = Array.from(componentMap).reduce((acc, [_zodType, component]) => {
if (component.type === 'complete') {
if (acc[component.ref]) {
throw new Error(`Parameter "${component.ref}" is already registered`);
}
acc[component.ref] = component.paramObject;
}
return acc;
}, customComponents);
return Object.keys(components).length ? components : undefined;
};
const createHeaderComponents = (component) => {
const components = Object.entries(component).reduce((acc, [key, value]) => {
if (value) {
acc[key] = value.headerObject;
const createHeaderComponents = (componentsObject, componentMap) => {
const customComponents = Object.entries(componentsObject.headers ?? {}).reduce((acc, [key, value]) => {
if (acc[key]) {
throw new Error(`Header "${key}" is already registered`);
}
acc[key] = value;
return acc;
}, {});
const components = Array.from(componentMap).reduce((acc, [_zodType, component]) => {
if (component.type === 'complete') {
if (acc[component.ref]) {
throw new Error(`Header "${component.ref}" is already registered`);
}
acc[component.ref] = component.headerObject;
}
return acc;
}, customComponents);
return Object.keys(components).length ? components : undefined;
};
//# sourceMappingURL=components.js.map

@@ -22,8 +22,6 @@ "use strict";

const createRegisteredParam = (zodSchema, ref, type, name, components) => {
const component = components.parameters[ref];
if (component) {
const component = components.parameters.get(zodSchema);
if (component && component.type === 'complete') {
if (!('$ref' in component.paramObject) &&
(component.zodSchema !== zodSchema ||
component.paramObject.in !== type ||
component.paramObject.name !== name)) {
(component.paramObject.in !== type || component.paramObject.name !== name)) {
throw new Error(`parameterRef "${ref}" is already registered`);

@@ -40,3 +38,4 @@ }

}
components.parameters[ref] = {
components.parameters.set(zodSchema, {
type: 'complete',
paramObject: {

@@ -47,4 +46,4 @@ in: type,

},
zodSchema,
};
ref,
});
return {

@@ -51,0 +50,0 @@ $ref: (0, exports.createComponentParamRef)(ref),

@@ -40,6 +40,5 @@ "use strict";

const createRegisteredHeader = (zodSchema, ref, components) => {
const component = components.headers[ref];
if (component) {
if (!('$ref' in component.headerObject) &&
component.zodSchema !== zodSchema) {
const component = components.headers.get(zodSchema);
if (component && component.type === 'complete') {
if (!('$ref' in component.headerObject)) {
throw new Error(`header "${ref}" is already registered`);

@@ -56,6 +55,7 @@ }

}
components.headers[ref] = {
components.headers.set(zodSchema, {
type: 'complete',
headerObject: baseParamOrRef,
zodSchema,
};
ref,
});
return {

@@ -62,0 +62,0 @@ $ref: (0, exports.createComponentHeaderRef)(ref),

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

schemaObject: schemaOrRef,
creationType: newState.effectType,
...(newState.effectType && { creationType: newState.effectType }),
});

@@ -154,0 +154,0 @@ return {

import { ZodType } from 'zod';
import { createBaseParameter } from './parameters';
import { createSchemaWithMetadata } from './schema/metadata';

@@ -6,4 +7,4 @@ export const getDefaultComponents = (componentsObject, openapi = '3.1.0') => {

schemas: new Map(),
parameters: {},
headers: {},
parameters: new Map(),
headers: new Map(),
openapi,

@@ -15,4 +16,4 @@ };

createSchemas(componentsObject.schemas, defaultComponents);
createParameters(componentsObject.parameters, defaultComponents);
createHeaders(componentsObject.headers, defaultComponents);
createParameters(componentsObject.requestParams, defaultComponents);
createHeaders(componentsObject.responseHeaders, defaultComponents);
return defaultComponents;

@@ -27,3 +28,3 @@ };

if (components.schemas.has(schema)) {
throw new Error(`schema ${JSON.stringify(schema._def)} is already registered`);
throw new Error(`Schema ${JSON.stringify(schema._def)} is already registered`);
}

@@ -37,43 +38,76 @@ const ref = schema._def.openapi?.ref ?? key;

});
return Array.from(components.schemas).forEach(([schema, { ref }]) => {
const state = {
components,
type: schema._def.openapi?.refType ?? 'output',
};
const schemaObject = createSchemaWithMetadata(schema, state);
components.schemas.set(schema, {
type: 'complete',
ref,
schemaObject,
creationType: state.effectType,
});
return Array.from(components.schemas).forEach(([schema, { ref, type }]) => {
if (type === 'partial') {
const state = {
components,
type: schema._def.openapi?.refType ?? 'output',
};
const schemaObject = createSchemaWithMetadata(schema, state);
components.schemas.set(schema, {
type: 'complete',
ref,
schemaObject,
...(state.effectType && { creationType: state.effectType }),
});
}
});
};
const createParameters = (parameters, components) => {
if (!parameters) {
const createParameters = (requestParams, components) => {
if (!requestParams) {
return;
}
return Object.entries(parameters).forEach(([key, schema]) => {
const component = components.parameters[key];
if (component) {
throw new Error(`parameter "${key}" is already registered`);
Object.entries(requestParams).forEach(([paramType, zodObject]) => {
Object.entries(zodObject._def.shape()).forEach(([key, schema]) => {
if (schema instanceof ZodType) {
if (components.parameters.has(schema)) {
throw new Error(`Parameter ${JSON.stringify(schema._def)} is already registered`);
}
const ref = schema._def.openapi?.param?.ref ?? key;
components.parameters.set(schema, {
type: 'partial',
ref,
in: paramType,
});
}
});
});
return Array.from(components.parameters).forEach(([schema, component]) => {
if (component.type === 'partial') {
const parameter = createBaseParameter(schema, components);
components.parameters.set(schema, {
type: 'complete',
ref: component.ref,
paramObject: {
in: component.in,
name: component.ref,
...parameter,
},
});
}
components.parameters[key] = {
paramObject: schema,
};
});
};
const createHeaders = (headers, components) => {
if (!headers) {
const createHeaders = (responseHeaders, components) => {
if (!responseHeaders) {
return;
}
return Object.entries(headers).forEach(([key, schema]) => {
const component = components.headers[key];
if (component) {
throw new Error(`header "${key}" is already registered`);
Object.entries(responseHeaders._def.shape()).forEach(([key, schema]) => {
if (components.parameters.has(schema)) {
throw new Error(`Header ${JSON.stringify(schema._def)} is already registered`);
}
components.headers[key] = {
headerObject: schema,
};
const ref = schema._def.openapi?.param?.ref ?? key;
components.headers.set(schema, {
type: 'partial',
ref,
});
});
return Array.from(components.headers).forEach(([schema, component]) => {
if (component.type === 'partial') {
const header = createBaseParameter(schema, components);
components.headers.set(schema, {
type: 'complete',
ref: component.ref,
headerObject: header,
});
}
});
};

@@ -83,4 +117,4 @@ export const createComponentSchemaRef = (schemaRef) => `#/components/schemas/${schemaRef}`;

const combinedSchemas = createSchemaComponents(componentsObject, components.schemas);
const combinedParameters = createParamComponents(components.parameters);
const combinedHeaders = createHeaderComponents(components.headers);
const combinedParameters = createParamComponents(componentsObject, components.parameters);
const combinedHeaders = createHeaderComponents(componentsObject, components.headers);
const { schemas, parameters, headers, ...rest } = componentsObject;

@@ -106,8 +140,8 @@ const finalComponents = {

}, {});
const components = Array.from(componentMap).reduce((acc, [_zodType, value]) => {
if (value.type === 'complete') {
if (acc[value.ref]) {
throw new Error(`Schema "${value.ref}" is already registered`);
const components = Array.from(componentMap).reduce((acc, [_zodType, component]) => {
if (component.type === 'complete') {
if (acc[component.ref]) {
throw new Error(`Schema "${component.ref}" is already registered`);
}
acc[value.ref] = value.schemaObject;
acc[component.ref] = component.schemaObject;
}

@@ -118,20 +152,40 @@ return acc;

};
const createParamComponents = (component) => {
const components = Object.entries(component).reduce((acc, [key, value]) => {
if (value) {
acc[key] = value.paramObject;
const createParamComponents = (componentsObject, componentMap) => {
const customComponents = Object.entries(componentsObject.parameters ?? {}).reduce((acc, [key, value]) => {
if (acc[key]) {
throw new Error(`Parameter "${key}" is already registered`);
}
acc[key] = value;
return acc;
}, {});
const components = Array.from(componentMap).reduce((acc, [_zodType, component]) => {
if (component.type === 'complete') {
if (acc[component.ref]) {
throw new Error(`Parameter "${component.ref}" is already registered`);
}
acc[component.ref] = component.paramObject;
}
return acc;
}, customComponents);
return Object.keys(components).length ? components : undefined;
};
const createHeaderComponents = (component) => {
const components = Object.entries(component).reduce((acc, [key, value]) => {
if (value) {
acc[key] = value.headerObject;
const createHeaderComponents = (componentsObject, componentMap) => {
const customComponents = Object.entries(componentsObject.headers ?? {}).reduce((acc, [key, value]) => {
if (acc[key]) {
throw new Error(`Header "${key}" is already registered`);
}
acc[key] = value;
return acc;
}, {});
const components = Array.from(componentMap).reduce((acc, [_zodType, component]) => {
if (component.type === 'complete') {
if (acc[component.ref]) {
throw new Error(`Header "${component.ref}" is already registered`);
}
acc[component.ref] = component.headerObject;
}
return acc;
}, customComponents);
return Object.keys(components).length ? components : undefined;
};
//# sourceMappingURL=components.js.map

@@ -17,8 +17,6 @@ import { createSchemaOrRef } from './schema';

const createRegisteredParam = (zodSchema, ref, type, name, components) => {
const component = components.parameters[ref];
if (component) {
const component = components.parameters.get(zodSchema);
if (component && component.type === 'complete') {
if (!('$ref' in component.paramObject) &&
(component.zodSchema !== zodSchema ||
component.paramObject.in !== type ||
component.paramObject.name !== name)) {
(component.paramObject.in !== type || component.paramObject.name !== name)) {
throw new Error(`parameterRef "${ref}" is already registered`);

@@ -35,3 +33,4 @@ }

}
components.parameters[ref] = {
components.parameters.set(zodSchema, {
type: 'complete',
paramObject: {

@@ -42,4 +41,4 @@ in: type,

},
zodSchema,
};
ref,
});
return {

@@ -46,0 +45,0 @@ $ref: createComponentParamRef(ref),

@@ -35,6 +35,5 @@ import { createContent } from './content';

const createRegisteredHeader = (zodSchema, ref, components) => {
const component = components.headers[ref];
if (component) {
if (!('$ref' in component.headerObject) &&
component.zodSchema !== zodSchema) {
const component = components.headers.get(zodSchema);
if (component && component.type === 'complete') {
if (!('$ref' in component.headerObject)) {
throw new Error(`header "${ref}" is already registered`);

@@ -51,6 +50,7 @@ }

}
components.headers[ref] = {
components.headers.set(zodSchema, {
type: 'complete',
headerObject: baseParamOrRef,
zodSchema,
};
ref,
});
return {

@@ -57,0 +57,0 @@ $ref: createComponentHeaderRef(ref),

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

schemaObject: schemaOrRef,
creationType: newState.effectType,
...(newState.effectType && { creationType: newState.effectType }),
});

@@ -150,0 +150,0 @@ return {

import { oas30, oas31 } from 'openapi3-ts';
import { ParameterLocation } from 'openapi3-ts/dist/mjs/oas31';
import { ZodType } from 'zod';

@@ -19,20 +20,31 @@ import { ZodOpenApiComponentsObject, ZodOpenApiVersion } from './document';

export type SchemaComponentMap = Map<ZodType, SchemaComponent>;
export interface ParameterComponent {
zodSchema?: ZodType;
export interface CompleteParameterComponent extends BaseParameterComponent {
type: 'complete';
paramObject: oas31.ParameterObject | oas31.ReferenceObject | oas30.ParameterObject | oas30.ReferenceObject;
}
interface ParametersComponentObject {
[ref: string]: ParameterComponent | undefined;
export interface PartialParameterComponent extends BaseParameterComponent {
type: 'partial';
in: ParameterLocation;
}
export interface Header {
zodSchema?: ZodType;
interface BaseParameterComponent {
ref: string;
}
export type ParameterComponent = CompleteParameterComponent | PartialParameterComponent;
export type ParameterComponentMap = Map<ZodType, ParameterComponent>;
export interface CompleteHeaderComponent extends BaseHeaderComponent {
type: 'complete';
headerObject: oas31.HeaderObject | oas31.ReferenceObject | oas30.HeaderObject | oas30.ReferenceObject;
}
interface HeadersComponentObject {
[ref: string]: Header | undefined;
export interface PartialHeaderComponent extends BaseHeaderComponent {
type: 'partial';
}
interface BaseHeaderComponent {
ref: string;
}
export type HeaderComponent = CompleteHeaderComponent | PartialHeaderComponent;
export type HeaderComponentMap = Map<ZodType, HeaderComponent>;
export interface ComponentsObject {
schemas: SchemaComponentMap;
parameters: ParametersComponentObject;
headers: HeadersComponentObject;
parameters: ParameterComponentMap;
headers: HeaderComponentMap;
openapi: ZodOpenApiVersion;

@@ -39,0 +51,0 @@ }

@@ -48,2 +48,4 @@ import { oas30, oas31 } from 'openapi3-ts';

};
requestParams?: ZodOpenApiParameters;
responseHeaders?: AnyZodObject;
}

@@ -50,0 +52,0 @@ export type ZodOpenApiVersion = OpenApiVersion;

{
"name": "zod-openapi",
"version": "0.8.0",
"version": "0.9.0",
"description": "A library to create full OpenAPI documents from your Zod types",

@@ -5,0 +5,0 @@ "homepage": "https://github.com/samchungy/zod-openapi#readme",

@@ -313,9 +313,9 @@ # zod-openapi

```typescript
{
"components": {
"schemas": {
MyJobSchema // this will register this Zod Schema as MyJobSchema unless `ref` in `.openapi()` is specified on the type
}
}
}
createDocument({
components: {
schemas: {
jobTitle, // this will register this Zod Schema as jobTitle unless `ref` in `.openapi()` is specified on the type
},
},
});
```

@@ -341,2 +341,21 @@

});
// or
const commonHeaders = z.object({
jobId: z.string(),
});
const path = z.string();
createDocument({
components: {
requestParams: {
header: commonHeaders,
path: z.object({ path }),
query: z.object({ query: z.string() }),
cookie: z.object({ cookie: z.string() }),
},
},
});
```

@@ -343,0 +362,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

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