zod-openapi
Advanced tools
Comparing version 0.5.0 to 0.5.1
@@ -32,2 +32,5 @@ "use strict"; | ||
const createSchema = (zodSchema, state) => { | ||
if (zodSchema._def.openapi?.type) { | ||
return (0, unknown_1.createUnknownSchema)(zodSchema); | ||
} | ||
if (zodSchema instanceof zod_1.ZodString) { | ||
@@ -34,0 +37,0 @@ return (0, string_1.createStringSchema)(zodSchema); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.createPreprocessSchema = void 0; | ||
const unknown_1 = require("./unknown"); | ||
const _1 = require("."); | ||
const createPreprocessSchema = (zodPreprocess, state) => { | ||
if (state.type === 'output') { | ||
state.effectType = 'output'; | ||
return (0, _1.createSchemaOrRef)(zodPreprocess._def.schema, state); | ||
} | ||
return (0, unknown_1.createUnknownSchema)(zodPreprocess); | ||
}; | ||
const createPreprocessSchema = (zodPreprocess, state) => (0, _1.createSchemaOrRef)(zodPreprocess._def.schema, state); | ||
exports.createPreprocessSchema = createPreprocessSchema; | ||
//# sourceMappingURL=preprocess.js.map |
@@ -7,4 +7,7 @@ "use strict"; | ||
const createTransformSchema = (zodTransform, state) => { | ||
if (state.type === 'input') { | ||
state.effectType = 'input'; | ||
const creationType = zodTransform._def.openapi?.effectType ?? state.type; | ||
if (creationType === 'input') { | ||
if (state.type === 'input') { | ||
state.effectType = 'input'; | ||
} | ||
return (0, _1.createSchemaOrRef)(zodTransform._def.schema, state); | ||
@@ -11,0 +14,0 @@ } |
@@ -8,6 +8,7 @@ "use strict"; | ||
const zodType = zodSchema.constructor.name; | ||
const schemaName = zodSchema instanceof zod_1.ZodEffects | ||
? `${zodType} - ${zodSchema._def.effect.type}` | ||
: zodType; | ||
throw new Error(`Unknown schema ${schemaName}. Please assign it a manual type`); | ||
if (zodSchema instanceof zod_1.ZodEffects) { | ||
const schemaName = `${zodType} - ${zodSchema._def.effect.type}`; | ||
throw new Error(`Unknown schema ${schemaName}. Please assign it a manual 'type', wrap it in a ZodPipeline or change the 'effectType'.`); | ||
} | ||
throw new Error(`Unknown schema ${zodType}. Please assign it a manual 'type'.`); | ||
} | ||
@@ -14,0 +15,0 @@ return { |
@@ -29,2 +29,5 @@ import { ZodArray, ZodBoolean, ZodCatch, ZodDate, ZodDefault, ZodDiscriminatedUnion, ZodEffects, ZodEnum, ZodIntersection, ZodLiteral, ZodNativeEnum, ZodNull, ZodNullable, ZodNumber, ZodObject, ZodOptional, ZodPipeline, ZodRecord, ZodString, ZodTuple, ZodUnion, } from 'zod'; | ||
export const createSchema = (zodSchema, state) => { | ||
if (zodSchema._def.openapi?.type) { | ||
return createUnknownSchema(zodSchema); | ||
} | ||
if (zodSchema instanceof ZodString) { | ||
@@ -31,0 +34,0 @@ return createStringSchema(zodSchema); |
@@ -1,10 +0,3 @@ | ||
import { createUnknownSchema } from './unknown'; | ||
import { createSchemaOrRef } from '.'; | ||
export const createPreprocessSchema = (zodPreprocess, state) => { | ||
if (state.type === 'output') { | ||
state.effectType = 'output'; | ||
return createSchemaOrRef(zodPreprocess._def.schema, state); | ||
} | ||
return createUnknownSchema(zodPreprocess); | ||
}; | ||
export const createPreprocessSchema = (zodPreprocess, state) => createSchemaOrRef(zodPreprocess._def.schema, state); | ||
//# sourceMappingURL=preprocess.js.map |
import { createUnknownSchema } from './unknown'; | ||
import { createSchemaOrRef } from '.'; | ||
export const createTransformSchema = (zodTransform, state) => { | ||
if (state.type === 'input') { | ||
state.effectType = 'input'; | ||
const creationType = zodTransform._def.openapi?.effectType ?? state.type; | ||
if (creationType === 'input') { | ||
if (state.type === 'input') { | ||
state.effectType = 'input'; | ||
} | ||
return createSchemaOrRef(zodTransform._def.schema, state); | ||
@@ -7,0 +10,0 @@ } |
@@ -5,6 +5,7 @@ import { ZodEffects } from 'zod'; | ||
const zodType = zodSchema.constructor.name; | ||
const schemaName = zodSchema instanceof ZodEffects | ||
? `${zodType} - ${zodSchema._def.effect.type}` | ||
: zodType; | ||
throw new Error(`Unknown schema ${schemaName}. Please assign it a manual type`); | ||
if (zodSchema instanceof ZodEffects) { | ||
const schemaName = `${zodType} - ${zodSchema._def.effect.type}`; | ||
throw new Error(`Unknown schema ${schemaName}. Please assign it a manual 'type', wrap it in a ZodPipeline or change the 'effectType'.`); | ||
} | ||
throw new Error(`Unknown schema ${zodType}. Please assign it a manual 'type'.`); | ||
} | ||
@@ -11,0 +12,0 @@ return { |
@@ -17,2 +17,6 @@ import { oas30, oas31 } from 'openapi3-ts'; | ||
refType?: CreationType; | ||
/** | ||
* Use this field to set the created type of an effect. | ||
*/ | ||
effectType?: CreationType; | ||
param?: Partial<oas31.ParameterObject> & { | ||
@@ -19,0 +23,0 @@ example?: TInferred; |
{ | ||
"name": "zod-openapi", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"description": "A library to create full OpenAPI documents from your Zod types", | ||
@@ -44,5 +44,5 @@ "homepage": "https://github.com/samchungy/zod-openapi#readme", | ||
"devDependencies": { | ||
"@types/node": "^16.18.3", | ||
"@types/node": "^18.15.11", | ||
"commitizen": "^4.2.4", | ||
"skuba": "6.0.0", | ||
"skuba": "6.0.2", | ||
"zod": "3.21.4" | ||
@@ -49,0 +49,0 @@ }, |
@@ -317,6 +317,8 @@ # zod-openapi | ||
`.transform()` and `.preprocess()` are complicated because they are technically two types (input & output). This means that we need to understand which type you are after. This means if you are adding the ZodSchema directly to the `components` section, we need to know whether you want the response or request type created. You can do this by setting the `refType` field to `input` or `output` in `.openapi()`. This defaults to `output` by default. | ||
`.transform()` is complicated because it technically comprises of two types (input & output). This means that we need to understand which type you are creating. If you are adding the ZodSchema directly to the `components` section, context is required with knowing to create an input schema or an output schema. You can do this by setting the `refType` field to `input` or `output` in `.openapi()`. This defaults to `output` by default. | ||
If you use a registered schema with a ZodEffect in both a request and response schema you will receive an error because we cannot register two different schemas under the same `ref`. | ||
`.preprocess()` will always return the `output` type even if we are creating an input schema. If a different input type is required you can achieve this with a `.transform()` combined with a `.pipe()` or simply declare a manual `type` in `.openapi()`. | ||
If a registered schema with a ZodEffect is used in both a request and response schema you will receive an error because the created schema for each will be different. To override the creation type for a specific ZodEffect, add an `.openapi()` field to the ZodEffect and set the `effectType` field to `input` or `output`. | ||
#### Parameters | ||
@@ -367,3 +369,3 @@ | ||
- `transform` support for request schemas. Wrap your transform in a ZodPipeline to enable response schema creation or declare a manual `type` in the `.openapi()` section of that schema. | ||
- `pre-process` support for response schemas. Wrap your transform in a ZodPipeline to enable request schema creation or declare a manual `type` in the `.openapi()` section of that schema. | ||
- `pre-process` full support. | ||
- `refine` full support. | ||
@@ -370,0 +372,0 @@ - ZodEnum |
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
205431
2553
451