@kubb/plugin-zod 5.0.0-beta.3 → 5.0.0-beta.31
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/README.md +25 -5
- package/dist/index.cjs +644 -197
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +103 -34
- package/dist/index.js +640 -199
- package/dist/index.js.map +1 -1
- package/extension.yaml +968 -0
- package/package.json +10 -13
- package/src/components/Operations.tsx +6 -5
- package/src/components/Zod.tsx +1 -1
- package/src/generators/zodGenerator.tsx +210 -60
- package/src/plugin.ts +23 -20
- package/src/printers/printerZod.ts +91 -68
- package/src/printers/printerZodMini.ts +46 -49
- package/src/resolvers/resolverZod.ts +31 -19
- package/src/types.ts +57 -21
- package/src/utils.ts +113 -36
package/src/types.ts
CHANGED
|
@@ -18,6 +18,21 @@ export type ResolverZod = Resolver &
|
|
|
18
18
|
* `resolver.resolveSchemaTypeName('pet') // → 'Pet'`
|
|
19
19
|
*/
|
|
20
20
|
resolveSchemaTypeName(this: ResolverZod, name: string): string
|
|
21
|
+
/**
|
|
22
|
+
* Resolves the schema function name for the request (input) direction of a
|
|
23
|
+
* date-bearing component, where `Date` is encoded back to a wire `string`.
|
|
24
|
+
*
|
|
25
|
+
* @example Input schema names
|
|
26
|
+
* `resolver.resolveInputSchemaName('order') // → 'orderInputSchema'`
|
|
27
|
+
*/
|
|
28
|
+
resolveInputSchemaName(this: ResolverZod, name: string): string
|
|
29
|
+
/**
|
|
30
|
+
* Resolves the inferred type name for the request (input) direction variant.
|
|
31
|
+
*
|
|
32
|
+
* @example Input schema type names
|
|
33
|
+
* `resolver.resolveInputSchemaTypeName('order') // → 'OrderInputSchema'`
|
|
34
|
+
*/
|
|
35
|
+
resolveInputSchemaTypeName(this: ResolverZod, name: string): string
|
|
21
36
|
/**
|
|
22
37
|
* Resolves the generated type name from the schema.
|
|
23
38
|
*
|
|
@@ -75,85 +90,106 @@ export type ResolverZod = Resolver &
|
|
|
75
90
|
|
|
76
91
|
export type Options = {
|
|
77
92
|
/**
|
|
78
|
-
*
|
|
93
|
+
* Where the generated Zod schemas are written and how they are exported.
|
|
94
|
+
*
|
|
95
|
+
* @default { path: 'zod', barrel: { type: 'named' } }
|
|
79
96
|
*/
|
|
80
97
|
output?: Output
|
|
81
98
|
/**
|
|
82
|
-
*
|
|
99
|
+
* Split generated files into subfolders based on the operation's tag.
|
|
83
100
|
*/
|
|
84
101
|
group?: Group
|
|
85
102
|
/**
|
|
86
|
-
*
|
|
103
|
+
* Skip operations matching at least one entry in the list.
|
|
87
104
|
*/
|
|
88
105
|
exclude?: Array<Exclude>
|
|
89
106
|
/**
|
|
90
|
-
*
|
|
107
|
+
* Restrict generation to operations matching at least one entry in the list.
|
|
91
108
|
*/
|
|
92
109
|
include?: Array<Include>
|
|
93
110
|
/**
|
|
94
|
-
*
|
|
111
|
+
* Apply a different options object to operations matching a pattern.
|
|
95
112
|
*/
|
|
96
113
|
override?: Array<Override<ResolvedOptions>>
|
|
97
114
|
/**
|
|
98
|
-
*
|
|
115
|
+
* Module specifier used in the `import { z } from '...'` statement.
|
|
116
|
+
* Use `'zod/mini'` for the tree-shakeable bundle.
|
|
99
117
|
*
|
|
100
118
|
* @default 'zod'
|
|
101
119
|
*/
|
|
102
120
|
importPath?: 'zod' | 'zod/mini' | (string & {})
|
|
103
121
|
/**
|
|
104
|
-
*
|
|
122
|
+
* Tie each Zod schema to its TypeScript type from `@kubb/plugin-ts`. Requires
|
|
123
|
+
* `@kubb/plugin-ts` in the plugins list. TypeScript fails compilation when the
|
|
124
|
+
* schema drifts from the type.
|
|
105
125
|
*/
|
|
106
126
|
typed?: boolean
|
|
107
127
|
/**
|
|
108
|
-
*
|
|
128
|
+
* Export a `z.infer<typeof schema>` type alias next to every generated schema.
|
|
129
|
+
* Lets the Zod schema act as the single source of truth.
|
|
109
130
|
*/
|
|
110
131
|
inferred?: boolean
|
|
111
132
|
/**
|
|
112
|
-
*
|
|
133
|
+
* Wrap schemas in `z.coerce` so input is coerced before validation. Useful for
|
|
134
|
+
* form data and query params where everything arrives as a string.
|
|
135
|
+
* - `true` coerces strings, numbers, and dates.
|
|
136
|
+
* - Object form picks per-primitive coercion.
|
|
137
|
+
*
|
|
138
|
+
* @default false
|
|
139
|
+
* @see https://zod.dev/?id=coercion-for-primitives
|
|
113
140
|
*/
|
|
114
141
|
coercion?: boolean | { dates?: boolean; strings?: boolean; numbers?: boolean }
|
|
115
142
|
/**
|
|
116
|
-
*
|
|
143
|
+
* Emit an `operations.ts` file with request body, query/path params, and per-status
|
|
144
|
+
* response schemas grouped by operation.
|
|
117
145
|
*/
|
|
118
146
|
operations?: boolean
|
|
119
147
|
/**
|
|
120
|
-
* Validator
|
|
148
|
+
* Validator for `format: uuid` properties.
|
|
149
|
+
* - `'uuid'` — `z.uuid()`. Standard RFC 4122.
|
|
150
|
+
* - `'guid'` — `z.guid()`. Accepts Microsoft-style GUIDs.
|
|
121
151
|
*
|
|
122
152
|
* @default 'uuid'
|
|
123
153
|
*/
|
|
124
154
|
guidType?: 'uuid' | 'guid'
|
|
125
155
|
/**
|
|
126
|
-
*
|
|
156
|
+
* Switch to Zod Mini's functional API for better tree-shaking. Also defaults
|
|
157
|
+
* `importPath` to `'zod/mini'`.
|
|
127
158
|
*
|
|
128
159
|
* @default false
|
|
160
|
+
* @beta
|
|
129
161
|
*/
|
|
130
162
|
mini?: boolean
|
|
131
163
|
/**
|
|
132
|
-
*
|
|
133
|
-
*
|
|
134
|
-
*
|
|
164
|
+
* Wrap the generated Zod schema string with extra calls. Receives the raw output
|
|
165
|
+
* and the originating `SchemaNode`. Useful for round-tripping OpenAPI metadata
|
|
166
|
+
* back into Zod (e.g. `.openapi(...)`).
|
|
135
167
|
*/
|
|
136
168
|
wrapOutput?: (arg: { output: string; schema: ast.SchemaNode }) => string | undefined
|
|
137
169
|
/**
|
|
138
|
-
*
|
|
170
|
+
* Rename properties inside path/query/header schemas. Body schemas are unaffected.
|
|
171
|
+
*
|
|
172
|
+
* @note Must match the value of `paramsCasing` on `@kubb/plugin-ts`.
|
|
139
173
|
*/
|
|
140
174
|
paramsCasing?: 'camelcase'
|
|
141
175
|
/**
|
|
142
|
-
*
|
|
176
|
+
* Custom generators that run alongside the built-in Zod generators.
|
|
143
177
|
*/
|
|
144
178
|
generators?: Array<Generator<PluginZod>>
|
|
145
179
|
/**
|
|
146
|
-
* Override
|
|
180
|
+
* Override how schema and operation names are built. Methods you omit fall back
|
|
181
|
+
* to the default `resolverZod`.
|
|
147
182
|
*/
|
|
148
183
|
resolver?: Partial<ResolverZod> & ThisType<ResolverZod>
|
|
149
184
|
/**
|
|
150
|
-
*
|
|
185
|
+
* Replace the Zod handler for a specific schema type (`'integer'`, `'date'`, ...).
|
|
186
|
+
* When `mini: true`, overrides target the Zod Mini printer instead.
|
|
151
187
|
*/
|
|
152
188
|
printer?: {
|
|
153
189
|
nodes?: PrinterZodNodes | PrinterZodMiniNodes
|
|
154
190
|
}
|
|
155
191
|
/**
|
|
156
|
-
* AST visitor to
|
|
192
|
+
* AST visitor applied to each schema or operation node before printing.
|
|
157
193
|
*/
|
|
158
194
|
transformer?: ast.Visitor
|
|
159
195
|
}
|
|
@@ -163,7 +199,7 @@ type ResolvedOptions = {
|
|
|
163
199
|
exclude: Array<Exclude>
|
|
164
200
|
include: Array<Include> | undefined
|
|
165
201
|
override: Array<Override<ResolvedOptions>>
|
|
166
|
-
group: Group |
|
|
202
|
+
group: Group | null
|
|
167
203
|
typed: NonNullable<Options['typed']>
|
|
168
204
|
inferred: NonNullable<Options['inferred']>
|
|
169
205
|
importPath: NonNullable<Options['importPath']>
|
package/src/utils.ts
CHANGED
|
@@ -12,6 +12,99 @@ export function shouldCoerce(coercion: PluginZod['resolvedOptions']['coercion']
|
|
|
12
12
|
return !!coercion[type]
|
|
13
13
|
}
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* A codec for a schema node whose runtime type differs from its JSON wire type:
|
|
17
|
+
* the output (response) schema decodes wire → runtime, and the input (request)
|
|
18
|
+
* variant encodes runtime → wire.
|
|
19
|
+
*
|
|
20
|
+
* To support another codec type, append a `Codec` to `codecs` and route that
|
|
21
|
+
* type's printer node handler through `getCodec`.
|
|
22
|
+
*/
|
|
23
|
+
export type Codec = {
|
|
24
|
+
/**
|
|
25
|
+
* Whether this node is encoded/decoded by this codec.
|
|
26
|
+
*/
|
|
27
|
+
matches(node: ast.SchemaNode): boolean
|
|
28
|
+
/**
|
|
29
|
+
* Output direction (response): decode the wire value into the runtime type.
|
|
30
|
+
*/
|
|
31
|
+
decode(node: ast.SchemaNode): string
|
|
32
|
+
/**
|
|
33
|
+
* Input direction (request): encode the runtime value back to the wire value.
|
|
34
|
+
*/
|
|
35
|
+
encode(node: ast.SchemaNode): string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* `dateType: 'date'` fields are typed as `Date` but travel as ISO `string`s.
|
|
40
|
+
* Output decodes `string → Date`; input encodes `Date → string`, preserving the
|
|
41
|
+
* `date` (`YYYY-MM-DD`) vs `date-time` precision carried on `node.format`.
|
|
42
|
+
*/
|
|
43
|
+
const dateCodec: Codec = {
|
|
44
|
+
matches(node) {
|
|
45
|
+
return node.type === 'date' && node.representation === 'date'
|
|
46
|
+
},
|
|
47
|
+
decode(node) {
|
|
48
|
+
return node.format === 'date' ? 'z.iso.date().transform((value) => new Date(value))' : 'z.iso.datetime().transform((value) => new Date(value))'
|
|
49
|
+
},
|
|
50
|
+
encode(node) {
|
|
51
|
+
return node.format === 'date' ? 'z.date().transform((value) => value.toISOString().slice(0, 10))' : 'z.date().transform((value) => value.toISOString())'
|
|
52
|
+
},
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Registered codecs, checked in order.
|
|
57
|
+
*/
|
|
58
|
+
const codecs: Array<Codec> = [dateCodec]
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Returns the codec for this node, or `undefined` when the node needs no
|
|
62
|
+
* encode/decode (its wire and runtime types match).
|
|
63
|
+
*/
|
|
64
|
+
export function getCodec(node: ast.SchemaNode | undefined): Codec | undefined {
|
|
65
|
+
if (!node) return undefined
|
|
66
|
+
return codecs.find((codec) => codec.matches(node))
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Returns `true` when the node itself is encoded/decoded by a codec.
|
|
71
|
+
*/
|
|
72
|
+
export function hasCodec(node: ast.SchemaNode | undefined): boolean {
|
|
73
|
+
return getCodec(node) !== undefined
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Returns `true` when the schema transitively contains a codec node —
|
|
78
|
+
* a value whose runtime type differs from its wire type (see {@link hasCodec}),
|
|
79
|
+
* so it must be decoded (response) or encoded (request) at the validation boundary.
|
|
80
|
+
* `$ref`s are followed via their resolved schema; a `seen` set guards cycles.
|
|
81
|
+
*/
|
|
82
|
+
export function containsCodec(node: ast.SchemaNode | undefined, seen: Set<string> = new Set()): boolean {
|
|
83
|
+
if (!node) return false
|
|
84
|
+
|
|
85
|
+
if (hasCodec(node)) return true
|
|
86
|
+
|
|
87
|
+
if (node.type === 'ref') {
|
|
88
|
+
if (!node.ref) return false
|
|
89
|
+
const refName = ast.extractRefName(node.ref)
|
|
90
|
+
if (refName) {
|
|
91
|
+
if (seen.has(refName)) return false
|
|
92
|
+
seen.add(refName)
|
|
93
|
+
}
|
|
94
|
+
const resolved = ast.syncSchemaRef(node)
|
|
95
|
+
if (resolved.type === 'ref') return false
|
|
96
|
+
return containsCodec(resolved, seen)
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
const children: Array<ast.SchemaNode | undefined> = []
|
|
100
|
+
if ('properties' in node && node.properties) children.push(...node.properties.map((prop) => prop.schema))
|
|
101
|
+
if ('items' in node && node.items) children.push(...node.items)
|
|
102
|
+
if ('members' in node && node.members) children.push(...node.members)
|
|
103
|
+
if ('additionalProperties' in node && node.additionalProperties && node.additionalProperties !== true) children.push(node.additionalProperties)
|
|
104
|
+
|
|
105
|
+
return children.some((child) => containsCodec(child, seen))
|
|
106
|
+
}
|
|
107
|
+
|
|
15
108
|
/**
|
|
16
109
|
* Collects all resolved schema names for an operation's parameters and responses
|
|
17
110
|
* into a single lookup object, useful for building imports and type references.
|
|
@@ -39,11 +132,11 @@ export function buildSchemaNames(node: ast.OperationNode, { params, resolver }:
|
|
|
39
132
|
responses['default'] = resolver.resolveResponseName(node)
|
|
40
133
|
|
|
41
134
|
return {
|
|
42
|
-
request: node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) :
|
|
135
|
+
request: node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : null,
|
|
43
136
|
parameters: {
|
|
44
|
-
path: pathParam ? resolver.resolvePathParamsName(node, pathParam) :
|
|
45
|
-
query: queryParam ? resolver.resolveQueryParamsName(node, queryParam) :
|
|
46
|
-
header: headerParam ? resolver.resolveHeaderParamsName(node, headerParam) :
|
|
137
|
+
path: pathParam ? resolver.resolvePathParamsName(node, pathParam) : null,
|
|
138
|
+
query: queryParam ? resolver.resolveQueryParamsName(node, queryParam) : null,
|
|
139
|
+
header: headerParam ? resolver.resolveHeaderParamsName(node, headerParam) : null,
|
|
47
140
|
},
|
|
48
141
|
responses,
|
|
49
142
|
errors,
|
|
@@ -133,7 +226,7 @@ export function lengthConstraints({ min, max, pattern }: LengthConstraints): str
|
|
|
133
226
|
* Build `.check(z.minimum(), z.maximum())` for `zod/mini` numeric constraints.
|
|
134
227
|
*/
|
|
135
228
|
export function numberChecksMini({ min, max, exclusiveMinimum, exclusiveMaximum, multipleOf }: NumericConstraints): string {
|
|
136
|
-
const checks: string
|
|
229
|
+
const checks: Array<string> = []
|
|
137
230
|
if (min !== undefined) checks.push(`z.minimum(${min})`)
|
|
138
231
|
if (max !== undefined) checks.push(`z.maximum(${max})`)
|
|
139
232
|
if (exclusiveMinimum !== undefined) checks.push(`z.minimum(${exclusiveMinimum}, { exclusive: true })`)
|
|
@@ -146,7 +239,7 @@ export function numberChecksMini({ min, max, exclusiveMinimum, exclusiveMaximum,
|
|
|
146
239
|
* Build `.check(z.minLength(), z.maxLength(), z.regex())` for `zod/mini` length constraints.
|
|
147
240
|
*/
|
|
148
241
|
export function lengthChecksMini({ min, max, pattern }: LengthConstraints): string {
|
|
149
|
-
const checks: string
|
|
242
|
+
const checks: Array<string> = []
|
|
150
243
|
if (min !== undefined) checks.push(`z.minLength(${min})`)
|
|
151
244
|
if (max !== undefined) checks.push(`z.maxLength(${max})`)
|
|
152
245
|
if (pattern !== undefined) checks.push(`z.regex(${toRegExpString(pattern, null)})`)
|
|
@@ -158,21 +251,14 @@ export function lengthChecksMini({ min, max, pattern }: LengthConstraints): stri
|
|
|
158
251
|
* to a schema value string using the chainable Zod v4 API.
|
|
159
252
|
*/
|
|
160
253
|
export function applyModifiers({ value, nullable, optional, nullish, defaultValue, description }: ModifierOptions): string {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
}
|
|
167
|
-
|
|
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
|
|
254
|
+
const withModifier = (() => {
|
|
255
|
+
if (nullish || (nullable && optional)) return `${value}.nullish()`
|
|
256
|
+
if (optional) return `${value}.optional()`
|
|
257
|
+
if (nullable) return `${value}.nullable()`
|
|
258
|
+
return value
|
|
259
|
+
})()
|
|
260
|
+
const withDefault = defaultValue !== undefined ? `${withModifier}.default(${formatDefault(defaultValue)})` : withModifier
|
|
261
|
+
return description ? `${withDefault}.describe(${stringify(description)})` : withDefault
|
|
176
262
|
}
|
|
177
263
|
|
|
178
264
|
/**
|
|
@@ -180,21 +266,12 @@ export function applyModifiers({ value, nullable, optional, nullish, defaultValu
|
|
|
180
266
|
* (`z.nullable()`, `z.optional()`, `z.nullish()`).
|
|
181
267
|
*/
|
|
182
268
|
export function applyMiniModifiers({ value, nullable, optional, nullish, defaultValue }: Omit<ModifierOptions, 'description'>): string {
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
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
|
|
269
|
+
const withModifier = (() => {
|
|
270
|
+
if (nullish) return `z.nullish(${value})`
|
|
271
|
+
const withNullable = nullable ? `z.nullable(${value})` : value
|
|
272
|
+
return optional ? `z.optional(${withNullable})` : withNullable
|
|
273
|
+
})()
|
|
274
|
+
return defaultValue !== undefined ? `z._default(${withModifier}, ${formatDefault(defaultValue)})` : withModifier
|
|
198
275
|
}
|
|
199
276
|
|
|
200
277
|
type BuildGroupedParamsSchemaOptions = {
|