@kubb/plugin-zod 5.0.0-beta.15 → 5.0.0-beta.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/types.ts CHANGED
@@ -75,85 +75,106 @@ export type ResolverZod = Resolver &
75
75
 
76
76
  export type Options = {
77
77
  /**
78
- * @default 'zod'
78
+ * Where the generated Zod schemas are written and how they are exported.
79
+ *
80
+ * @default { path: 'zod', barrel: { type: 'named' } }
79
81
  */
80
82
  output?: Output
81
83
  /**
82
- * Group the Zod schemas based on the provided name.
84
+ * Split generated files into subfolders based on the operation's tag.
83
85
  */
84
86
  group?: Group
85
87
  /**
86
- * Tags, operations, or paths to exclude from generation.
88
+ * Skip operations matching at least one entry in the list.
87
89
  */
88
90
  exclude?: Array<Exclude>
89
91
  /**
90
- * Tags, operations, or paths to include in generation.
92
+ * Restrict generation to operations matching at least one entry in the list.
91
93
  */
92
94
  include?: Array<Include>
93
95
  /**
94
- * Override options for specific tags, operations, or paths.
96
+ * Apply a different options object to operations matching a pattern.
95
97
  */
96
98
  override?: Array<Override<ResolvedOptions>>
97
99
  /**
98
- * Import path for Zod package.
100
+ * Module specifier used in the `import { z } from '...'` statement.
101
+ * Use `'zod/mini'` for the tree-shakeable bundle.
99
102
  *
100
103
  * @default 'zod'
101
104
  */
102
105
  importPath?: 'zod' | 'zod/mini' | (string & {})
103
106
  /**
104
- * Add TypeScript type annotations to generated schemas.
107
+ * Tie each Zod schema to its TypeScript type from `@kubb/plugin-ts`. Requires
108
+ * `@kubb/plugin-ts` in the plugins list. TypeScript fails compilation when the
109
+ * schema drifts from the type.
105
110
  */
106
111
  typed?: boolean
107
112
  /**
108
- * Return schemas as inferred types using `z.infer`.
113
+ * Export a `z.infer<typeof schema>` type alias next to every generated schema.
114
+ * Lets the Zod schema act as the single source of truth.
109
115
  */
110
116
  inferred?: boolean
111
117
  /**
112
- * Apply coercion to string values or configure coercion per type.
118
+ * Wrap schemas in `z.coerce` so input is coerced before validation. Useful for
119
+ * form data and query params where everything arrives as a string.
120
+ * - `true` coerces strings, numbers, and dates.
121
+ * - Object form picks per-primitive coercion.
122
+ *
123
+ * @default false
124
+ * @see https://zod.dev/?id=coercion-for-primitives
113
125
  */
114
126
  coercion?: boolean | { dates?: boolean; strings?: boolean; numbers?: boolean }
115
127
  /**
116
- * Generate operation-level schemas (grouped by operationId).
128
+ * Emit an `operations.ts` file with request body, query/path params, and per-status
129
+ * response schemas grouped by operation.
117
130
  */
118
131
  operations?: boolean
119
132
  /**
120
- * Validator to use for UUID format: `uuid` or `guid`.
133
+ * Validator for `format: uuid` properties.
134
+ * - `'uuid'` — `z.uuid()`. Standard RFC 4122.
135
+ * - `'guid'` — `z.guid()`. Accepts Microsoft-style GUIDs.
121
136
  *
122
137
  * @default 'uuid'
123
138
  */
124
139
  guidType?: 'uuid' | 'guid'
125
140
  /**
126
- * Use Zod Mini's functional API for better tree-shaking.
141
+ * Switch to Zod Mini's functional API for better tree-shaking. Also defaults
142
+ * `importPath` to `'zod/mini'`.
127
143
  *
128
144
  * @default false
145
+ * @beta
129
146
  */
130
147
  mini?: boolean
131
148
  /**
132
- * Callback to wrap the generated schema output.
133
- *
134
- * Useful for adding metadata like `.openapi()` or extension helpers.
149
+ * Wrap the generated Zod schema string with extra calls. Receives the raw output
150
+ * and the originating `SchemaNode`. Useful for round-tripping OpenAPI metadata
151
+ * back into Zod (e.g. `.openapi(...)`).
135
152
  */
136
153
  wrapOutput?: (arg: { output: string; schema: ast.SchemaNode }) => string | undefined
137
154
  /**
138
- * Apply casing to parameter names.
155
+ * Rename properties inside path/query/header schemas. Body schemas are unaffected.
156
+ *
157
+ * @note Must match the value of `paramsCasing` on `@kubb/plugin-ts`.
139
158
  */
140
159
  paramsCasing?: 'camelcase'
141
160
  /**
142
- * Additional generators alongside the default generators.
161
+ * Custom generators that run alongside the built-in Zod generators.
143
162
  */
144
163
  generators?: Array<Generator<PluginZod>>
145
164
  /**
146
- * Override naming conventions for schema names and types.
165
+ * Override how schema and operation names are built. Methods you omit fall back
166
+ * to the default `resolverZod`.
147
167
  */
148
168
  resolver?: Partial<ResolverZod> & ThisType<ResolverZod>
149
169
  /**
150
- * Override printer node handlers to customize rendering of specific schema types.
170
+ * Replace the Zod handler for a specific schema type (`'integer'`, `'date'`, ...).
171
+ * When `mini: true`, overrides target the Zod Mini printer instead.
151
172
  */
152
173
  printer?: {
153
174
  nodes?: PrinterZodNodes | PrinterZodMiniNodes
154
175
  }
155
176
  /**
156
- * AST visitor to transform schema and operation nodes.
177
+ * AST visitor applied to each schema or operation node before printing.
157
178
  */
158
179
  transformer?: ast.Visitor
159
180
  }
