@kubb/plugin-zod 5.3.0-canary.20260924T130132 → 5.3.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 CHANGED
@@ -744,71 +744,6 @@ function mapSchemaItems(node, transform) {
744
744
  }));
745
745
  }
746
746
  //#endregion
747
- //#region src/components/Zod.tsx
748
- function Zod({ name, node, printer, inferTypeName, typeGuards, isName, assertName, mini, cyclic }) {
749
- const output = printer.print(node);
750
- if (!output) return;
751
- const needsAnnotation = cyclic && node.type !== "object";
752
- const targetType = inferTypeName ?? `z.infer<typeof ${name}>`;
753
- const shouldGenerateIs = isName && (typeof typeGuards === "object" ? typeGuards.is ?? true : Boolean(typeGuards));
754
- const shouldGenerateAssert = assertName && (typeof typeGuards === "object" ? typeGuards.assert ?? true : Boolean(typeGuards));
755
- return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx_jsx_runtime.Fragment, { children: [
756
- /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
757
- name,
758
- isExportable: true,
759
- isIndexable: true,
760
- children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Const, {
761
- export: true,
762
- name,
763
- type: needsAnnotation ? "z.ZodType" : void 0,
764
- children: output
765
- })
766
- }),
767
- inferTypeName && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
768
- name: inferTypeName,
769
- isExportable: true,
770
- isIndexable: true,
771
- isTypeOnly: true,
772
- children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Type, {
773
- export: true,
774
- name: inferTypeName,
775
- children: `z.infer<typeof ${name}>`
776
- })
777
- }),
778
- shouldGenerateIs && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
779
- name: isName,
780
- isExportable: true,
781
- isIndexable: true,
782
- children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Const, {
783
- export: true,
784
- name: isName,
785
- JSDoc: { comments: [`Type guard for {@link ${name}}`] },
786
- children: mini ? `(data: unknown): data is ${targetType} => z.validate(${name}, data)` : `(data: unknown): data is ${targetType} => ${name}.validate(data)`
787
- })
788
- }),
789
- shouldGenerateAssert && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
790
- name: assertName,
791
- isExportable: true,
792
- isIndexable: true,
793
- children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Function, {
794
- export: true,
795
- name: assertName,
796
- params: "data: unknown",
797
- returnType: `asserts data is ${targetType}`,
798
- JSDoc: { comments: [`Asserter for {@link ${name}}`, "@throws {z.ZodError} If data is invalid"] },
799
- children: mini ? `if (!z.validate(${name}, data)) {\n z.parse(${name}, data)\n}` : `if (!${name}.validate(data)) {\n ${name}.parse(data)\n}`
800
- })
801
- })
802
- ] });
803
- }
804
- //#endregion
805
- //#region src/constants.ts
806
- /**
807
- * Import paths that use a namespace import (`import * as z from '...'`).
808
- * All other import paths use a named import (`import { z } from '...'`).
809
- */
810
- const ZOD_NAMESPACE_IMPORTS = /* @__PURE__ */ new Set(["zod", "zod/mini"]);
811
- //#endregion
812
747
  //#region src/utils.ts
