@kubb/plugin-zod 5.2.0-canary.20260922T120902 → 5.2.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
@@ -745,31 +745,61 @@ function mapSchemaItems(node, transform) {
745
745
  }
746
746
  //#endregion
747
747
  //#region src/components/Zod.tsx
748
- function Zod({ name, node, printer, inferTypeName, cyclic }) {
748
+ function Zod({ name, node, printer, inferTypeName, typeGuards, isName, assertName, mini, cyclic }) {
749
749
  const output = printer.print(node);
750
750
  if (!output) return;
751
751
  const needsAnnotation = cyclic && node.type !== "object";
752
- return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
753
- name,
754
- isExportable: true,
755
- isIndexable: true,
756
- children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Const, {
757
- export: true,
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, {
758
757
  name,
759
- type: needsAnnotation ? "z.ZodType" : void 0,
760
- children: output
761
- })
762
- }), inferTypeName && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
763
- name: inferTypeName,
764
- isExportable: true,
765
- isIndexable: true,
766
- isTypeOnly: true,
767
- children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Type, {
768
- export: true,
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, {
769
768
  name: inferTypeName,
770
- children: `z.infer<typeof ${name}>`
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
+ })
771
801
  })
772
- })] });
802
+ ] });
773
803
  }
774
804
  //#endregion
775
805
  //#region src/constants.ts
