@kubb/plugin-zod 5.5.0-canary.20261023T194004 → 5.5.0
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 +41 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +21 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +42 -35
- package/dist/index.js.map +1 -1
- package/docs/options.md +25 -0
- package/docs/overview.md +1 -0
- package/kubb-extension.json +18 -0
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1043,14 +1043,15 @@ function modifierDepth(schema) {
|
|
|
1043
1043
|
* `.optional()` / `.nullish()` / `.default()` wrappers. `.omit()` lives on the inner `ZodObject`,
|
|
1044
1044
|
* not on those `ZodNullable` / `ZodOptional` / `ZodDefault` wrappers, so the omit has to unwrap
|
|
1045
1045
|
* down to the object first. This mirrors plugin-ts emitting `Omit<NonNullable<T>, …>`. Inline
|
|
1046
|
-
* objects need no unwrap because the printer adds their modifiers after `.omit()`.
|
|
1046
|
+
* objects need no unwrap because the printer adds their modifiers after `.omit()`. Zod Mini has no
|
|
1047
|
+
* `.unwrap()`, so its printer passes `._zod.def.innerType` as the `step`.
|
|
1047
1048
|
*/
|
|
1048
|
-
function omitUnwrapChain(node) {
|
|
1049
|
+
function omitUnwrapChain(node, step = ".unwrap()") {
|
|
1049
1050
|
const ref = kubb_kit.ast.narrowSchema(node, "ref");
|
|
1050
1051
|
if (!ref) return "";
|
|
1051
1052
|
const target = ref.schema ?? ref;
|
|
1052
1053
|
const depth = modifierDepth(target) + (target.default !== void 0 ? 1 : 0);
|
|
1053
|
-
return
|
|
1054
|
+
return step.repeat(depth);
|
|
1054
1055
|
}
|
|
1055
1056
|
/**
|
|
1056
1057
|
* Apply nullable / optional / nullish modifiers using the functional `zod/mini` API
|
|
@@ -1130,20 +1131,21 @@ function buildObjectShape(ctx, node, modify) {
|
|
|
1130
1131
|
}));
|
|
1131
1132
|
}
|
|
1132
1133
|
/**
|
|
1133
|
-
* Shared `print` body: transforms the node, applies `keysToOmit` as `.omit(...)
|
|
1134
|
+
* Shared `print` body: transforms the node, applies `keysToOmit` as `.omit(...)` (or `z.omit(...)` when `mini`), then `modify` adds the modifiers.
|
|
1134
1135
|
*/
|
|
1135
|
-
function printWithOmit(ctx, node, modify) {
|
|
1136
|
+
function printWithOmit(ctx, node, modify, mini = false) {
|
|
1136
1137
|
const { keysToOmit } = ctx.options;
|
|
1137
1138
|
const transformed = ctx.transform(node);
|
|
1138
1139
|
if (!transformed) return null;
|
|
1139
1140
|
const meta = (0, kubb_kit.syncSchemaRef)(node);
|
|
1140
1141
|
return modify((() => {
|
|
1141
1142
|
if (!keysToOmit?.length || meta.primitive !== "object" || meta.type === "union" && meta.discriminatorPropertyName) return transformed;
|
|
1142
|
-
const unwrap = omitUnwrapChain(node);
|
|
1143
|
-
const
|
|
1143
|
+
const unwrap = omitUnwrapChain(node, mini ? "._zod.def.innerType" : ".unwrap()");
|
|
1144
|
+
const mask = `{ ${keysToOmit.map((k) => `"${k}": true`).join(", ")} }`;
|
|
1145
|
+
const omit = (target) => mini ? `z.omit(${target}${unwrap}, ${mask})` : `${target}${unwrap}.omit(${mask})`;
|
|
1144
1146
|
const lazyMatch = transformed.match(/^z\.lazy\(\(\)\s*=>\s*(.+)\)$/);
|
|
1145
|
-
if (lazyMatch) return `z.lazy(() => ${lazyMatch[1]}
|
|
1146
|
-
return
|
|
1147
|
+
if (lazyMatch) return `z.lazy(() => ${omit(lazyMatch[1])})`;
|
|
1148
|
+
return omit(transformed);
|
|
1147
1149
|
})(), meta);
|
|
1148
1150
|
}
|
|
1149
1151
|
//#endregion
|
|
@@ -1431,11 +1433,14 @@ const printerZod = kubb_kit.ast.createPrinter((options) => {
|
|
|
1431
1433
|
},
|
|
1432
1434
|
union(node) {
|
|
1433
1435
|
const nodeMembers = node.members ?? [];
|
|
1434
|
-
const
|
|
1436
|
+
const isDiscriminated = !!node.discriminatorPropertyName && nodeMembers.every((m) => isObjectSchemaNode(m, cyclicSchemaNames));
|
|
1437
|
+
const isXor = node.strategy === "one" && !isDiscriminated && this.options.oneOfType === "xor";
|
|
1438
|
+
const isStrict = node.strategy === "one" && !isXor;
|
|
1439
|
+
const members = mapSchemaMembers(node, (memberNode) => this.transform(isStrict ? strictOneOfNode(memberNode) : memberNode)).map(({ schema, output }) => output && isStrict ? strictOneOfMember(output, schema, cyclicSchemaNames) : output).filter(Boolean);
|
|
1435
1440
|
if (members.length === 0) return "";
|
|
1436
1441
|
if (members.length === 1) return members[0];
|
|
1437
|
-
|
|
1438
|
-
if (
|
|
1442
|
+
if (isDiscriminated) return `z.discriminatedUnion(${stringify(node.discriminatorPropertyName)}, ${buildList(members)})`;
|
|
1443
|
+
if (isXor) return `z.xor(${buildList(members)})`;
|
|
1439
1444
|
return `z.union(${buildList(members)})`;
|
|
1440
1445
|
},
|
|
1441
1446
|
intersection(node) {
|
|
@@ -1633,11 +1638,14 @@ const printerZodMini = kubb_kit.ast.createPrinter((options) => {
|
|
|
1633
1638
|
},
|
|
1634
1639
|
union(node) {
|
|
1635
1640
|
const nodeMembers = node.members ?? [];
|
|
1636
|
-
const
|
|
1641
|
+
const isDiscriminated = !!node.discriminatorPropertyName && nodeMembers.every((m) => isObjectSchemaNode(m, cyclicSchemaNames));
|
|
1642
|
+
const isXor = node.strategy === "one" && !isDiscriminated && this.options.oneOfType === "xor";
|
|
1643
|
+
const isStrict = node.strategy === "one" && !isXor;
|
|
1644
|
+
const members = mapSchemaMembers(node, (memberNode) => this.transform(isStrict ? strictOneOfNode(memberNode) : memberNode)).map(({ output }) => output).filter(Boolean);
|
|
1637
1645
|
if (members.length === 0) return "";
|
|
1638
1646
|
if (members.length === 1) return members[0];
|
|
1639
|
-
|
|
1640
|
-
if (
|
|
1647
|
+
if (isDiscriminated) return `z.discriminatedUnion(${stringify(node.discriminatorPropertyName)}, ${buildList(members)})`;
|
|
1648
|
+
if (isXor) return `z.xor(${buildList(members)})`;
|
|
1641
1649
|
return `z.union(${buildList(members)})`;
|
|
1642
1650
|
},
|
|
1643
1651
|
intersection(node) {
|
|
@@ -1670,7 +1678,7 @@ const printerZodMini = kubb_kit.ast.createPrinter((options) => {
|
|
|
1670
1678
|
optional: meta.optional,
|
|
1671
1679
|
nullish: meta.nullish,
|
|
1672
1680
|
defaultValue: meta.default
|
|
1673
|
-
}));
|
|
1681
|
+
}), true);
|
|
1674
1682
|
}
|
|
1675
1683
|
};
|
|
1676
1684
|
});
|
|
@@ -1684,7 +1692,7 @@ const zodMiniPrinterCache = /* @__PURE__ */ new WeakMap();
|
|
|
1684
1692
|
*/
|
|
1685
1693
|
function getStdPrinters(resolver, params) {
|
|
1686
1694
|
const cached = zodPrinterCache.get(resolver);
|
|
1687
|
-
if (cached && cached.coercion === params.coercion && cached.guidType === params.guidType && cached.regexType === params.regexType && cached.dateType === params.dateType && cached.nodes === params.nodes) return {
|
|
1695
|
+
if (cached && cached.coercion === params.coercion && cached.guidType === params.guidType && cached.regexType === params.regexType && cached.oneOfType === params.oneOfType && cached.dateType === params.dateType && cached.nodes === params.nodes) return {
|
|
1688
1696
|
decode: cached.decode,
|
|
1689
1697
|
encode: cached.encode
|
|
1690
1698
|
};
|
|
@@ -1704,6 +1712,7 @@ function getStdPrinters(resolver, params) {
|
|
|
1704
1712
|
coercion: params.coercion,
|
|
1705
1713
|
guidType: params.guidType,
|
|
1706
1714
|
regexType: params.regexType,
|
|
1715
|
+
oneOfType: params.oneOfType,
|
|
1707
1716
|
dateType: params.dateType,
|
|
1708
1717
|
nodes: params.nodes
|
|
1709
1718
|
});
|
|
@@ -1714,7 +1723,7 @@ function getStdPrinters(resolver, params) {
|
|
|
1714
1723
|
}
|
|
1715
1724
|
function getMiniPrinter(resolver, params) {
|
|
1716
1725
|
const cached = zodMiniPrinterCache.get(resolver);
|
|
1717
|
-
if (cached && cached.coercion === params.coercion && cached.guidType === params.guidType && cached.regexType === params.regexType && cached.nodes === params.nodes) return cached.printer;
|
|
1726
|
+
if (cached && cached.coercion === params.coercion && cached.guidType === params.guidType && cached.regexType === params.regexType && cached.oneOfType === params.oneOfType && cached.nodes === params.nodes) return cached.printer;
|
|
1718
1727
|
const p = printerZodMini({
|
|
1719
1728
|
...params,
|
|
1720
1729
|
resolver
|
|
@@ -1724,6 +1733,7 @@ function getMiniPrinter(resolver, params) {
|
|
|
1724
1733
|
coercion: params.coercion,
|
|
1725
1734
|
guidType: params.guidType,
|
|
1726
1735
|
regexType: params.regexType,
|
|
1736
|
+
oneOfType: params.oneOfType,
|
|
1727
1737
|
nodes: params.nodes
|
|
1728
1738
|
});
|
|
1729
1739
|
return p;
|
|
@@ -1739,7 +1749,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1739
1749
|
renderer: kubb_jsx.jsxRenderer,
|
|
1740
1750
|
schema(node, ctx) {
|
|
1741
1751
|
const { adapter, config, resolver, root } = ctx;
|
|
1742
|
-
const { output, coercion, guidType, regexType, compile, mini, inferred, typeGuards, importPath, group, printer } = ctx.options;
|
|
1752
|
+
const { output, coercion, guidType, regexType, oneOfType, compile, mini, inferred, typeGuards, importPath, group, printer } = ctx.options;
|
|
1743
1753
|
const dateType = getOasAdapter(adapter).options.dateType;
|
|
1744
1754
|
if (!node.name) return;
|
|
1745
1755
|
const isZodImport = importPath === "zod" || importPath === "zod/mini";
|
|
@@ -1748,6 +1758,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1748
1758
|
coercion,
|
|
1749
1759
|
guidType,
|
|
1750
1760
|
regexType,
|
|
1761
|
+
oneOfType,
|
|
1751
1762
|
dateType,
|
|
1752
1763
|
resolver,
|
|
1753
1764
|
cyclicSchemas,
|
|
@@ -1795,6 +1806,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1795
1806
|
coercion,
|
|
1796
1807
|
guidType,
|
|
1797
1808
|
regexType,
|
|
1809
|
+
oneOfType,
|
|
1798
1810
|
dateType,
|
|
1799
1811
|
cyclicSchemas,
|
|
1800
1812
|
nodes: printer?.nodes
|
|
@@ -1803,6 +1815,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1803
1815
|
coercion,
|
|
1804
1816
|
guidType,
|
|
1805
1817
|
regexType,
|
|
1818
|
+
oneOfType,
|
|
1806
1819
|
cyclicSchemas,
|
|
1807
1820
|
nodes: printer?.nodes
|
|
1808
1821
|
}) : stdPrinters.decode;
|
|
@@ -1871,7 +1884,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1871
1884
|
operation(node, ctx) {
|
|
1872
1885
|
if (!kubb_kit.ast.isHttpOperationNode(node)) return null;
|
|
1873
1886
|
const { adapter, config, resolver, root } = ctx;
|
|
1874
|
-
const { output, coercion, guidType, regexType, compile, mini, inferred, importPath, group, printer } = ctx.options;
|
|
1887
|
+
const { output, coercion, guidType, regexType, oneOfType, compile, mini, inferred, importPath, group, printer } = ctx.options;
|
|
1875
1888
|
const dateType = getOasAdapter(adapter).options.dateType;
|
|
1876
1889
|
const isZodImport = importPath === "zod" || importPath === "zod/mini";
|
|
1877
1890
|
const meta = { file: resolver.file({
|
|
@@ -1888,6 +1901,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1888
1901
|
coercion,
|
|
1889
1902
|
guidType,
|
|
1890
1903
|
regexType,
|
|
1904
|
+
oneOfType,
|
|
1891
1905
|
dateType,
|
|
1892
1906
|
resolver,
|
|
1893
1907
|
cyclicSchemas,
|
|
@@ -1911,6 +1925,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1911
1925
|
coercion,
|
|
1912
1926
|
guidType,
|
|
1913
1927
|
regexType,
|
|
1928
|
+
oneOfType,
|
|
1914
1929
|
resolver,
|
|
1915
1930
|
keysToOmit,
|
|
1916
1931
|
cyclicSchemas,
|
|
@@ -1919,12 +1934,14 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1919
1934
|
coercion,
|
|
1920
1935
|
guidType,
|
|
1921
1936
|
regexType,
|
|
1937
|
+
oneOfType,
|
|
1922
1938
|
cyclicSchemas,
|
|
1923
1939
|
nodes: printer?.nodes
|
|
1924
1940
|
}) : keysToOmit?.length ? printerZod({
|
|
1925
1941
|
coercion,
|
|
1926
1942
|
guidType,
|
|
1927
1943
|
regexType,
|
|
1944
|
+
oneOfType,
|
|
1928
1945
|
dateType,
|
|
1929
1946
|
resolver,
|
|
1930
1947
|
keysToOmit,
|
|
@@ -1935,6 +1952,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
|
|
|
1935
1952
|
coercion,
|
|
1936
1953
|
guidType,
|
|
1937
1954
|
regexType,
|
|
1955
|
+
oneOfType,
|
|
1938
1956
|
dateType,
|
|
1939
1957
|
cyclicSchemas,
|
|
1940
1958
|
nodes: printer?.nodes
|
|
@@ -2125,7 +2143,7 @@ var compatibility = {
|
|
|
2125
2143
|
};
|
|
2126
2144
|
//#endregion
|
|
2127
2145
|
//#region package.json
|
|
2128
|
-
var version = "5.5.0
|
|
2146
|
+
var version = "5.5.0";
|
|
2129
2147
|
//#endregion
|
|
2130
2148
|
//#region src/resolvers/resolverZod.ts
|
|
2131
2149
|
/**
|
|
@@ -2224,19 +2242,7 @@ const pluginZod = (0, kubb_kit.defineExtension)({
|
|
|
2224
2242
|
const { output = {
|
|
2225
2243
|
path: "zod",
|
|
2226
2244
|
barrel: { type: "named" }
|
|
2227
|
-
}, 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;
|
|
2228
|
-
(0, kubb_kit.assertPluginOption)({
|
|
2229
|
-
plugin: pluginZodName,
|
|
2230
|
-
option: "guidType",
|
|
2231
|
-
value: guidType,
|
|
2232
|
-
allowed: ["uuid", "guid"]
|
|
2233
|
-
});
|
|
2234
|
-
(0, kubb_kit.assertPluginOption)({
|
|
2235
|
-
plugin: pluginZodName,
|
|
2236
|
-
option: "regexType",
|
|
2237
|
-
value: regexType,
|
|
2238
|
-
allowed: ["literal", "constructor"]
|
|
2239
|
-
});
|
|
2245
|
+
}, group, exclude = [], include, override = [], compile = false, mini = false, guidType = "uuid", regexType = "literal", oneOfType = "union", importPath = mini ? "zod/mini" : "zod", coercion = false, inferred = false, typeGuards = false, printer, resolver: userResolver, macros: userMacros } = options;
|
|
2240
2246
|
(0, kubb_kit.setResolver)(userResolver ? kubb_kit.Resolver.merge(resolverZod, userResolver) : resolverZod);
|
|
2241
2247
|
if (userMacros?.length) (0, kubb_kit.setMacros)(userMacros);
|
|
2242
2248
|
(0, kubb_kit.addGenerator)(zodGenerator);
|
|
@@ -2251,6 +2257,7 @@ const pluginZod = (0, kubb_kit.defineExtension)({
|
|
|
2251
2257
|
inferred,
|
|
2252
2258
|
guidType,
|
|
2253
2259
|
regexType,
|
|
2260
|
+
oneOfType,
|
|
2254
2261
|
compile,
|
|
2255
2262
|
mini,
|
|
2256
2263
|
typeGuards,
|