@kubb/plugin-zod 5.4.3 → 5.4.4

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.cjs CHANGED
@@ -905,13 +905,12 @@ function isObjectComposableIntersection(node, cyclicSchemas) {
905
905
  }
906
906
  /**
907
907
  * Format a default value as a code-level literal.
908
- * Arrays keep their contents, other objects become `{}`, primitives become their string
908
+ * Arrays and objects keep their contents as a JSON literal, primitives become their string
909
909
  * representation, strings are quoted.
910
910
  */
911
911
  function formatDefault(value) {
912
912
  if (typeof value === "string") return stringify(value);
913
- if (Array.isArray(value)) return JSON.stringify(value);
914
- if (typeof value === "object" && value !== null) return "{}";
913
+ if (typeof value === "object" && value !== null) return JSON.stringify(value);
915
914
  return String(value ?? "");
916
915
  }
917
916
  /**
@@ -1598,41 +1597,46 @@ const printerZodMini = kubb_kit.ast.createPrinter((options) => {
1598
1597
  boolean: () => "z.boolean()",
1599
1598
  null: () => "z.null()",
1600
1599
  string(node) {
1601
- if (node.format === "byte" || node.format === "base64") return `z.base64()${lengthChecksMini({
1602
- ...node,
1603
- regexType: this.options.regexType
1604
- })}`;
1605
- if (node.format === "base64url") return `z.base64url()${lengthChecksMini({
1606
- ...node,
1607
- regexType: this.options.regexType
1608
- })}`;
1609
- if (node.format === "jwt") return `z.jwt()${lengthChecksMini({
1610
- ...node,
1611
- regexType: this.options.regexType
1612
- })}`;
1613
- if (node.format === "ulid") return `z.ulid()${lengthChecksMini({
1614
- ...node,
1615
- regexType: this.options.regexType
1616
- })}`;
1617
- if (node.format === "iban") return `z.iban()${lengthChecksMini({
1618
- ...node,
1619
- regexType: this.options.regexType
1620
- })}`;
1621
- if (node.format === "duration") return `z.iso.duration()${lengthChecksMini({
1622
- ...node,
1623
- regexType: this.options.regexType
1624
- })}`;
1600
+ const coerce = shouldCoerce(this.options.coercion, "strings");
1601
+ if (!coerce) {
1602
+ if (node.format === "byte" || node.format === "base64") return `z.base64()${lengthChecksMini({
1603
+ ...node,
1604
+ regexType: this.options.regexType
1605
+ })}`;
1606
+ if (node.format === "base64url") return `z.base64url()${lengthChecksMini({
1607
+ ...node,
1608
+ regexType: this.options.regexType
1609
+ })}`;
1610
+ if (node.format === "jwt") return `z.jwt()${lengthChecksMini({
1611
+ ...node,
1612
+ regexType: this.options.regexType
1613
+ })}`;
1614
+ if (node.format === "ulid") return `z.ulid()${lengthChecksMini({
1615
+ ...node,
1616
+ regexType: this.options.regexType
1617
+ })}`;
1618
+ if (node.format === "iban") return `z.iban()${lengthChecksMini({
1619
+ ...node,
1620
+ regexType: this.options.regexType
1621
+ })}`;
1622
+ if (node.format === "duration") return `z.iso.duration()${lengthChecksMini({
1623
+ ...node,
1624
+ regexType: this.options.regexType
1625
+ })}`;
1626
+ }
1627
+ const base = coerce ? "z.coerce.string()" : "z.string()";
1625
1628
  const pattern = node.pattern ?? integerFormatPattern(node.format);
1626
- return `z.string()${lengthChecksMini({
1629
+ return `${base}${lengthChecksMini({
1627
1630
  ...node,
1628
1631
  pattern,
1629
1632
  regexType: this.options.regexType
1630
1633
  })}`;
1631
1634
  },
1632
1635
  number(node) {
1633
- return `z.number()${numberChecksMini(node)}`;
1636
+ return `${shouldCoerce(this.options.coercion, "numbers") ? "z.coerce.number()" : "z.number()"}${numberChecksMini(node)}`;
1634
1637
  },
1635
1638
  integer(node) {
1639
+ if (shouldCoerce(this.options.coercion, "numbers")) return `z.coerce.number().check(z.int())${numberChecksMini(node)}`;
1636
1640
  if (node.format === "int32") return `z.int32()${numberChecksMini(node)}`;
1637
1641
  if (node.format === "uint32") return `z.uint32()${numberChecksMini(node)}`;
1638
1642
  return `z.int()${numberChecksMini(node)}`;
@@ -1642,7 +1646,7 @@ const printerZodMini = kubb_kit.ast.createPrinter((options) => {
1642
1646
  },
1643
1647
  date(node) {
1644
1648
  if (node.representation === "string") return "z.iso.date()";
1645
- return "z.date()";
1649
+ return shouldCoerce(this.options.coercion, "dates") ? "z.coerce.date()" : "z.date()";
1646
1650
  },
1647
1651
  datetime() {
1648
1652
  return "z.string()";
@@ -1823,13 +1827,14 @@ function getStdPrinters(resolver, params) {
1823
1827
  }
1824
1828
  function getMiniPrinter(resolver, params) {
1825
1829
  const cached = zodMiniPrinterCache.get(resolver);
1826
- if (cached && cached.guidType === params.guidType && cached.regexType === params.regexType && cached.nodes === params.nodes) return cached.printer;
1830
+ if (cached && cached.coercion === params.coercion && cached.guidType === params.guidType && cached.regexType === params.regexType && cached.nodes === params.nodes) return cached.printer;
1827
1831
  const p = printerZodMini({
1828
1832
  ...params,
1829
1833
  resolver
1830
1834
  });
1831
1835
  zodMiniPrinterCache.set(resolver, {
1832
1836
  printer: p,
1837
+ coercion: params.coercion,
1833
1838
  guidType: params.guidType,
1834
1839
  regexType: params.regexType,
1835
1840
  nodes: params.nodes
@@ -1914,6 +1919,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1914
1919
  nodes: printer?.nodes
1915
1920
  });
1916
1921
  const schemaPrinter = mini ? getMiniPrinter(resolver, {
1922
+ coercion,
1917
1923
  guidType,
1918
1924
  regexType,
1919
1925
  cyclicSchemas,
@@ -2021,6 +2027,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
2021
2027
  name: (schemaName) => directionalRefNames?.has(schemaName) ? resolver.schema.inputName(schemaName) : resolver.name(schemaName)
2022
2028
  });
2023
2029
  const schemaPrinter = mini ? keysToOmit?.length ? printerZodMini({
2030
+ coercion,
2024
2031
  guidType,
2025
2032
  regexType,
2026
2033
  resolver,
@@ -2028,6 +2035,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
2028
2035
  cyclicSchemas,
2029
2036
  nodes: printer?.nodes
2030
2037
  }) : getMiniPrinter(resolver, {
2038
+ coercion,
2031
2039
  guidType,
2032
2040
  regexType,
2033
2041
  cyclicSchemas,