813
748
  /**
814
749
  * Returns `true` when the given coercion option enables coercion for the specified type.
@@ -827,6 +762,16 @@ function isPlainInlineObject(node) {
827
762
  return node.type === "object" && !node.nullable && !node.optional && !node.nullish && node.additionalProperties === void 0 && !node.patternProperties;
828
763
  }
829
764
  /**
765
+ * Whether the node is a bare reference schema (a `$ref` with no modifiers, descriptions, or omit keys).
766
+ * Bare references resolve directly to another schema variable and should not be re-wrapped in `z.compile(...)`.
767
+ */
768
+ function isBareRef(node, keysToOmit) {
769
+ const ref = kubb_kit.ast.narrowSchema(node, "ref");
770
+ if (!ref) return false;
771
+ const meta = (0, kubb_kit.syncSchemaRef)(ref);
772
+ return !meta.nullable && !meta.optional && !meta.nullish && meta.default === void 0 && !meta.description && !meta.examples?.length && !keysToOmit?.length;
773
+ }
774
+ /**
830
775
  * Whether a node renders as a bare Zod object — one that accepts `.extend(…)` and can be a
831
776
  * `z.discriminatedUnion` option. Covers object nodes, `$ref`s that resolve to (or are still
832
777
  * unresolved) objects, single-member unions of an object, and object-composable `allOf`.
@@ -1073,6 +1018,74 @@ function buildGroupedParamsSchema({ params, optional }) {
1073
1018
  });
1074
1019
  }
1075
1020
  //#endregion
1021
+ //#region src/components/Zod.tsx
1022
+ function Zod({ name, node, printer, inferTypeName, typeGuards, isName, assertName, mini, cyclic, compile }) {
1023
+ const output = printer.print(node);
1024
+ if (!output) return;
1025
+ const needsAnnotation = cyclic && node.type !== "object";
1026
+ const isBare = isBareRef(node, printer.options.keysToOmit);
1027
+ const isCyclic = Boolean(cyclic || printer.options.cyclicSchemas && (0, kubb_kit.containsCircularRef)(node, { circularSchemas: printer.options.cyclicSchemas }));
1028
+ const value = Boolean(compile) && !isBare && !isCyclic ? typeof compile === "object" && compile.strict ? `z.compile(${output}, { strict: true })` : `z.compile(${output})` : output;
1029
+ const targetType = inferTypeName ?? `z.infer<typeof ${name}>`;
1030
+ const shouldGenerateIs = isName && (typeof typeGuards === "object" ? typeGuards.is ?? true : Boolean(typeGuards));
1031
+ const shouldGenerateAssert = assertName && (typeof typeGuards === "object" ? typeGuards.assert ?? true : Boolean(typeGuards));
1032
+ return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx_jsx_runtime.Fragment, { children: [
1033
+ /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
1034
+ name,
1035
+ isExportable: true,
1036
+ isIndexable: true,
1037
+ children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Const, {
1038
+ export: true,
1039
+ name,
1040
+ type: needsAnnotation ? "z.ZodType" : void 0,
1041
+ children: value
1042
+ })
1043
+ }),
1044
+ inferTypeName && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
1045
+ name: inferTypeName,
1046
+ isExportable: true,
1047
+ isIndexable: true,
1048
+ isTypeOnly: true,
1049
+ children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Type, {
1050
+ export: true,
1051
+ name: inferTypeName,
1052
+ children: `z.infer<typeof ${name}>`
1053
+ })
1054
+ }),
1055
+ shouldGenerateIs && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
1056
+ name: isName,
1057
+ isExportable: true,
1058
+ isIndexable: true,
1059
+ children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Const, {
1060
+ export: true,
1061
+ name: isName,
1062
+ JSDoc: { comments: [`Type guard for {@link ${name}}`] },
1063
+ children: mini ? `(data: unknown): data is ${targetType} => z.validate(${name}, data)` : `(data: unknown): data is ${targetType} => ${name}.validate(data)`
1064
+ })
1065
+ }),
1066
+ shouldGenerateAssert && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
1067
+ name: assertName,
1068
+ isExportable: true,
1069
+ isIndexable: true,
1070
+ children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Function, {
1071
+ export: true,
1072
+ name: assertName,
1073
+ params: "data: unknown",
1074
+ returnType: `asserts data is ${targetType}`,
1075
+ JSDoc: { comments: [`Asserter for {@link ${name}}`, "@throws {z.ZodError} If data is invalid"] },
1076
+ children: mini ? `if (!z.validate(${name}, data)) {\n z.parse(${name}, data)\n}` : `if (!${name}.validate(data)) {\n ${name}.parse(data)\n}`
1077
+ })
1078
+ })
1079
+ ] });
1080
+ }
1081
+ //#endregion
1082
+ //#region src/constants.ts
1083
+ /**
1084
+ * Import paths that use a namespace import (`import * as z from '...'`).
1085
+ * All other import paths use a named import (`import { z } from '...'`).
1086
+ */
1087
+ const ZOD_NAMESPACE_IMPORTS = /* @__PURE__ */ new Set(["zod", "zod/mini"]);
1088
+ //#endregion
1076
1089
  //#region src/printers/printerZod.ts
