@kubb/plugin-zod 5.0.0-beta.103 → 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.js
CHANGED
|
@@ -69,13 +69,24 @@ function resolveContentTypeVariants(entries, baseName) {
|
|
|
69
69
|
};
|
|
70
70
|
});
|
|
71
71
|
}
|
|
72
|
+
const operationParameterGroupsByNode = /* @__PURE__ */ new WeakMap();
|
|
73
|
+
/**
|
|
74
|
+
* Groups an operation's parameters by location (`path`/`query`/`header`/`cookie`), deduping each
|
|
75
|
+
* group by name. Every plugin generator visiting the same `OperationNode` shares one AST instance
|
|
76
|
+
* (see `KubbDriver`), so the result is cached per node to avoid re-filtering and re-deduping the
|
|
77
|
+
* same parameters once per plugin.
|
|
78
|
+
*/
|
|
72
79
|
function getOperationParameters(node) {
|
|
73
|
-
|
|
80
|
+
const cached = operationParameterGroupsByNode.get(node);
|
|
81
|
+
if (cached) return cached;
|
|
82
|
+
const groups = {
|
|
74
83
|
path: dedupeParams(node.parameters.filter((param) => param.in === "path")),
|
|
75
84
|
query: dedupeParams(node.parameters.filter((param) => param.in === "query")),
|
|
76
85
|
header: dedupeParams(node.parameters.filter((param) => param.in === "header")),
|
|
77
86
|
cookie: dedupeParams(node.parameters.filter((param) => param.in === "cookie"))
|
|
78
87
|
};
|
|
88
|
+
operationParameterGroupsByNode.set(node, groups);
|
|
89
|
+
return groups;
|
|
79
90
|
}
|
|
80
91
|
/**
|
|
81
92
|
* Builds the combined `{ body, path, query, headers }` options object schema for an operation,
|
|
@@ -870,6 +881,20 @@ function buildEnum(values) {
|
|
|
870
881
|
return `z.union([${literals.join(", ")}])`;
|
|
871
882
|
}
|
|
872
883
|
/**
|
|
884
|
+
* Digit pattern for a `type: 'string'` schema that carries an integer `format`, the way ProtoJSON
|
|
885
|
+
* encodes 64-bit integers. Returns `undefined` for every other format, and a `pattern` from the
|
|
886
|
+
* spec takes precedence over this fallback.
|
|
887
|
+
*
|
|
888
|
+
* @example
|
|
889
|
+
* ```ts
|
|
890
|
+
* integerFormatPattern('uint64') // '^\\d+$'
|
|
891
|
+
* ```
|
|
892
|
+
*/
|
|
893
|
+
function integerFormatPattern(format) {
|
|
894
|
+
if (format === "int32" || format === "int64") return "^-?\\d+$";
|
|
895
|
+
if (format === "uint64") return "^\\d+$";
|
|
896
|
+
}
|
|
897
|
+
/**
|
|
873
898
|
* Map a `regexType` to the `func` argument of `toRegExpString`: `'constructor'` emits
|
|
874
899
|
* `new RegExp(...)`, while `'literal'` (the default) emits a regex literal.
|
|
875
900
|
*/
|
|
@@ -1096,8 +1121,11 @@ const printerZod = ast.createPrinter((options) => {
|
|
|
1096
1121
|
boolean: () => "z.boolean()",
|
|
1097
1122
|
null: () => "z.null()",
|
|
1098
1123
|
string(node) {
|
|
1099
|
-
|
|
1124
|
+
const base = shouldCoerce(this.options.coercion, "strings") ? "z.coerce.string()" : "z.string()";
|
|
1125
|
+
const pattern = node.pattern ?? integerFormatPattern(node.format);
|
|
1126
|
+
return `${base}${lengthConstraints({
|
|
1100
1127
|
...node,
|
|
1128
|
+
pattern,
|
|
1101
1129
|
regexType: this.options.regexType
|
|
1102
1130
|
})}`;
|
|
1103
1131
|
},
|
|
@@ -1333,8 +1361,10 @@ const printerZodMini = ast.createPrinter((options) => {
|
|
|
1333
1361
|
boolean: () => "z.boolean()",
|
|
1334
1362
|
null: () => "z.null()",
|
|
1335
1363
|
string(node) {
|
|
1364
|
+
const pattern = node.pattern ?? integerFormatPattern(node.format);
|
|
1336
1365
|
return `z.string()${lengthChecksMini({
|
|
1337
1366
|
...node,
|
|
1367
|
+
pattern,
|
|
1338
1368
|
regexType: this.options.regexType
|
|
1339
1369
|
})}`;
|
|
1340
1370
|
},
|