zod-openapi
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -5,3 +5,3 @@ "use strict"; | ||
const zod_1 = require("zod"); | ||
const schema_1 = require("./schema"); | ||
const metadata_1 = require("./schema/metadata"); | ||
const getDefaultComponents = (componentsObject) => { | ||
@@ -29,3 +29,3 @@ const defaultComponents = { schemas: {}, parameters: {}, headers: {} }; | ||
components.schemas[key] = { | ||
schemaObject: (0, schema_1.createSchemaOrRef)(schema, components), | ||
schemaObject: (0, metadata_1.createSchemaWithMetadata)(schema, components), | ||
zodSchema: schema, | ||
@@ -32,0 +32,0 @@ }; |
@@ -1,127 +0,144 @@ | ||
"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 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); | ||
} | ||
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 {}; | ||
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 {}; | ||
}; | ||
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`); | ||
} | ||
return { | ||
$ref: (0, components_1.createComponentSchemaRef)(schemaRef), | ||
}; | ||
const component = components.schemas[schemaRef]; | ||
if (component) { | ||
if (component.zodSchema !== zodSchema) { | ||
throw new Error(`schemaRef "${schemaRef}" is already registered`); | ||
} | ||
// 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 |
import { ZodType } from 'zod'; | ||
import { createSchemaOrRef } from './schema'; | ||
import { createSchemaWithMetadata } from './schema/metadata'; | ||
export const getDefaultComponents = (componentsObject) => { | ||
@@ -24,3 +24,3 @@ const defaultComponents = { schemas: {}, parameters: {}, headers: {} }; | ||
components.schemas[key] = { | ||
schemaObject: createSchemaOrRef(schema, components), | ||
schemaObject: createSchemaWithMetadata(schema, components), | ||
zodSchema: schema, | ||
@@ -27,0 +27,0 @@ }; |
{ | ||
"name": "zod-openapi", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "A plugin to create full OpenAPI documents from your Zod types", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/samchungy/zod-openapi#readme", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2182
173490