@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 +39 -31
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.js +39 -31
- package/dist/index.js.map +1 -1
- package/package.json +4 -7
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
|
*
|
package/dist/index.js
CHANGED
|
@@ -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
|
|
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 (
|
|
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
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
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
|
|
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
|
|
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,
|