@@ -163,7 +184,7 @@ type ResolvedOptions = {
163
184
  exclude: Array<Exclude>
164
185
  include: Array<Include> | undefined
165
186
  override: Array<Override<ResolvedOptions>>
166
- group: Group | undefined
187
+ group: Group | null
167
188
  typed: NonNullable<Options['typed']>
168
189
  inferred: NonNullable<Options['inferred']>
169
190
  importPath: NonNullable<Options['importPath']>
package/src/utils.ts CHANGED
@@ -39,11 +39,11 @@ export function buildSchemaNames(node: ast.OperationNode, { params, resolver }:
39
39
  responses['default'] = resolver.resolveResponseName(node)
40
40
 
41
41
  return {
42
- request: node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : undefined,
42
+ request: node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : null,
43
43
  parameters: {
44
- path: pathParam ? resolver.resolvePathParamsName(node, pathParam) : undefined,
45
- query: queryParam ? resolver.resolveQueryParamsName(node, queryParam) : undefined,
46
- header: headerParam ? resolver.resolveHeaderParamsName(node, headerParam) : undefined,
44
+ path: pathParam ? resolver.resolvePathParamsName(node, pathParam) : null,
45
+ query: queryParam ? resolver.resolveQueryParamsName(node, queryParam) : null,
46
+ header: headerParam ? resolver.resolveHeaderParamsName(node, headerParam) : null,
47
47
  },
48
48
  responses,
49
49
  errors,
@@ -133,7 +133,7 @@ export function lengthConstraints({ min, max, pattern }: LengthConstraints): str
133
133
  * Build `.check(z.minimum(), z.maximum())` for `zod/mini` numeric constraints.
134
134
  */
135
135
  export function numberChecksMini({ min, max, exclusiveMinimum, exclusiveMaximum, multipleOf }: NumericConstraints): string {
136
- const checks: string[] = []
136
+ const checks: Array<string> = []
137
137
  if (min !== undefined) checks.push(`z.minimum(${min})`)
138
138
  if (max !== undefined) checks.push(`z.maximum(${max})`)
139
139
  if (exclusiveMinimum !== undefined) checks.push(`z.minimum(${exclusiveMinimum}, { exclusive: true })`)
@@ -146,7 +146,7 @@ export function numberChecksMini({ min, max, exclusiveMinimum, exclusiveMaximum,
146
146
  * Build `.check(z.minLength(), z.maxLength(), z.regex())` for `zod/mini` length constraints.
147
147
  */
148
148
  export function lengthChecksMini({ min, max, pattern }: LengthConstraints): string {
149
- const checks: string[] = []
149
+ const checks: Array<string> = []
150
150
  if (min !== undefined) checks.push(`z.minLength(${min})`)
151
151
  if (max !== undefined) checks.push(`z.maxLength(${max})`)
152
152
  if (pattern !== undefined) checks.push(`z.regex(${toRegExpString(pattern, null)})`)
@@ -158,21 +158,14 @@ export function lengthChecksMini({ min, max, pattern }: LengthConstraints): stri
158
158
  * to a schema value string using the chainable Zod v4 API.
159
159
  */
160
160
  export function applyModifiers({ value, nullable, optional, nullish, defaultValue, description }: ModifierOptions): string {
161
- let result = value
162
- if (nullish || (nullable && optional)) {
163
- result = `${result}.nullish()`
164
- } else if (optional) {
165
- result = `${result}.optional()`
166
- } else if (nullable) {
167
- result = `${result}.nullable()`
168
- }
169
- if (defaultValue !== undefined) {
170
- result = `${result}.default(${formatDefault(defaultValue)})`
171
- }
172
- if (description) {
173
- result = `${result}.describe(${stringify(description)})`
174
- }
175
- return result
161
+ const withModifier = (() => {
162
+ if (nullish || (nullable && optional)) return `${value}.nullish()`
163
+ if (optional) return `${value}.optional()`
164
+ if (nullable) return `${value}.nullable()`
165
+ return value
166
+ })()
167
+ const withDefault = defaultValue !== undefined ? `${withModifier}.default(${formatDefault(defaultValue)})` : withModifier
168
+ return description ? `${withDefault}.describe(${stringify(description)})` : withDefault
176
169
  }
177
170
 
178
171
  /**
@@ -180,21 +173,12 @@ export function applyModifiers({ value, nullable, optional, nullish, defaultValu
180
173
  * (`z.nullable()`, `z.optional()`, `z.nullish()`).
181
174
  */
182
175
  export function applyMiniModifiers({ value, nullable, optional, nullish, defaultValue }: Omit<ModifierOptions, 'description'>): string {
183
- let result = value
184
- if (nullish) {
185
- result = `z.nullish(${result})`
186
- } else {
187
- if (nullable) {
188
- result = `z.nullable(${result})`
189
- }
190
- if (optional) {
191
- result = `z.optional(${result})`
192
- }
193
- }
194
- if (defaultValue !== undefined) {
195
- result = `z._default(${result}, ${formatDefault(defaultValue)})`
196
- }
197
- return result
176
+ const withModifier = (() => {
177
+ if (nullish) return `z.nullish(${value})`
178
+ const withNullable = nullable ? `z.nullable(${value})` : value
179
+ return optional ? `z.optional(${withNullable})` : withNullable
180
+ })()
181
+ return defaultValue !== undefined ? `z._default(${withModifier}, ${formatDefault(defaultValue)})` : withModifier
198
182
  }
199
183
 
200
184
  type BuildGroupedParamsSchemaOptions = {