@@ -1200,6 +1230,32 @@ const scalarNodes = {
1200
1230
  boolean: () => "z.boolean()",
1201
1231
  null: () => "z.null()",
1202
1232
  string(node) {
1233
+ if (!shouldCoerce(this.options.coercion, "strings")) {
1234
+ if (node.format === "byte" || node.format === "base64") return `z.base64()${lengthConstraints({
1235
+ ...node,
1236
+ regexType: this.options.regexType
1237
+ })}`;
1238
+ if (node.format === "base64url") return `z.base64url()${lengthConstraints({
1239
+ ...node,
1240
+ regexType: this.options.regexType
1241
+ })}`;
1242
+ if (node.format === "jwt") return `z.jwt()${lengthConstraints({
1243
+ ...node,
1244
+ regexType: this.options.regexType
1245
+ })}`;
1246
+ if (node.format === "ulid") return `z.ulid()${lengthConstraints({
1247
+ ...node,
1248
+ regexType: this.options.regexType
1249
+ })}`;
1250
+ if (node.format === "iban") return `z.iban()${lengthConstraints({
1251
+ ...node,
1252
+ regexType: this.options.regexType
1253
+ })}`;
1254
+ if (node.format === "duration") return `z.iso.duration()${lengthConstraints({
1255
+ ...node,
1256
+ regexType: this.options.regexType
1257
+ })}`;
1258
+ }
1203
1259
  const base = shouldCoerce(this.options.coercion, "strings") ? "z.coerce.string()" : "z.string()";
1204
1260
  const pattern = node.pattern ?? integerFormatPattern(node.format);
1205
1261
  return `${base}${lengthConstraints({
@@ -1212,6 +1268,10 @@ const scalarNodes = {
1212
1268
  return `${shouldCoerce(this.options.coercion, "numbers") ? "z.coerce.number()" : "z.number()"}${numberConstraints(node)}`;
1213
1269
  },
1214
1270
  integer(node) {
1271
+ if (!shouldCoerce(this.options.coercion, "numbers")) {
1272
+ if (node.format === "int32") return `z.int32()${numberConstraints(node)}`;
1273
+ if (node.format === "uint32") return `z.uint32()${numberConstraints(node)}`;
1274
+ }
1215
1275
  return `${shouldCoerce(this.options.coercion, "numbers") ? "z.coerce.number().int()" : "z.int()"}${numberConstraints(node)}`;
1216
1276
  },
1217
1277
  bigint() {
@@ -1461,6 +1521,30 @@ const printerZodMini = kubb_kit.ast.createPrinter((options) => {
1461
1521
  boolean: () => "z.boolean()",
1462
1522
  null: () => "z.null()",
1463
1523
  string(node) {
1524
+ if (node.format === "byte" || node.format === "base64") return `z.base64()${lengthChecksMini({
1525
+ ...node,
1526
+ regexType: this.options.regexType
1527
+ })}`;
1528
+ if (node.format === "base64url") return `z.base64url()${lengthChecksMini({
1529
+ ...node,
1530
+ regexType: this.options.regexType
1531
+ })}`;
1532
+ if (node.format === "jwt") return `z.jwt()${lengthChecksMini({
1533
+ ...node,
1534
+ regexType: this.options.regexType
1535
+ })}`;
1536
+ if (node.format === "ulid") return `z.ulid()${lengthChecksMini({
1537
+ ...node,
1538
+ regexType: this.options.regexType
1539
+ })}`;
1540
+ if (node.format === "iban") return `z.iban()${lengthChecksMini({
1541
+ ...node,
1542
+ regexType: this.options.regexType
1543
+ })}`;
1544
+ if (node.format === "duration") return `z.iso.duration()${lengthChecksMini({
1545
+ ...node,
1546
+ regexType: this.options.regexType
1547
+ })}`;
1464
1548
  const pattern = node.pattern ?? integerFormatPattern(node.format);
1465
1549
  return `z.string()${lengthChecksMini({
1466
1550
  ...node,
@@ -1472,6 +1556,8 @@ const printerZodMini = kubb_kit.ast.createPrinter((options) => {
1472
1556
  return `z.number()${numberChecksMini(node)}`;
1473
1557
  },
1474
1558
  integer(node) {
1559
+ if (node.format === "int32") return `z.int32()${numberChecksMini(node)}`;
1560
+ if (node.format === "uint32") return `z.uint32()${numberChecksMini(node)}`;
1475
1561
  return `z.int()${numberChecksMini(node)}`;
1476
1562
  },
1477
1563
  bigint(node) {
@@ -1677,7 +1763,7 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1677
1763
  renderer: kubb_jsx.jsxRenderer,
1678
1764
  schema(node, ctx) {
1679
1765
  const { adapter, config, resolver, root } = ctx;
1680
- const { output, coercion, guidType, regexType, mini, inferred, importPath, group, printer } = ctx.options;
1766
+ const { output, coercion, guidType, regexType, mini, inferred, typeGuards, importPath, group, printer } = ctx.options;
1681
1767
  const dateType = getOasAdapter(adapter).options.dateType;
1682
1768
  if (!node.name) return;
1683
1769
  const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
@@ -1733,6 +1819,8 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1733
1819
  })
1734
1820
  };
1735
1821
  const inferTypeName = inferred ? resolver.schema.typeName(node.name) : null;
1822
+ const isName = typeGuards ? resolver.schema.isName(node.name) : null;
1823
+ const assertName = typeGuards ? resolver.schema.assertName(node.name) : null;
1736
1824
  const stdPrinters = mini ? null : getStdPrinters(resolver, {
1737
1825
  coercion,
1738
1826
  guidType,
@@ -1787,6 +1875,10 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1787
1875
  node,
1788
1876
  printer: schemaPrinter,
1789
1877
  inferTypeName,
1878
+ typeGuards,
1879
+ isName,
1880
+ assertName,
1881
+ mini,
1790
1882
  cyclic: cyclicSchemas.has(node.name)
1791
1883
  }),
1792
1884
  hasDirectionalNode && stdPrinters && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(Zod, {
@@ -1794,6 +1886,10 @@ const zodGenerator = (0, kubb_kit.defineGenerator)({
1794
1886
  node,
1795
1887
  printer: stdPrinters.encode,
1796
1888
  inferTypeName: inferred ? resolver.schema.inputTypeName(node.name) : null,
1889
+ typeGuards,
1890
+ isName: typeGuards ? resolver.schema.isName(resolver.schema.inputName(node.name)) : null,
1891
+ assertName: typeGuards ? resolver.schema.assertName(resolver.schema.inputName(node.name)) : null,
1892
+ mini,
1797
1893
  cyclic: cyclicSchemas.has(node.name)
1798
1894
  })
1799
1895
  ]
@@ -2072,6 +2168,12 @@ const resolverZod = (0, kubb_kit.createResolver)({
2072
2168
  },
2073
2169
  inputTypeName(name) {
2074
2170
  return this.schema.typeName(`${name} input`);
2171
+ },
2172
+ isName(name) {
2173
+ return ensureValidVarName(camelCase(name, { prefix: "is" }));
2174
+ },
2175
+ assertName(name) {
2176
+ return ensureValidVarName(camelCase(name, { prefix: "assert" }));
2075
2177
  }
2076
2178
  },
2077
2179
  param: createOperationParamResolver(),
@@ -2122,7 +2224,7 @@ const pluginZod = (0, kubb_kit.definePlugin)((options) => {
2122
2224
  const { output = {
2123
2225
  path: "zod",
2124
2226
  barrel: { type: "named" }
2125
- }, group, exclude = [], include, override = [], mini = false, guidType = "uuid", regexType = "literal", importPath = mini ? "zod/mini" : "zod", coercion = false, inferred = false, printer, resolver: userResolver, macros: userMacros } = options;
2227
+ }, 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;
2126
2228
  const groupConfig = createGroupConfig(group);
2127
2229
  return {
2128
2230
  name: pluginZodName,
@@ -2140,6 +2242,7 @@ const pluginZod = (0, kubb_kit.definePlugin)((options) => {
2140
2242
  guidType,
2141
2243
  regexType,
2142
2244
  mini,
2245
+ typeGuards,
2143
2246
  printer
2144
2247
  });
2145
2248
  ctx.setResolver(userResolver ? kubb_kit.Resolver.merge(resolverZod, userResolver) : resolverZod);