@kubb/plugin-zod 5.3.0 → 5.4.0-canary.20260925T211808

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,6 +744,74 @@ 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, compile }) {
749
+ const output = printer.print(node);
750
+ if (!output) return;
751
+ const needsAnnotation = cyclic && node.type !== "object";
752
+ const isBare = kubb_kit.ast.isBareRef(node, printer.options.keysToOmit);
753
+ const isCyclic = Boolean(cyclic || printer.options.cyclicSchemas && (0, kubb_kit.containsCircularRef)(node, { circularSchemas: printer.options.cyclicSchemas }));
754
+ const value = Boolean(compile) && !isBare && !isCyclic ? typeof compile === "object" && compile.strict ? `z.compile(${output}, { strict: true })` : `z.compile(${output})` : output;
755
+ const targetType = inferTypeName ?? `z.infer<typeof ${name}>`;
756
+ const shouldGenerateIs = isName && (typeof typeGuards === "object" ? typeGuards.is ?? true : Boolean(typeGuards));
757
+ const shouldGenerateAssert = assertName && (typeof typeGuards === "object" ? typeGuards.assert ?? true : Boolean(typeGuards));
758
+ return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx_jsx_runtime.Fragment, { children: [
759
+ /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
760
+ name,
761
+ isExportable: true,
762
+ isIndexable: true,
763
+ children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Const, {
764
+ export: true,
765
+ name,
766
+ type: needsAnnotation ? "z.ZodType" : void 0,
767
+ children: value
768
+ })
769
+ }),
770
+ inferTypeName && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
771
+ name: inferTypeName,
772
+ isExportable: true,
773
+ isIndexable: true,
774
+ isTypeOnly: true,
775
+ children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Type, {
776
+ export: true,
777
+ name: inferTypeName,
778
+ children: `z.infer<typeof ${name}>`
779
+ })
780
+ }),
781
+ shouldGenerateIs && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
782
+ name: isName,
783
+ isExportable: true,
784
+ isIndexable: true,
785
+ children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Const, {
786
+ export: true,
787
+ name: isName,
788
+ JSDoc: { comments: [`Type guard for {@link ${name}}`] },
789
+ children: mini ? `(data: unknown): data is ${targetType} => z.validate(${name}, data)` : `(data: unknown): data is ${targetType} => ${name}.validate(data)`
790
+ })
791
+ }),
792
+ shouldGenerateAssert && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
793
+ name: assertName,
794
+ isExportable: true,
795
+ isIndexable: true,
796
+ children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Function, {
797
+ export: true,
798
+ name: assertName,
799
+ params: "data: unknown",
800
+ returnType: `asserts data is ${targetType}`,
801
+ JSDoc: { comments: [`Asserter for {@link ${name}}`, "@throws {z.ZodError} If data is invalid"] },
802
+ children: mini ? `if (!z.validate(${name}, data)) {\n z.parse(${name}, data)\n}` : `if (!${name}.validate(data)) {\n ${name}.parse(data)\n}`
803
+ })
804
+ })
805
+ ] });
806
+ }
807
+ //#endregion
808
+ //#region src/constants.ts
809
+ /**
810
+ * Import paths that use a namespace import (`import * as z from '...'`).
811
+ * All other import paths use a named import (`import { z } from '...'`).
812
+ */
813
+ const ZOD_NAMESPACE_IMPORTS = /* @__PURE__ */ new Set(["zod", "zod/mini"]);
814
+ //#endregion
747
815
  //#region src/utils.ts
748
816
  /**
749
817
  * Returns `true` when the given coercion option enables coercion for the specified type.
@@ -762,16 +830,6 @@ function isPlainInlineObject(node) {
762
830
  return node.type === "object" && !node.nullable && !node.optional && !node.nullish && node.additionalProperties === void 0 && !node.patternProperties;
763
831
  }
764
832
  /**
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
- /**
775
833
  * Whether a node renders as a bare Zod object — one that accepts `.extend(…)` and can be a
776
834
  * `z.discriminatedUnion` option. Covers object nodes, `$ref`s that resolve to (or are still
777
835
  * unresolved) objects, single-member unions of an object, and object-composable `allOf`.
@@ -1018,74 +1076,6 @@ function buildGroupedParamsSchema({ params, optional }) {
1018
1076
  });
1019
1077
  }
1020
1078
  //#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
1089
1079
  //#region src/printers/printerZod.ts
1090
1080
  /**
1091
1081
  * A `oneOf` member that is an inline object is exhaustive, so it prints as a strict object.