1077
1090
  /**
1078
1091
  * A `oneOf` member that is an inline object is exhaustive, so it prints as a strict object.
@@ -1785,7 +1798,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1785
1798
  renderer: kubb_jsx.jsxRenderer,
1786
1799
  schema(node, ctx) {
1787
1800
  const { adapter, config, resolver, root } = ctx;
1788
- const { output, coercion, guidType, regexType, mini, inferred, typeGuards, importPath, group, printer } = ctx.options;
1801
+ const { output, coercion, guidType, regexType, compile, mini, inferred, typeGuards, importPath, group, printer } = ctx.options;
1789
1802
  const dateType = getOasAdapter(adapter).options.dateType;
1790
1803
  if (!node.name) return;
1791
1804
  const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
@@ -1901,7 +1914,8 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1901
1914
  isName,
1902
1915
  assertName,
1903
1916
  mini,
1904
- cyclic: cyclicSchemas.has(node.name)
1917
+ cyclic: cyclicSchemas.has(node.name),
1918
+ compile
1905
1919
  }),
1906
1920
  hasDirectionalNode && stdPrinters && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(Zod, {
1907
1921
  name: resolver.schema.inputName(node.name),
@@ -1912,7 +1926,8 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1912
1926
  isName: typeGuards ? resolver.schema.isName(resolver.schema.inputName(node.name)) : null,
1913
1927
  assertName: typeGuards ? resolver.schema.assertName(resolver.schema.inputName(node.name)) : null,
1914
1928
  mini,
1915
- cyclic: cyclicSchemas.has(node.name)
1929
+ cyclic: cyclicSchemas.has(node.name),
1930
+ compile
1916
1931
  })
1917
1932
  ]
1918
1933
  });
@@ -1920,7 +1935,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1920
1935
  operation(node, ctx) {
1921
1936
  if (!kubb_kit.ast.isHttpOperationNode(node)) return null;
1922
1937
  const { adapter, config, resolver, root } = ctx;
1923
- const { output, coercion, guidType, regexType, mini, inferred, importPath, group, printer } = ctx.options;
1938
+ const { output, coercion, guidType, regexType, compile, mini, inferred, importPath, group, printer } = ctx.options;
1924
1939
  const dateType = getOasAdapter(adapter).options.dateType;
1925
1940
  const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
1926
1941
  const meta = { file: resolver.file({
@@ -1999,7 +2014,8 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1999
2014
  node: schema,
2000
2015
  printer: schemaPrinter,
2001
2016
  inferTypeName,
2002
- cyclic: false
2017
+ cyclic: false,
2018
+ compile
2003
2019
  })] });
2004
2020
  }
2005
2021
  function buildContentTypeVariants(entries, baseName, decorate, direction) {
@@ -2246,7 +2262,7 @@ const pluginZod = (0, kubb_kit.definePlugin)((options) => {
2246
2262
  const { output = {
2247
2263
  path: "zod",
2248
2264
  barrel: { type: "named" }
2249
- }, group, exclude = [], include, override = [], mini = false, guidType = "uuid", regexType = "literal", importPath = mini ? "zod/mini" : "zod", coercion = false, inferred = false, typeGuards = false, printer, resolver: userResolver, macros: userMacros } = options;
2265
+ }, 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;
2250
2266
  const groupConfig = createGroupConfig(group);
2251
2267
  return {
2252
2268
  name: pluginZodName,
@@ -2263,6 +2279,7 @@ const pluginZod = (0, kubb_kit.definePlugin)((options) => {
2263
2279
  inferred,
2264
2280
  guidType,
2265
2281
  regexType,
2282
+ compile,
2266
2283
  mini,
2267
2284
  typeGuards,
2268
2285
  printer