@kubb/plugin-zod 3.11.1 → 3.12.1

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/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/plugin.ts"],"names":["options"],"mappings":";;;;;;;;AAcO,IAAM,aAAgB,GAAA;AAEhB,IAAA,SAAA,GAAY,YAAwB,CAAA,CAAC,OAAY,KAAA;AAC5D,EAAM,MAAA;AAAA,IACJ,MAAS,GAAA,EAAE,IAAM,EAAA,KAAA,EAAO,YAAY,OAAQ,EAAA;AAAA,IAC5C,KAAA;AAAA,IACA,UAAU,EAAC;AAAA,IACX,OAAA;AAAA,IACA,WAAW,EAAC;AAAA,IACZ,eAAe,EAAC;AAAA,IAChB,QAAW,GAAA,QAAA;AAAA,IACX,WAAc,GAAA,KAAA;AAAA,IACd,KAAQ,GAAA,KAAA;AAAA,IACR,SAAS,EAAC;AAAA,IACV,UAAa,GAAA,KAAA;AAAA,IACb,UAAa,GAAA,KAAA;AAAA,IACb,QAAW,GAAA,KAAA;AAAA,IACX,QAAW,GAAA,KAAA;AAAA,IACX,UAAA,GAAa,CAAC,YAAc,EAAA,UAAA,GAAa,sBAAsB,MAAS,CAAA,CAAE,OAAO,OAAO,CAAA;AAAA,IACxF,UAAa,GAAA,MAAA;AAAA,IACb;AAAA,GACE,GAAA,OAAA;AAEJ,EAAO,OAAA;AAAA,IACL,IAAM,EAAA,aAAA;AAAA,IACN,OAAS,EAAA;AAAA,MACP,MAAA;AAAA,MACA,YAAA;AAAA,MACA,OAAA;AAAA,MACA,OAAA;AAAA,MACA,QAAA;AAAA,MACA,KAAA;AAAA,MACA,QAAA;AAAA,MACA,WAAA;AAAA,MACA,MAAA;AAAA,MACA,UAAA;AAAA,MACA,QAAA;AAAA,MACA,UAAA;AAAA,MACA,QAAA;AAAA,MACA,KAAA;AAAA,MACA;AAAA,KACF;AAAA,IACA,GAAA,EAAK,CAAC,aAA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,OAChB,CAAA;AAED,MAAA,MAAM,WAAc,GAAA,MAAM,eAAgB,CAAA,KAAA,CAAM,GAAG,UAAU,CAAA;AAC7D,MAAM,MAAA,IAAA,CAAK,OAAQ,CAAA,GAAG,WAAW,CAAA;AAEjC,MAAA,MAAM,kBAAqB,GAAA,IAAI,kBAAmB,CAAA,IAAA,CAAK,OAAO,OAAS,EAAA;AAAA,QACrE,GAAA;AAAA,QACA,eAAe,IAAK,CAAA,aAAA;AAAA,QACpB,QAAQ,IAAK,CAAA,MAAA;AAAA,QACb,WAAA;AAAA,QACA,OAAA;AAAA,QACA,OAAA;AAAA,QACA,QAAA;AAAA,QACA;AAAA,OACD,CAAA;AAED,MAAA,MAAM,cAAiB,GAAA,MAAM,kBAAmB,CAAA,KAAA,CAAM,GAAG,UAAU,CAAA;AACnE,MAAM,MAAA,IAAA,CAAK,OAAQ,CAAA,GAAG,cAAc,CAAA;AAEpC,MAAA,MAAM,WAAc,GAAA,MAAM,IAAK,CAAA,WAAA,CAAY,cAAe,CAAA;AAAA,QACxD,IAAA,EAAM,OAAO,UAAc,IAAA,OAAA;AAAA,QAC3B,IAAA;AAAA,QACA,MAAA;AAAA,QACA,KAAA,EAAO,KAAK,WAAY,CAAA,KAAA;AAAA,QACxB,IAAM,EAAA;AAAA,UACJ,SAAA,EAAW,KAAK,MAAO,CAAA;AAAA,SACzB;AAAA,QACA,QAAQ,IAAK,CAAA;AAAA,OACd,CAAA;AAED,MAAM,MAAA,IAAA,CAAK,OAAQ,CAAA,GAAG,WAAW,CAAA;AAAA;AACnC,GACF;AACF,CAAC","file":"index.js","sourcesContent":["import path from 'node:path'\n\nimport { FileManager, type Group, PluginManager, createPlugin } from '@kubb/core'\nimport { camelCase, pascalCase } from '@kubb/core/transformers'\nimport { OperationGenerator, SchemaGenerator, pluginOasName } from '@kubb/plugin-oas'\n\nimport { pluginTsName } from '@kubb/plugin-ts'\n\nimport type { Plugin } from '@kubb/core'\nimport type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'\nimport { operationsGenerator } from './generators'\nimport { zodGenerator } from './generators/zodGenerator.tsx'\nimport type { PluginZod } from './types.ts'\n\nexport const pluginZodName = 'plugin-zod' satisfies PluginZod['name']\n\nexport const pluginZod = createPlugin<PluginZod>((options) => {\n const {\n output = { path: 'zod', barrelType: 'named' },\n group,\n exclude = [],\n include,\n override = [],\n transformers = {},\n dateType = 'string',\n unknownType = 'any',\n typed = false,\n mapper = {},\n operations = false,\n importPath = 'zod',\n coercion = false,\n inferred = false,\n generators = [zodGenerator, operations ? operationsGenerator : undefined].filter(Boolean),\n wrapOutput = undefined,\n contentType,\n } = options\n\n return {\n name: pluginZodName,\n options: {\n output,\n transformers,\n include,\n exclude,\n override,\n typed,\n dateType,\n unknownType,\n mapper,\n importPath,\n coercion,\n operations,\n inferred,\n group,\n wrapOutput,\n },\n pre: [pluginOasName, typed ? pluginTsName : undefined].filter(Boolean),\n resolvePath(baseName, pathMode, options) {\n const root = path.resolve(this.config.root, this.config.output.path)\n const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))\n\n if (mode === 'single') {\n /**\n * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend\n * Other plugins then need to call addOrAppend instead of just add from the fileManager class\n */\n return path.resolve(root, output.path)\n }\n\n if (group && (options?.group?.path || options?.group?.tag)) {\n const groupName: Group['name'] = group?.name\n ? group.name\n : (ctx) => {\n if (group?.type === 'path') {\n return `${ctx.group.split('/')[1]}`\n }\n return `${camelCase(ctx.group)}Controller`\n }\n\n return path.resolve(\n root,\n output.path,\n groupName({\n group: group.type === 'path' ? options.group.path! : options.group.tag!,\n }),\n baseName,\n )\n }\n\n return path.resolve(root, output.path, baseName)\n },\n resolveName(name, type) {\n let resolvedName = camelCase(name, {\n suffix: type ? 'schema' : undefined,\n isFile: type === 'file',\n })\n\n if (type === 'type') {\n resolvedName = pascalCase(resolvedName)\n }\n\n if (type) {\n return transformers?.name?.(resolvedName, type) || resolvedName\n }\n\n return resolvedName\n },\n async buildStart() {\n const [swaggerPlugin]: [Plugin<SwaggerPluginOptions>] = PluginManager.getDependedPlugins<SwaggerPluginOptions>(this.plugins, [pluginOasName])\n\n const oas = await swaggerPlugin.context.getOas()\n const root = path.resolve(this.config.root, this.config.output.path)\n const mode = FileManager.getMode(path.resolve(root, output.path))\n\n const schemaGenerator = new SchemaGenerator(this.plugin.options, {\n oas,\n pluginManager: this.pluginManager,\n plugin: this.plugin,\n contentType,\n include: undefined,\n override,\n mode,\n output: output.path,\n })\n\n const schemaFiles = await schemaGenerator.build(...generators)\n await this.addFile(...schemaFiles)\n\n const operationGenerator = new OperationGenerator(this.plugin.options, {\n oas,\n pluginManager: this.pluginManager,\n plugin: this.plugin,\n contentType,\n exclude,\n include,\n override,\n mode,\n })\n\n const operationFiles = await operationGenerator.build(...generators)\n await this.addFile(...operationFiles)\n\n const barrelFiles = await this.fileManager.getBarrelFiles({\n type: output.barrelType ?? 'named',\n root,\n output,\n files: this.fileManager.files,\n meta: {\n pluginKey: this.plugin.key,\n },\n logger: this.logger,\n })\n\n await this.addFile(...barrelFiles)\n },\n }\n})\n"]}
1
+ {"version":3,"sources":["../src/plugin.ts"],"names":["options"],"mappings":";;;;;;;;AAoBO,IAAM,aAAgB,GAAA;AAEhB,IAAA,SAAA,GAAY,YAAwB,CAAA,CAAC,OAAY,KAAA;AAC5D,EAAM,MAAA;AAAA,IACJ,MAAS,GAAA,EAAE,IAAM,EAAA,KAAA,EAAO,YAAY,OAAQ,EAAA;AAAA,IAC5C,KAAA;AAAA,IACA,UAAU,EAAC;AAAA,IACX,OAAA;AAAA,IACA,WAAW,EAAC;AAAA,IACZ,eAAe,EAAC;AAAA,IAChB,QAAW,GAAA,QAAA;AAAA,IACX,WAAc,GAAA,KAAA;AAAA,IACd,KAAQ,GAAA,KAAA;AAAA,IACR,SAAS,EAAC;AAAA,IACV,UAAa,GAAA,KAAA;AAAA,IACb,OAAU,GAAA,GAAA;AAAA,IACV,UAAA,GAAa,OAAY,K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AAc,GAAA,MAAM,IAAK,CAAA,WAAA,CAAY,cAAe,CAAA;AAAA,QACxD,IAAA,EAAM,OAAO,UAAc,IAAA,OAAA;AAAA,QAC3B,IAAA;AAAA,QACA,MAAA;AAAA,QACA,KAAA,EAAO,KAAK,WAAY,CAAA,KAAA;AAAA,QACxB,IAAM,EAAA;AAAA,UACJ,SAAA,EAAW,KAAK,MAAO,CAAA;AAAA,SACzB;AAAA,QACA,QAAQ,IAAK,CAAA;AAAA,OACd,CAAA;AAED,MAAM,MAAA,IAAA,CAAK,OAAQ,CAAA,GAAG,WAAW,CAAA;AAAA;AACnC,GACF;AACF,CAAC","file":"index.js","sourcesContent":["import path from \"node:path\";\nimport type { Plugin } from \"@kubb/core\";\nimport {\n createPlugin,\n FileManager,\n type Group,\n PluginManager,\n} from \"@kubb/core\";\nimport { camelCase, pascalCase } from \"@kubb/core/transformers\";\nimport type { PluginOas as SwaggerPluginOptions } from \"@kubb/plugin-oas\";\nimport {\n OperationGenerator,\n pluginOasName,\n SchemaGenerator,\n} from \"@kubb/plugin-oas\";\nimport { pluginTsName } from \"@kubb/plugin-ts\";\nimport { operationsGenerator } from \"./generators\";\nimport { zodGenerator } from \"./generators/zodGenerator.tsx\";\nimport type { PluginZod } from \"./types.ts\";\n\nexport const pluginZodName = \"plugin-zod\" satisfies PluginZod[\"name\"];\n\nexport const pluginZod = createPlugin<PluginZod>((options) => {\n const {\n output = { path: \"zod\", barrelType: \"named\" },\n group,\n exclude = [],\n include,\n override = [],\n transformers = {},\n dateType = \"string\",\n unknownType = \"any\",\n typed = false,\n mapper = {},\n operations = false,\n version = \"3\",\n importPath = version === \"4\" ? \"zod/v4\" : \"zod\",\n coercion = false,\n inferred = false,\n generators = [\n zodGenerator,\n operations ? operationsGenerator : undefined,\n ].filter(Boolean),\n wrapOutput = undefined,\n contentType,\n } = options;\n\n return {\n name: pluginZodName,\n options: {\n output,\n transformers,\n include,\n exclude,\n override,\n typed,\n dateType,\n unknownType,\n mapper,\n importPath,\n coercion,\n operations,\n inferred,\n group,\n wrapOutput,\n version,\n },\n pre: [pluginOasName, typed ? pluginTsName : undefined].filter(Boolean),\n resolvePath(baseName, pathMode, options) {\n const root = path.resolve(this.config.root, this.config.output.path);\n const mode =\n pathMode ?? FileManager.getMode(path.resolve(root, output.path));\n\n if (mode === \"single\") {\n /**\n * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend\n * Other plugins then need to call addOrAppend instead of just add from the fileManager class\n */\n return path.resolve(root, output.path);\n }\n\n if (group && (options?.group?.path || options?.group?.tag)) {\n const groupName: Group[\"name\"] = group?.name\n ? group.name\n : (ctx) => {\n if (group?.type === \"path\") {\n return `${ctx.group.split(\"/\")[1]}`;\n }\n return `${camelCase(ctx.group)}Controller`;\n };\n\n return path.resolve(\n root,\n output.path,\n groupName({\n group:\n group.type === \"path\" ? options.group.path! : options.group.tag!,\n }),\n baseName\n );\n }\n\n return path.resolve(root, output.path, baseName);\n },\n resolveName(name, type) {\n let resolvedName = camelCase(name, {\n suffix: type ? \"schema\" : undefined,\n isFile: type === \"file\",\n });\n\n if (type === \"type\") {\n resolvedName = pascalCase(resolvedName);\n }\n\n if (type) {\n return transformers?.name?.(resolvedName, type) || resolvedName;\n }\n\n return resolvedName;\n },\n async buildStart() {\n const [swaggerPlugin]: [Plugin<SwaggerPluginOptions>] =\n PluginManager.getDependedPlugins<SwaggerPluginOptions>(this.plugins, [\n pluginOasName,\n ]);\n\n const oas = await swaggerPlugin.context.getOas();\n const root = path.resolve(this.config.root, this.config.output.path);\n const mode = FileManager.getMode(path.resolve(root, output.path));\n\n const schemaGenerator = new SchemaGenerator(this.plugin.options, {\n oas,\n pluginManager: this.pluginManager,\n plugin: this.plugin,\n contentType,\n include: undefined,\n override,\n mode,\n output: output.path,\n });\n\n const schemaFiles = await schemaGenerator.build(...generators);\n await this.addFile(...schemaFiles);\n\n const operationGenerator = new OperationGenerator(this.plugin.options, {\n oas,\n pluginManager: this.pluginManager,\n plugin: this.plugin,\n contentType,\n exclude,\n include,\n override,\n mode,\n });\n\n const operationFiles = await operationGenerator.build(...generators);\n await this.addFile(...operationFiles);\n\n const barrelFiles = await this.fileManager.getBarrelFiles({\n type: output.barrelType ?? \"named\",\n root,\n output,\n files: this.fileManager.files,\n meta: {\n pluginKey: this.plugin.key,\n },\n logger: this.logger,\n });\n\n await this.addFile(...barrelFiles);\n },\n };\n});\n"]}
@@ -82,6 +82,11 @@ type Options = {
82
82
  parentName?: string;
83
83
  }, defaultSchemas: Schema[]) => Schema[] | undefined;
