@anatine/zod-openapi
Advanced tools
Comparing version 2.0.1 to 2.1.0
@@ -5,2 +5,9 @@ # Changelog | ||
## [2.1.0](https://github.com/anatine/zod-plugins/compare/zod-openapi-2.0.1...zod-openapi-2.1.0) (2023-08-03) | ||
### Features | ||
* hide definitions purposal ([469a22a](https://github.com/anatine/zod-plugins/commit/469a22ad933c9ea220b221e9d4b9fe42b63aa4f2)) | ||
### [2.0.1](https://github.com/anatine/zod-plugins/compare/zod-openapi-2.0.0...zod-openapi-2.0.1) (2023-06-30) | ||
@@ -7,0 +14,0 @@ |
{ | ||
"name": "@anatine/zod-openapi", | ||
"version": "2.0.1", | ||
"version": "2.1.0", | ||
"description": "Zod to OpenAPI converter", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
import type { SchemaObject } from 'openapi3-ts/oas31'; | ||
import { ZodTypeAny } from 'zod'; | ||
type AnatineSchemaObject = SchemaObject & { | ||
hideDefinitions?: string[]; | ||
}; | ||
export interface OpenApiZodAny extends ZodTypeAny { | ||
metaOpenApi?: SchemaObject | SchemaObject[]; | ||
metaOpenApi?: AnatineSchemaObject | AnatineSchemaObject[]; | ||
} | ||
export declare function extendApi<T extends OpenApiZodAny>(schema: T, SchemaObject?: SchemaObject): T; | ||
export declare function extendApi<T extends OpenApiZodAny>(schema: T, schemaObject?: AnatineSchemaObject): T; | ||
export declare function generateSchema(zodRef: OpenApiZodAny, useOutput?: boolean): SchemaObject; | ||
export {}; |
@@ -6,9 +6,12 @@ "use strict"; | ||
const zod_1 = require("zod"); | ||
function extendApi(schema, SchemaObject = {}) { | ||
schema.metaOpenApi = Object.assign(schema.metaOpenApi || {}, SchemaObject); | ||
function extendApi(schema, schemaObject = {}) { | ||
schema.metaOpenApi = Object.assign(schema.metaOpenApi || {}, schemaObject); | ||
return schema; | ||
} | ||
exports.extendApi = extendApi; | ||
function iterateZodObject({ zodRef, useOutput, }) { | ||
return Object.keys(zodRef.shape).reduce((carry, key) => (Object.assign(Object.assign({}, carry), { [key]: generateSchema(zodRef.shape[key], useOutput) })), {}); | ||
function iterateZodObject({ zodRef, useOutput, hideDefinitions, }) { | ||
const reduced = Object.keys(zodRef.shape) | ||
.filter((key) => (hideDefinitions === null || hideDefinitions === void 0 ? void 0 : hideDefinitions.includes(key)) === false) | ||
.reduce((carry, key) => (Object.assign(Object.assign({}, carry), { [key]: generateSchema(zodRef.shape[key], useOutput) })), {}); | ||
return reduced; | ||
} | ||
@@ -114,3 +117,12 @@ function parseTransformation({ zodRef, schemas, useOutput, }) { | ||
} | ||
function parseObject({ zodRef, schemas, useOutput, }) { | ||
function getExcludedDefinitionsFromSchema(schemas) { | ||
const excludedDefinitions = []; | ||
for (const schema of schemas) { | ||
if (Array.isArray(schema.hideDefinitions)) { | ||
excludedDefinitions.push(...schema.hideDefinitions); | ||
} | ||
} | ||
return excludedDefinitions; | ||
} | ||
function parseObject({ zodRef, schemas, useOutput, hideDefinitions, }) { | ||
var _a; | ||
@@ -136,7 +148,8 @@ let additionalProperties; | ||
const required = requiredProperties.length > 0 ? { required: requiredProperties } : {}; | ||
return (0, ts_deepmerge_1.default)(Object.assign(Object.assign({ type: 'object', properties: iterateZodObject({ | ||
return (0, ts_deepmerge_1.default)(Object.assign(Object.assign(Object.assign({ type: 'object', properties: iterateZodObject({ | ||
zodRef: zodRef, | ||
schemas, | ||
useOutput, | ||
}) }, required), additionalProperties), zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
hideDefinitions: getExcludedDefinitionsFromSchema(schemas), | ||
}) }, required), additionalProperties), hideDefinitions), zodRef.description ? { description: zodRef.description, hideDefinitions } : {}, ...schemas); | ||
} | ||
@@ -210,10 +223,9 @@ function parseRecord({ zodRef, schemas, useOutput, }) { | ||
const literals = contents; | ||
const type = literals | ||
.reduce((prev, content) => !prev || prev === typeof content._def.value ? | ||
typeof content._def.value : | ||
null, null); | ||
const type = literals.reduce((prev, content) => !prev || prev === typeof content._def.value | ||
? typeof content._def.value | ||
: null, null); | ||
if (type) { | ||
return (0, ts_deepmerge_1.default)({ | ||
type: type, | ||
enum: literals.map((literal) => literal._def.value) | ||
enum: literals.map((literal) => literal._def.value), | ||
}, zodRef.description ? { description: zodRef.description } : {}, ...schemas); | ||
@@ -220,0 +232,0 @@ } |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
42342
367