@kubb/plugin-zod 5.0.0-beta.100 → 5.0.0-beta.106
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 +32 -2
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +32 -2
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -79,13 +79,24 @@ function resolveContentTypeVariants(entries, baseName) {
|
|
|
79
79
|
};
|
|
80
80
|
});
|
|
81
81
|
}
|
|
82
|
+
const operationParameterGroupsByNode = /* @__PURE__ */ new WeakMap();
|
|
83
|
+
/**
|
|
84
|
+
* Groups an operation's parameters by location (`path`/`query`/`header`/`cookie`), deduping each
|
|
85
|
+
* group by name. Every plugin generator visiting the same `OperationNode` shares one AST instance
|
|
86
|
+
* (see `KubbDriver`), so the result is cached per node to avoid re-filtering and re-deduping the
|
|
87
|
+
* same parameters once per plugin.
|
|
88
|
+
*/
|
|
82
89
|
function getOperationParameters(node) {
|
|
83
|
-
|
|
90
|
+
const cached = operationParameterGroupsByNode.get(node);
|
|
91
|
+
if (cached) return cached;
|
|
92
|
+
const groups = {
|
|
84
93
|
path: dedupeParams(node.parameters.filter((param) => param.in === "path")),
|
|
85
94
|
query: dedupeParams(node.parameters.filter((param) => param.in === "query")),
|
|
86
95
|
header: dedupeParams(node.parameters.filter((param) => param.in === "header")),
|
|
87
96
|
cookie: dedupeParams(node.parameters.filter((param) => param.in === "cookie"))
|
|
88
97
|
};
|
|
98
|
+
operationParameterGroupsByNode.set(node, groups);
|
|
99
|
+
return groups;
|
|
89
100
|
}
|
|
90
101
|
/**
|
|
91
102
|
* Builds the combined `{ body, path, query, headers }` options object schema for an operation,
|
|
@@ -880,6 +891,20 @@ function buildEnum(values) {
|
|
|
880
891
|
return `z.union([${literals.join(", ")}])`;
|
|
881
892
|
}
|
|
882
893
|
/**
|
|
894
|
+
* Digit pattern for a `type: 'string'` schema that carries an integer `format`, the way ProtoJSON
|
|
895
|
+
* encodes 64-bit integers. Returns `undefined` for every other format, and a `pattern` from the
|
|
896
|
+
* spec takes precedence over this fallback.
|
|
897
|
+
*
|
|
898
|
+
* @example
|
|
899
|
+
* ```ts
|
|
900
|
+
* integerFormatPattern('uint64') // '^\\d+$'
|
|
901
|
+
* ```
|
|
902
|
+
*/
|
|
903
|
+
function integerFormatPattern(format) {
|
|
904
|
+
if (format === "int32" || format === "int64") return "^-?\\d+$";
|
|
905
|
+
if (format === "uint64") return "^\\d+$";
|
|
906
|
+
}
|
|
907
|
+
/**
|
|
883
908
|
* Map a `regexType` to the `func` argument of `toRegExpString`: `'constructor'` emits
|
|
884
909
|
* `new RegExp(...)`, while `'literal'` (the default) emits a regex literal.
|
|
885
910
|
*/
|
|
@@ -1106,8 +1131,11 @@ const printerZod = kubb_kit.ast.createPrinter((options) => {
|
|
|
1106
1131
|
boolean: () => "z.boolean()",
|
|
1107
1132
|
null: () => "z.null()",
|
|
1108
1133
|
string(node) {
|
|
1109
|
-
|
|
1134
|
+
const base = shouldCoerce(this.options.coercion, "strings") ? "z.coerce.string()" : "z.string()";
|
|
1135
|
+
const pattern = node.pattern ?? integerFormatPattern(node.format);
|
|
1136
|
+
return `${base}${lengthConstraints({
|
|
1110
1137
|
...node,
|
|
1138
|
+
pattern,
|
|
1111
1139
|
regexType: this.options.regexType
|
|
1112
1140
|
})}`;
|
|
1113
1141
|
},
|
|
@@ -1343,8 +1371,10 @@ const printerZodMini = kubb_kit.ast.createPrinter((options) => {
|
|
|
1343
1371
|
boolean: () => "z.boolean()",
|
|
1344
1372
|
null: () => "z.null()",
|
|
1345
1373
|
string(node) {
|
|
1374
|
+
const pattern = node.pattern ?? integerFormatPattern(node.format);
|
|
1346
1375
|
return `z.string()${lengthChecksMini({
|
|
1347
1376
|
...node,
|
|
1377
|
+
pattern,
|
|
1348
1378
|
regexType: this.options.regexType
|
|
1349
1379
|
})}`;
|
|
1350
1380
|
},
|