84
84
  };
85
+ /**
86
+ * Which version of Zod should be used
87
+ * @default '3
88
+ */
89
+ version?: '3' | '4';
85
90
  /**
86
91
  * Callback function to wrap the output of the generated zod schema
87
92
  *
@@ -112,6 +117,7 @@ type ResolvedOptions = {
112
117
  coercion: NonNullable<Options['coercion']>;
113
118
  operations: NonNullable<Options['operations']>;
114
119
  wrapOutput: Options['wrapOutput'];
120
+ version: NonNullable<Options['version']>;
115
121
  };
116
122
  type PluginZod = PluginFactoryOptions<'plugin-zod', Options, ResolvedOptions, never, ResolvePathOptions>;
117
123
 
@@ -82,6 +82,11 @@ type Options = {
82
82
  parentName?: string;
83
83
  }, defaultSchemas: Schema[]) => Schema[] | undefined;
84
84
  };
85
+ /**
86
+ * Which version of Zod should be used
87
+ * @default '3
88
+ */
89
+ version?: '3' | '4';
85
90
  /**
86
91
  * Callback function to wrap the output of the generated zod schema
87
92
  *
@@ -112,6 +117,7 @@ type ResolvedOptions = {
112
117
  coercion: NonNullable<Options['coercion']>;
113
118
  operations: NonNullable<Options['operations']>;
114
119
  wrapOutput: Options['wrapOutput'];
120
+ version: NonNullable<Options['version']>;
115
121
  };
116
122
  type PluginZod = PluginFactoryOptions<'plugin-zod', Options, ResolvedOptions, never, ResolvePathOptions>;
117
123
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubb/plugin-zod",
3
- "version": "3.11.1",
3
+ "version": "3.12.1",
4
4
  "description": "Generator plugin-zod",
5
5
  "keywords": [
6
6
  "typescript",
@@ -66,21 +66,21 @@
66
66
  "!/**/__tests__/**"
67
67
  ],
68
68
  "dependencies": {
69
- "@kubb/core": "3.11.1",
70
- "@kubb/oas": "3.11.1",
71
- "@kubb/parser-ts": "3.11.1",
72
- "@kubb/plugin-oas": "3.11.1",
73
- "@kubb/plugin-ts": "3.11.1",
74
- "@kubb/react": "3.11.1"
69
+ "@kubb/core": "3.12.1",
70
+ "@kubb/oas": "3.12.1",
71
+ "@kubb/parser-ts": "3.12.1",
72
+ "@kubb/plugin-oas": "3.12.1",
73
+ "@kubb/plugin-ts": "3.12.1",
74
+ "@kubb/react": "3.12.1"
75
75
  },
76
76
  "devDependencies": {
77
77
  "@asteasolutions/zod-to-openapi": "^7.3.4",
78
78
  "@hono/zod-openapi": "0.19.2",
79
79
  "tsup": "^8.5.0",
80
80
  "zod": "~3.24.4",
81
- "@kubb/config-ts": "3.11.1",
82
- "@kubb/config-tsup": "3.11.1",
83
- "@kubb/plugin-oas": "3.11.1"
81
+ "@kubb/config-ts": "3.12.1",
82
+ "@kubb/config-tsup": "3.12.1",
83
+ "@kubb/plugin-oas": "3.12.1"
84
84
  },
85
85
  "peerDependencies": {
86
86
  "@kubb/react": "^3.0.0"
@@ -1,10 +1,9 @@
1
1
  import transformers from '@kubb/core/transformers'
2
- import { type Schema, schemaKeywords } from '@kubb/plugin-oas'
3
- import { isKeyword } from '@kubb/plugin-oas'
2
+ import type { SchemaObject } from '@kubb/oas'
3
+ import { isKeyword, type Schema, schemaKeywords } from '@kubb/plugin-oas'
4
4
  import { Const, File, Type } from '@kubb/react'
5
5
  import * as parserZod from '../parser.ts'
6
6
  import type { PluginZod } from '../types.ts'
7
- import type { SchemaObject } from '@kubb/oas'
8
7
 
9
8
  type Props = {
10
9
  name: string
@@ -17,9 +16,10 @@ type Props = {
17
16
  mapper: PluginZod['resolvedOptions']['mapper']
18
17
  keysToOmit?: string[]
19
18
  wrapOutput?: PluginZod['resolvedOptions']['wrapOutput']
19
+ version: '3' | '4'
20
20
  }
21
21
 
22
- export function Zod({ name, typeName, tree, rawSchema, inferTypeName, mapper, coercion, keysToOmit, description, wrapOutput }: Props) {
22
+ export function Zod({ name, typeName, tree, rawSchema, inferTypeName, mapper, coercion, keysToOmit, description, wrapOutput, version }: Props) {
23
23
  const hasTuple = tree.some((item) => isKeyword(item, schemaKeywords.tuple))
24
24
  const schemas = parserZod.sort(tree).filter((item) => {
25
25
  if (hasTuple && (isKeyword(item, schemaKeywords.min) || isKeyword(item, schemaKeywords.max))) {
@@ -31,7 +31,10 @@ export function Zod({ name, typeName, tree, rawSchema, inferTypeName, mapper, co
31
31
 
32
32
  const output = schemas
33
33
  .map((schema, _index, siblings) =>
34
- parserZod.parse({ parent: undefined, current: schema, siblings }, { name, keysToOmit, typeName, description, mapper, coercion, wrapOutput, rawSchema }),
34
+ parserZod.parse(
35
+ { parent: undefined, current: schema, siblings },
36
+ { name, keysToOmit, typeName, description, mapper, coercion, wrapOutput, rawSchema, version },
37
+ ),
35
38
  )
36
39
  .filter(Boolean)
37
40
  .join('')
@@ -42,12 +45,16 @@ export function Zod({ name, typeName, tree, rawSchema, inferTypeName, mapper, co
42
45
 
43
46
  if (lastSchema && isKeyword(lastSchema, schemaKeywords.nullable)) {
44
47
  if (firstSchema && isKeyword(firstSchema, schemaKeywords.ref)) {
45
- suffix = '.unwrap().schema.unwrap()'
48
+ if (version === '3') {
49
+ suffix = '.unwrap().schema.unwrap()'
50
+ } else {
51
+ suffix = '.unwrap().unwrap()'
52
+ }
46
53
  } else {
47
54
  suffix = '.unwrap()'
48
55
  }
49
56
  } else {
50
- if (firstSchema && isKeyword(firstSchema, schemaKeywords.ref)) {
57
+ if (firstSchema && isKeyword(firstSchema, schemaKeywords.ref) && version === '3') {
51
58
  suffix = '.schema'
52
59
  }
53
60
  }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Generated by Kubb (https://kubb.dev/).
3
+ * Do not edit manually.
4
+ */
5
+ import { z } from 'zod'
6
+
7
+ export const pet = z.object({
8
+ id: z.int(),
9
+ name: z.string(),
10
+ date: z.date().optional(),
11
+ uuid: z.uuid().optional(),
12
+ email: z.email().optional(),
13
+ pattern: z
14
+ .string()
15
+ .regex(/^[a-zA-Z0-9]{3}$/)
16
+ .optional(),
17
+ tag: z.string().min(5).max(100).optional(),
18
+ })
@@ -78,6 +78,7 @@ export const zodGenerator = createReactGenerator<PluginZod>({
78
78
  coercion={coercion}
79
79
  keysToOmit={keysToOmit}
80
80
  wrapOutput={wrapOutput}
81
+ version={plugin.options.version}
81
82
  />
82
83
  </Oas.Schema>
83
84
  )
@@ -97,7 +98,7 @@ export const zodGenerator = createReactGenerator<PluginZod>({
97
98
  )
98
99
  },
99
100
  Schema({ schema, options }) {
100
- const { coercion, inferred, typed, mapper, importPath, wrapOutput } = options
101
+ const { coercion, inferred, typed, mapper, importPath, wrapOutput, version } = options
101
102
 
102
103
  const { getName, getFile, getImports } = useSchemaManager()
103
104
  const {
@@ -146,6 +147,7 @@ export const zodGenerator = createReactGenerator<PluginZod>({
146
147
  mapper={mapper}
147
148
  coercion={coercion}
148
149
  wrapOutput={wrapOutput}
150
+ version={version}
149
151
  />
150
152
  </File>
151
153
  )
package/src/parser.ts CHANGED
@@ -1,13 +1,9 @@
1
- import { PackageManager } from '@kubb/core'
2
1
  import transformers from '@kubb/core/transformers'
3
2
  import type { SchemaObject } from '@kubb/oas'
4
3
 
5
4
  import type { Schema, SchemaKeywordBase, SchemaMapper } from '@kubb/plugin-oas'
6
5
  import { isKeyword, SchemaGenerator, type SchemaKeywordMapper, type SchemaTree, schemaKeywords } from '@kubb/plugin-oas'
7
6
 
8
- const packageManager = new PackageManager()
9
- const isV4 = () => packageManager.isValidSync('zod', '>=4') || packageManager.isValidSync('zod', 'next')
10
-
11
7
  const zodKeywordMapper = {
12
8
  any: () => 'z.any()',
13
9
  unknown: () => 'z.unknown()',
@@ -17,9 +13,9 @@ const zodKeywordMapper = {
17
13
  .filter(Boolean)
18
14
  .join('')
19
15
  },
20
- integer: (coercion?: boolean, min?: number, max?: number) => {
16
+ integer: (coercion?: boolean, min?: number, max?: number, version: '3' | '4' = '3') => {
21
17
  return [
22
- coercion ? 'z.coerce.number().int()' : isV4() ? 'z.int()' : 'z.number().int()',
18
+ coercion ? 'z.coerce.number().int()' : version === '4' ? 'z.int()' : 'z.number().int()',
23
19
  min !== undefined ? `.min(${min})` : undefined,
24
20
  max !== undefined ? `.max(${max})` : undefined,
25
21
  ]
@@ -36,8 +32,8 @@ const zodKeywordMapper = {
36
32
  ${value}
37
33
  })`
38
34
  },
39
- object: (value?: string, strict?: boolean) => {
40
- if (isV4() && strict) {
35
+ object: (value?: string, strict?: boolean, version: '3' | '4' = '3') => {
36
+ if (version === '4' && strict) {
41
37
  return `z.strictObject({
42
38
  ${value}
43
39
  })`
@@ -81,13 +77,13 @@ const zodKeywordMapper = {
81
77
  /**
82
78
  * ISO 8601
83
79
  */
84
- datetime: (offset = false, local = false) => {
80
+ datetime: (offset = false, local = false, version: '3' | '4' = '3') => {
85
81
  if (offset) {
86
- return isV4() ? `z.iso.datetime({ offset: ${offset} })` : `z.string().datetime({ offset: ${offset} })`
82
+ return version === '4' ? `z.iso.datetime({ offset: ${offset} })` : `z.string().datetime({ offset: ${offset} })`
87
83
  }
88
84
 
89
85
  if (local) {
90
- return isV4() ? `z.iso.datetime({ local: ${local} })` : `z.string().datetime({ local: ${local} })`
86
+ return version === '4' ? `z.iso.datetime({ local: ${local} })` : `z.string().datetime({ local: ${local} })`
91
87
  }
92
88
 
93
89
  return 'z.string().datetime()'
@@ -97,9 +93,9 @@ const zodKeywordMapper = {
97
93
  * Type `'string'` ISO date format (YYYY-MM-DD)
98
94
  * @default ISO date format (YYYY-MM-DD)
99
95
  */
100
- date: (type: 'date' | 'string' = 'string', coercion?: boolean) => {
96
+ date: (type: 'date' | 'string' = 'string', coercion?: boolean, version: '3' | '4' = '3') => {
101
97
  if (type === 'string') {
102
- return isV4() ? 'z.iso.date()' : 'z.string().date()'
98
+ return version === '4' ? 'z.iso.date()' : 'z.string().date()'
103
99
  }
104
100
 
105
101
  if (coercion) {
@@ -113,9 +109,9 @@ const zodKeywordMapper = {
113
109
  * Type `'string'` ISO time format (HH:mm:ss[.SSSSSS])
114
110
  * @default ISO time format (HH:mm:ss[.SSSSSS])
115
111
  */
116
- time: (type: 'date' | 'string' = 'string', coercion?: boolean) => {
112
+ time: (type: 'date' | 'string' = 'string', coercion?: boolean, version: '3' | '4' = '3') => {
117
113
  if (type === 'string') {
118
- return isV4() ? 'z.iso.time()' : 'z.string().time()'
114
+ return version === '4' ? 'z.iso.time()' : 'z.string().time()'
119
115
  }
120
116
 
121
117
  if (coercion) {
@@ -124,8 +120,10 @@ const zodKeywordMapper = {
124
120
 
125
121
  return 'z.date()'
126
122
  },
127
- uuid: (coercion?: boolean) => (isV4() ? (coercion ? 'z.coerce.string().uuid()' : 'z.uuid()') : coercion ? 'z.coerce.string().uuid()' : 'z.string().uuid()'),
128
- url: (coercion?: boolean) => (isV4() ? (coercion ? 'z.coerce.string().url()' : 'z.url()') : coercion ? 'z.coerce.string().url()' : 'z.string().url()'),
123
+ uuid: (coercion?: boolean, version: '3' | '4' = '3') =>
124
+ version === '4' ? (coercion ? 'z.coerce.string().uuid()' : 'z.uuid()') : coercion ? 'z.coerce.string().uuid()' : 'z.string().uuid()',
125
+ url: (coercion?: boolean, version: '3' | '4' = '3') =>
126
+ version === '4' ? (coercion ? 'z.coerce.string().url()' : 'z.url()') : coercion ? 'z.coerce.string().url()' : 'z.string().url()',
129
127
  default: (value?: string | number | true | object) => {
130
128
  if (typeof value === 'object') {
131
129
  return '.default({})'
@@ -138,20 +136,20 @@ const zodKeywordMapper = {
138
136
  max: (value?: number) => `.max(${value ?? ''})`,
139
137
  optional: () => '.optional()',
140
138
  matches: (value = '', coercion?: boolean) => (coercion ? `z.coerce.string().regex(${value})` : `z.string().regex(${value})`),
141
- email: (coercion?: boolean) =>
142
- isV4() ? (coercion ? 'z.coerce.string().email()' : 'z.email()') : coercion ? 'z.coerce.string().email()' : 'z.string().email()',
139
+ email: (coercion?: boolean, version: '3' | '4' = '3') =>
140
+ version === '4' ? (coercion ? 'z.coerce.string().email()' : 'z.email()') : coercion ? 'z.coerce.string().email()' : 'z.string().email()',
143
141
  firstName: undefined,
144
142
  lastName: undefined,
145
143
  password: undefined,
146
144
  phone: undefined,
147
145
  readOnly: undefined,
148
146
  writeOnly: undefined,
149
- ref: (value?: string) => {
147
+ ref: (value?: string, version: '3' | '4' = '3') => {
150
148
  if (!value) {
151
149
  return undefined
152
150
  }
153
151
 
154
- return isV4() ? value : `z.lazy(() => ${value})`
152
+ return version === '4' ? value : `z.lazy(() => ${value})`
155
153
  },
156
154
  blob: () => 'z.instanceof(File)',
157
155
  deprecated: undefined,
@@ -219,6 +217,7 @@ type ParserOptions = {
219
217
  coercion?: boolean | { dates?: boolean; strings?: boolean; numbers?: boolean }
220
218
  wrapOutput?: (opts: { output: string; schema: any }) => string | undefined
221
219
  rawSchema: SchemaObject
220
+ version: '3' | '4'
222
221
  }
223
222
 
224
223
  export function parse({ parent, current, name, siblings }: SchemaTree, options: ParserOptions): string | undefined {
@@ -304,7 +303,7 @@ export function parse({ parent, current, name, siblings }: SchemaTree, options:
304
303
  }
305
304
 
306
305
  if (isKeyword(current, schemaKeywords.ref)) {
307
- return zodKeywordMapper.ref(current.args?.name)
306
+ return zodKeywordMapper.ref(current.args?.name, options.version)
308
307
  }
309
308
 
310
309
  if (isKeyword(current, schemaKeywords.object)) {
@@ -332,7 +331,7 @@ export function parse({ parent, current, name, siblings }: SchemaTree, options:
332
331
  ? options.wrapOutput({ output: baseSchemaOutput, schema: options.rawSchema?.properties?.[name] }) || baseSchemaOutput
333
332
  : baseSchemaOutput
334
333
 
335
- if (isV4() && SchemaGenerator.find(schemas, schemaKeywords.ref)) {
334
+ if (options.version === '4' && SchemaGenerator.find(schemas, schemaKeywords.ref)) {
336
335
  return `get ${name}(){
337
336
  return ${objectValue}
338
337
  }`
@@ -350,7 +349,7 @@ export function parse({ parent, current, name, siblings }: SchemaTree, options:
350
349
  : undefined
351
350
 
352
351
  const text = [
353
- zodKeywordMapper.object(properties, current.args?.strict),
352
+ zodKeywordMapper.object(properties, current.args?.strict, options.version),
354
353
  additionalProperties ? zodKeywordMapper.catchall(additionalProperties) : undefined,
355
354
  ].filter(Boolean)
356
355
 
@@ -397,15 +396,15 @@ export function parse({ parent, current, name, siblings }: SchemaTree, options:
397
396
  }
398
397
 
399
398
  if (isKeyword(current, schemaKeywords.uuid)) {
400
- return zodKeywordMapper.uuid(shouldCoerce(options.coercion, 'strings'))
399
+ return zodKeywordMapper.uuid(shouldCoerce(options.coercion, 'strings'), options.version)
401
400
  }
402
401
 
403
402
  if (isKeyword(current, schemaKeywords.email)) {
404
- return zodKeywordMapper.email(shouldCoerce(options.coercion, 'strings'))
403
+ return zodKeywordMapper.email(shouldCoerce(options.coercion, 'strings'), options.version)
405
404
  }
406
405
 
407
406
  if (isKeyword(current, schemaKeywords.url)) {
408
- return zodKeywordMapper.url(shouldCoerce(options.coercion, 'strings'))
407
+ return zodKeywordMapper.url(shouldCoerce(options.coercion, 'strings'), options.version)
409
408
  }
410
409
 
411
410
  if (isKeyword(current, schemaKeywords.number)) {
@@ -413,7 +412,7 @@ export function parse({ parent, current, name, siblings }: SchemaTree, options:
413
412
  }
414
413
 
415
414
  if (isKeyword(current, schemaKeywords.integer)) {
416
- return zodKeywordMapper.integer(shouldCoerce(options.coercion, 'numbers'))
415
+ return zodKeywordMapper.integer(shouldCoerce(options.coercion, 'numbers'), undefined, undefined, options.version)
417
416
  }
418
417
 
419
418
  if (isKeyword(current, schemaKeywords.min)) {
@@ -424,15 +423,15 @@ export function parse({ parent, current, name, siblings }: SchemaTree, options:
424
423
  }
425
424
 
426
425
  if (isKeyword(current, schemaKeywords.datetime)) {
427
- return zodKeywordMapper.datetime(current.args.offset, current.args.local)
426
+ return zodKeywordMapper.datetime(current.args.offset, current.args.local, options.version)
428
427
  }
429
428
 
430
429
  if (isKeyword(current, schemaKeywords.date)) {
431
- return zodKeywordMapper.date(current.args.type, shouldCoerce(options.coercion, 'dates'))
430
+ return zodKeywordMapper.date(current.args.type, shouldCoerce(options.coercion, 'dates'), options.version)
432
431
  }
433
432
 
434
433
  if (isKeyword(current, schemaKeywords.time)) {
435
- return zodKeywordMapper.time(current.args.type, shouldCoerce(options.coercion, 'dates'))
434
+ return zodKeywordMapper.time(current.args.type, shouldCoerce(options.coercion, 'dates'), options.version)
436
435
  }
437
436
 
438
437
  if (current.keyword in zodKeywordMapper && 'args' in current) {
package/src/plugin.ts CHANGED
@@ -1,39 +1,49 @@
1
- import path from 'node:path'
2
-
3
- import { FileManager, type Group, PluginManager, createPlugin } from '@kubb/core'
4
- import { camelCase, pascalCase } from '@kubb/core/transformers'
5
- import { OperationGenerator, SchemaGenerator, pluginOasName } from '@kubb/plugin-oas'
6
-
7
- import { pluginTsName } from '@kubb/plugin-ts'
8
-
9
- import type { Plugin } from '@kubb/core'
10
- import type { PluginOas as SwaggerPluginOptions } from '@kubb/plugin-oas'
11
- import { operationsGenerator } from './generators'
12
- import { zodGenerator } from './generators/zodGenerator.tsx'
13
- import type { PluginZod } from './types.ts'
14
-
15
- export const pluginZodName = 'plugin-zod' satisfies PluginZod['name']
1
+ import path from "node:path";
2
+ import type { Plugin } from "@kubb/core";
3
+ import {
4
+ createPlugin,
5
+ FileManager,
6
+ type Group,
7
+ PluginManager,
8
+ } from "@kubb/core";
9
+ import { camelCase, pascalCase } from "@kubb/core/transformers";
10
+ import type { PluginOas as SwaggerPluginOptions } from "@kubb/plugin-oas";
11
+ import {
12
+ OperationGenerator,
13
+ pluginOasName,
14
+ SchemaGenerator,
15
+ } from "@kubb/plugin-oas";
16
+ import { pluginTsName } from "@kubb/plugin-ts";
17
+ import { operationsGenerator } from "./generators";
18
+ import { zodGenerator } from "./generators/zodGenerator.tsx";
19
+ import type { PluginZod } from "./types.ts";
20
+
21
+ export const pluginZodName = "plugin-zod" satisfies PluginZod["name"];
16
22
 
17
23
  export const pluginZod = createPlugin<PluginZod>((options) => {
18
24
  const {
19
- output = { path: 'zod', barrelType: 'named' },
25
+ output = { path: "zod", barrelType: "named" },
20
26
  group,
21
27
  exclude = [],
22
28
  include,
23
29
  override = [],
24
30
  transformers = {},
25
- dateType = 'string',
26
- unknownType = 'any',
31
+ dateType = "string",
32
+ unknownType = "any",
27
33
  typed = false,
28
34
  mapper = {},
29
35
  operations = false,
30
- importPath = 'zod',
36
+ version = "3",
37
+ importPath = version === "4" ? "zod/v4" : "zod",
31
38
  coercion = false,
32
39
  inferred = false,
33
- generators = [zodGenerator, operations ? operationsGenerator : undefined].filter(Boolean),
40
+ generators = [
41
+ zodGenerator,
42
+ operations ? operationsGenerator : undefined,
43
+ ].filter(Boolean),
34
44
  wrapOutput = undefined,
35
45
  contentType,
36
- } = options
46
+ } = options;
37
47
 
38
48
  return {
39
49
  name: pluginZodName,
@@ -53,64 +63,70 @@ export const pluginZod = createPlugin<PluginZod>((options) => {
53
63
  inferred,
54
64
  group,
55
65
  wrapOutput,
66
+ version,
56
67
  },
57
68
  pre: [pluginOasName, typed ? pluginTsName : undefined].filter(Boolean),
58
69
  resolvePath(baseName, pathMode, options) {
59
- const root = path.resolve(this.config.root, this.config.output.path)
60
- const mode = pathMode ?? FileManager.getMode(path.resolve(root, output.path))
70
+ const root = path.resolve(this.config.root, this.config.output.path);
71
+ const mode =
72
+ pathMode ?? FileManager.getMode(path.resolve(root, output.path));
61
73
 
62
- if (mode === 'single') {
74
+ if (mode === "single") {
63
75
  /**
64
76
  * when output is a file then we will always append to the same file(output file), see fileManager.addOrAppend
65
77
  * Other plugins then need to call addOrAppend instead of just add from the fileManager class
66
78
  */
67
- return path.resolve(root, output.path)
79
+ return path.resolve(root, output.path);
68
80
  }
69
81
 
70
82
  if (group && (options?.group?.path || options?.group?.tag)) {
71
- const groupName: Group['name'] = group?.name
83
+ const groupName: Group["name"] = group?.name
72
84
  ? group.name
73
85
  : (ctx) => {
74
- if (group?.type === 'path') {
75
- return `${ctx.group.split('/')[1]}`
86
+ if (group?.type === "path") {
87
+ return `${ctx.group.split("/")[1]}`;
76
88
  }
77
- return `${camelCase(ctx.group)}Controller`
78
- }
89
+ return `${camelCase(ctx.group)}Controller`;
90
+ };
79
91
 
80
92
  return path.resolve(
81
93
  root,
82
94
  output.path,
83
95
  groupName({
84
- group: group.type === 'path' ? options.group.path! : options.group.tag!,
96
+ group:
97
+ group.type === "path" ? options.group.path! : options.group.tag!,
85
98
  }),
86
- baseName,
87
- )
99
+ baseName
100
+ );
88
101
  }
89
102
 
90
- return path.resolve(root, output.path, baseName)
103
+ return path.resolve(root, output.path, baseName);
91
104
  },
92
105
  resolveName(name, type) {
93
106
  let resolvedName = camelCase(name, {
94
- suffix: type ? 'schema' : undefined,
95
- isFile: type === 'file',
96
- })
107
+ suffix: type ? "schema" : undefined,
108
+ isFile: type === "file",
109
+ });
97
110
 
98
- if (type === 'type') {
99
- resolvedName = pascalCase(resolvedName)
111
+ if (type === "type") {
112
+ resolvedName = pascalCase(resolvedName);
100
113
  }
101
114
 
102
115
  if (type) {
103
- return transformers?.name?.(resolvedName, type) || resolvedName
116
+ return transformers?.name?.(resolvedName, type) || resolvedName;
104
117
  }
105
118
 
106
- return resolvedName
119
+ return resolvedName;
107
120
  },
108
121
  async buildStart() {
109
- const [swaggerPlugin]: [Plugin<SwaggerPluginOptions>] = PluginManager.getDependedPlugins<SwaggerPluginOptions>(this.plugins, [pluginOasName])
122
+ const [swaggerPlugin]: [Plugin<SwaggerPluginOptions>] =
123
+ PluginManager.getDependedPlugins<SwaggerPluginOptions>(this.plugins, [
124
+ pluginOasName,
125
+ ]);
110
126
 
111
- const oas = await swaggerPlugin.context.getOas()
112
- const root = path.resolve(this.config.root, this.config.output.path)
113
- const mode = FileManager.getMode(path.resolve(root, output.path))
127
+ const oas = await swaggerPlugin.context.getOas();
128
+ const root = path.resolve(this.config.root, this.config.output.path);
129
+ const mode = FileManager.getMode(path.resolve(root, output.path));
114
130
 
115
131
  const schemaGenerator = new SchemaGenerator(this.plugin.options, {
116
132
  oas,
@@ -121,10 +137,10 @@ export const pluginZod = createPlugin<PluginZod>((options) => {
121
137
  override,
122
138
  mode,
123
139
  output: output.path,
124
- })
140
+ });
125
141
 
126
- const schemaFiles = await schemaGenerator.build(...generators)
127
- await this.addFile(...schemaFiles)
142
+ const schemaFiles = await schemaGenerator.build(...generators);
143
+ await this.addFile(...schemaFiles);
128
144
 
129
145
  const operationGenerator = new OperationGenerator(this.plugin.options, {
130
146
  oas,
@@ -135,13 +151,13 @@ export const pluginZod = createPlugin<PluginZod>((options) => {
135
151
  include,
136
152
  override,
137
153
  mode,
138
- })
154
+ });
139
155
 
140
- const operationFiles = await operationGenerator.build(...generators)
141
- await this.addFile(...operationFiles)
156
+ const operationFiles = await operationGenerator.build(...generators);
157
+ await this.addFile(...operationFiles);
142
158
 
143
159
  const barrelFiles = await this.fileManager.getBarrelFiles({
144
- type: output.barrelType ?? 'named',
160
+ type: output.barrelType ?? "named",
145
161
  root,
146
162
  output,
147
163
  files: this.fileManager.files,
@@ -149,9 +165,9 @@ export const pluginZod = createPlugin<PluginZod>((options) => {
149
165
  pluginKey: this.plugin.key,
150
166
  },
151
167
  logger: this.logger,
152
- })
168
+ });
153
169
 
154
- await this.addFile(...barrelFiles)
170
+ await this.addFile(...barrelFiles);
155
171
  },
156
- }
157
- })
172
+ };
173
+ });