@kubb/plugin-zod 5.0.0-beta.81 → 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 +94 -90
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -4
- package/dist/index.js +48 -44
- 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,7 +416,7 @@ 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 });
|
|
421
420
|
}
|
|
422
421
|
/**
|
|
423
422
|
* Whether the node is a plain inline object whose shape can be lifted into an `.extend({ … })`
|
|
@@ -438,9 +437,9 @@ function isObjectSchemaNode(node, cyclicSchemas) {
|
|
|
438
437
|
if (node.nullable || node.optional || node.nullish) return false;
|
|
439
438
|
if (node.type === "object") return true;
|
|
440
439
|
if (node.type === "ref") {
|
|
441
|
-
const refName = (node.ref ?
|
|
440
|
+
const refName = (node.ref ? kubb_kit.ast.extractRefName(node.ref) : void 0) ?? node.name;
|
|
442
441
|
if (refName && cyclicSchemas?.has(refName)) return false;
|
|
443
|
-
const resolved =
|
|
442
|
+
const resolved = kubb_kit.ast.syncSchemaRef(node);
|
|
444
443
|
return resolved.type === "ref" || isObjectSchemaNode(resolved, cyclicSchemas);
|
|
445
444
|
}
|
|
446
445
|
if (node.type === "union") {
|
|
@@ -464,7 +463,7 @@ function isObjectComposableIntersection(node, cyclicSchemas) {
|
|
|
464
463
|
* Objects become `{}`, primitives become their string representation, strings are quoted.
|
|
465
464
|
*/
|
|
466
465
|
function formatDefault(value) {
|
|
467
|
-
if (typeof value === "string") return
|
|
466
|
+
if (typeof value === "string") return kubb_kit.ast.stringify(value);
|
|
468
467
|
if (typeof value === "object" && value !== null) return "{}";
|
|
469
468
|
return String(value ?? "");
|
|
470
469
|
}
|
|
@@ -479,13 +478,13 @@ function formatDefault(value) {
|
|
|
479
478
|
*/
|
|
480
479
|
function defaultLiteral(node, value) {
|
|
481
480
|
if (value === null) return null;
|
|
482
|
-
if (node &&
|
|
481
|
+
if (node && kubb_kit.ast.narrowSchema(node, "bigint")) {
|
|
483
482
|
if (typeof value === "bigint") return `BigInt(${value})`;
|
|
484
483
|
if (typeof value === "number" && Number.isInteger(value)) return `BigInt(${value})`;
|
|
485
484
|
return null;
|
|
486
485
|
}
|
|
487
|
-
if (node &&
|
|
488
|
-
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;
|
|
489
488
|
if (enumNode) {
|
|
490
489
|
const values = enumNode.namedEnumValues?.map((member) => member.value) ?? enumNode.enumValues ?? [];
|
|
491
490
|
if (values.length) {
|
|
@@ -500,7 +499,7 @@ function defaultLiteral(node, value) {
|
|
|
500
499
|
* Strings are quoted; numbers and booleans are emitted raw.
|
|
501
500
|
*/
|
|
502
501
|
function formatLiteral(v) {
|
|
503
|
-
if (typeof v === "string") return
|
|
502
|
+
if (typeof v === "string") return kubb_kit.ast.stringify(v);
|
|
504
503
|
return String(v);
|
|
505
504
|
}
|
|
506
505
|
/**
|
|
@@ -542,7 +541,8 @@ function patternKeySchemaMini({ patterns, regexType }) {
|
|
|
542
541
|
})}))`;
|
|
543
542
|
}
|
|
544
543
|
function patternKeySource({ patterns, regexType }) {
|
|
545
|
-
|
|
544
|
+
const source = patterns.length === 1 ? patterns[0] : patterns.map((pattern) => `(${pattern})`).join("|");
|
|
545
|
+
return kubb_kit.ast.toRegExpString(source, regexFunc(regexType));
|
|
546
546
|
}
|
|
547
547
|
/**
|
|
548
548
|
* Build `.min()` / `.max()` / `.gt()` / `.lt()` constraint chains for numbers
|
|
@@ -565,7 +565,7 @@ function lengthConstraints({ min, max, pattern, regexType }) {
|
|
|
565
565
|
return [
|
|
566
566
|
min !== void 0 ? `.min(${min})` : "",
|
|
567
567
|
max !== void 0 ? `.max(${max})` : "",
|
|
568
|
-
pattern !== void 0 ? `.regex(${
|
|
568
|
+
pattern !== void 0 ? `.regex(${kubb_kit.ast.toRegExpString(pattern, regexFunc(regexType))})` : ""
|
|
569
569
|
].join("");
|
|
570
570
|
}
|
|
571
571
|
/**
|
|
@@ -587,7 +587,7 @@ function lengthChecksMini({ min, max, pattern, regexType }) {
|
|
|
587
587
|
const checks = [];
|
|
588
588
|
if (min !== void 0) checks.push(`z.minLength(${min})`);
|
|
589
589
|
if (max !== void 0) checks.push(`z.maxLength(${max})`);
|
|
590
|
-
if (pattern !== void 0) checks.push(`z.regex(${
|
|
590
|
+
if (pattern !== void 0) checks.push(`z.regex(${kubb_kit.ast.toRegExpString(pattern, regexFunc(regexType))})`);
|
|
591
591
|
return checks.length ? `.check(${checks.join(", ")})` : "";
|
|
592
592
|
}
|
|
593
593
|
/**
|
|
@@ -603,7 +603,7 @@ function applyModifiers({ value, schema, nullable, optional, nullish, defaultVal
|
|
|
603
603
|
})();
|
|
604
604
|
const literal = defaultValue !== void 0 ? defaultLiteral(schema, defaultValue) : null;
|
|
605
605
|
const withDefault = literal !== null ? `${withModifier}.default(${literal})` : withModifier;
|
|
606
|
-
const withDescription = description ? `${withDefault}.describe(${
|
|
606
|
+
const withDescription = description ? `${withDefault}.describe(${kubb_kit.ast.stringify(description)})` : withDefault;
|
|
607
607
|
return examples?.length ? `${withDescription}.meta({ examples: [${examples.map(formatDefault).join(", ")}] })` : withDescription;
|
|
608
608
|
}
|
|
609
609
|
function modifierDepth(schema) {
|
|
@@ -621,7 +621,7 @@ function modifierDepth(schema) {
|
|
|
621
621
|
* objects need no unwrap because the printer adds their modifiers after `.omit()`.
|
|
622
622
|
*/
|
|
623
623
|
function omitUnwrapChain(node) {
|
|
624
|
-
const ref =
|
|
624
|
+
const ref = kubb_kit.ast.narrowSchema(node, "ref");
|
|
625
625
|
if (!ref) return "";
|
|
626
626
|
const target = ref.schema ?? ref;
|
|
627
627
|
const depth = modifierDepth(target) + (target.default !== void 0 ? 1 : 0);
|
|
@@ -646,9 +646,9 @@ function strictOneOfMember$1(member, node, cyclicSchemas) {
|
|
|
646
646
|
if (node.type === "object" && node.additionalProperties === void 0) return `${member}.strict()`;
|
|
647
647
|
if (node.type === "ref") {
|
|
648
648
|
if (member.startsWith("z.lazy(")) return member;
|
|
649
|
-
const refName = (node.ref ?
|
|
649
|
+
const refName = (node.ref ? kubb_kit.ast.extractRefName(node.ref) : void 0) ?? node.name;
|
|
650
650
|
if (refName && cyclicSchemas?.has(refName)) return member;
|
|
651
|
-
const schema =
|
|
651
|
+
const schema = kubb_kit.ast.syncSchemaRef(node);
|
|
652
652
|
if (schema.nullable || schema.optional || node.nullable || node.optional) return member;
|
|
653
653
|
if (schema.type === "object" && (schema.additionalProperties === void 0 || schema.additionalProperties === false)) return `${member}.strict()`;
|
|
654
654
|
}
|
|
@@ -657,12 +657,12 @@ function strictOneOfMember$1(member, node, cyclicSchemas) {
|
|
|
657
657
|
__name(strictOneOfMember$1, "strictOneOfMember");
|
|
658
658
|
function getMemberConstraint({ member, regexType }) {
|
|
659
659
|
if (member.primitive === "string") return lengthConstraints({
|
|
660
|
-
...
|
|
660
|
+
...kubb_kit.ast.narrowSchema(member, "string") ?? {},
|
|
661
661
|
regexType
|
|
662
662
|
}) || void 0;
|
|
663
|
-
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;
|
|
664
664
|
if (member.primitive === "array") return lengthConstraints({
|
|
665
|
-
...
|
|
665
|
+
...kubb_kit.ast.narrowSchema(member, "array") ?? {},
|
|
666
666
|
regexType
|
|
667
667
|
}) || void 0;
|
|
668
668
|
}
|
|
@@ -671,19 +671,19 @@ function getMemberConstraint({ member, regexType }) {
|
|
|
671
671
|
* `.extend(...)` renderings so they stay in lockstep.
|
|
672
672
|
*/
|
|
673
673
|
function buildZodObjectShape(ctx, node) {
|
|
674
|
-
const objectNode =
|
|
674
|
+
const objectNode = kubb_kit.ast.narrowSchema(node, "object");
|
|
675
675
|
if (!objectNode) return "{}";
|
|
676
|
-
const isCyclic = (schema) => ctx.options.cyclicSchemas != null &&
|
|
677
|
-
|
|
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
678
|
const hasSelfRef = isCyclic(schema);
|
|
679
679
|
const savedCyclicSchemas = ctx.options.cyclicSchemas;
|
|
680
680
|
if (hasSelfRef) ctx.options.cyclicSchemas = void 0;
|
|
681
|
-
const baseOutput = ctx.transform(schema) ?? ctx.transform(
|
|
681
|
+
const baseOutput = ctx.transform(schema) ?? ctx.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }));
|
|
682
682
|
if (hasSelfRef) ctx.options.cyclicSchemas = savedCyclicSchemas;
|
|
683
683
|
return baseOutput;
|
|
684
684
|
}).map(({ name: propName, property, output: baseOutput }) => {
|
|
685
685
|
const { schema } = property;
|
|
686
|
-
const meta =
|
|
686
|
+
const meta = kubb_kit.ast.syncSchemaRef(schema);
|
|
687
687
|
const descriptionToApply = schema.type !== "ref" && meta.type === "ref" ? void 0 : meta.description;
|
|
688
688
|
const value = applyModifiers({
|
|
689
689
|
value: baseOutput,
|
|
@@ -695,11 +695,12 @@ function buildZodObjectShape(ctx, node) {
|
|
|
695
695
|
description: descriptionToApply,
|
|
696
696
|
examples: meta.examples
|
|
697
697
|
});
|
|
698
|
-
return isCyclic(schema) ?
|
|
698
|
+
return isCyclic(schema) ? kubb_kit.ast.lazyGetter({
|
|
699
699
|
name: propName,
|
|
700
700
|
body: value
|
|
701
|
-
}) : `${
|
|
702
|
-
})
|
|
701
|
+
}) : `${kubb_kit.ast.objectKey(propName)}: ${value}`;
|
|
702
|
+
});
|
|
703
|
+
return kubb_kit.ast.buildObject(entries);
|
|
703
704
|
}
|
|
704
705
|
/**
|
|
705
706
|
* Zod v4 printer built with `definePrinter`.
|
|
@@ -713,7 +714,7 @@ function buildZodObjectShape(ctx, node) {
|
|
|
713
714
|
* const code = printer.print(stringNode) // "z.string()"
|
|
714
715
|
* ```
|
|
715
716
|
*/
|
|
716
|
-
const printerZod =
|
|
717
|
+
const printerZod = kubb_kit.ast.createPrinter((options) => {
|
|
717
718
|
const cyclicSchemaNames = options.cyclicSchemas;
|
|
718
719
|
return {
|
|
719
720
|
name: "zod",
|
|
@@ -791,7 +792,7 @@ const printerZod = _kubb_core.ast.createPrinter((options) => {
|
|
|
791
792
|
},
|
|
792
793
|
ref(node) {
|
|
793
794
|
if (!node.name) return null;
|
|
794
|
-
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;
|
|
795
796
|
const useInputVariant = node.ref != null && this.options.direction === "input" && containsCodec(node);
|
|
796
797
|
const resolvedName = node.ref ? useInputVariant ? this.options.resolver?.resolveInputSchemaName(refName) ?? refName : this.options.resolver?.default(refName, "function") ?? refName : node.name;
|
|
797
798
|
if (node.ref && this.options.cyclicSchemas?.has(refName)) return `z.lazy(() => ${resolvedName})`;
|
|
@@ -806,11 +807,11 @@ const printerZod = _kubb_core.ast.createPrinter((options) => {
|
|
|
806
807
|
const catchallType = this.transform(node.additionalProperties);
|
|
807
808
|
return catchallType ? `${objectBase}.catchall(${catchallType})` : objectBase;
|
|
808
809
|
}
|
|
809
|
-
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" }))})`;
|
|
810
811
|
if (node.additionalProperties === false && patterns.length === 0) return `${objectBase}.strict()`;
|
|
811
812
|
if (patterns.length > 0) {
|
|
812
813
|
const values = patterns.map(([, valueSchema]) => {
|
|
813
|
-
const valueType = this.transform(valueSchema) ?? this.transform(
|
|
814
|
+
const valueType = this.transform(valueSchema) ?? this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }));
|
|
814
815
|
return valueSchema.nullable ? `${valueType}.nullable()` : valueType;
|
|
815
816
|
});
|
|
816
817
|
const distinct = [...new Set(values)];
|
|
@@ -825,23 +826,24 @@ const printerZod = _kubb_core.ast.createPrinter((options) => {
|
|
|
825
826
|
})();
|
|
826
827
|
},
|
|
827
828
|
array(node) {
|
|
828
|
-
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({
|
|
829
830
|
...node,
|
|
830
831
|
regexType: this.options.regexType
|
|
831
832
|
})}`;
|
|
832
833
|
return node.unique ? `${base}.refine(items => new Set(items).size === items.length, { message: "Array entries must be unique" })` : base;
|
|
833
834
|
},
|
|
834
835
|
tuple(node) {
|
|
835
|
-
|
|
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)})`;
|
|
836
838
|
},
|
|
837
839
|
union(node) {
|
|
838
840
|
const nodeMembers = node.members ?? [];
|
|
839
|
-
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);
|
|
840
842
|
if (members.length === 0) return "";
|
|
841
843
|
if (members.length === 1) return members[0];
|
|
842
844
|
const allDiscriminable = nodeMembers.every((m) => isObjectSchemaNode(m, cyclicSchemaNames));
|
|
843
|
-
if (node.discriminatorPropertyName && allDiscriminable) return `z.discriminatedUnion(${
|
|
844
|
-
return `z.union(${
|
|
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)})`;
|
|
845
847
|
},
|
|
846
848
|
intersection(node) {
|
|
847
849
|
const members = node.members ?? [];
|
|
@@ -867,7 +869,7 @@ const printerZod = _kubb_core.ast.createPrinter((options) => {
|
|
|
867
869
|
const { keysToOmit } = this.options;
|
|
868
870
|
const transformed = this.transform(node);
|
|
869
871
|
if (!transformed) return null;
|
|
870
|
-
const meta =
|
|
872
|
+
const meta = kubb_kit.ast.syncSchemaRef(node);
|
|
871
873
|
return applyModifiers({
|
|
872
874
|
value: (() => {
|
|
873
875
|
if (!keysToOmit?.length || meta.primitive !== "object" || meta.type === "union" && meta.discriminatorPropertyName) return transformed;
|
|
@@ -896,12 +898,12 @@ function strictOneOfMember(member, node) {
|
|
|
896
898
|
}
|
|
897
899
|
function getMemberConstraintMini({ member, regexType }) {
|
|
898
900
|
if (member.primitive === "string") return lengthChecksMini({
|
|
899
|
-
...
|
|
901
|
+
...kubb_kit.ast.narrowSchema(member, "string") ?? {},
|
|
900
902
|
regexType
|
|
901
903
|
}) || void 0;
|
|
902
|
-
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;
|
|
903
905
|
if (member.primitive === "array") return lengthChecksMini({
|
|
904
|
-
...
|
|
906
|
+
...kubb_kit.ast.narrowSchema(member, "array") ?? {},
|
|
905
907
|
regexType
|
|
906
908
|
}) || void 0;
|
|
907
909
|
}
|
|
@@ -910,19 +912,19 @@ function getMemberConstraintMini({ member, regexType }) {
|
|
|
910
912
|
* shared by the `z.object(...)` and `z.extend(...)` renderings so they stay in lockstep.
|
|
911
913
|
*/
|
|
912
914
|
function buildZodMiniObjectShape(ctx, node) {
|
|
913
|
-
const objectNode =
|
|
915
|
+
const objectNode = kubb_kit.ast.narrowSchema(node, "object");
|
|
914
916
|
if (!objectNode) return "{}";
|
|
915
|
-
const isCyclic = (schema) => ctx.options.cyclicSchemas != null &&
|
|
916
|
-
|
|
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) => {
|
|
917
919
|
const hasSelfRef = isCyclic(schema);
|
|
918
920
|
const savedCyclicSchemas = ctx.options.cyclicSchemas;
|
|
919
921
|
if (hasSelfRef) ctx.options.cyclicSchemas = void 0;
|
|
920
|
-
const baseOutput = ctx.transform(schema) ?? ctx.transform(
|
|
922
|
+
const baseOutput = ctx.transform(schema) ?? ctx.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }));
|
|
921
923
|
if (hasSelfRef) ctx.options.cyclicSchemas = savedCyclicSchemas;
|
|
922
924
|
return baseOutput;
|
|
923
925
|
}).map(({ name: propName, property, output: baseOutput }) => {
|
|
924
926
|
const { schema } = property;
|
|
925
|
-
const meta =
|
|
927
|
+
const meta = kubb_kit.ast.syncSchemaRef(schema);
|
|
926
928
|
const value = applyMiniModifiers({
|
|
927
929
|
value: baseOutput,
|
|
928
930
|
schema,
|
|
@@ -931,11 +933,12 @@ function buildZodMiniObjectShape(ctx, node) {
|
|
|
931
933
|
nullish: schema.nullish,
|
|
932
934
|
defaultValue: meta.default
|
|
933
935
|
});
|
|
934
|
-
return isCyclic(schema) ?
|
|
936
|
+
return isCyclic(schema) ? kubb_kit.ast.lazyGetter({
|
|
935
937
|
name: propName,
|
|
936
938
|
body: value
|
|
937
|
-
}) : `${
|
|
938
|
-
})
|
|
939
|
+
}) : `${kubb_kit.ast.objectKey(propName)}: ${value}`;
|
|
940
|
+
});
|
|
941
|
+
return kubb_kit.ast.buildObject(entries);
|
|
939
942
|
}
|
|
940
943
|
/**
|
|
941
944
|
* Zod v4 Mini printer built with `definePrinter`.
|
|
@@ -949,7 +952,7 @@ function buildZodMiniObjectShape(ctx, node) {
|
|
|
949
952
|
* const code = printer.print(optionalStringNode) // "z.optional(z.string())"
|
|
950
953
|
* ```
|
|
951
954
|
*/
|
|
952
|
-
const printerZodMini =
|
|
955
|
+
const printerZodMini = kubb_kit.ast.createPrinter((options) => {
|
|
953
956
|
const cyclicSchemaNames = options.cyclicSchemas;
|
|
954
957
|
return {
|
|
955
958
|
name: "zod-mini",
|
|
@@ -1019,7 +1022,7 @@ const printerZodMini = _kubb_core.ast.createPrinter((options) => {
|
|
|
1019
1022
|
},
|
|
1020
1023
|
ref(node) {
|
|
1021
1024
|
if (!node.name) return null;
|
|
1022
|
-
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;
|
|
1023
1026
|
const resolvedName = node.ref ? this.options.resolver?.default(refName, "function") ?? refName : node.name;
|
|
1024
1027
|
if (node.ref && this.options.cyclicSchemas?.has(refName)) return `z.lazy(() => ${resolvedName})`;
|
|
1025
1028
|
return resolvedName;
|
|
@@ -1032,11 +1035,11 @@ const printerZodMini = _kubb_core.ast.createPrinter((options) => {
|
|
|
1032
1035
|
const catchallType = this.transform(node.additionalProperties);
|
|
1033
1036
|
return catchallType ? `z.catchall(${objectBase}, ${catchallType})` : objectBase;
|
|
1034
1037
|
}
|
|
1035
|
-
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" }))})`;
|
|
1036
1039
|
if (node.additionalProperties === false && patterns.length === 0) return objectBase.replace(/^z\.object\(/, "z.strictObject(");
|
|
1037
1040
|
if (patterns.length > 0) {
|
|
1038
1041
|
const values = patterns.map(([, valueSchema]) => {
|
|
1039
|
-
const valueType = this.transform(valueSchema) ?? this.transform(
|
|
1042
|
+
const valueType = this.transform(valueSchema) ?? this.transform(kubb_kit.ast.factory.createSchema({ type: "unknown" }));
|
|
1040
1043
|
return valueSchema.nullable ? `z.nullable(${valueType})` : valueType;
|
|
1041
1044
|
});
|
|
1042
1045
|
const distinct = [...new Set(values)];
|
|
@@ -1050,23 +1053,24 @@ const printerZodMini = _kubb_core.ast.createPrinter((options) => {
|
|
|
1050
1053
|
return objectBase;
|
|
1051
1054
|
},
|
|
1052
1055
|
array(node) {
|
|
1053
|
-
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({
|
|
1054
1057
|
...node,
|
|
1055
1058
|
regexType: this.options.regexType
|
|
1056
1059
|
})}`;
|
|
1057
1060
|
return node.unique ? `${base}.refine(items => new Set(items).size === items.length, { message: "Array entries must be unique" })` : base;
|
|
1058
1061
|
},
|
|
1059
1062
|
tuple(node) {
|
|
1060
|
-
|
|
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)})`;
|
|
1061
1065
|
},
|
|
1062
1066
|
union(node) {
|
|
1063
1067
|
const nodeMembers = node.members ?? [];
|
|
1064
|
-
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);
|
|
1065
1069
|
if (members.length === 0) return "";
|
|
1066
1070
|
if (members.length === 1) return members[0];
|
|
1067
1071
|
const allDiscriminable = nodeMembers.every((m) => isObjectSchemaNode(m, cyclicSchemaNames));
|
|
1068
|
-
if (node.discriminatorPropertyName && allDiscriminable) return `z.discriminatedUnion(${
|
|
1069
|
-
return `z.union(${
|
|
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)})`;
|
|
1070
1074
|
},
|
|
1071
1075
|
intersection(node) {
|
|
1072
1076
|
const members = node.members ?? [];
|
|
@@ -1092,7 +1096,7 @@ const printerZodMini = _kubb_core.ast.createPrinter((options) => {
|
|
|
1092
1096
|
const { keysToOmit } = this.options;
|
|
1093
1097
|
const transformed = this.transform(node);
|
|
1094
1098
|
if (!transformed) return null;
|
|
1095
|
-
const meta =
|
|
1099
|
+
const meta = kubb_kit.ast.syncSchemaRef(node);
|
|
1096
1100
|
return applyMiniModifiers({
|
|
1097
1101
|
value: (() => {
|
|
1098
1102
|
if (!keysToOmit?.length || meta.primitive !== "object" || meta.type === "union" && meta.discriminatorPropertyName) return transformed;
|
|
@@ -1173,9 +1177,9 @@ function getMiniPrinter(resolver, params) {
|
|
|
1173
1177
|
* When `mini: true`, schemas use the Zod Mini functional API instead of
|
|
1174
1178
|
* chainable methods.
|
|
1175
1179
|
*/
|
|
1176
|
-
const zodGenerator = (0,
|
|
1180
|
+
const zodGenerator = (0, kubb_kit.defineGenerator)({
|
|
1177
1181
|
name: "zod",
|
|
1178
|
-
renderer:
|
|
1182
|
+
renderer: kubb_jsx.jsxRenderer,
|
|
1179
1183
|
schema(node, ctx) {
|
|
1180
1184
|
const { adapter, config, resolver, root } = ctx;
|
|
1181
1185
|
const { output, coercion, guidType, regexType, mini, inferred, importPath, group, printer } = ctx.options;
|
|
@@ -1243,7 +1247,7 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1243
1247
|
nameMapping,
|
|
1244
1248
|
nodes: printer?.nodes
|
|
1245
1249
|
}) : stdPrinters.output;
|
|
1246
|
-
return /* @__PURE__ */ (0,
|
|
1250
|
+
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx.File, {
|
|
1247
1251
|
baseName: meta.file.baseName,
|
|
1248
1252
|
path: meta.file.path,
|
|
1249
1253
|
meta: meta.file.meta,
|
|
@@ -1264,12 +1268,12 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1264
1268
|
}
|
|
1265
1269
|
}),
|
|
1266
1270
|
children: [
|
|
1267
|
-
/* @__PURE__ */ (0,
|
|
1271
|
+
/* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Import, {
|
|
1268
1272
|
name: isZodImport ? "z" : ["z"],
|
|
1269
1273
|
path: importPath,
|
|
1270
1274
|
isNameSpace: isZodImport
|
|
1271
1275
|
}),
|
|
1272
|
-
imports.map((imp) => /* @__PURE__ */ (0,
|
|
1276
|
+
imports.map((imp) => /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Import, {
|
|
1273
1277
|
root: meta.file.path,
|
|
1274
1278
|
path: imp.path,
|
|
1275
1279
|
name: imp.name
|
|
@@ -1278,14 +1282,14 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1278
1282
|
imp.path,
|
|
1279
1283
|
imp.name
|
|
1280
1284
|
].join("-"))),
|
|
1281
|
-
/* @__PURE__ */ (0,
|
|
1285
|
+
/* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(Zod, {
|
|
1282
1286
|
name: meta.name,
|
|
1283
1287
|
node,
|
|
1284
1288
|
printer: schemaPrinter,
|
|
1285
1289
|
inferTypeName,
|
|
1286
1290
|
cyclic: cyclicSchemas.has(node.name)
|
|
1287
1291
|
}),
|
|
1288
|
-
hasCodec && stdPrinters && /* @__PURE__ */ (0,
|
|
1292
|
+
hasCodec && stdPrinters && /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(Zod, {
|
|
1289
1293
|
name: resolver.resolveInputSchemaName(node.name),
|
|
1290
1294
|
node,
|
|
1291
1295
|
printer: stdPrinters.input,
|
|
@@ -1296,7 +1300,7 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1296
1300
|
});
|
|
1297
1301
|
},
|
|
1298
1302
|
operation(node, ctx) {
|
|
1299
|
-
if (!
|
|
1303
|
+
if (!kubb_kit.ast.isHttpOperationNode(node)) return null;
|
|
1300
1304
|
const { adapter, config, resolver, root } = ctx;
|
|
1301
1305
|
const { output, coercion, guidType, regexType, mini, inferred, importPath, group, printer } = ctx.options;
|
|
1302
1306
|
const dateType = adapter.options.dateType;
|
|
@@ -1363,7 +1367,7 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1363
1367
|
nameMapping,
|
|
1364
1368
|
nodes: printer?.nodes
|
|
1365
1369
|
})[direction];
|
|
1366
|
-
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, {
|
|
1367
1371
|
root: meta.file.path,
|
|
1368
1372
|
path: imp.path,
|
|
1369
1373
|
name: imp.name
|
|
@@ -1371,7 +1375,7 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1371
1375
|
name,
|
|
1372
1376
|
imp.path,
|
|
1373
1377
|
imp.name
|
|
1374
|
-
].join("-"))), /* @__PURE__ */ (0,
|
|
1378
|
+
].join("-"))), /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(Zod, {
|
|
1375
1379
|
name,
|
|
1376
1380
|
node: schema,
|
|
1377
1381
|
printer: schemaPrinter,
|
|
@@ -1381,14 +1385,14 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1381
1385
|
}
|
|
1382
1386
|
function buildContentTypeVariants(entries, baseName, decorate, direction) {
|
|
1383
1387
|
const variants = resolveContentTypeVariants(entries, baseName);
|
|
1384
|
-
const unionSchema =
|
|
1388
|
+
const unionSchema = kubb_kit.ast.factory.createSchema({
|
|
1385
1389
|
type: "union",
|
|
1386
|
-
members: variants.map((variant) =>
|
|
1390
|
+
members: variants.map((variant) => kubb_kit.ast.factory.createSchema({
|
|
1387
1391
|
type: "ref",
|
|
1388
1392
|
name: variant.name
|
|
1389
1393
|
}))
|
|
1390
1394
|
});
|
|
1391
|
-
return /* @__PURE__ */ (0,
|
|
1395
|
+
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx_jsx_runtime.Fragment, { children: [variants.map((variant) => renderSchemaEntry({
|
|
1392
1396
|
schema: decorate ? decorate(variant.schema) : variant.schema,
|
|
1393
1397
|
name: variant.name,
|
|
1394
1398
|
keysToOmit: variant.keysToOmit,
|
|
@@ -1422,16 +1426,16 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1422
1426
|
name: resolver.resolveSchemaName(schemaName),
|
|
1423
1427
|
path: ""
|
|
1424
1428
|
})).flatMap((imp) => Array.isArray(imp.name) ? imp.name : [imp.name]) : []))).has(responseUnionName)) return null;
|
|
1425
|
-
const members = successResponsesWithSchema.map((res) =>
|
|
1429
|
+
const members = successResponsesWithSchema.map((res) => kubb_kit.ast.factory.createSchema({
|
|
1426
1430
|
type: "ref",
|
|
1427
1431
|
name: resolver.resolveResponseStatusName(node, res.statusCode)
|
|
1428
1432
|
}));
|
|
1429
1433
|
if (members.length === 0) return renderSchemaEntry({
|
|
1430
|
-
schema:
|
|
1434
|
+
schema: kubb_kit.ast.factory.createSchema({ type: "unknown" }),
|
|
1431
1435
|
name: responseUnionName
|
|
1432
1436
|
});
|
|
1433
1437
|
return renderSchemaEntry({
|
|
1434
|
-
schema: members.length === 1 ? members[0] :
|
|
1438
|
+
schema: members.length === 1 ? members[0] : kubb_kit.ast.factory.createSchema({
|
|
1435
1439
|
type: "union",
|
|
1436
1440
|
members
|
|
1437
1441
|
}),
|
|
@@ -1445,12 +1449,12 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1445
1449
|
name: resolver.resolveSchemaName(schemaName),
|
|
1446
1450
|
path: ""
|
|
1447
1451
|
})).flatMap((imp) => Array.isArray(imp.name) ? imp.name : [imp.name]) : []))).has(errorUnionName)) return null;
|
|
1448
|
-
const members = errorResponsesWithSchema.map((res) =>
|
|
1452
|
+
const members = errorResponsesWithSchema.map((res) => kubb_kit.ast.factory.createSchema({
|
|
1449
1453
|
type: "ref",
|
|
1450
1454
|
name: resolver.resolveResponseStatusName(node, res.statusCode)
|
|
1451
1455
|
}));
|
|
1452
1456
|
return renderSchemaEntry({
|
|
1453
|
-
schema: members.length === 1 ? members[0] :
|
|
1457
|
+
schema: members.length === 1 ? members[0] : kubb_kit.ast.factory.createSchema({
|
|
1454
1458
|
type: "union",
|
|
1455
1459
|
members
|
|
1456
1460
|
}),
|
|
@@ -1478,7 +1482,7 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1478
1482
|
description: node.requestBody.description ?? schema.description
|
|
1479
1483
|
}), "input");
|
|
1480
1484
|
})();
|
|
1481
|
-
return /* @__PURE__ */ (0,
|
|
1485
|
+
return /* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsxs)(kubb_jsx.File, {
|
|
1482
1486
|
baseName: meta.file.baseName,
|
|
1483
1487
|
path: meta.file.path,
|
|
1484
1488
|
meta: meta.file.meta,
|
|
@@ -1499,7 +1503,7 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1499
1503
|
}
|
|
1500
1504
|
}),
|
|
1501
1505
|
children: [
|
|
1502
|
-
/* @__PURE__ */ (0,
|
|
1506
|
+
/* @__PURE__ */ (0, kubb_jsx_jsx_runtime.jsx)(kubb_jsx.File.Import, {
|
|
1503
1507
|
name: isZodImport ? "z" : ["z"],
|
|
1504
1508
|
path: importPath,
|
|
1505
1509
|
isNameSpace: isZodImport
|
|
@@ -1530,7 +1534,7 @@ const zodGenerator = (0, _kubb_core.defineGenerator)({
|
|
|
1530
1534
|
* resolverZod.resolveSchemaTypeName('pet') // 'PetSchemaType'
|
|
1531
1535
|
* ```
|
|
1532
1536
|
*/
|
|
1533
|
-
const resolverZod = (0,
|
|
1537
|
+
const resolverZod = (0, kubb_kit.defineResolver)(() => {
|
|
1534
1538
|
return {
|
|
1535
1539
|
name: "default",
|
|
1536
1540
|
pluginName: "plugin-zod",
|
|
@@ -1601,7 +1605,7 @@ const pluginZodName = "plugin-zod";
|
|
|
1601
1605
|
*
|
|
1602
1606
|
* @example
|
|
1603
1607
|
* ```ts
|
|
1604
|
-
* import { defineConfig } from 'kubb'
|
|
1608
|
+
* import { defineConfig } from 'kubb/config'
|
|
1605
1609
|
* import { pluginTs } from '@kubb/plugin-ts'
|
|
1606
1610
|
* import { pluginZod } from '@kubb/plugin-zod'
|
|
1607
1611
|
*
|
|
@@ -1618,7 +1622,7 @@ const pluginZodName = "plugin-zod";
|
|
|
1618
1622
|
* })
|
|
1619
1623
|
* ```
|
|
1620
1624
|
*/
|
|
1621
|
-
const pluginZod = (0,
|
|
1625
|
+
const pluginZod = (0, kubb_kit.definePlugin)((options) => {
|
|
1622
1626
|
const { output = {
|
|
1623
1627
|
path: "zod",
|
|
1624
1628
|
barrel: { type: "named" }
|