@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 +68 -78
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +68 -78
- package/dist/index.js.map +1 -1
- package/package.json +5 -5
package/dist/index.js
CHANGED
|
@@ -734,6 +734,74 @@ function mapSchemaItems(node, transform) {
|
|
|
734
734
|
}));
|
|
735
735
|
}
|
|
736
736
|
//#endregion
|
|
737
|
+
//#region src/components/Zod.tsx
|
|
738
|
+
function Zod({ name, node, printer, inferTypeName, typeGuards, isName, assertName, mini, cyclic, compile }) {
|
|
739
|
+
const output = printer.print(node);
|
|
740
|
+
if (!output) return;
|
|
741
|
+
const needsAnnotation = cyclic && node.type !== "object";
|
|
742
|
+
const isBare = ast.isBareRef(node, printer.options.keysToOmit);
|
|
743
|
+
const isCyclic = Boolean(cyclic || printer.options.cyclicSchemas && containsCircularRef(node, { circularSchemas: printer.options.cyclicSchemas }));
|
|
744
|
+
const value = Boolean(compile) && !isBare && !isCyclic ? typeof compile === "object" && compile.strict ? `z.compile(${output}, { strict: true })` : `z.compile(${output})` : output;
|
|
745
|
+
const targetType = inferTypeName ?? `z.infer<typeof ${name}>`;
|
|
746
|
+
const shouldGenerateIs = isName && (typeof typeGuards === "object" ? typeGuards.is ?? true : Boolean(typeGuards));
|
|
747
|
+
const shouldGenerateAssert = assertName && (typeof typeGuards === "object" ? typeGuards.assert ?? true : Boolean(typeGuards));
|
|
748
|
+
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
749
|
+
/* @__PURE__ */ jsx(File.Source, {
|
|
750
|
+
name,
|
|
751
|
+
isExportable: true,
|
|
752
|
+
isIndexable: true,
|
|
753
|
+
children: /* @__PURE__ */ jsx(Const, {
|
|
754
|
+
export: true,
|
|
755
|
+
name,
|
|
756
|
+
type: needsAnnotation ? "z.ZodType" : void 0,
|
|
757
|
+
children: value
|
|
758
|
+
})
|
|
759
|
+
}),
|
|
760
|
+
inferTypeName && /* @__PURE__ */ jsx(File.Source, {
|
|
761
|
+
name: inferTypeName,
|
|
762
|
+
isExportable: true,
|
|
763
|
+
isIndexable: true,
|
|
764
|
+
isTypeOnly: true,
|
|
765
|
+
children: /* @__PURE__ */ jsx(Type, {
|
|
766
|
+
export: true,
|
|
767
|
+
name: inferTypeName,
|
|
768
|
+
children: `z.infer<typeof ${name}>`
|
|
769
|
+
})
|
|
770
|
+
}),
|
|
771
|
+
shouldGenerateIs && /* @__PURE__ */ jsx(File.Source, {
|
|
772
|
+
name: isName,
|
|
773
|
+
isExportable: true,
|
|
774
|
+
isIndexable: true,
|
|
775
|
+
children: /* @__PURE__ */ jsx(Const, {
|
|
776
|
+
export: true,
|
|
777
|
+
name: isName,
|
|
778
|
+
JSDoc: { comments: [`Type guard for {@link ${name}}`] },
|
|
779
|
+
children: mini ? `(data: unknown): data is ${targetType} => z.validate(${name}, data)` : `(data: unknown): data is ${targetType} => ${name}.validate(data)`
|
|
780
|
+
})
|
|
781
|
+
}),
|
|
782
|
+
shouldGenerateAssert && /* @__PURE__ */ jsx(File.Source, {
|
|
783
|
+
name: assertName,
|
|
784
|
+
isExportable: true,
|
|
785
|
+
isIndexable: true,
|
|
786
|
+
children: /* @__PURE__ */ jsx(Function, {
|
|
787
|
+
export: true,
|
|
788
|
+
name: assertName,
|
|
789
|
+
params: "data: unknown",
|
|
790
|
+
returnType: `asserts data is ${targetType}`,
|
|
791
|
+
JSDoc: { comments: [`Asserter for {@link ${name}}`, "@throws {z.ZodError} If data is invalid"] },
|
|
792
|
+
children: mini ? `if (!z.validate(${name}, data)) {\n z.parse(${name}, data)\n}` : `if (!${name}.validate(data)) {\n ${name}.parse(data)\n}`
|
|
793
|
+
})
|
|
794
|
+
})
|
|
795
|
+
] });
|
|
796
|
+
}
|
|
797
|
+
//#endregion
|
|
798
|
+
//#region src/constants.ts
|
|
799
|
+
/**
|
|
800
|
+
* Import paths that use a namespace import (`import * as z from '...'`).
|
|
801
|
+
* All other import paths use a named import (`import { z } from '...'`).
|
|
802
|
+
*/
|
|
803
|
+
const ZOD_NAMESPACE_IMPORTS = /* @__PURE__ */ new Set(["zod", "zod/mini"]);
|
|
804
|
+
//#endregion
|
|
737
805
|
//#region src/utils.ts
|
|
738
806
|
/**
|
|
739
807
|
* Returns `true` when the given coercion option enables coercion for the specified type.
|
|
@@ -752,16 +820,6 @@ function isPlainInlineObject(node) {
|
|
|
752
820
|
return node.type === "object" && !node.nullable && !node.optional && !node.nullish && node.additionalProperties === void 0 && !node.patternProperties;
|
|
753
821
|
}
|
|
754
822
|
/**
|
|
755
|
-
* Whether the node is a bare reference schema (a `$ref` with no modifiers, descriptions, or omit keys).
|
|
756
|
-
* Bare references resolve directly to another schema variable and should not be re-wrapped in `z.compile(...)`.
|
|
757
|
-
*/
|
|
758
|
-
function isBareRef(node, keysToOmit) {
|
|
759
|
-
const ref = ast.narrowSchema(node, "ref");
|
|
760
|
-
if (!ref) return false;
|
|
761
|
-
const meta = syncSchemaRef(ref);
|
|
762
|
-
return !meta.nullable && !meta.optional && !meta.nullish && meta.default === void 0 && !meta.description && !meta.examples?.length && !keysToOmit?.length;
|
|
763
|
-
}
|
|
764
|
-
/**
|
|
765
823
|
* Whether a node renders as a bare Zod object — one that accepts `.extend(…)` and can be a
|
|
766
824
|
* `z.discriminatedUnion` option. Covers object nodes, `$ref`s that resolve to (or are still
|
|
767
825
|
* unresolved) objects, single-member unions of an object, and object-composable `allOf`.
|
|
@@ -1008,74 +1066,6 @@ function buildGroupedParamsSchema({ params, optional }) {
|
|
|
1008
1066
|
});
|
|
1009
1067
|
}
|
|
1010
1068
|
//#endregion
|
|
1011
|
-
//#region src/components/Zod.tsx
|
|
1012
|
-
function Zod({ name, node, printer, inferTypeName, typeGuards, isName, assertName, mini, cyclic, compile }) {
|
|
1013
|
-
const output = printer.print(node);
|
|
1014
|
-
if (!output) return;
|
|
1015
|
-
const needsAnnotation = cyclic && node.type !== "object";
|
|
1016
|
-
const isBare = isBareRef(node, printer.options.keysToOmit);
|
|
1017
|
-
const isCyclic = Boolean(cyclic || printer.options.cyclicSchemas && containsCircularRef(node, { circularSchemas: printer.options.cyclicSchemas }));
|
|
1018
|
-
const value = Boolean(compile) && !isBare && !isCyclic ? typeof compile === "object" && compile.strict ? `z.compile(${output}, { strict: true })` : `z.compile(${output})` : output;
|
|
1019
|
-
const targetType = inferTypeName ?? `z.infer<typeof ${name}>`;
|
|
1020
|
-
const shouldGenerateIs = isName && (typeof typeGuards === "object" ? typeGuards.is ?? true : Boolean(typeGuards));
|
|
1021
|
-
const shouldGenerateAssert = assertName && (typeof typeGuards === "object" ? typeGuards.assert ?? true : Boolean(typeGuards));
|
|
1022
|
-
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
1023
|
-
/* @__PURE__ */ jsx(File.Source, {
|
|
1024
|
-
name,
|
|
1025
|
-
isExportable: true,
|
|
1026
|
-
isIndexable: true,
|
|
1027
|
-
children: /* @__PURE__ */ jsx(Const, {
|
|
1028
|
-
export: true,
|
|
1029
|
-
name,
|
|
1030
|
-
type: needsAnnotation ? "z.ZodType" : void 0,
|
|
1031
|
-
children: value
|
|
1032
|
-
})
|
|
1033
|
-
}),
|
|
1034
|
-
inferTypeName && /* @__PURE__ */ jsx(File.Source, {
|
|
1035
|
-
name: inferTypeName,
|
|
1036
|
-
isExportable: true,
|
|
1037
|
-
isIndexable: true,
|
|
1038
|
-
isTypeOnly: true,
|
|
1039
|
-
children: /* @__PURE__ */ jsx(Type, {
|
|
1040
|
-
export: true,
|
|
1041
|
-
name: inferTypeName,
|
|
1042
|
-
children: `z.infer<typeof ${name}>`
|
|
1043
|
-
})
|
|
1044
|
-
}),
|
|
1045
|
-
shouldGenerateIs && /* @__PURE__ */ jsx(File.Source, {
|
|
1046
|
-
name: isName,
|
|
1047
|
-
isExportable: true,
|
|
1048
|
-
isIndexable: true,
|
|
1049
|
-
children: /* @__PURE__ */ jsx(Const, {
|
|
1050
|
-
export: true,
|
|
1051
|
-
name: isName,
|
|
1052
|
-
JSDoc: { comments: [`Type guard for {@link ${name}}`] },
|
|
1053
|
-
children: mini ? `(data: unknown): data is ${targetType} => z.validate(${name}, data)` : `(data: unknown): data is ${targetType} => ${name}.validate(data)`
|
|
1054
|
-
})
|
|
1055
|
-
}),
|
|
1056
|
-
shouldGenerateAssert && /* @__PURE__ */ jsx(File.Source, {
|
|
1057
|
-
name: assertName,
|
|
1058
|
-
isExportable: true,
|
|
1059
|
-
isIndexable: true,
|
|
1060
|
-
children: /* @__PURE__ */ jsx(Function, {
|
|
1061
|
-
export: true,
|
|
1062
|
-
name: assertName,
|
|
1063
|
-
params: "data: unknown",
|
|
1064
|
-
returnType: `asserts data is ${targetType}`,
|
|
1065
|
-
JSDoc: { comments: [`Asserter for {@link ${name}}`, "@throws {z.ZodError} If data is invalid"] },
|
|
1066
|
-
children: mini ? `if (!z.validate(${name}, data)) {\n z.parse(${name}, data)\n}` : `if (!${name}.validate(data)) {\n ${name}.parse(data)\n}`
|
|
1067
|
-
})
|
|
1068
|
-
})
|
|
1069
|
-
] });
|
|
1070
|
-
}
|
|
1071
|
-
//#endregion
|
|
1072
|
-
//#region src/constants.ts
|
|
1073
|
-
/**
|
|
1074
|
-
* Import paths that use a namespace import (`import * as z from '...'`).
|
|
1075
|
-
* All other import paths use a named import (`import { z } from '...'`).
|
|
1076
|
-
*/
|
|
1077
|
-
const ZOD_NAMESPACE_IMPORTS = /* @__PURE__ */ new Set(["zod", "zod/mini"]);
|
|
1078
|
-
//#endregion
|
|
1079
1069
|
//#region src/printers/printerZod.ts
|
|
1080
1070
|
/**
|
|
1081
1071
|
* A `oneOf` member that is an inline object is exhaustive, so it prints as a strict object.
|