@kubb/plugin-zod 5.0.0-beta.15 → 5.0.0-beta.25
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 +130 -133
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +79 -34
- package/dist/index.js +130 -133
- package/dist/index.js.map +1 -1
- package/extension.yaml +646 -185
- package/package.json +4 -4
- package/src/components/Operations.tsx +4 -4
- package/src/components/Zod.tsx +1 -1
- package/src/generators/zodGenerator.tsx +33 -21
- package/src/plugin.ts +21 -8
- package/src/printers/printerZod.ts +36 -61
- package/src/printers/printerZodMini.ts +26 -47
- package/src/resolvers/resolverZod.ts +9 -4
- package/src/types.ts +42 -21
- package/src/utils.ts +20 -36
package/dist/index.cjs
CHANGED
|
@@ -271,11 +271,11 @@ function buildSchemaNames(node, { params, resolver }) {
|
|
|
271
271
|
}
|
|
272
272
|
responses["default"] = resolver.resolveResponseName(node);
|
|
273
273
|
return {
|
|
274
|
-
request: node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) :
|
|
274
|
+
request: node.requestBody?.content?.[0]?.schema ? resolver.resolveDataName(node) : null,
|
|
275
275
|
parameters: {
|
|
276
|
-
path: pathParam ? resolver.resolvePathParamsName(node, pathParam) :
|
|
277
|
-
query: queryParam ? resolver.resolveQueryParamsName(node, queryParam) :
|
|
278
|
-
header: headerParam ? resolver.resolveHeaderParamsName(node, headerParam) :
|
|
276
|
+
path: pathParam ? resolver.resolvePathParamsName(node, pathParam) : null,
|
|
277
|
+
query: queryParam ? resolver.resolveQueryParamsName(node, queryParam) : null,
|
|
278
|
+
header: headerParam ? resolver.resolveHeaderParamsName(node, headerParam) : null
|
|
279
279
|
},
|
|
280
280
|
responses,
|
|
281
281
|
errors
|
|
@@ -349,27 +349,26 @@ function lengthChecksMini({ min, max, pattern }) {
|
|
|
349
349
|
* to a schema value string using the chainable Zod v4 API.
|
|
350
350
|
*/
|
|
351
351
|
function applyModifiers({ value, nullable, optional, nullish, defaultValue, description }) {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
352
|
+
const withModifier = (() => {
|
|
353
|
+
if (nullish || nullable && optional) return `${value}.nullish()`;
|
|
354
|
+
if (optional) return `${value}.optional()`;
|
|
355
|
+
if (nullable) return `${value}.nullable()`;
|
|
356
|
+
return value;
|
|
357
|
+
})();
|
|
358
|
+
const withDefault = defaultValue !== void 0 ? `${withModifier}.default(${formatDefault(defaultValue)})` : withModifier;
|
|
359
|
+
return description ? `${withDefault}.describe(${stringify(description)})` : withDefault;
|
|
359
360
|
}
|
|
360
361
|
/**
|
|
361
362
|
* Apply nullable / optional / nullish modifiers using the functional `zod/mini` API
|
|
362
363
|
* (`z.nullable()`, `z.optional()`, `z.nullish()`).
|
|
363
364
|
*/
|
|
364
365
|
function applyMiniModifiers({ value, nullable, optional, nullish, defaultValue }) {
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
}
|
|
371
|
-
if (defaultValue !== void 0) result = `z._default(${result}, ${formatDefault(defaultValue)})`;
|
|
372
|
-
return result;
|
|
366
|
+
const withModifier = (() => {
|
|
367
|
+
if (nullish) return `z.nullish(${value})`;
|
|
368
|
+
const withNullable = nullable ? `z.nullable(${value})` : value;
|
|
369
|
+
return optional ? `z.optional(${withNullable})` : withNullable;
|
|
370
|
+
})();
|
|
371
|
+
return defaultValue !== void 0 ? `z._default(${withModifier}, ${formatDefault(defaultValue)})` : withModifier;
|
|
373
372
|
}
|
|
374
373
|
//#endregion
|
|
375
374
|
//#region src/printers/printerZod.ts
|
|
@@ -383,6 +382,11 @@ function strictOneOfMember$1(member, node) {
|
|
|
383
382
|
return member;
|
|
384
383
|
}
|
|
385
384
|
__name(strictOneOfMember$1, "strictOneOfMember");
|
|
385
|
+
function getMemberConstraint(member) {
|
|
386
|
+
if (member.primitive === "string") return lengthConstraints(_kubb_core.ast.narrowSchema(member, "string") ?? {}) || void 0;
|
|
387
|
+
if (member.primitive === "number" || member.primitive === "integer") return numberConstraints(_kubb_core.ast.narrowSchema(member, "number") ?? _kubb_core.ast.narrowSchema(member, "integer") ?? {}) || void 0;
|
|
388
|
+
if (member.primitive === "array") return lengthConstraints(_kubb_core.ast.narrowSchema(member, "array") ?? {}) || void 0;
|
|
389
|
+
}
|
|
386
390
|
/**
|
|
387
391
|
* Zod v4 printer built with `definePrinter`.
|
|
388
392
|
*
|
|
@@ -455,14 +459,14 @@ const printerZod = _kubb_core.ast.definePrinter((options) => {
|
|
|
455
459
|
return `z.enum([${nonNullValues.map(formatLiteral).join(", ")}])`;
|
|
456
460
|
},
|
|
457
461
|
ref(node) {
|
|
458
|
-
if (!node.name) return
|
|
462
|
+
if (!node.name) return null;
|
|
459
463
|
const refName = node.ref ? _kubb_core.ast.extractRefName(node.ref) ?? node.name : node.name;
|
|
460
464
|
const resolvedName = node.ref ? this.options.resolver?.default(refName, "function") ?? refName : node.name;
|
|
461
465
|
if (node.ref && this.options.cyclicSchemas?.has(refName)) return `z.lazy(() => ${resolvedName})`;
|
|
462
466
|
return resolvedName;
|
|
463
467
|
},
|
|
464
468
|
object(node) {
|
|
465
|
-
|
|
469
|
+
const objectBase = `z.object({\n ${node.properties.map((prop) => {
|
|
466
470
|
const { name: propName, schema } = prop;
|
|
467
471
|
const meta = _kubb_core.ast.syncSchemaRef(schema);
|
|
468
472
|
const isNullable = meta.nullable;
|
|
@@ -477,8 +481,7 @@ const printerZod = _kubb_core.ast.definePrinter((options) => {
|
|
|
477
481
|
output: baseOutput,
|
|
478
482
|
schema
|
|
479
483
|
}) || baseOutput : baseOutput;
|
|
480
|
-
|
|
481
|
-
if (schema.type !== "ref" && meta.type === "ref") descriptionToApply = void 0;
|
|
484
|
+
const descriptionToApply = schema.type !== "ref" && meta.type === "ref" ? void 0 : meta.description;
|
|
482
485
|
const value = applyModifiers({
|
|
483
486
|
value: wrappedOutput,
|
|
484
487
|
nullable: isNullable,
|
|
@@ -490,17 +493,19 @@ const printerZod = _kubb_core.ast.definePrinter((options) => {
|
|
|
490
493
|
if (hasSelfRef) return `get "${propName}"() { return ${value} }`;
|
|
491
494
|
return `"${propName}": ${value}`;
|
|
492
495
|
}).join(",\n ")}\n })`;
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
496
|
+
return (() => {
|
|
497
|
+
if (node.additionalProperties && node.additionalProperties !== true) {
|
|
498
|
+
const catchallType = this.transform(node.additionalProperties);
|
|
499
|
+
return catchallType ? `${objectBase}.catchall(${catchallType})` : objectBase;
|
|
500
|
+
}
|
|
501
|
+
if (node.additionalProperties === true) return `${objectBase}.catchall(${this.transform(_kubb_core.ast.createSchema({ type: "unknown" }))})`;
|
|
502
|
+
if (node.additionalProperties === false) return `${objectBase}.strict()`;
|
|
503
|
+
return objectBase;
|
|
504
|
+
})();
|
|
499
505
|
},
|
|
500
506
|
array(node) {
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
return result;
|
|
507
|
+
const base = `z.array(${(node.items ?? []).map((item) => this.transform(item)).filter(Boolean).join(", ") || this.transform(_kubb_core.ast.createSchema({ type: "unknown" }))})${lengthConstraints(node)}`;
|
|
508
|
+
return node.unique ? `${base}.refine(items => new Set(items).size === items.length, { message: "Array entries must be unique" })` : base;
|
|
504
509
|
},
|
|
505
510
|
tuple(node) {
|
|
506
511
|
return `z.tuple([${(node.items ?? []).map((item) => this.transform(item)).filter(Boolean).join(", ")}])`;
|
|
@@ -521,47 +526,29 @@ const printerZod = _kubb_core.ast.definePrinter((options) => {
|
|
|
521
526
|
if (members.length === 0) return "";
|
|
522
527
|
const [first, ...rest] = members;
|
|
523
528
|
if (!first) return "";
|
|
524
|
-
|
|
525
|
-
if (!
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
if (c) {
|
|
530
|
-
base += c;
|
|
531
|
-
continue;
|
|
532
|
-
}
|
|
533
|
-
} else if (member.primitive === "number" || member.primitive === "integer") {
|
|
534
|
-
const c = numberConstraints(_kubb_core.ast.narrowSchema(member, "number") ?? _kubb_core.ast.narrowSchema(member, "integer") ?? {});
|
|
535
|
-
if (c) {
|
|
536
|
-
base += c;
|
|
537
|
-
continue;
|
|
538
|
-
}
|
|
539
|
-
} else if (member.primitive === "array") {
|
|
540
|
-
const c = lengthConstraints(_kubb_core.ast.narrowSchema(member, "array") ?? {});
|
|
541
|
-
if (c) {
|
|
542
|
-
base += c;
|
|
543
|
-
continue;
|
|
544
|
-
}
|
|
545
|
-
}
|
|
529
|
+
const firstBase = this.transform(first);
|
|
530
|
+
if (!firstBase) return "";
|
|
531
|
+
return rest.reduce((acc, member) => {
|
|
532
|
+
const constraint = getMemberConstraint(member);
|
|
533
|
+
if (constraint) return acc + constraint;
|
|
546
534
|
const transformed = this.transform(member);
|
|
547
|
-
|
|
548
|
-
}
|
|
549
|
-
return base;
|
|
535
|
+
return transformed ? `${acc}.and(${transformed})` : acc;
|
|
536
|
+
}, firstBase);
|
|
550
537
|
},
|
|
551
538
|
...options.nodes
|
|
552
539
|
},
|
|
553
540
|
print(node) {
|
|
554
541
|
const { keysToOmit } = this.options;
|
|
555
|
-
|
|
556
|
-
if (!
|
|
542
|
+
const transformed = this.transform(node);
|
|
543
|
+
if (!transformed) return null;
|
|
557
544
|
const meta = _kubb_core.ast.syncSchemaRef(node);
|
|
558
|
-
if (keysToOmit?.length && meta.primitive === "object" && !(meta.type === "union" && meta.discriminatorPropertyName)) {
|
|
559
|
-
const lazyMatch = base.match(/^z\.lazy\(\(\)\s*=>\s*(.+)\)$/);
|
|
560
|
-
if (lazyMatch) base = `z.lazy(() => ${lazyMatch[1]}.omit({ ${keysToOmit.map((k) => `"${k}": true`).join(", ")} }))`;
|
|
561
|
-
else base = `${base}.omit({ ${keysToOmit.map((k) => `"${k}": true`).join(", ")} })`;
|
|
562
|
-
}
|
|
563
545
|
return applyModifiers({
|
|
564
|
-
value:
|
|
546
|
+
value: (() => {
|
|
547
|
+
if (!keysToOmit?.length || meta.primitive !== "object" || meta.type === "union" && meta.discriminatorPropertyName) return transformed;
|
|
548
|
+
const lazyMatch = transformed.match(/^z\.lazy\(\(\)\s*=>\s*(.+)\)$/);
|
|
549
|
+
if (lazyMatch) return `z.lazy(() => ${lazyMatch[1]}.omit({ ${keysToOmit.map((k) => `"${k}": true`).join(", ")} }))`;
|
|
550
|
+
return `${transformed}.omit({ ${keysToOmit.map((k) => `"${k}": true`).join(", ")} })`;
|
|
551
|
+
})(),
|
|
565
552
|
nullable: meta.nullable,
|
|
566
553
|
optional: meta.optional,
|
|
567
554
|
nullish: meta.nullish,
|
|
@@ -577,6 +564,11 @@ function strictOneOfMember(member, node) {
|
|
|
577
564
|
if (node.type === "object" && (node.additionalProperties === void 0 || node.additionalProperties === false)) return member.replace(/^z\.object\(/, "z.strictObject(");
|
|
578
565
|
return member;
|
|
579
566
|
}
|
|
567
|
+
function getMemberConstraintMini(member) {
|
|
568
|
+
if (member.primitive === "string") return lengthChecksMini(_kubb_core.ast.narrowSchema(member, "string") ?? {}) || void 0;
|
|
569
|
+
if (member.primitive === "number" || member.primitive === "integer") return numberChecksMini(_kubb_core.ast.narrowSchema(member, "number") ?? _kubb_core.ast.narrowSchema(member, "integer") ?? {}) || void 0;
|
|
570
|
+
if (member.primitive === "array") return lengthChecksMini(_kubb_core.ast.narrowSchema(member, "array") ?? {}) || void 0;
|
|
571
|
+
}
|
|
580
572
|
/**
|
|
581
573
|
* Zod v4 **Mini** printer built with `definePrinter`.
|
|
582
574
|
*
|
|
@@ -645,7 +637,7 @@ const printerZodMini = _kubb_core.ast.definePrinter((options) => {
|
|
|
645
637
|
return `z.enum([${nonNullValues.map(formatLiteral).join(", ")}])`;
|
|
646
638
|
},
|
|
647
639
|
ref(node) {
|
|
648
|
-
if (!node.name) return
|
|
640
|
+
if (!node.name) return null;
|
|
649
641
|
const refName = node.ref ? _kubb_core.ast.extractRefName(node.ref) ?? node.name : node.name;
|
|
650
642
|
const resolvedName = node.ref ? this.options.resolver?.default(refName, "function") ?? refName : node.name;
|
|
651
643
|
if (node.ref && this.options.cyclicSchemas?.has(refName)) return `z.lazy(() => ${resolvedName})`;
|
|
@@ -678,9 +670,8 @@ const printerZodMini = _kubb_core.ast.definePrinter((options) => {
|
|
|
678
670
|
}).join(",\n ")}\n })`;
|
|
679
671
|
},
|
|
680
672
|
array(node) {
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
return result;
|
|
673
|
+
const base = `z.array(${(node.items ?? []).map((item) => this.transform(item)).filter(Boolean).join(", ") || this.transform(_kubb_core.ast.createSchema({ type: "unknown" }))})${lengthChecksMini(node)}`;
|
|
674
|
+
return node.unique ? `${base}.refine(items => new Set(items).size === items.length, { message: "Array entries must be unique" })` : base;
|
|
684
675
|
},
|
|
685
676
|
tuple(node) {
|
|
686
677
|
return `z.tuple([${(node.items ?? []).map((item) => this.transform(item)).filter(Boolean).join(", ")}])`;
|
|
@@ -701,47 +692,29 @@ const printerZodMini = _kubb_core.ast.definePrinter((options) => {
|
|
|
701
692
|
if (members.length === 0) return "";
|
|
702
693
|
const [first, ...rest] = members;
|
|
703
694
|
if (!first) return "";
|
|
704
|
-
|
|
705
|
-
if (!
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
if (c) {
|
|
710
|
-
base += c;
|
|
711
|
-
continue;
|
|
712
|
-
}
|
|
713
|
-
} else if (member.primitive === "number" || member.primitive === "integer") {
|
|
714
|
-
const c = numberChecksMini(_kubb_core.ast.narrowSchema(member, "number") ?? _kubb_core.ast.narrowSchema(member, "integer") ?? {});
|
|
715
|
-
if (c) {
|
|
716
|
-
base += c;
|
|
717
|
-
continue;
|
|
718
|
-
}
|
|
719
|
-
} else if (member.primitive === "array") {
|
|
720
|
-
const c = lengthChecksMini(_kubb_core.ast.narrowSchema(member, "array") ?? {});
|
|
721
|
-
if (c) {
|
|
722
|
-
base += c;
|
|
723
|
-
continue;
|
|
724
|
-
}
|
|
725
|
-
}
|
|
695
|
+
const firstBase = this.transform(first);
|
|
696
|
+
if (!firstBase) return "";
|
|
697
|
+
return rest.reduce((acc, member) => {
|
|
698
|
+
const constraint = getMemberConstraintMini(member);
|
|
699
|
+
if (constraint) return acc + constraint;
|
|
726
700
|
const transformed = this.transform(member);
|
|
727
|
-
|
|
728
|
-
}
|
|
729
|
-
return base;
|
|
701
|
+
return transformed ? `z.intersection(${acc}, ${transformed})` : acc;
|
|
702
|
+
}, firstBase);
|
|
730
703
|
},
|
|
731
704
|
...options.nodes
|
|
732
705
|
},
|
|
733
706
|
print(node) {
|
|
734
707
|
const { keysToOmit } = this.options;
|
|
735
|
-
|
|
736
|
-
if (!
|
|
708
|
+
const transformed = this.transform(node);
|
|
709
|
+
if (!transformed) return null;
|
|
737
710
|
const meta = _kubb_core.ast.syncSchemaRef(node);
|
|
738
|
-
if (keysToOmit?.length && meta.primitive === "object" && !(meta.type === "union" && meta.discriminatorPropertyName)) {
|
|
739
|
-
const lazyMatch = base.match(/^z\.lazy\(\(\)\s*=>\s*(.+)\)$/);
|
|
740
|
-
if (lazyMatch) base = `z.lazy(() => ${lazyMatch[1]}.omit({ ${keysToOmit.map((k) => `"${k}": true`).join(", ")} }))`;
|
|
741
|
-
else base = `${base}.omit({ ${keysToOmit.map((k) => `"${k}": true`).join(", ")} })`;
|
|
742
|
-
}
|
|
743
711
|
return applyMiniModifiers({
|
|
744
|
-
value:
|
|
712
|
+
value: (() => {
|
|
713
|
+
if (!keysToOmit?.length || meta.primitive !== "object" || meta.type === "union" && meta.discriminatorPropertyName) return transformed;
|
|
714
|
+
const lazyMatch = transformed.match(/^z\.lazy\(\(\)\s*=>\s*(.+)\)$/);
|
|
715
|
+
if (lazyMatch) return `z.lazy(() => ${lazyMatch[1]}.omit({ ${keysToOmit.map((k) => `"${k}": true`).join(", ")} }))`;
|
|
716
|
+
return `${transformed}.omit({ ${keysToOmit.map((k) => `"${k}": true`).join(", ")} })`;
|
|
717
|
+
})(),
|
|
745
718
|
nullable: meta.nullable,
|
|
746
719
|
optional: meta.optional,
|
|
747
720
|
nullish: meta.nullish,
|
|
@@ -754,11 +727,17 @@ const printerZodMini = _kubb_core.ast.definePrinter((options) => {
|
|
|
754
727
|
//#region src/generators/zodGenerator.tsx
|
|
755
728
|
const zodPrinterCache = /* @__PURE__ */ new WeakMap();
|
|
756
729
|
const zodMiniPrinterCache = /* @__PURE__ */ new WeakMap();
|
|
730
|
+
/**
|
|
731
|
+
* Built-in generator for `@kubb/plugin-zod`. Emits one Zod schema per
|
|
732
|
+
* schema in the spec plus per-operation request/response/parameter schemas.
|
|
733
|
+
* When `mini: true`, schemas use the Zod Mini functional API instead of
|
|
734
|
+
* chainable methods.
|
|
735
|
+
*/
|
|
757
736
|
const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
758
737
|
name: "zod",
|
|
759
738
|
renderer: _kubb_renderer_jsx.jsxRendererSync,
|
|
760
739
|
schema(node, ctx) {
|
|
761
|
-
const { adapter, config, resolver, root
|
|
740
|
+
const { adapter, config, resolver, root } = ctx;
|
|
762
741
|
const { output, coercion, guidType, mini, wrapOutput, inferred, importPath, group, printer } = ctx.options;
|
|
763
742
|
const dateType = adapter.options.dateType;
|
|
764
743
|
if (!node.name) return;
|
|
@@ -772,7 +751,7 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
772
751
|
}, {
|
|
773
752
|
root,
|
|
774
753
|
output,
|
|
775
|
-
group
|
|
754
|
+
group: group ?? void 0
|
|
776
755
|
}).path
|
|
777
756
|
}));
|
|
778
757
|
const meta = {
|
|
@@ -783,11 +762,11 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
783
762
|
}, {
|
|
784
763
|
root,
|
|
785
764
|
output,
|
|
786
|
-
group
|
|
765
|
+
group: group ?? void 0
|
|
787
766
|
})
|
|
788
767
|
};
|
|
789
|
-
const inferTypeName = inferred ? resolver.resolveSchemaTypeName(node.name) :
|
|
790
|
-
const cyclicSchemas =
|
|
768
|
+
const inferTypeName = inferred ? resolver.resolveSchemaTypeName(node.name) : null;
|
|
769
|
+
const cyclicSchemas = new Set(ctx.meta.circularNames);
|
|
791
770
|
const schemaPrinter = mini ? getCachedMiniPrinter() : getCachedStdPrinter();
|
|
792
771
|
function getCachedStdPrinter() {
|
|
793
772
|
const cached = zodPrinterCache.get(resolver);
|
|
@@ -829,11 +808,11 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
829
808
|
baseName: meta.file.baseName,
|
|
830
809
|
path: meta.file.path,
|
|
831
810
|
meta: meta.file.meta,
|
|
832
|
-
banner: resolver.resolveBanner(
|
|
811
|
+
banner: resolver.resolveBanner(ctx.meta, {
|
|
833
812
|
output,
|
|
834
813
|
config
|
|
835
814
|
}),
|
|
836
|
-
footer: resolver.resolveFooter(
|
|
815
|
+
footer: resolver.resolveFooter(ctx.meta, {
|
|
837
816
|
output,
|
|
838
817
|
config
|
|
839
818
|
}),
|
|
@@ -858,7 +837,7 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
858
837
|
});
|
|
859
838
|
},
|
|
860
839
|
operation(node, ctx) {
|
|
861
|
-
const { adapter, config, resolver, root
|
|
840
|
+
const { adapter, config, resolver, root } = ctx;
|
|
862
841
|
const { output, coercion, guidType, mini, wrapOutput, inferred, importPath, group, paramsCasing, printer } = ctx.options;
|
|
863
842
|
const dateType = adapter.options.dateType;
|
|
864
843
|
const mode = ctx.getMode(output);
|
|
@@ -872,12 +851,12 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
872
851
|
}, {
|
|
873
852
|
root,
|
|
874
853
|
output,
|
|
875
|
-
group
|
|
854
|
+
group: group ?? void 0
|
|
876
855
|
}) };
|
|
877
|
-
const cyclicSchemas =
|
|
856
|
+
const cyclicSchemas = new Set(ctx.meta.circularNames);
|
|
878
857
|
function renderSchemaEntry({ schema, name, keysToOmit }) {
|
|
879
858
|
if (!schema) return null;
|
|
880
|
-
const inferTypeName = inferred ? resolver.resolveTypeName(name) :
|
|
859
|
+
const inferTypeName = inferred ? resolver.resolveTypeName(name) : null;
|
|
881
860
|
const imports = adapter.getImports(schema, (schemaName) => ({
|
|
882
861
|
name: resolver.resolveSchemaName(schemaName),
|
|
883
862
|
path: resolver.resolveFile({
|
|
@@ -886,7 +865,7 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
886
865
|
}, {
|
|
887
866
|
root,
|
|
888
867
|
output,
|
|
889
|
-
group
|
|
868
|
+
group: group ?? void 0
|
|
890
869
|
}).path
|
|
891
870
|
}));
|
|
892
871
|
const cachedStd = zodPrinterCache.get(resolver);
|
|
@@ -977,11 +956,11 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
977
956
|
baseName: meta.file.baseName,
|
|
978
957
|
path: meta.file.path,
|
|
979
958
|
meta: meta.file.meta,
|
|
980
|
-
banner: resolver.resolveBanner(
|
|
959
|
+
banner: resolver.resolveBanner(ctx.meta, {
|
|
981
960
|
output,
|
|
982
961
|
config
|
|
983
962
|
}),
|
|
984
|
-
footer: resolver.resolveFooter(
|
|
963
|
+
footer: resolver.resolveFooter(ctx.meta, {
|
|
985
964
|
output,
|
|
986
965
|
config
|
|
987
966
|
}),
|
|
@@ -999,7 +978,7 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
999
978
|
});
|
|
1000
979
|
},
|
|
1001
980
|
operations(nodes, ctx) {
|
|
1002
|
-
const { config, resolver, root
|
|
981
|
+
const { config, resolver, root } = ctx;
|
|
1003
982
|
const { output, importPath, group, operations, paramsCasing } = ctx.options;
|
|
1004
983
|
if (!operations) return;
|
|
1005
984
|
const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
|
|
@@ -1009,7 +988,7 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1009
988
|
}, {
|
|
1010
989
|
root,
|
|
1011
990
|
output,
|
|
1012
|
-
group
|
|
991
|
+
group: group ?? void 0
|
|
1013
992
|
}) };
|
|
1014
993
|
const transformedOperations = nodes.map((node) => {
|
|
1015
994
|
return {
|
|
@@ -1034,7 +1013,7 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1034
1013
|
}, {
|
|
1035
1014
|
root,
|
|
1036
1015
|
output,
|
|
1037
|
-
group
|
|
1016
|
+
group: group ?? void 0
|
|
1038
1017
|
});
|
|
1039
1018
|
return names.map((name) => /* @__PURE__ */ (0, _kubb_renderer_jsx_jsx_runtime.jsx)(_kubb_renderer_jsx.File.Import, {
|
|
1040
1019
|
name: [name],
|
|
@@ -1046,11 +1025,11 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1046
1025
|
baseName: meta.file.baseName,
|
|
1047
1026
|
path: meta.file.path,
|
|
1048
1027
|
meta: meta.file.meta,
|
|
1049
|
-
banner: resolver.resolveBanner(
|
|
1028
|
+
banner: resolver.resolveBanner(ctx.meta, {
|
|
1050
1029
|
output,
|
|
1051
1030
|
config
|
|
1052
1031
|
}),
|
|
1053
|
-
footer: resolver.resolveFooter(
|
|
1032
|
+
footer: resolver.resolveFooter(ctx.meta, {
|
|
1054
1033
|
output,
|
|
1055
1034
|
config
|
|
1056
1035
|
}),
|
|
@@ -1073,12 +1052,17 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1073
1052
|
//#endregion
|
|
1074
1053
|
//#region src/resolvers/resolverZod.ts
|
|
1075
1054
|
/**
|
|
1076
|
-
*
|
|
1055
|
+
* Default resolver used by `@kubb/plugin-zod`. Decides the names and file
|
|
1056
|
+
* paths for every generated Zod schema. Schemas use camelCase with a
|
|
1057
|
+
* `Schema` suffix (`listPetsSchema`); their inferred types use PascalCase.
|
|
1077
1058
|
*
|
|
1078
|
-
*
|
|
1059
|
+
* @example Resolve schema and type names
|
|
1060
|
+
* ```ts
|
|
1061
|
+
* import { resolverZod } from '@kubb/plugin-zod'
|
|
1079
1062
|
*
|
|
1080
|
-
*
|
|
1081
|
-
*
|
|
1063
|
+
* resolverZod.default('list pets', 'function') // 'listPetsSchema'
|
|
1064
|
+
* resolverZod.resolveSchemaTypeName('pet') // 'PetSchema'
|
|
1065
|
+
* ```
|
|
1082
1066
|
*/
|
|
1083
1067
|
const resolverZod = (0, _kubb_core.defineResolver)(() => {
|
|
1084
1068
|
return {
|
|
@@ -1131,19 +1115,32 @@ const resolverZod = (0, _kubb_core.defineResolver)(() => {
|
|
|
1131
1115
|
//#endregion
|
|
1132
1116
|
//#region src/plugin.ts
|
|
1133
1117
|
/**
|
|
1134
|
-
* Canonical plugin name for `@kubb/plugin-zod
|
|
1118
|
+
* Canonical plugin name for `@kubb/plugin-zod`. Used for driver lookups and
|
|
1119
|
+
* cross-plugin dependency references.
|
|
1135
1120
|
*/
|
|
1136
1121
|
const pluginZodName = "plugin-zod";
|
|
1137
1122
|
/**
|
|
1138
|
-
* Generates Zod
|
|
1139
|
-
*
|
|
1140
|
-
*
|
|
1123
|
+
* Generates Zod v4 schemas from an OpenAPI spec. Use them to validate API
|
|
1124
|
+
* responses at runtime, build form schemas, or feed back into router libraries
|
|
1125
|
+
* that consume Zod (tRPC, Hono, Elysia). Pair with `@kubb/plugin-client` and
|
|
1126
|
+
* set the client's `parser: 'zod'` to validate every response automatically.
|
|
1141
1127
|
*
|
|
1142
|
-
* @example
|
|
1128
|
+
* @example
|
|
1143
1129
|
* ```ts
|
|
1144
|
-
* import
|
|
1130
|
+
* import { defineConfig } from 'kubb'
|
|
1131
|
+
* import { pluginTs } from '@kubb/plugin-ts'
|
|
1132
|
+
* import { pluginZod } from '@kubb/plugin-zod'
|
|
1133
|
+
*
|
|
1145
1134
|
* export default defineConfig({
|
|
1146
|
-
*
|
|
1135
|
+
* input: { path: './petStore.yaml' },
|
|
1136
|
+
* output: { path: './src/gen' },
|
|
1137
|
+
* plugins: [
|
|
1138
|
+
* pluginTs(),
|
|
1139
|
+
* pluginZod({
|
|
1140
|
+
* output: { path: './zod' },
|
|
1141
|
+
* typed: true,
|
|
1142
|
+
* }),
|
|
1143
|
+
* ],
|
|
1147
1144
|
* })
|
|
1148
1145
|
* ```
|
|
1149
1146
|
*/
|
|
@@ -1158,7 +1155,7 @@ const pluginZod = (0, _kubb_core.definePlugin)((options) => {
|
|
|
1158
1155
|
if (group.type === "path") return `${ctx.group.split("/")[1]}`;
|
|
1159
1156
|
return `${camelCase(ctx.group)}Controller`;
|
|
1160
1157
|
}
|
|
1161
|
-
} :
|
|
1158
|
+
} : null;
|
|
1162
1159
|
return {
|
|
1163
1160
|
name: pluginZodName,
|
|
1164
1161
|
options,
|