@kubb/plugin-zod 5.0.0-beta.80 → 5.0.0-beta.84
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 +212 -157
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +34 -40
- package/dist/index.js +170 -115
- package/dist/index.js.map +1 -1
- package/package.json +3 -7
package/dist/index.cjs
CHANGED
|
@@ -9,10 +9,9 @@ var __name = (target, value) => __defProp(target, "name", {
|
|
|
9
9
|
configurable: true
|
|
10
10
|
});
|
|
11
11
|
//#endregion
|
|
12
|
-
let
|
|
13
|
-
let
|
|
14
|
-
let
|
|
15
|
-
let _kubb_ast_utils = require("@kubb/ast/utils");
|
|
12
|
+
let kubb_kit = require("kubb/kit");
|
|
13
|
+
let kubb_jsx = require("kubb/jsx");
|
|
14
|
+
let kubb_jsx_jsx_runtime = require("kubb/jsx/jsx-runtime");
|
|
16
15
|
//#region ../../internals/utils/src/casing.ts
|
|
17
16
|
/**
|
|
18
17
|
* Shared implementation for camelCase and PascalCase conversion.
|
|
@@ -318,22 +317,22 @@ function Zod({ name, node, printer, inferTypeName, cyclic }) {
|
|
|
318
317
|
const output = printer.print(node);
|
|
319
318
|
if (!output) return;
|
|
320
319
|
const needsAnnotation = cyclic && node.type !== "object";
|
|
321
|
-
return /* @__PURE__ */ (0,
|
|
320
|
+
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, {
|
|
322
321
|
name,
|
|
323
322
|
isExportable: true,
|
|
324
323
|
isIndexable: true,
|
|
325
|
-
children: /* @__PURE__ */ (0,
|
|
324
|
+
children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Const, {
|
|
326
325
|
export: true,
|
|
327
326
|
name,
|
|
328
327
|
type: needsAnnotation ? "z.ZodType" : void 0,
|
|
329
328
|
children: output
|
|
330
329
|
})
|
|
331
|
-
}), inferTypeName && /* @__PURE__ */ (0,
|
|
330
|
+
}), inferTypeName && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Source, {
|
|
332
331
|
name: inferTypeName,
|
|
333
332
|
isExportable: true,
|
|
334
333
|
isIndexable: true,
|
|
335
334
|
isTypeOnly: true,
|
|
336
|
-
children: /* @__PURE__ */ (0,
|
|
335
|
+
children: /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.Type, {
|
|
337
336
|
export: true,
|
|
338
337
|
name: inferTypeName,
|
|
339
338
|
children: `z.infer<typeof ${name}>`
|
|
@@ -396,12 +395,12 @@ function containsCodec(node, seen = /* @__PURE__ */ new Set()) {
|
|
|
396
395
|
if (hasCodec(node)) return true;
|
|
397
396
|
if (node.type === "ref") {
|
|
398
397
|
if (!node.ref) return false;
|
|
399
|
-
const refName =
|
|
398
|
+
const refName = kubb_kit.ast.extractRefName(node.ref);
|
|
400
399
|
if (refName) {
|
|
401
400
|
if (seen.has(refName)) return false;
|
|
402
401
|
seen.add(refName);
|
|
403
402
|
}
|
|
404
|
-
const resolved =
|
|
403
|
+
const resolved = kubb_kit.ast.syncSchemaRef(node);
|
|
405
404
|
if (resolved.type === "ref") return false;
|
|
406
405
|
return containsCodec(resolved, seen);
|
|
407
406
|
}
|
|
@@ -417,14 +416,54 @@ function containsCodec(node, seen = /* @__PURE__ */ new Set()) {
|
|
|
417
416
|
* them to their input (encode) variant.
|
|
418
417
|
*/
|
|
419
418
|
function collectCodecRefNames(node) {
|
|
420
|
-
return
|
|
419
|
+
return kubb_kit.ast.collect(node, { schema: (n) => n.type === "ref" && n.ref && containsCodec(n) ? kubb_kit.ast.extractRefName(n.ref) ?? void 0 : void 0 });
|
|
420
|
+
}
|
|
421
|
+
/**
|
|
422
|
+
* Whether the node is a plain inline object whose shape can be lifted into an `.extend({ … })`
|
|
423
|
+
* argument. A catchall, `patternProperties`, or a nullable/optional wrapper cannot, so those stay
|
|
424
|
+
* on `.and(…)`.
|
|
425
|
+
*/
|
|
426
|
+
function isPlainInlineObject(node) {
|
|
427
|
+
return node.type === "object" && !node.nullable && !node.optional && !node.nullish && node.additionalProperties === void 0 && !node.patternProperties;
|
|
428
|
+
}
|
|
429
|
+
/**
|
|
430
|
+
* Whether a node renders as a bare Zod object — one that accepts `.extend(…)` and can be a
|
|
431
|
+
* `z.discriminatedUnion` option. Covers object nodes, `$ref`s that resolve to (or are still
|
|
432
|
+
* unresolved) objects, single-member unions of an object, and object-composable `allOf`.
|
|
433
|
+
*
|
|
434
|
+
* `cyclicSchemas` bounds the recursion: a ref into a cycle renders as `z.lazy(…)`, not an object.
|
|
435
|
+
*/
|
|
436
|
+
function isObjectSchemaNode(node, cyclicSchemas) {
|
|
437
|
+
if (node.nullable || node.optional || node.nullish) return false;
|
|
438
|
+
if (node.type === "object") return true;
|
|
439
|
+
if (node.type === "ref") {
|
|
440
|
+
const refName = (node.ref ? kubb_kit.ast.extractRefName(node.ref) : void 0) ?? node.name;
|
|
441
|
+
if (refName && cyclicSchemas?.has(refName)) return false;
|
|
442
|
+
const resolved = kubb_kit.ast.syncSchemaRef(node);
|
|
443
|
+
return resolved.type === "ref" || isObjectSchemaNode(resolved, cyclicSchemas);
|
|
444
|
+
}
|
|
445
|
+
if (node.type === "union") {
|
|
446
|
+
const members = node.members ?? [];
|
|
447
|
+
return members.length === 1 && isObjectSchemaNode(members[0], cyclicSchemas);
|
|
448
|
+
}
|
|
449
|
+
return isObjectComposableIntersection(node, cyclicSchemas);
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Whether an `allOf` is a pure object composition — an object base plus inline object members whose
|
|
453
|
+
* shapes merge with `.extend(…)`. These stay a Zod object instead of a `ZodIntersection`, which
|
|
454
|
+
* `z.discriminatedUnion` rejects.
|
|
455
|
+
*/
|
|
456
|
+
function isObjectComposableIntersection(node, cyclicSchemas) {
|
|
457
|
+
if (node.type !== "intersection") return false;
|
|
458
|
+
const [first, ...rest] = node.members ?? [];
|
|
459
|
+
return !!first && isObjectSchemaNode(first, cyclicSchemas) && rest.every(isPlainInlineObject);
|
|
421
460
|
}
|
|
422
461
|
/**
|
|
423
462
|
* Format a default value as a code-level literal.
|
|
424
463
|
* Objects become `{}`, primitives become their string representation, strings are quoted.
|
|
425
464
|
*/
|
|
426
465
|
function formatDefault(value) {
|
|
427
|
-
if (typeof value === "string") return
|
|
466
|
+
if (typeof value === "string") return kubb_kit.ast.stringify(value);
|
|
428
467
|
if (typeof value === "object" && value !== null) return "{}";
|
|
429
468
|
return String(value ?? "");
|
|
430
469
|
}
|
|
@@ -439,13 +478,13 @@ function formatDefault(value) {
|
|
|
439
478
|
*/
|
|
440
479
|
function defaultLiteral(node, value) {
|
|
441
480
|
if (value === null) return null;
|
|
442
|
-
if (node &&
|
|
481
|
+
if (node && kubb_kit.ast.narrowSchema(node, "bigint")) {
|
|
443
482
|
if (typeof value === "bigint") return `BigInt(${value})`;
|
|
444
483
|
if (typeof value === "number" && Number.isInteger(value)) return `BigInt(${value})`;
|
|
445
484
|
return null;
|
|
446
485
|
}
|
|
447
|
-
if (node &&
|
|
448
|
-
const enumNode = node ?
|
|
486
|
+
if (node && kubb_kit.ast.narrowSchema(node, "array")) return Array.isArray(value) ? JSON.stringify(value) : null;
|
|
487
|
+
const enumNode = node ? kubb_kit.ast.narrowSchema(node, "enum") : void 0;
|
|
449
488
|
if (enumNode) {
|
|
450
489
|
const values = enumNode.namedEnumValues?.map((member) => member.value) ?? enumNode.enumValues ?? [];
|
|
451
490
|
if (values.length) {
|
|
@@ -460,7 +499,7 @@ function defaultLiteral(node, value) {
|
|
|
460
499
|
* Strings are quoted; numbers and booleans are emitted raw.
|
|
461
500
|
*/
|
|
462
501
|
function formatLiteral(v) {
|
|
463
|
-
if (typeof v === "string") return
|
|
502
|
+
if (typeof v === "string") return kubb_kit.ast.stringify(v);
|
|
464
503
|
return String(v);
|
|
465
504
|
}
|
|
466
505
|
/**
|
|
@@ -502,7 +541,8 @@ function patternKeySchemaMini({ patterns, regexType }) {
|
|
|
502
541
|
})}))`;
|
|
503
542
|
}
|
|
504
543
|
function patternKeySource({ patterns, regexType }) {
|
|
505
|
-
|
|
544
|
+
const source = patterns.length === 1 ? patterns[0] : patterns.map((pattern) => `(${pattern})`).join("|");
|
|
545
|
+
return kubb_kit.ast.toRegExpString(source, regexFunc(regexType));
|
|
506
546
|
}
|
|
507
547
|
/**
|
|
508
548
|
* Build `.min()` / `.max()` / `.gt()` / `.lt()` constraint chains for numbers
|
|
@@ -525,7 +565,7 @@ function lengthConstraints({ min, max, pattern, regexType }) {
|
|
|
525
565
|
return [
|
|
526
566
|
min !== void 0 ? `.min(${min})` : "",
|
|
527
567
|
max !== void 0 ? `.max(${max})` : "",
|
|
528
|
-
pattern !== void 0 ? `.regex(${
|
|
568
|
+
pattern !== void 0 ? `.regex(${kubb_kit.ast.toRegExpString(pattern, regexFunc(regexType))})` : ""
|
|
529
569
|
].join("");
|
|
530
570
|
}
|
|
531
571
|
/**
|
|
@@ -547,7 +587,7 @@ function lengthChecksMini({ min, max, pattern, regexType }) {
|
|
|
547
587
|
const checks = [];
|
|
548
588
|
if (min !== void 0) checks.push(`z.minLength(${min})`);
|
|
549
589
|
if (max !== void 0) checks.push(`z.maxLength(${max})`);
|
|
550
|
-
if (pattern !== void 0) checks.push(`z.regex(${
|
|
590
|
+
if (pattern !== void 0) checks.push(`z.regex(${kubb_kit.ast.toRegExpString(pattern, regexFunc(regexType))})`);
|
|
551
591
|
return checks.length ? `.check(${checks.join(", ")})` : "";
|
|
552
592
|
}
|
|
553
593
|
/**
|
|
@@ -563,7 +603,7 @@ function applyModifiers({ value, schema, nullable, optional, nullish, defaultVal
|
|
|
563
603
|
})();
|
|
564
604
|
const literal = defaultValue !== void 0 ? defaultLiteral(schema, defaultValue) : null;
|
|
565
605
|
const withDefault = literal !== null ? `${withModifier}.default(${literal})` : withModifier;
|
|
566
|
-
const withDescription = description ? `${withDefault}.describe(${
|
|
606
|
+
const withDescription = description ? `${withDefault}.describe(${kubb_kit.ast.stringify(description)})` : withDefault;
|
|
567
607
|
return examples?.length ? `${withDescription}.meta({ examples: [${examples.map(formatDefault).join(", ")}] })` : withDescription;
|
|
568
608
|
}
|
|
569
609
|
function modifierDepth(schema) {
|
|
@@ -581,7 +621,7 @@ function modifierDepth(schema) {
|
|
|
581
621
|
* objects need no unwrap because the printer adds their modifiers after `.omit()`.
|
|
582
622
|
*/
|
|
583
623
|
function omitUnwrapChain(node) {
|
|
584
|
-
const ref =
|
|
624
|
+
const ref = kubb_kit.ast.narrowSchema(node, "ref");
|
|
585
625
|
if (!ref) return "";
|
|
586
626
|
const target = ref.schema ?? ref;
|
|
587
627
|
const depth = modifierDepth(target) + (target.default !== void 0 ? 1 : 0);
|
|
@@ -606,9 +646,9 @@ function strictOneOfMember$1(member, node, cyclicSchemas) {
|
|
|
606
646
|
if (node.type === "object" && node.additionalProperties === void 0) return `${member}.strict()`;
|
|
607
647
|
if (node.type === "ref") {
|
|
608
648
|
if (member.startsWith("z.lazy(")) return member;
|
|
609
|
-
const refName = (node.ref ?
|
|
649
|
+
const refName = (node.ref ? kubb_kit.ast.extractRefName(node.ref) : void 0) ?? node.name;
|
|
610
650
|
if (refName && cyclicSchemas?.has(refName)) return member;
|
|
611
|
-
const schema =
|
|
651
|
+
const schema = kubb_kit.ast.syncSchemaRef(node);
|
|
612
652
|
if (schema.nullable || schema.optional || node.nullable || node.optional) return member;
|
|
613
653
|
if (schema.type === "object" && (schema.additionalProperties === void 0 || schema.additionalProperties === false)) return `${member}.strict()`;
|
|
614
654
|
}
|
|
@@ -617,16 +657,52 @@ function strictOneOfMember$1(member, node, cyclicSchemas) {
|
|
|
617
657
|
__name(strictOneOfMember$1, "strictOneOfMember");
|
|
618
658
|
function getMemberConstraint({ member, regexType }) {
|
|
619
659
|
if (member.primitive === "string") return lengthConstraints({
|
|
620
|
-
...
|
|
660
|
+
...kubb_kit.ast.narrowSchema(member, "string") ?? {},
|
|
621
661
|
regexType
|
|
622
662
|
}) || void 0;
|
|
623
|
-
if (member.primitive === "number" || member.primitive === "integer") return numberConstraints(
|
|
663
|
+
if (member.primitive === "number" || member.primitive === "integer") return numberConstraints(kubb_kit.ast.narrowSchema(member, "number") ?? kubb_kit.ast.narrowSchema(member, "integer") ?? {}) || void 0;
|
|
624
664
|
if (member.primitive === "array") return lengthConstraints({
|
|
625
|
-
...
|
|
665
|
+
...kubb_kit.ast.narrowSchema(member, "array") ?? {},
|
|
626
666
|
regexType
|
|
627
667
|
}) || void 0;
|
|
628
668
|
}
|
|
629
669
|
/**
|
|
670
|
+
* Builds the `{ key: value, … }` shape for an object node, shared by the `z.object(...)` and
|
|
671
|
+
* `.extend(...)` renderings so they stay in lockstep.
|
|
672
|
+
*/
|
|
673
|
+
function buildZodObjectShape(ctx, node) {
|
|
674
|
+
const objectNode = kubb_kit.ast.narrowSchema(node, "object");
|
|
675
|
+
if (!objectNode) return "{}";
|
|
676
|
+
const isCyclic = (schema) => ctx.options.cyclicSchemas != null && kubb_kit.ast.containsCircularRef(schema, { circularSchemas: ctx.options.cyclicSchemas });
|
|
677
|
+
const entries = kubb_kit.ast.mapSchemaProperties(objectNode, (schema) => {
|
|
678
|
+
const hasSelfRef = isCyclic(schema);
|
|
679
|
+
const savedCyclicSchemas = ctx.options.cyclicSchemas;
|
|
680
|
+
if (hasSelfRef) ctx.options.cyclicSchemas = void 0;
|
|
681
|
+
const baseOutput = ctx.transform(schema) ?? ctx.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }));
|
|
682
|
+
if (hasSelfRef) ctx.options.cyclicSchemas = savedCyclicSchemas;
|
|
683
|
+
return baseOutput;
|
|
684
|
+
}).map(({ name: propName, property, output: baseOutput }) => {
|
|
685
|
+
const { schema } = property;
|
|
686
|
+
const meta = kubb_kit.ast.syncSchemaRef(schema);
|
|
687
|
+
const descriptionToApply = schema.type !== "ref" && meta.type === "ref" ? void 0 : meta.description;
|
|
688
|
+
const value = applyModifiers({
|
|
689
|
+
value: baseOutput,
|
|
690
|
+
schema,
|
|
691
|
+
nullable: meta.nullable,
|
|
692
|
+
optional: schema.optional || property.required === false,
|
|
693
|
+
nullish: schema.nullish,
|
|
694
|
+
defaultValue: meta.default,
|
|
695
|
+
description: descriptionToApply,
|
|
696
|
+
examples: meta.examples
|
|
697
|
+
});
|
|
698
|
+
return isCyclic(schema) ? kubb_kit.ast.lazyGetter({
|
|
699
|
+
name: propName,
|
|
700
|
+
body: value
|
|
701
|
+
}) : `${kubb_kit.ast.objectKey(propName)}: ${value}`;
|
|
702
|
+
});
|
|
703
|
+
return kubb_kit.ast.buildObject(entries);
|
|
704
|
+
}
|
|
705
|
+
/**
|
|
630
706
|
* Zod v4 printer built with `definePrinter`.
|
|
631
707
|
*
|
|
632
708
|
* Converts a `SchemaNode` AST into a Zod v4 code string using the chainable API
|
|
@@ -638,7 +714,7 @@ function getMemberConstraint({ member, regexType }) {
|
|
|
638
714
|
* const code = printer.print(stringNode) // "z.string()"
|
|
639
715
|
* ```
|
|
640
716
|
*/
|
|
641
|
-
const printerZod =
|
|
717
|
+
const printerZod = kubb_kit.ast.createPrinter((options) => {
|
|
642
718
|
const cyclicSchemaNames = options.cyclicSchemas;
|
|
643
719
|
return {
|
|
644
720
|
name: "zod",
|
|
@@ -667,7 +743,10 @@ const printerZod = _kubb_core.ast.createPrinter((options) => {
|
|
|
667
743
|
},
|
|
668
744
|
date(node) {
|
|
669
745
|
const codec = getCodec(node);
|
|
670
|
-
if (codec)
|
|
746
|
+
if (codec) {
|
|
747
|
+
if (this.options.direction === "input") return codec.encode(node);
|
|
748
|
+
return shouldCoerce(this.options.coercion, "dates") ? "z.coerce.date()" : codec.decode(node);
|
|
749
|
+
}
|
|
671
750
|
return "z.iso.date()";
|
|
672
751
|
},
|
|
673
752
|
datetime(node) {
|
|
@@ -713,56 +792,26 @@ const printerZod = _kubb_core.ast.createPrinter((options) => {
|
|
|
713
792
|
},
|
|
714
793
|
ref(node) {
|
|
715
794
|
if (!node.name) return null;
|
|
716
|
-
const refName = node.ref ? this.options.nameMapping?.get(node.ref) ??
|
|
795
|
+
const refName = node.ref ? this.options.nameMapping?.get(node.ref) ?? kubb_kit.ast.extractRefName(node.ref) ?? node.name : node.name;
|
|
717
796
|
const useInputVariant = node.ref != null && this.options.direction === "input" && containsCodec(node);
|
|
718
797
|
const resolvedName = node.ref ? useInputVariant ? this.options.resolver?.resolveInputSchemaName(refName) ?? refName : this.options.resolver?.default(refName, "function") ?? refName : node.name;
|
|
719
798
|
if (node.ref && this.options.cyclicSchemas?.has(refName)) return `z.lazy(() => ${resolvedName})`;
|
|
720
799
|
return resolvedName;
|
|
721
800
|
},
|
|
722
801
|
object(node) {
|
|
723
|
-
const
|
|
724
|
-
const
|
|
725
|
-
const hasSelfRef = isCyclic(schema);
|
|
726
|
-
const savedCyclicSchemas = this.options.cyclicSchemas;
|
|
727
|
-
if (hasSelfRef) this.options.cyclicSchemas = void 0;
|
|
728
|
-
const baseOutput = this.transform(schema) ?? this.transform(_kubb_core.ast.factory.createSchema({ type: "unknown" }));
|
|
729
|
-
if (hasSelfRef) this.options.cyclicSchemas = savedCyclicSchemas;
|
|
730
|
-
return baseOutput;
|
|
731
|
-
}).map(({ name: propName, property, output: baseOutput }) => {
|
|
732
|
-
const { schema } = property;
|
|
733
|
-
const meta = (0, _kubb_ast_utils.syncSchemaRef)(schema);
|
|
734
|
-
const wrappedOutput = this.options.wrapOutput ? this.options.wrapOutput({
|
|
735
|
-
output: baseOutput,
|
|
736
|
-
schema
|
|
737
|
-
}) || baseOutput : baseOutput;
|
|
738
|
-
const descriptionToApply = schema.type !== "ref" && meta.type === "ref" ? void 0 : meta.description;
|
|
739
|
-
const value = applyModifiers({
|
|
740
|
-
value: wrappedOutput,
|
|
741
|
-
schema,
|
|
742
|
-
nullable: meta.nullable,
|
|
743
|
-
optional: schema.optional || property.required === false,
|
|
744
|
-
nullish: schema.nullish,
|
|
745
|
-
defaultValue: meta.default,
|
|
746
|
-
description: descriptionToApply,
|
|
747
|
-
examples: meta.examples
|
|
748
|
-
});
|
|
749
|
-
return isCyclic(schema) ? (0, _kubb_ast_utils.lazyGetter)({
|
|
750
|
-
name: propName,
|
|
751
|
-
body: value
|
|
752
|
-
}) : `${(0, _kubb_ast_utils.objectKey)(propName)}: ${value}`;
|
|
753
|
-
});
|
|
754
|
-
const objectBase = `z.object(${(0, _kubb_ast_utils.buildObject)(entries)})`;
|
|
802
|
+
const entries = node.properties ?? [];
|
|
803
|
+
const objectBase = `z.object(${buildZodObjectShape(this, node)})`;
|
|
755
804
|
return (() => {
|
|
756
805
|
const patterns = node.patternProperties ? Object.entries(node.patternProperties) : [];
|
|
757
806
|
if (node.additionalProperties && node.additionalProperties !== true) {
|
|
758
807
|
const catchallType = this.transform(node.additionalProperties);
|
|
759
808
|
return catchallType ? `${objectBase}.catchall(${catchallType})` : objectBase;
|
|
760
809
|
}
|
|
761
|
-
if (node.additionalProperties === true) return `${objectBase}.catchall(${this.transform(
|
|
810
|
+
if (node.additionalProperties === true) return `${objectBase}.catchall(${this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }))})`;
|
|
762
811
|
if (node.additionalProperties === false && patterns.length === 0) return `${objectBase}.strict()`;
|
|
763
812
|
if (patterns.length > 0) {
|
|
764
813
|
const values = patterns.map(([, valueSchema]) => {
|
|
765
|
-
const valueType = this.transform(valueSchema) ?? this.transform(
|
|
814
|
+
const valueType = this.transform(valueSchema) ?? this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }));
|
|
766
815
|
return valueSchema.nullable ? `${valueType}.nullable()` : valueType;
|
|
767
816
|
});
|
|
768
817
|
const distinct = [...new Set(values)];
|
|
@@ -777,23 +826,24 @@ const printerZod = _kubb_core.ast.createPrinter((options) => {
|
|
|
777
826
|
})();
|
|
778
827
|
},
|
|
779
828
|
array(node) {
|
|
780
|
-
const base = `z.array(${
|
|
829
|
+
const base = `z.array(${kubb_kit.ast.mapSchemaItems(node, (item) => this.transform(item)).map(({ output }) => output).filter(Boolean).join(", ") || this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }))})${lengthConstraints({
|
|
781
830
|
...node,
|
|
782
831
|
regexType: this.options.regexType
|
|
783
832
|
})}`;
|
|
784
833
|
return node.unique ? `${base}.refine(items => new Set(items).size === items.length, { message: "Array entries must be unique" })` : base;
|
|
785
834
|
},
|
|
786
835
|
tuple(node) {
|
|
787
|
-
|
|
836
|
+
const items = kubb_kit.ast.mapSchemaItems(node, (item) => this.transform(item)).map(({ output }) => output).filter(Boolean);
|
|
837
|
+
return `z.tuple(${kubb_kit.ast.buildList(items)})`;
|
|
788
838
|
},
|
|
789
839
|
union(node) {
|
|
790
840
|
const nodeMembers = node.members ?? [];
|
|
791
|
-
const members =
|
|
841
|
+
const members = kubb_kit.ast.mapSchemaMembers(node, (memberNode) => this.transform(memberNode)).map(({ schema, output }) => output && node.strategy === "one" ? strictOneOfMember$1(output, schema, cyclicSchemaNames) : output).filter(Boolean);
|
|
792
842
|
if (members.length === 0) return "";
|
|
793
843
|
if (members.length === 1) return members[0];
|
|
794
|
-
const allDiscriminable = nodeMembers.every((m) => (m
|
|
795
|
-
if (node.discriminatorPropertyName && allDiscriminable) return `z.discriminatedUnion(${
|
|
796
|
-
return `z.union(${
|
|
844
|
+
const allDiscriminable = nodeMembers.every((m) => isObjectSchemaNode(m, cyclicSchemaNames));
|
|
845
|
+
if (node.discriminatorPropertyName && allDiscriminable) return `z.discriminatedUnion(${kubb_kit.ast.stringify(node.discriminatorPropertyName)}, ${kubb_kit.ast.buildList(members)})`;
|
|
846
|
+
return `z.union(${kubb_kit.ast.buildList(members)})`;
|
|
797
847
|
},
|
|
798
848
|
intersection(node) {
|
|
799
849
|
const members = node.members ?? [];
|
|
@@ -802,6 +852,7 @@ const printerZod = _kubb_core.ast.createPrinter((options) => {
|
|
|
802
852
|
if (!first) return "";
|
|
803
853
|
const firstBase = this.transform(first);
|
|
804
854
|
if (!firstBase) return "";
|
|
855
|
+
if (rest.length > 0 && isObjectComposableIntersection(node, cyclicSchemaNames)) return rest.reduce((acc, member) => `${acc}.extend(${buildZodObjectShape(this, member)})`, firstBase);
|
|
805
856
|
return rest.reduce((acc, member) => {
|
|
806
857
|
const constraint = getMemberConstraint({
|
|
807
858
|
member,
|
|
@@ -811,14 +862,14 @@ const printerZod = _kubb_core.ast.createPrinter((options) => {
|
|
|
811
862
|
const transformed = this.transform(member);
|
|
812
863
|
return transformed ? `${acc}.and(${transformed})` : acc;
|
|
813
864
|
}, firstBase);
|
|
814
|
-
}
|
|
815
|
-
...options.nodes
|
|
865
|
+
}
|
|
816
866
|
},
|
|
867
|
+
overrides: options.nodes,
|
|
817
868
|
print(node) {
|
|
818
869
|
const { keysToOmit } = this.options;
|
|
819
870
|
const transformed = this.transform(node);
|
|
820
871
|
if (!transformed) return null;
|
|
821
|
-
const meta =
|
|
872
|
+
const meta = kubb_kit.ast.syncSchemaRef(node);
|
|
822
873
|
return applyModifiers({
|
|
823
874
|
value: (() => {
|
|
824
875
|
if (!keysToOmit?.length || meta.primitive !== "object" || meta.type === "union" && meta.discriminatorPropertyName) return transformed;
|
|
@@ -847,17 +898,50 @@ function strictOneOfMember(member, node) {
|
|
|
847
898
|
}
|
|
848
899
|
function getMemberConstraintMini({ member, regexType }) {
|
|
849
900
|
if (member.primitive === "string") return lengthChecksMini({
|
|
850
|
-
...
|
|
901
|
+
...kubb_kit.ast.narrowSchema(member, "string") ?? {},
|
|
851
902
|
regexType
|
|
852
903
|
}) || void 0;
|
|
853
|
-
if (member.primitive === "number" || member.primitive === "integer") return numberChecksMini(
|
|
904
|
+
if (member.primitive === "number" || member.primitive === "integer") return numberChecksMini(kubb_kit.ast.narrowSchema(member, "number") ?? kubb_kit.ast.narrowSchema(member, "integer") ?? {}) || void 0;
|
|
854
905
|
if (member.primitive === "array") return lengthChecksMini({
|
|
855
|
-
...
|
|
906
|
+
...kubb_kit.ast.narrowSchema(member, "array") ?? {},
|
|
856
907
|
regexType
|
|
857
908
|
}) || void 0;
|
|
858
909
|
}
|
|
859
910
|
/**
|
|
860
|
-
*
|
|
911
|
+
* Builds the `{ key: value, … }` shape for an object node with the functional `zod/mini` modifiers,
|
|
912
|
+
* shared by the `z.object(...)` and `z.extend(...)` renderings so they stay in lockstep.
|
|
913
|
+
*/
|
|
914
|
+
function buildZodMiniObjectShape(ctx, node) {
|
|
915
|
+
const objectNode = kubb_kit.ast.narrowSchema(node, "object");
|
|
916
|
+
if (!objectNode) return "{}";
|
|
917
|
+
const isCyclic = (schema) => ctx.options.cyclicSchemas != null && kubb_kit.ast.containsCircularRef(schema, { circularSchemas: ctx.options.cyclicSchemas });
|
|
918
|
+
const entries = kubb_kit.ast.mapSchemaProperties(objectNode, (schema) => {
|
|
919
|
+
const hasSelfRef = isCyclic(schema);
|
|
920
|
+
const savedCyclicSchemas = ctx.options.cyclicSchemas;
|
|
921
|
+
if (hasSelfRef) ctx.options.cyclicSchemas = void 0;
|
|
922
|
+
const baseOutput = ctx.transform(schema) ?? ctx.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }));
|
|
923
|
+
if (hasSelfRef) ctx.options.cyclicSchemas = savedCyclicSchemas;
|
|
924
|
+
return baseOutput;
|
|
925
|
+
}).map(({ name: propName, property, output: baseOutput }) => {
|
|
926
|
+
const { schema } = property;
|
|
927
|
+
const meta = kubb_kit.ast.syncSchemaRef(schema);
|
|
928
|
+
const value = applyMiniModifiers({
|
|
929
|
+
value: baseOutput,
|
|
930
|
+
schema,
|
|
931
|
+
nullable: meta.nullable,
|
|
932
|
+
optional: schema.optional || property.required === false,
|
|
933
|
+
nullish: schema.nullish,
|
|
934
|
+
defaultValue: meta.default
|
|
935
|
+
});
|
|
936
|
+
return isCyclic(schema) ? kubb_kit.ast.lazyGetter({
|
|
937
|
+
name: propName,
|
|
938
|
+
body: value
|
|
939
|
+
}) : `${kubb_kit.ast.objectKey(propName)}: ${value}`;
|
|
940
|
+
});
|
|
941
|
+
return kubb_kit.ast.buildObject(entries);
|
|
942
|
+
}
|
|
943
|
+
/**
|
|
944
|
+
* Zod v4 Mini printer built with `definePrinter`.
|
|
861
945
|
*
|
|
862
946
|
* Converts a `SchemaNode` AST into a Zod v4 code string using the functional API
|
|
863
947
|
* (`z.optional(z.string())`) for improved tree-shaking. See {@link printerZod} for the chainable API.
|
|
@@ -868,7 +952,8 @@ function getMemberConstraintMini({ member, regexType }) {
|
|
|
868
952
|
* const code = printer.print(optionalStringNode) // "z.optional(z.string())"
|
|
869
953
|
* ```
|
|
870
954
|
*/
|
|
871
|
-
const printerZodMini =
|
|
955
|
+
const printerZodMini = kubb_kit.ast.createPrinter((options) => {
|
|
956
|
+
const cyclicSchemaNames = options.cyclicSchemas;
|
|
872
957
|
return {
|
|
873
958
|
name: "zod-mini",
|
|
874
959
|
options,
|
|
@@ -937,50 +1022,24 @@ const printerZodMini = _kubb_core.ast.createPrinter((options) => {
|
|
|
937
1022
|
},
|
|
938
1023
|
ref(node) {
|
|
939
1024
|
if (!node.name) return null;
|
|
940
|
-
const refName = node.ref ? this.options.nameMapping?.get(node.ref) ??
|
|
1025
|
+
const refName = node.ref ? this.options.nameMapping?.get(node.ref) ?? kubb_kit.ast.extractRefName(node.ref) ?? node.name : node.name;
|
|
941
1026
|
const resolvedName = node.ref ? this.options.resolver?.default(refName, "function") ?? refName : node.name;
|
|
942
1027
|
if (node.ref && this.options.cyclicSchemas?.has(refName)) return `z.lazy(() => ${resolvedName})`;
|
|
943
1028
|
return resolvedName;
|
|
944
1029
|
},
|
|
945
1030
|
object(node) {
|
|
946
|
-
const
|
|
947
|
-
const
|
|
948
|
-
const hasSelfRef = isCyclic(schema);
|
|
949
|
-
const savedCyclicSchemas = this.options.cyclicSchemas;
|
|
950
|
-
if (hasSelfRef) this.options.cyclicSchemas = void 0;
|
|
951
|
-
const baseOutput = this.transform(schema) ?? this.transform(_kubb_core.ast.factory.createSchema({ type: "unknown" }));
|
|
952
|
-
if (hasSelfRef) this.options.cyclicSchemas = savedCyclicSchemas;
|
|
953
|
-
return baseOutput;
|
|
954
|
-
}).map(({ name: propName, property, output: baseOutput }) => {
|
|
955
|
-
const { schema } = property;
|
|
956
|
-
const meta = (0, _kubb_ast_utils.syncSchemaRef)(schema);
|
|
957
|
-
const value = applyMiniModifiers({
|
|
958
|
-
value: this.options.wrapOutput ? this.options.wrapOutput({
|
|
959
|
-
output: baseOutput,
|
|
960
|
-
schema
|
|
961
|
-
}) || baseOutput : baseOutput,
|
|
962
|
-
schema,
|
|
963
|
-
nullable: meta.nullable,
|
|
964
|
-
optional: schema.optional || property.required === false,
|
|
965
|
-
nullish: schema.nullish,
|
|
966
|
-
defaultValue: meta.default
|
|
967
|
-
});
|
|
968
|
-
return isCyclic(schema) ? (0, _kubb_ast_utils.lazyGetter)({
|
|
969
|
-
name: propName,
|
|
970
|
-
body: value
|
|
971
|
-
}) : `${(0, _kubb_ast_utils.objectKey)(propName)}: ${value}`;
|
|
972
|
-
});
|
|
973
|
-
const objectBase = `z.object(${(0, _kubb_ast_utils.buildObject)(entries)})`;
|
|
1031
|
+
const entries = node.properties ?? [];
|
|
1032
|
+
const objectBase = `z.object(${buildZodMiniObjectShape(this, node)})`;
|
|
974
1033
|
const patterns = node.patternProperties ? Object.entries(node.patternProperties) : [];
|
|
975
1034
|
if (node.additionalProperties && node.additionalProperties !== true) {
|
|
976
1035
|
const catchallType = this.transform(node.additionalProperties);
|
|
977
1036
|
return catchallType ? `z.catchall(${objectBase}, ${catchallType})` : objectBase;
|
|
978
1037
|
}
|
|
979
|
-
if (node.additionalProperties === true) return `z.catchall(${objectBase}, ${this.transform(
|
|
1038
|
+
if (node.additionalProperties === true) return `z.catchall(${objectBase}, ${this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }))})`;
|
|
980
1039
|
if (node.additionalProperties === false && patterns.length === 0) return objectBase.replace(/^z\.object\(/, "z.strictObject(");
|
|
981
1040
|
if (patterns.length > 0) {
|
|
982
1041
|
const values = patterns.map(([, valueSchema]) => {
|
|
983
|
-
const valueType = this.transform(valueSchema) ?? this.transform(
|
|
1042
|
+
const valueType = this.transform(valueSchema) ?? this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }));
|
|
984
1043
|
return valueSchema.nullable ? `z.nullable(${valueType})` : valueType;
|
|
985
1044
|
});
|
|
986
1045
|
const distinct = [...new Set(values)];
|
|
@@ -994,23 +1053,24 @@ const printerZodMini = _kubb_core.ast.createPrinter((options) => {
|
|
|
994
1053
|
return objectBase;
|
|
995
1054
|
},
|
|
996
1055
|
array(node) {
|
|
997
|
-
const base = `z.array(${
|
|
1056
|
+
const base = `z.array(${kubb_kit.ast.mapSchemaItems(node, (item) => this.transform(item)).map(({ output }) => output).filter(Boolean).join(", ") || this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }))})${lengthChecksMini({
|
|
998
1057
|
...node,
|
|
999
1058
|
regexType: this.options.regexType
|
|
1000
1059
|
})}`;
|
|
1001
1060
|
return node.unique ? `${base}.refine(items => new Set(items).size === items.length, { message: "Array entries must be unique" })` : base;
|
|
1002
1061
|
},
|
|
1003
1062
|
tuple(node) {
|
|
1004
|
-
|
|
1063
|
+
const items = kubb_kit.ast.mapSchemaItems(node, (item) => this.transform(item)).map(({ output }) => output).filter(Boolean);
|
|
1064
|
+
return `z.tuple(${kubb_kit.ast.buildList(items)})`;
|
|
1005
1065
|
},
|
|
1006
1066
|
union(node) {
|
|
1007
1067
|
const nodeMembers = node.members ?? [];
|
|
1008
|
-
const members =
|
|
1068
|
+
const members = kubb_kit.ast.mapSchemaMembers(node, (memberNode) => this.transform(memberNode)).map(({ schema, output }) => output && node.strategy === "one" ? strictOneOfMember(output, schema) : output).filter(Boolean);
|
|
1009
1069
|
if (members.length === 0) return "";
|
|
1010
1070
|
if (members.length === 1) return members[0];
|
|
1011
|
-
const allDiscriminable = nodeMembers.every((m) => (m
|
|
1012
|
-
if (node.discriminatorPropertyName && allDiscriminable) return `z.discriminatedUnion(${
|
|
1013
|
-
return `z.union(${
|
|
1071
|
+
const allDiscriminable = nodeMembers.every((m) => isObjectSchemaNode(m, cyclicSchemaNames));
|
|
1072
|
+
if (node.discriminatorPropertyName && allDiscriminable) return `z.discriminatedUnion(${kubb_kit.ast.stringify(node.discriminatorPropertyName)}, ${kubb_kit.ast.buildList(members)})`;
|
|
1073
|
+
return `z.union(${kubb_kit.ast.buildList(members)})`;
|
|
1014
1074
|
},
|
|
1015
1075
|
intersection(node) {
|
|
1016
1076
|
const members = node.members ?? [];
|
|
@@ -1019,6 +1079,7 @@ const printerZodMini = _kubb_core.ast.createPrinter((options) => {
|
|
|
1019
1079
|
if (!first) return "";
|
|
1020
1080
|
const firstBase = this.transform(first);
|
|
1021
1081
|
if (!firstBase) return "";
|
|
1082
|
+
if (rest.length > 0 && isObjectComposableIntersection(node, cyclicSchemaNames)) return rest.reduce((acc, member) => `z.extend(${acc}, ${buildZodMiniObjectShape(this, member)})`, firstBase);
|
|
1022
1083
|
return rest.reduce((acc, member) => {
|
|
1023
1084
|
const constraint = getMemberConstraintMini({
|
|
1024
1085
|
member,
|
|
@@ -1028,14 +1089,14 @@ const printerZodMini = _kubb_core.ast.createPrinter((options) => {
|
|
|
1028
1089
|
const transformed = this.transform(member);
|
|
1029
1090
|
return transformed ? `z.intersection(${acc}, ${transformed})` : acc;
|
|
1030
1091
|
}, firstBase);
|
|
1031
|
-
}
|
|
1032
|
-
...options.nodes
|
|
1092
|
+
}
|
|
1033
1093
|
},
|
|
1094
|
+
overrides: options.nodes,
|
|
1034
1095
|
print(node) {
|
|
1035
1096
|
const { keysToOmit } = this.options;
|
|
1036
1097
|
const transformed = this.transform(node);
|
|
1037
1098
|
if (!transformed) return null;
|
|
1038
|
-
const meta =
|
|
1099
|
+
const meta = kubb_kit.ast.syncSchemaRef(node);
|
|
1039
1100
|
return applyMiniModifiers({
|
|
1040
1101
|
value: (() => {
|
|
1041
1102
|
if (!keysToOmit?.length || meta.primitive !== "object" || meta.type === "union" && meta.discriminatorPropertyName) return transformed;
|
|
@@ -1060,12 +1121,12 @@ const zodPrinterCache = /* @__PURE__ */ new WeakMap();
|
|
|
1060
1121
|
const zodMiniPrinterCache = /* @__PURE__ */ new WeakMap();
|
|
1061
1122
|
/**
|
|
1062
1123
|
* Returns the cached `output`/`input` direction printers for a resolver, building them on
|
|
1063
|
-
* first use. The `input` printer encodes `Date → string` for request bodies
|
|
1124
|
+
* first use. The `input` printer encodes `Date → string` for request bodies, and `output` decodes
|
|
1064
1125
|
* `string → Date` for responses. Schemas without `dateType: 'date'` fields print identically.
|
|
1065
1126
|
*/
|
|
1066
1127
|
function getStdPrinters(resolver, params) {
|
|
1067
1128
|
const cached = zodPrinterCache.get(resolver);
|
|
1068
|
-
if (cached && cached.coercion === params.coercion && cached.guidType === params.guidType && cached.regexType === params.regexType && cached.dateType === params.dateType) return {
|
|
1129
|
+
if (cached && cached.coercion === params.coercion && cached.guidType === params.guidType && cached.regexType === params.regexType && cached.dateType === params.dateType && cached.nodes === params.nodes) return {
|
|
1069
1130
|
output: cached.output,
|
|
1070
1131
|
input: cached.input
|
|
1071
1132
|
};
|
|
@@ -1087,7 +1148,8 @@ function getStdPrinters(resolver, params) {
|
|
|
1087
1148
|
coercion: params.coercion,
|
|
1088
1149
|
guidType: params.guidType,
|
|
1089
1150
|
regexType: params.regexType,
|
|
1090
|
-
dateType: params.dateType
|
|
1151
|
+
dateType: params.dateType,
|
|
1152
|
+
nodes: params.nodes
|
|
1091
1153
|
});
|
|
1092
1154
|
return {
|
|
1093
1155
|
output,
|
|
@@ -1096,7 +1158,7 @@ function getStdPrinters(resolver, params) {
|
|
|
1096
1158
|
}
|
|
1097
1159
|
function getMiniPrinter(resolver, params) {
|
|
1098
1160
|
const cached = zodMiniPrinterCache.get(resolver);
|
|
1099
|
-
if (cached && cached.guidType === params.guidType && cached.regexType === params.regexType) return cached.printer;
|
|
1161
|
+
if (cached && cached.guidType === params.guidType && cached.regexType === params.regexType && cached.nodes === params.nodes) return cached.printer;
|
|
1100
1162
|
const p = printerZodMini({
|
|
1101
1163
|
...params,
|
|
1102
1164
|
resolver
|
|
@@ -1104,7 +1166,8 @@ function getMiniPrinter(resolver, params) {
|
|
|
1104
1166
|
zodMiniPrinterCache.set(resolver, {
|
|
1105
1167
|
printer: p,
|
|
1106
1168
|
guidType: params.guidType,
|
|
1107
|
-
regexType: params.regexType
|
|
1169
|
+
regexType: params.regexType,
|
|
1170
|
+
nodes: params.nodes
|
|
1108
1171
|
});
|
|
1109
1172
|
return p;
|
|
1110
1173
|
}
|
|
@@ -1114,12 +1177,12 @@ function getMiniPrinter(resolver, params) {
|
|
|
1114
1177
|
* When `mini: true`, schemas use the Zod Mini functional API instead of
|
|
1115
1178
|
* chainable methods.
|
|
1116
1179
|
*/
|
|
1117
|
-
const zodGenerator = (0,
|
|
1180
|
+
const zodGenerator = (0, kubb_kit.defineGenerator)({
|
|
1118
1181
|
name: "zod",
|
|
1119
|
-
renderer:
|
|
1182
|
+
renderer: kubb_jsx.jsxRenderer,
|
|
1120
1183
|
schema(node, ctx) {
|
|
1121
1184
|
const { adapter, config, resolver, root } = ctx;
|
|
1122
|
-
const { output, coercion, guidType, regexType, mini,
|
|
1185
|
+
const { output, coercion, guidType, regexType, mini, inferred, importPath, group, printer } = ctx.options;
|
|
1123
1186
|
const dateType = adapter.options.dateType;
|
|
1124
1187
|
if (!node.name) return;
|
|
1125
1188
|
const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
|
|
@@ -1173,7 +1236,6 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1173
1236
|
guidType,
|
|
1174
1237
|
regexType,
|
|
1175
1238
|
dateType,
|
|
1176
|
-
wrapOutput,
|
|
1177
1239
|
cyclicSchemas,
|
|
1178
1240
|
nameMapping,
|
|
1179
1241
|
nodes: printer?.nodes
|
|
@@ -1181,12 +1243,11 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1181
1243
|
const schemaPrinter = mini ? getMiniPrinter(resolver, {
|
|
1182
1244
|
guidType,
|
|
1183
1245
|
regexType,
|
|
1184
|
-
wrapOutput,
|
|
1185
1246
|
cyclicSchemas,
|
|
1186
1247
|
nameMapping,
|
|
1187
1248
|
nodes: printer?.nodes
|
|
1188
1249
|
}) : stdPrinters.output;
|
|
1189
|
-
return /* @__PURE__ */ (0,
|
|
1250
|
+
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx.File, {
|
|
1190
1251
|
baseName: meta.file.baseName,
|
|
1191
1252
|
path: meta.file.path,
|
|
1192
1253
|
meta: meta.file.meta,
|
|
@@ -1207,12 +1268,12 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1207
1268
|
}
|
|
1208
1269
|
}),
|
|
1209
1270
|
children: [
|
|
1210
|
-
/* @__PURE__ */ (0,
|
|
1271
|
+
/* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Import, {
|
|
1211
1272
|
name: isZodImport ? "z" : ["z"],
|
|
1212
1273
|
path: importPath,
|
|
1213
1274
|
isNameSpace: isZodImport
|
|
1214
1275
|
}),
|
|
1215
|
-
imports.map((imp) => /* @__PURE__ */ (0,
|
|
1276
|
+
imports.map((imp) => /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Import, {
|
|
1216
1277
|
root: meta.file.path,
|
|
1217
1278
|
path: imp.path,
|
|
1218
1279
|
name: imp.name
|
|
@@ -1221,14 +1282,14 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1221
1282
|
imp.path,
|
|
1222
1283
|
imp.name
|
|
1223
1284
|
].join("-"))),
|
|
1224
|
-
/* @__PURE__ */ (0,
|
|
1285
|
+
/* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(Zod, {
|
|
1225
1286
|
name: meta.name,
|
|
1226
1287
|
node,
|
|
1227
1288
|
printer: schemaPrinter,
|
|
1228
1289
|
inferTypeName,
|
|
1229
1290
|
cyclic: cyclicSchemas.has(node.name)
|
|
1230
1291
|
}),
|
|
1231
|
-
hasCodec && stdPrinters && /* @__PURE__ */ (0,
|
|
1292
|
+
hasCodec && stdPrinters && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(Zod, {
|
|
1232
1293
|
name: resolver.resolveInputSchemaName(node.name),
|
|
1233
1294
|
node,
|
|
1234
1295
|
printer: stdPrinters.input,
|
|
@@ -1239,9 +1300,9 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1239
1300
|
});
|
|
1240
1301
|
},
|
|
1241
1302
|
operation(node, ctx) {
|
|
1242
|
-
if (!
|
|
1303
|
+
if (!kubb_kit.ast.isHttpOperationNode(node)) return null;
|
|
1243
1304
|
const { adapter, config, resolver, root } = ctx;
|
|
1244
|
-
const { output, coercion, guidType, regexType, mini,
|
|
1305
|
+
const { output, coercion, guidType, regexType, mini, inferred, importPath, group, printer } = ctx.options;
|
|
1245
1306
|
const dateType = adapter.options.dateType;
|
|
1246
1307
|
const isZodImport = ZOD_NAMESPACE_IMPORTS.has(importPath);
|
|
1247
1308
|
const params = caseParams(node.parameters, "camelcase");
|
|
@@ -1275,7 +1336,6 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1275
1336
|
const schemaPrinter = mini ? keysToOmit?.length ? printerZodMini({
|
|
1276
1337
|
guidType,
|
|
1277
1338
|
regexType,
|
|
1278
|
-
wrapOutput,
|
|
1279
1339
|
resolver,
|
|
1280
1340
|
keysToOmit,
|
|
1281
1341
|
cyclicSchemas,
|
|
@@ -1284,7 +1344,6 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1284
1344
|
}) : getMiniPrinter(resolver, {
|
|
1285
1345
|
guidType,
|
|
1286
1346
|
regexType,
|
|
1287
|
-
wrapOutput,
|
|
1288
1347
|
cyclicSchemas,
|
|
1289
1348
|
nameMapping,
|
|
1290
1349
|
nodes: printer?.nodes
|
|
@@ -1293,7 +1352,6 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1293
1352
|
guidType,
|
|
1294
1353
|
regexType,
|
|
1295
1354
|
dateType,
|
|
1296
|
-
wrapOutput,
|
|
1297
1355
|
resolver,
|
|
1298
1356
|
keysToOmit,
|
|
1299
1357
|
cyclicSchemas,
|
|
@@ -1305,12 +1363,11 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1305
1363
|
guidType,
|
|
1306
1364
|
regexType,
|
|
1307
1365
|
dateType,
|
|
1308
|
-
wrapOutput,
|
|
1309
1366
|
cyclicSchemas,
|
|
1310
1367
|
nameMapping,
|
|
1311
1368
|
nodes: printer?.nodes
|
|
1312
1369
|
})[direction];
|
|
1313
|
-
return /* @__PURE__ */ (0,
|
|
1370
|
+
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx_jsx_runtime.Fragment, { children: [imports.map((imp) => /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Import, {
|
|
1314
1371
|
root: meta.file.path,
|
|
1315
1372
|
path: imp.path,
|
|
1316
1373
|
name: imp.name
|
|
@@ -1318,7 +1375,7 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1318
1375
|
name,
|
|
1319
1376
|
imp.path,
|
|
1320
1377
|
imp.name
|
|
1321
|
-
].join("-"))), /* @__PURE__ */ (0,
|
|
1378
|
+
].join("-"))), /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(Zod, {
|
|
1322
1379
|
name,
|
|
1323
1380
|
node: schema,
|
|
1324
1381
|
printer: schemaPrinter,
|
|
@@ -1328,14 +1385,14 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1328
1385
|
}
|
|
1329
1386
|
function buildContentTypeVariants(entries, baseName, decorate, direction) {
|
|
1330
1387
|
const variants = resolveContentTypeVariants(entries, baseName);
|
|
1331
|
-
const unionSchema =
|
|
1388
|
+
const unionSchema = kubb_kit.ast.factory.createSchema({
|
|
1332
1389
|
type: "union",
|
|
1333
|
-
members: variants.map((variant) =>
|
|
1390
|
+
members: variants.map((variant) => kubb_kit.ast.factory.createSchema({
|
|
1334
1391
|
type: "ref",
|
|
1335
1392
|
name: variant.name
|
|
1336
1393
|
}))
|
|
1337
1394
|
});
|
|
1338
|
-
return /* @__PURE__ */ (0,
|
|
1395
|
+
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx_jsx_runtime.Fragment, { children: [variants.map((variant) => renderSchemaEntry({
|
|
1339
1396
|
schema: decorate ? decorate(variant.schema) : variant.schema,
|
|
1340
1397
|
name: variant.name,
|
|
1341
1398
|
keysToOmit: variant.keysToOmit,
|
|
@@ -1369,16 +1426,16 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1369
1426
|
name: resolver.resolveSchemaName(schemaName),
|
|
1370
1427
|
path: ""
|
|
1371
1428
|
})).flatMap((imp) => Array.isArray(imp.name) ? imp.name : [imp.name]) : []))).has(responseUnionName)) return null;
|
|
1372
|
-
const members = successResponsesWithSchema.map((res) =>
|
|
1429
|
+
const members = successResponsesWithSchema.map((res) => kubb_kit.ast.factory.createSchema({
|
|
1373
1430
|
type: "ref",
|
|
1374
1431
|
name: resolver.resolveResponseStatusName(node, res.statusCode)
|
|
1375
1432
|
}));
|
|
1376
1433
|
if (members.length === 0) return renderSchemaEntry({
|
|
1377
|
-
schema:
|
|
1434
|
+
schema: kubb_kit.ast.factory.createSchema({ type: "unknown" }),
|
|
1378
1435
|
name: responseUnionName
|
|
1379
1436
|
});
|
|
1380
1437
|
return renderSchemaEntry({
|
|
1381
|
-
schema: members.length === 1 ? members[0] :
|
|
1438
|
+
schema: members.length === 1 ? members[0] : kubb_kit.ast.factory.createSchema({
|
|
1382
1439
|
type: "union",
|
|
1383
1440
|
members
|
|
1384
1441
|
}),
|
|
@@ -1392,12 +1449,12 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1392
1449
|
name: resolver.resolveSchemaName(schemaName),
|
|
1393
1450
|
path: ""
|
|
1394
1451
|
})).flatMap((imp) => Array.isArray(imp.name) ? imp.name : [imp.name]) : []))).has(errorUnionName)) return null;
|
|
1395
|
-
const members = errorResponsesWithSchema.map((res) =>
|
|
1452
|
+
const members = errorResponsesWithSchema.map((res) => kubb_kit.ast.factory.createSchema({
|
|
1396
1453
|
type: "ref",
|
|
1397
1454
|
name: resolver.resolveResponseStatusName(node, res.statusCode)
|
|
1398
1455
|
}));
|
|
1399
1456
|
return renderSchemaEntry({
|
|
1400
|
-
schema: members.length === 1 ? members[0] :
|
|
1457
|
+
schema: members.length === 1 ? members[0] : kubb_kit.ast.factory.createSchema({
|
|
1401
1458
|
type: "union",
|
|
1402
1459
|
members
|
|
1403
1460
|
}),
|
|
@@ -1425,7 +1482,7 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1425
1482
|
description: node.requestBody.description ?? schema.description
|
|
1426
1483
|
}), "input");
|
|
1427
1484
|
})();
|
|
1428
|
-
return /* @__PURE__ */ (0,
|
|
1485
|
+
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx.File, {
|
|
1429
1486
|
baseName: meta.file.baseName,
|
|
1430
1487
|
path: meta.file.path,
|
|
1431
1488
|
meta: meta.file.meta,
|
|
@@ -1446,7 +1503,7 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1446
1503
|
}
|
|
1447
1504
|
}),
|
|
1448
1505
|
children: [
|
|
1449
|
-
/* @__PURE__ */ (0,
|
|
1506
|
+
/* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Import, {
|
|
1450
1507
|
name: isZodImport ? "z" : ["z"],
|
|
1451
1508
|
path: importPath,
|
|
1452
1509
|
isNameSpace: isZodImport
|
|
@@ -1477,7 +1534,7 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1477
1534
|
* resolverZod.resolveSchemaTypeName('pet') // 'PetSchemaType'
|
|
1478
1535
|
* ```
|
|
1479
1536
|
*/
|
|
1480
|
-
const resolverZod = (0,
|
|
1537
|
+
const resolverZod = (0, kubb_kit.defineResolver)(() => {
|
|
1481
1538
|
return {
|
|
1482
1539
|
name: "default",
|
|
1483
1540
|
pluginName: "plugin-zod",
|
|
@@ -1548,7 +1605,7 @@ const pluginZodName = "plugin-zod";
|
|
|
1548
1605
|
*
|
|
1549
1606
|
* @example
|
|
1550
1607
|
* ```ts
|
|
1551
|
-
* import { defineConfig } from 'kubb'
|
|
1608
|
+
* import { defineConfig } from 'kubb/config'
|
|
1552
1609
|
* import { pluginTs } from '@kubb/plugin-ts'
|
|
1553
1610
|
* import { pluginZod } from '@kubb/plugin-zod'
|
|
1554
1611
|
*
|
|
@@ -1559,17 +1616,17 @@ const pluginZodName = "plugin-zod";
|
|
|
1559
1616
|
* pluginTs(),
|
|
1560
1617
|
* pluginZod({
|
|
1561
1618
|
* output: { path: './zod' },
|
|
1562
|
-
*
|
|
1619
|
+
* inferred: true,
|
|
1563
1620
|
* }),
|
|
1564
1621
|
* ],
|
|
1565
1622
|
* })
|
|
1566
1623
|
* ```
|
|
1567
1624
|
*/
|
|
1568
|
-
const pluginZod = (0,
|
|
1625
|
+
const pluginZod = (0, kubb_kit.definePlugin)((options) => {
|
|
1569
1626
|
const { output = {
|
|
1570
1627
|
path: "zod",
|
|
1571
1628
|
barrel: { type: "named" }
|
|
1572
|
-
}, group, exclude = [], include, override = [],
|
|
1629
|
+
}, 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;
|
|
1573
1630
|
const groupConfig = createGroupConfig(group);
|
|
1574
1631
|
return {
|
|
1575
1632
|
name: pluginZodName,
|
|
@@ -1581,14 +1638,12 @@ const pluginZod = (0, _kubb_core.definePlugin)((options) => {
|
|
|
1581
1638
|
include,
|
|
1582
1639
|
override,
|
|
1583
1640
|
group: groupConfig,
|
|
1584
|
-
typed,
|
|
1585
1641
|
importPath,
|
|
1586
1642
|
coercion,
|
|
1587
1643
|
inferred,
|
|
1588
1644
|
guidType,
|
|
1589
1645
|
regexType,
|
|
1590
1646
|
mini,
|
|
1591
|
-
wrapOutput,
|
|
1592
1647
|
printer
|
|
1593
1648
|
});
|
|
1594
1649
|
ctx.setResolver(userResolver ? {
|