@kubb/plugin-zod 5.4.3 → 5.4.5

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.d.ts CHANGED
@@ -133,6 +133,10 @@ export declare const printerZod: (options?: PrinterZodOptions | undefined) => as
133
133
  */
134
134
  type PrinterZodMiniNodes = ast.PrinterPartial<string, PrinterZodMiniOptions>;
135
135
  type PrinterZodMiniOptions = {
136
+ /**
137
+ * Enable automatic type coercion for strings, numbers, and dates through `z.coerce.*()`.
138
+ */
139
+ coercion?: PluginZod['resolvedOptions']['coercion'];
136
140
  /**
137
141
  * Use `z.guid()` or `z.uuid()` for UUID/GUID validation.
138
142
  *
@@ -529,7 +533,7 @@ export declare const pluginZodName = "plugin-zod";
529
533
  * })
530
534
  * ```
531
535
  */
532
- export declare const pluginZod: (options?: Options | undefined) => import("kubb/kit").Plugin<PluginZod>;
536
+ export declare const pluginZod: import("kubb/kit").PluginFactory<PluginZod>;
533
537
  //#endregion
534
538
  //#region src/resolvers/resolverZod.d.ts
535
539
  /**
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { t as __name } from "./rolldown-runtime-C0LytTxp.js";
2
- import { Resolver, ast, containsCircularRef, createResolver, defineGenerator, definePlugin, extractRefName, syncSchemaRef } from "kubb/kit";
2
+ import { Resolver, addGenerator, ast, containsCircularRef, createResolver, defineGenerator, definePlugin, extractRefName, setMacros, setResolver, syncSchemaRef } from "kubb/kit";
3
3
  import { Const, File, Function, Type, jsxRenderer } from "kubb/jsx";
4
4
  import { Fragment, jsx, jsxs } from "kubb/jsx/jsx-runtime";
5
5
  //#region ../../internals/shared/src/params.ts
@@ -895,13 +895,12 @@ function isObjectComposableIntersection(node, cyclicSchemas) {
895
895
  }
896
896
  /**
897
897
  * Format a default value as a code-level literal.
898
- * Arrays keep their contents, other objects become `{}`, primitives become their string
898
+ * Arrays and objects keep their contents as a JSON literal, primitives become their string
899
899
  * representation, strings are quoted.
900
900
  */
901
901
  function formatDefault(value) {
902
902
  if (typeof value === "string") return stringify(value);
903
- if (Array.isArray(value)) return JSON.stringify(value);
904
- if (typeof value === "object" && value !== null) return "{}";
903
+ if (typeof value === "object" && value !== null) return JSON.stringify(value);
905
904
  return String(value ?? "");
906
905
  }
907
906
  /**
@@ -1588,41 +1587,46 @@ const printerZodMini = ast.createPrinter((options) => {
1588
1587
  boolean: () => "z.boolean()",
1589
1588
  null: () => "z.null()",
1590
1589
  string(node) {
1591
- if (node.format === "byte" || node.format === "base64") return `z.base64()${lengthChecksMini({
1592
- ...node,
1593
- regexType: this.options.regexType
1594
- })}`;
1595
- if (node.format === "base64url") return `z.base64url()${lengthChecksMini({
1596
- ...node,
1597
- regexType: this.options.regexType
1598
- })}`;
1599
- if (node.format === "jwt") return `z.jwt()${lengthChecksMini({
1600
- ...node,
1601
- regexType: this.options.regexType
1602
- })}`;
1603
- if (node.format === "ulid") return `z.ulid()${lengthChecksMini({
1604
- ...node,
1605
- regexType: this.options.regexType
1606
- })}`;
1607
- if (node.format === "iban") return `z.iban()${lengthChecksMini({
1608
- ...node,
1609
- regexType: this.options.regexType
1610
- })}`;
1611
- if (node.format === "duration") return `z.iso.duration()${lengthChecksMini({
1612
- ...node,
1613
- regexType: this.options.regexType
1614
- })}`;
1590
+ const coerce = shouldCoerce(this.options.coercion, "strings");
1591
+ if (!coerce) {
1592
+ if (node.format === "byte" || node.format === "base64") return `z.base64()${lengthChecksMini({
1593
+ ...node,
1594
+ regexType: this.options.regexType
1595
+ })}`;
1596
+ if (node.format === "base64url") return `z.base64url()${lengthChecksMini({
1597
+ ...node,
1598
+ regexType: this.options.regexType
1599
+ })}`;
1600
+ if (node.format === "jwt") return `z.jwt()${lengthChecksMini({
1601
+ ...node,
1602
+ regexType: this.options.regexType
1603
+ })}`;
1604
+ if (node.format === "ulid") return `z.ulid()${lengthChecksMini({
1605
+ ...node,
1606
+ regexType: this.options.regexType
1607
+ })}`;
1608
+ if (node.format === "iban") return `z.iban()${lengthChecksMini({
1609
+ ...node,
1610
+ regexType: this.options.regexType
1611
+ })}`;
1612
+ if (node.format === "duration") return `z.iso.duration()${lengthChecksMini({
1613
+ ...node,
1614
+ regexType: this.options.regexType
1615
+ })}`;
1616
+ }
1617
+ const base = coerce ? "z.coerce.string()" : "z.string()";
1615
1618
  const pattern = node.pattern ?? integerFormatPattern(node.format);
1616
- return `z.string()${lengthChecksMini({
1619
+ return `${base}${lengthChecksMini({
1617
1620
  ...node,
1618
1621
  pattern,
1619
1622
  regexType: this.options.regexType
1620
1623
  })}`;
1621
1624
  },
1622
1625
  number(node) {
1623
- return `z.number()${numberChecksMini(node)}`;
1626
+ return `${shouldCoerce(this.options.coercion, "numbers") ? "z.coerce.number()" : "z.number()"}${numberChecksMini(node)}`;
1624
1627
  },
1625
1628
  integer(node) {
1629
+ if (shouldCoerce(this.options.coercion, "numbers")) return `z.coerce.number().check(z.int())${numberChecksMini(node)}`;
1626
1630
  if (node.format === "int32") return `z.int32()${numberChecksMini(node)}`;
1627
1631
  if (node.format === "uint32") return `z.uint32()${numberChecksMini(node)}`;
1628
1632
  return `z.int()${numberChecksMini(node)}`;
@@ -1632,7 +1636,7 @@ const printerZodMini = ast.createPrinter((options) => {
1632
1636
  },
1633
1637
  date(node) {
1634
1638
  if (node.representation === "string") return "z.iso.date()";
1635
- return "z.date()";
1639
+ return shouldCoerce(this.options.coercion, "dates") ? "z.coerce.date()" : "z.date()";
1636
1640
  },
1637
1641
  datetime() {
1638
1642
  return "z.string()";
@@ -1813,13 +1817,14 @@ function getStdPrinters(resolver, params) {
1813
1817
  }
1814
1818
  function getMiniPrinter(resolver, params) {
1815
1819
  const cached = zodMiniPrinterCache.get(resolver);
1816
- if (cached && cached.guidType === params.guidType && cached.regexType === params.regexType && cached.nodes === params.nodes) return cached.printer;
1820
+ if (cached && cached.coercion === params.coercion && cached.guidType === params.guidType && cached.regexType === params.regexType && cached.nodes === params.nodes) return cached.printer;
1817
1821
  const p = printerZodMini({
1818
1822
  ...params,
1819
1823
  resolver
1820
1824
  });
1821
1825
  zodMiniPrinterCache.set(resolver, {
1822
1826
  printer: p,
1827
+ coercion: params.coercion,
1823
1828
  guidType: params.guidType,
1824
1829
  regexType: params.regexType,
1825
1830
  nodes: params.nodes
@@ -1904,6 +1909,7 @@ const zodGenerator = defineGenerator({
1904
1909
  nodes: printer?.nodes
1905
1910
  });
1906
1911
  const schemaPrinter = mini ? getMiniPrinter(resolver, {
1912
+ coercion,
1907
1913
  guidType,
1908
1914
  regexType,
1909
1915
  cyclicSchemas,
@@ -2011,6 +2017,7 @@ const zodGenerator = defineGenerator({
2011
2017
  name: (schemaName) => directionalRefNames?.has(schemaName) ? resolver.schema.inputName(schemaName) : resolver.name(schemaName)
2012
2018
  });
2013
2019
  const schemaPrinter = mini ? keysToOmit?.length ? printerZodMini({
2020
+ coercion,
2014
2021
  guidType,
2015
2022
  regexType,
2016
2023
  resolver,
@@ -2018,6 +2025,7 @@ const zodGenerator = defineGenerator({
2018
2025
  cyclicSchemas,
2019
2026
  nodes: printer?.nodes
2020
2027
  }) : getMiniPrinter(resolver, {
2028
+ coercion,
2021
2029
  guidType,
2022
2030
  regexType,
2023
2031
  cyclicSchemas,
@@ -2211,6 +2219,9 @@ const zodGenerator = defineGenerator({
2211
2219
  }
2212
2220
  });
2213
2221
  //#endregion
2222
+ //#region package.json
2223
+ var version = "5.4.5";
2224
+ //#endregion
2214
2225
  //#region src/resolvers/resolverZod.ts
2215
2226
  /**
2216
2227
  * Default resolver used by `@kubb/plugin-zod`. Decides the names and file
@@ -2297,37 +2308,37 @@ const pluginZodName = "plugin-zod";
2297
2308
  * })
2298
2309
  * ```
2299
2310
  */
2300
- const pluginZod = definePlugin((options) => {
2301
- const { output = {
2302
- path: "zod",
2303
- barrel: { type: "named" }
2304
- }, group, exclude = [], include, override = [], compile = false, mini = false, guidType = "uuid", regexType = "literal", importPath = mini ? "zod/mini" : "zod", coercion = false, inferred = false, typeGuards = false, printer, resolver: userResolver, macros: userMacros } = options;
2305
- const groupConfig = createGroupConfig(group);
2306
- return {
2311
+ const pluginZod = definePlugin({
2312
+ meta: {
2307
2313
  name: pluginZodName,
2308
- options,
2309
- hooks: { "kubb:plugin:setup"(ctx) {
2310
- ctx.setOptions({
2311
- output,
2312
- exclude,
2313
- include,
2314
- override,
2315
- group: groupConfig,
2316
- importPath,
2317
- coercion,
2318
- inferred,
2319
- guidType,
2320
- regexType,
2321
- compile,
2322
- mini,
2323
- typeGuards,
2324
- printer
2325
- });
2326
- ctx.setResolver(userResolver ? Resolver.merge(resolverZod, userResolver) : resolverZod);
2327
- if (userMacros?.length) ctx.setMacros(userMacros);
2328
- ctx.addGenerator(zodGenerator);
2329
- } }
2330
- };
2314
+ version,
2315
+ compatibility: { kubb: ">=5.8.0" }
2316
+ },
2317
+ setup(options) {
2318
+ const { output = {
2319
+ path: "zod",
2320
+ barrel: { type: "named" }
2321
+ }, group, exclude = [], include, override = [], compile = false, mini = false, guidType = "uuid", regexType = "literal", importPath = mini ? "zod/mini" : "zod", coercion = false, inferred = false, typeGuards = false, printer, resolver: userResolver, macros: userMacros } = options;
2322
+ setResolver(userResolver ? Resolver.merge(resolverZod, userResolver) : resolverZod);
2323
+ if (userMacros?.length) setMacros(userMacros);
2324
+ addGenerator(zodGenerator);
2325
+ return {
2326
+ output,
2327
+ exclude,
2328
+ include,
2329
+ override,
2330
+ group: createGroupConfig(group),
2331
+ importPath,
2332
+ coercion,
2333
+ inferred,
2334
+ guidType,
2335
+ regexType,
2336
+ compile,
2337
+ mini,
2338
+ typeGuards,
2339
+ printer
2340
+ };
2341
+ }
2331
2342
  });
2332
2343
  //#endregion
2333
2344
  export { pluginZod as default, pluginZod, pluginZodName, printerZod, printerZodMini, resolverZod, zodGenerator };