@kubb/plugin-zod 5.0.0-alpha.3 → 5.0.0-alpha.31
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 +1619 -100
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +418 -4
- package/dist/index.js +1614 -100
- package/dist/index.js.map +1 -1
- package/package.json +6 -34
- package/src/components/Operations.tsx +22 -15
- package/src/components/Zod.tsx +20 -119
- package/src/constants.ts +5 -0
- package/src/generators/zodGenerator.tsx +129 -159
- package/src/generators/zodGeneratorLegacy.tsx +365 -0
- package/src/index.ts +12 -1
- package/src/plugin.ts +102 -148
- package/src/presets.ts +30 -0
- package/src/printers/printerZod.ts +298 -0
- package/src/printers/printerZodMini.ts +273 -0
- package/src/resolvers/resolverZod.ts +61 -0
- package/src/resolvers/resolverZodLegacy.ts +60 -0
- package/src/types.ts +172 -93
- package/src/utils.ts +248 -0
- package/dist/components-B7zUFnAm.cjs +0 -890
- package/dist/components-B7zUFnAm.cjs.map +0 -1
- package/dist/components-eECfXVou.js +0 -842
- package/dist/components-eECfXVou.js.map +0 -1
- package/dist/components.cjs +0 -4
- package/dist/components.d.ts +0 -56
- package/dist/components.js +0 -2
- package/dist/generators-CRKtFRi1.js +0 -290
- package/dist/generators-CRKtFRi1.js.map +0 -1
- package/dist/generators-CzSLRVqQ.cjs +0 -301
- package/dist/generators-CzSLRVqQ.cjs.map +0 -1
- package/dist/generators.cjs +0 -4
- package/dist/generators.d.ts +0 -503
- package/dist/generators.js +0 -2
- package/dist/templates/ToZod.source.cjs +0 -7
- package/dist/templates/ToZod.source.cjs.map +0 -1
- package/dist/templates/ToZod.source.d.ts +0 -7
- package/dist/templates/ToZod.source.js +0 -6
- package/dist/templates/ToZod.source.js.map +0 -1
- package/dist/types-D0wsPC6Y.d.ts +0 -172
- package/src/components/index.ts +0 -2
- package/src/generators/index.ts +0 -2
- package/src/generators/operationsGenerator.tsx +0 -50
- package/src/parser.ts +0 -909
- package/src/templates/ToZod.source.ts +0 -4
- package/templates/ToZod.ts +0 -61
|
@@ -1,842 +0,0 @@
|
|
|
1
|
-
import "./chunk--u3MIqq1.js";
|
|
2
|
-
import { SchemaGenerator, createParser, findSchemaKeyword, isKeyword, schemaKeywords } from "@kubb/plugin-oas";
|
|
3
|
-
import { Const, File, Type } from "@kubb/react-fabric";
|
|
4
|
-
import { Fragment, jsx, jsxs } from "@kubb/react-fabric/jsx-runtime";
|
|
5
|
-
import { sortBy } from "remeda";
|
|
6
|
-
//#region ../../internals/utils/src/string.ts
|
|
7
|
-
/**
|
|
8
|
-
* Strips a single matching pair of `"..."`, `'...'`, or `` `...` `` from both ends of `text`.
|
|
9
|
-
* Returns the string unchanged when no balanced quote pair is found.
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* trimQuotes('"hello"') // 'hello'
|
|
13
|
-
* trimQuotes('hello') // 'hello'
|
|
14
|
-
*/
|
|
15
|
-
function trimQuotes(text) {
|
|
16
|
-
if (text.length >= 2) {
|
|
17
|
-
const first = text[0];
|
|
18
|
-
const last = text[text.length - 1];
|
|
19
|
-
if (first === "\"" && last === "\"" || first === "'" && last === "'" || first === "`" && last === "`") return text.slice(1, -1);
|
|
20
|
-
}
|
|
21
|
-
return text;
|
|
22
|
-
}
|
|
23
|
-
/**
|
|
24
|
-
* Escapes characters that are not allowed inside JS string literals.
|
|
25
|
-
* Handles quotes, backslashes, and Unicode line terminators (U+2028 / U+2029).
|
|
26
|
-
*
|
|
27
|
-
* @see http://www.ecma-international.org/ecma-262/5.1/#sec-7.8.4
|
|
28
|
-
*/
|
|
29
|
-
function jsStringEscape(input) {
|
|
30
|
-
return `${input}`.replace(/["'\\\n\r\u2028\u2029]/g, (character) => {
|
|
31
|
-
switch (character) {
|
|
32
|
-
case "\"":
|
|
33
|
-
case "'":
|
|
34
|
-
case "\\": return `\\${character}`;
|
|
35
|
-
case "\n": return "\\n";
|
|
36
|
-
case "\r": return "\\r";
|
|
37
|
-
case "\u2028": return "\\u2028";
|
|
38
|
-
case "\u2029": return "\\u2029";
|
|
39
|
-
default: return "";
|
|
40
|
-
}
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
//#endregion
|
|
44
|
-
//#region ../../internals/utils/src/object.ts
|
|
45
|
-
/**
|
|
46
|
-
* Serializes a primitive value to a JSON string literal, stripping any surrounding quote characters first.
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* stringify('hello') // '"hello"'
|
|
50
|
-
* stringify('"hello"') // '"hello"'
|
|
51
|
-
*/
|
|
52
|
-
function stringify(value) {
|
|
53
|
-
if (value === void 0 || value === null) return "\"\"";
|
|
54
|
-
return JSON.stringify(trimQuotes(value.toString()));
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Converts a plain object into a multiline key-value string suitable for embedding in generated code.
|
|
58
|
-
* Nested objects are recursively stringified with indentation.
|
|
59
|
-
*
|
|
60
|
-
* @example
|
|
61
|
-
* stringifyObject({ foo: 'bar', nested: { a: 1 } })
|
|
62
|
-
* // 'foo: bar,\nnested: {\n a: 1\n }'
|
|
63
|
-
*/
|
|
64
|
-
function stringifyObject(value) {
|
|
65
|
-
return Object.entries(value).map(([key, val]) => {
|
|
66
|
-
if (val !== null && typeof val === "object") return `${key}: {\n ${stringifyObject(val)}\n }`;
|
|
67
|
-
return `${key}: ${val}`;
|
|
68
|
-
}).filter(Boolean).join(",\n");
|
|
69
|
-
}
|
|
70
|
-
//#endregion
|
|
71
|
-
//#region ../../internals/utils/src/regexp.ts
|
|
72
|
-
/**
|
|
73
|
-
* Converts a pattern string into a `new RegExp(...)` constructor call or a regex literal string.
|
|
74
|
-
* Inline flags expressed as `^(?im)` prefixes are extracted and applied to the resulting expression.
|
|
75
|
-
* Pass `null` as the second argument to emit a `/pattern/flags` literal instead.
|
|
76
|
-
*
|
|
77
|
-
* @example
|
|
78
|
-
* toRegExpString('^(?im)foo') // → 'new RegExp("foo", "im")'
|
|
79
|
-
* toRegExpString('^(?im)foo', null) // → '/foo/im'
|
|
80
|
-
*/
|
|
81
|
-
function toRegExpString(text, func = "RegExp") {
|
|
82
|
-
const raw = trimQuotes(text);
|
|
83
|
-
const match = raw.match(/^\^(\(\?([igmsuy]+)\))/i);
|
|
84
|
-
const replacementTarget = match?.[1] ?? "";
|
|
85
|
-
const matchedFlags = match?.[2];
|
|
86
|
-
const cleaned = raw.replace(/^\\?\//, "").replace(/\\?\/$/, "").replace(replacementTarget, "");
|
|
87
|
-
const { source, flags } = new RegExp(cleaned, matchedFlags);
|
|
88
|
-
if (func === null) return `/${source}/${flags}`;
|
|
89
|
-
return `new ${func}(${JSON.stringify(source)}${flags ? `, ${JSON.stringify(flags)}` : ""})`;
|
|
90
|
-
}
|
|
91
|
-
//#endregion
|
|
92
|
-
//#region src/components/Operations.tsx
|
|
93
|
-
function Operations({ name, operations }) {
|
|
94
|
-
const operationsJSON = operations.reduce((prev, acc) => {
|
|
95
|
-
prev[`"${acc.operation.getOperationId()}"`] = acc.data;
|
|
96
|
-
return prev;
|
|
97
|
-
}, {});
|
|
98
|
-
const pathsJSON = operations.reduce((prev, acc) => {
|
|
99
|
-
prev[`"${acc.operation.path}"`] = {
|
|
100
|
-
...prev[`"${acc.operation.path}"`] || {},
|
|
101
|
-
[acc.operation.method]: `operations["${acc.operation.getOperationId()}"]`
|
|
102
|
-
};
|
|
103
|
-
return prev;
|
|
104
|
-
}, {});
|
|
105
|
-
return /* @__PURE__ */ jsxs(Fragment, { children: [
|
|
106
|
-
/* @__PURE__ */ jsx(File.Source, {
|
|
107
|
-
name: "OperationSchema",
|
|
108
|
-
isExportable: true,
|
|
109
|
-
isIndexable: true,
|
|
110
|
-
children: /* @__PURE__ */ jsx(Type, {
|
|
111
|
-
name: "OperationSchema",
|
|
112
|
-
export: true,
|
|
113
|
-
children: `{
|
|
114
|
-
readonly request: z.ZodTypeAny | undefined;
|
|
115
|
-
readonly parameters: {
|
|
116
|
-
readonly path: z.ZodTypeAny | undefined;
|
|
117
|
-
readonly query: z.ZodTypeAny | undefined;
|
|
118
|
-
readonly header: z.ZodTypeAny | undefined;
|
|
119
|
-
};
|
|
120
|
-
readonly responses: {
|
|
121
|
-
readonly [status: number]: z.ZodTypeAny;
|
|
122
|
-
readonly default: z.ZodTypeAny;
|
|
123
|
-
};
|
|
124
|
-
readonly errors: {
|
|
125
|
-
readonly [status: number]: z.ZodTypeAny;
|
|
126
|
-
};
|
|
127
|
-
}`
|
|
128
|
-
})
|
|
129
|
-
}),
|
|
130
|
-
/* @__PURE__ */ jsx(File.Source, {
|
|
131
|
-
name: "OperationsMap",
|
|
132
|
-
isExportable: true,
|
|
133
|
-
isIndexable: true,
|
|
134
|
-
children: /* @__PURE__ */ jsx(Type, {
|
|
135
|
-
name: "OperationsMap",
|
|
136
|
-
export: true,
|
|
137
|
-
children: "Record<string, OperationSchema>"
|
|
138
|
-
})
|
|
139
|
-
}),
|
|
140
|
-
/* @__PURE__ */ jsx(File.Source, {
|
|
141
|
-
name,
|
|
142
|
-
isExportable: true,
|
|
143
|
-
isIndexable: true,
|
|
144
|
-
children: /* @__PURE__ */ jsx(Const, {
|
|
145
|
-
export: true,
|
|
146
|
-
name,
|
|
147
|
-
asConst: true,
|
|
148
|
-
children: `{${stringifyObject(operationsJSON)}}`
|
|
149
|
-
})
|
|
150
|
-
}),
|
|
151
|
-
/* @__PURE__ */ jsx(File.Source, {
|
|
152
|
-
name: "paths",
|
|
153
|
-
isExportable: true,
|
|
154
|
-
isIndexable: true,
|
|
155
|
-
children: /* @__PURE__ */ jsx(Const, {
|
|
156
|
-
export: true,
|
|
157
|
-
name: "paths",
|
|
158
|
-
asConst: true,
|
|
159
|
-
children: `{${stringifyObject(pathsJSON)}}`
|
|
160
|
-
})
|
|
161
|
-
})
|
|
162
|
-
] });
|
|
163
|
-
}
|
|
164
|
-
//#endregion
|
|
165
|
-
//#region src/parser.ts
|
|
166
|
-
/**
|
|
167
|
-
* Helper to build string/array length constraint checks for Zod Mini mode
|
|
168
|
-
*/
|
|
169
|
-
function buildLengthChecks(min, max) {
|
|
170
|
-
const checks = [];
|
|
171
|
-
if (min !== void 0) checks.push(`z.minLength(${min})`);
|
|
172
|
-
if (max !== void 0) checks.push(`z.maxLength(${max})`);
|
|
173
|
-
return checks;
|
|
174
|
-
}
|
|
175
|
-
const zodKeywordMapper = {
|
|
176
|
-
any: () => "z.any()",
|
|
177
|
-
unknown: () => "z.unknown()",
|
|
178
|
-
void: () => "z.void()",
|
|
179
|
-
number: (coercion, min, max, exclusiveMinimum, exclusiveMaximum, mini) => {
|
|
180
|
-
if (mini) {
|
|
181
|
-
const checks = [];
|
|
182
|
-
if (min !== void 0) checks.push(`z.minimum(${min})`);
|
|
183
|
-
if (max !== void 0) checks.push(`z.maximum(${max})`);
|
|
184
|
-
if (exclusiveMinimum !== void 0) checks.push(`z.minimum(${exclusiveMinimum}, { exclusive: true })`);
|
|
185
|
-
if (exclusiveMaximum !== void 0) checks.push(`z.maximum(${exclusiveMaximum}, { exclusive: true })`);
|
|
186
|
-
if (checks.length > 0) return `z.number().check(${checks.join(", ")})`;
|
|
187
|
-
return "z.number()";
|
|
188
|
-
}
|
|
189
|
-
return [
|
|
190
|
-
coercion ? "z.coerce.number()" : "z.number()",
|
|
191
|
-
min !== void 0 ? `.min(${min})` : void 0,
|
|
192
|
-
max !== void 0 ? `.max(${max})` : void 0,
|
|
193
|
-
exclusiveMinimum !== void 0 ? `.gt(${exclusiveMinimum})` : void 0,
|
|
194
|
-
exclusiveMaximum !== void 0 ? `.lt(${exclusiveMaximum})` : void 0
|
|
195
|
-
].filter(Boolean).join("");
|
|
196
|
-
},
|
|
197
|
-
integer: (coercion, min, max, version = "3", exclusiveMinimum, exclusiveMaximum, mini) => {
|
|
198
|
-
if (mini) {
|
|
199
|
-
const checks = [];
|
|
200
|
-
if (min !== void 0) checks.push(`z.minimum(${min})`);
|
|
201
|
-
if (max !== void 0) checks.push(`z.maximum(${max})`);
|
|
202
|
-
if (exclusiveMinimum !== void 0) checks.push(`z.minimum(${exclusiveMinimum}, { exclusive: true })`);
|
|
203
|
-
if (exclusiveMaximum !== void 0) checks.push(`z.maximum(${exclusiveMaximum}, { exclusive: true })`);
|
|
204
|
-
if (checks.length > 0) return `z.int().check(${checks.join(", ")})`;
|
|
205
|
-
return "z.int()";
|
|
206
|
-
}
|
|
207
|
-
return [
|
|
208
|
-
coercion ? "z.coerce.number().int()" : version === "4" ? "z.int()" : "z.number().int()",
|
|
209
|
-
min !== void 0 ? `.min(${min})` : void 0,
|
|
210
|
-
max !== void 0 ? `.max(${max})` : void 0,
|
|
211
|
-
exclusiveMinimum !== void 0 ? `.gt(${exclusiveMinimum})` : void 0,
|
|
212
|
-
exclusiveMaximum !== void 0 ? `.lt(${exclusiveMaximum})` : void 0
|
|
213
|
-
].filter(Boolean).join("");
|
|
214
|
-
},
|
|
215
|
-
bigint: (coercion) => coercion ? "z.coerce.bigint()" : "z.bigint()",
|
|
216
|
-
interface: (value, strict) => {
|
|
217
|
-
if (strict) return `z.strictInterface({
|
|
218
|
-
${value}
|
|
219
|
-
})`;
|
|
220
|
-
return `z.interface({
|
|
221
|
-
${value}
|
|
222
|
-
})`;
|
|
223
|
-
},
|
|
224
|
-
object: (value, strict, version = "3") => {
|
|
225
|
-
if (version === "4" && strict) return `z.strictObject({
|
|
226
|
-
${value}
|
|
227
|
-
})`;
|
|
228
|
-
if (strict) return `z.object({
|
|
229
|
-
${value}
|
|
230
|
-
}).strict()`;
|
|
231
|
-
return `z.object({
|
|
232
|
-
${value}
|
|
233
|
-
})`;
|
|
234
|
-
},
|
|
235
|
-
string: (coercion, min, max, mini) => {
|
|
236
|
-
if (mini) {
|
|
237
|
-
const checks = buildLengthChecks(min, max);
|
|
238
|
-
if (checks.length > 0) return `z.string().check(${checks.join(", ")})`;
|
|
239
|
-
return "z.string()";
|
|
240
|
-
}
|
|
241
|
-
return [
|
|
242
|
-
coercion ? "z.coerce.string()" : "z.string()",
|
|
243
|
-
min !== void 0 ? `.min(${min})` : void 0,
|
|
244
|
-
max !== void 0 ? `.max(${max})` : void 0
|
|
245
|
-
].filter(Boolean).join("");
|
|
246
|
-
},
|
|
247
|
-
boolean: () => "z.boolean()",
|
|
248
|
-
undefined: () => "z.undefined()",
|
|
249
|
-
nullable: (value) => {
|
|
250
|
-
if (value) return `z.nullable(${value})`;
|
|
251
|
-
return ".nullable()";
|
|
252
|
-
},
|
|
253
|
-
null: () => "z.null()",
|
|
254
|
-
nullish: (value) => {
|
|
255
|
-
if (value) return `z.nullish(${value})`;
|
|
256
|
-
return ".nullish()";
|
|
257
|
-
},
|
|
258
|
-
array: (items = [], min, max, unique, mini) => {
|
|
259
|
-
if (mini) {
|
|
260
|
-
const checks = buildLengthChecks(min, max);
|
|
261
|
-
if (unique) checks.push(`z.refine(items => new Set(items).size === items.length, { message: "Array entries must be unique" })`);
|
|
262
|
-
if (checks.length > 0) return `z.array(${items?.join("")}).check(${checks.join(", ")})`;
|
|
263
|
-
return `z.array(${items?.join("")})`;
|
|
264
|
-
}
|
|
265
|
-
return [
|
|
266
|
-
`z.array(${items?.join("")})`,
|
|
267
|
-
min !== void 0 ? `.min(${min})` : void 0,
|
|
268
|
-
max !== void 0 ? `.max(${max})` : void 0,
|
|
269
|
-
unique ? `.refine(items => new Set(items).size === items.length, { message: "Array entries must be unique" })` : void 0
|
|
270
|
-
].filter(Boolean).join("");
|
|
271
|
-
},
|
|
272
|
-
tuple: (items = []) => `z.tuple([${items?.join(", ")}])`,
|
|
273
|
-
enum: (items = []) => `z.enum([${items?.join(", ")}])`,
|
|
274
|
-
union: (items = []) => `z.union([${items?.join(", ")}])`,
|
|
275
|
-
const: (value) => `z.literal(${value ?? ""})`,
|
|
276
|
-
datetime: (offset = false, local = false, version = "3", mini) => {
|
|
277
|
-
if (mini) return "z.string()";
|
|
278
|
-
if (offset) return version === "4" ? `z.iso.datetime({ offset: ${offset} })` : `z.string().datetime({ offset: ${offset} })`;
|
|
279
|
-
if (local) return version === "4" ? `z.iso.datetime({ local: ${local} })` : `z.string().datetime({ local: ${local} })`;
|
|
280
|
-
return version === "4" ? "z.iso.datetime()" : "z.string().datetime()";
|
|
281
|
-
},
|
|
282
|
-
date: (type = "string", coercion, version = "3") => {
|
|
283
|
-
if (type === "string") return version === "4" ? "z.iso.date()" : "z.string().date()";
|
|
284
|
-
if (coercion) return "z.coerce.date()";
|
|
285
|
-
return "z.date()";
|
|
286
|
-
},
|
|
287
|
-
time: (type = "string", coercion, version = "3") => {
|
|
288
|
-
if (type === "string") return version === "4" ? "z.iso.time()" : "z.string().time()";
|
|
289
|
-
if (coercion) return "z.coerce.date()";
|
|
290
|
-
return "z.date()";
|
|
291
|
-
},
|
|
292
|
-
uuid: ({ coercion, version = "3", guidType = "uuid", min, max, mini } = {}) => {
|
|
293
|
-
const zodGuidType = version === "4" && guidType === "guid" ? "guid" : "uuid";
|
|
294
|
-
if (mini) {
|
|
295
|
-
const checks = buildLengthChecks(min, max);
|
|
296
|
-
if (checks.length > 0) return `z.${zodGuidType}().check(${checks.join(", ")})`;
|
|
297
|
-
return `z.${zodGuidType}()`;
|
|
298
|
-
}
|
|
299
|
-
const zodV4UuidSchema = `z.${zodGuidType}()`;
|
|
300
|
-
return [
|
|
301
|
-
coercion ? version === "4" ? zodV4UuidSchema : "z.coerce.string().uuid()" : version === "4" ? zodV4UuidSchema : "z.string().uuid()",
|
|
302
|
-
min !== void 0 ? `.min(${min})` : void 0,
|
|
303
|
-
max !== void 0 ? `.max(${max})` : void 0
|
|
304
|
-
].filter(Boolean).join("");
|
|
305
|
-
},
|
|
306
|
-
url: (coercion, version = "3", min, max, mini) => {
|
|
307
|
-
if (mini) {
|
|
308
|
-
const checks = buildLengthChecks(min, max);
|
|
309
|
-
if (checks.length > 0) return `z.url().check(${checks.join(", ")})`;
|
|
310
|
-
return "z.url()";
|
|
311
|
-
}
|
|
312
|
-
return [
|
|
313
|
-
coercion ? version === "4" ? "z.url()" : "z.coerce.string().url()" : version === "4" ? "z.url()" : "z.string().url()",
|
|
314
|
-
min !== void 0 ? `.min(${min})` : void 0,
|
|
315
|
-
max !== void 0 ? `.max(${max})` : void 0
|
|
316
|
-
].filter(Boolean).join("");
|
|
317
|
-
},
|
|
318
|
-
default: (value, innerSchema, mini, isBigInt) => {
|
|
319
|
-
if (mini && innerSchema) return `z._default(${innerSchema}, ${isBigInt && typeof value === "number" ? `BigInt(${value})` : typeof value === "object" ? "{}" : value ?? ""})`;
|
|
320
|
-
if (typeof value === "object") return ".default({})";
|
|
321
|
-
if (value === void 0) return ".default()";
|
|
322
|
-
if (typeof value === "string" && !value) return `.default('')`;
|
|
323
|
-
if (isBigInt && typeof value === "number") return `.default(BigInt(${value}))`;
|
|
324
|
-
return `.default(${value ?? ""})`;
|
|
325
|
-
},
|
|
326
|
-
and: (items = [], mini) => {
|
|
327
|
-
if (mini && items.length > 0) {
|
|
328
|
-
const checks = [];
|
|
329
|
-
for (const item of items) {
|
|
330
|
-
const checkStart = item.indexOf(".check(");
|
|
331
|
-
if (checkStart !== -1) {
|
|
332
|
-
let depth = 0;
|
|
333
|
-
let i = checkStart + 7;
|
|
334
|
-
let checkContent = "";
|
|
335
|
-
while (i < item.length) {
|
|
336
|
-
const char = item[i];
|
|
337
|
-
if (char === "(") depth++;
|
|
338
|
-
else if (char === ")") {
|
|
339
|
-
if (depth === 0) break;
|
|
340
|
-
depth--;
|
|
341
|
-
}
|
|
342
|
-
checkContent += char;
|
|
343
|
-
i++;
|
|
344
|
-
}
|
|
345
|
-
if (checkContent) checks.push(checkContent);
|
|
346
|
-
}
|
|
347
|
-
}
|
|
348
|
-
if (checks.length > 0) return `.check(${checks.join(", ")})`;
|
|
349
|
-
return "";
|
|
350
|
-
}
|
|
351
|
-
return items?.map((item) => `.and(${item})`).join("");
|
|
352
|
-
},
|
|
353
|
-
describe: (value = "", innerSchema, mini) => {
|
|
354
|
-
if (mini) return;
|
|
355
|
-
if (innerSchema) return `z.describe(${innerSchema}, ${value})`;
|
|
356
|
-
return `.describe(${value})`;
|
|
357
|
-
},
|
|
358
|
-
max: void 0,
|
|
359
|
-
min: void 0,
|
|
360
|
-
optional: (value) => {
|
|
361
|
-
if (value) return `z.optional(${value})`;
|
|
362
|
-
return ".optional()";
|
|
363
|
-
},
|
|
364
|
-
matches: (value = "", coercion, mini, min, max) => {
|
|
365
|
-
if (mini) {
|
|
366
|
-
const checks = buildLengthChecks(min, max);
|
|
367
|
-
checks.push(`z.regex(${value})`);
|
|
368
|
-
return `z.string().check(${checks.join(", ")})`;
|
|
369
|
-
}
|
|
370
|
-
return [
|
|
371
|
-
coercion ? "z.coerce.string()" : "z.string()",
|
|
372
|
-
min !== void 0 ? `.min(${min})` : void 0,
|
|
373
|
-
max !== void 0 ? `.max(${max})` : void 0,
|
|
374
|
-
`.regex(${value})`
|
|
375
|
-
].filter(Boolean).join("");
|
|
376
|
-
},
|
|
377
|
-
email: (coercion, version = "3", min, max, mini) => {
|
|
378
|
-
if (mini) {
|
|
379
|
-
const checks = buildLengthChecks(min, max);
|
|
380
|
-
if (checks.length > 0) return `z.email().check(${checks.join(", ")})`;
|
|
381
|
-
return "z.email()";
|
|
382
|
-
}
|
|
383
|
-
return [
|
|
384
|
-
coercion ? version === "4" ? "z.email()" : "z.coerce.string().email()" : version === "4" ? "z.email()" : "z.string().email()",
|
|
385
|
-
min !== void 0 ? `.min(${min})` : void 0,
|
|
386
|
-
max !== void 0 ? `.max(${max})` : void 0
|
|
387
|
-
].filter(Boolean).join("");
|
|
388
|
-
},
|
|
389
|
-
firstName: void 0,
|
|
390
|
-
lastName: void 0,
|
|
391
|
-
password: void 0,
|
|
392
|
-
phone: void 0,
|
|
393
|
-
readOnly: void 0,
|
|
394
|
-
writeOnly: void 0,
|
|
395
|
-
ref: (value) => {
|
|
396
|
-
if (!value) return;
|
|
397
|
-
return `z.lazy(() => ${value})`;
|
|
398
|
-
},
|
|
399
|
-
blob: () => "z.instanceof(File)",
|
|
400
|
-
deprecated: void 0,
|
|
401
|
-
example: void 0,
|
|
402
|
-
schema: void 0,
|
|
403
|
-
catchall: (value, mini) => {
|
|
404
|
-
if (mini) return;
|
|
405
|
-
return value ? `.catchall(${value})` : void 0;
|
|
406
|
-
},
|
|
407
|
-
name: void 0,
|
|
408
|
-
exclusiveMinimum: void 0,
|
|
409
|
-
exclusiveMaximum: void 0
|
|
410
|
-
};
|
|
411
|
-
/**
|
|
412
|
-
* @link based on https://github.com/cellular/oazapfts/blob/7ba226ebb15374e8483cc53e7532f1663179a22c/src/codegen/generate.ts#L398
|
|
413
|
-
*/
|
|
414
|
-
function sort(items) {
|
|
415
|
-
const order = [
|
|
416
|
-
schemaKeywords.string,
|
|
417
|
-
schemaKeywords.datetime,
|
|
418
|
-
schemaKeywords.date,
|
|
419
|
-
schemaKeywords.time,
|
|
420
|
-
schemaKeywords.tuple,
|
|
421
|
-
schemaKeywords.number,
|
|
422
|
-
schemaKeywords.object,
|
|
423
|
-
schemaKeywords.enum,
|
|
424
|
-
schemaKeywords.url,
|
|
425
|
-
schemaKeywords.email,
|
|
426
|
-
schemaKeywords.firstName,
|
|
427
|
-
schemaKeywords.lastName,
|
|
428
|
-
schemaKeywords.password,
|
|
429
|
-
schemaKeywords.matches,
|
|
430
|
-
schemaKeywords.uuid,
|
|
431
|
-
schemaKeywords.null,
|
|
432
|
-
schemaKeywords.min,
|
|
433
|
-
schemaKeywords.max,
|
|
434
|
-
schemaKeywords.default,
|
|
435
|
-
schemaKeywords.describe,
|
|
436
|
-
schemaKeywords.optional,
|
|
437
|
-
schemaKeywords.nullable,
|
|
438
|
-
schemaKeywords.nullish
|
|
439
|
-
];
|
|
440
|
-
if (!items) return [];
|
|
441
|
-
return sortBy(items, [(v) => order.indexOf(v.keyword), "asc"]);
|
|
442
|
-
}
|
|
443
|
-
/**
|
|
444
|
-
* Keywords that represent modifiers for mini mode
|
|
445
|
-
* These are separated from the base schema and wrapped around it
|
|
446
|
-
* Note: describe is included to filter it out, but won't be wrapped (Zod Mini doesn't support describe)
|
|
447
|
-
*/
|
|
448
|
-
const miniModifierKeywords = [
|
|
449
|
-
schemaKeywords.optional,
|
|
450
|
-
schemaKeywords.nullable,
|
|
451
|
-
schemaKeywords.nullish,
|
|
452
|
-
schemaKeywords.default,
|
|
453
|
-
schemaKeywords.describe
|
|
454
|
-
];
|
|
455
|
-
/**
|
|
456
|
-
* Extracts mini mode modifiers from a schemas array
|
|
457
|
-
* This can be reused by other parsers (e.g., valibot) that need similar functionality
|
|
458
|
-
* Note: describe is not included as Zod Mini doesn't support it
|
|
459
|
-
*/
|
|
460
|
-
function extractMiniModifiers(schemas) {
|
|
461
|
-
const defaultSchema = schemas.find((item) => isKeyword(item, schemaKeywords.default));
|
|
462
|
-
const isBigInt = schemas.some((item) => isKeyword(item, schemaKeywords.bigint));
|
|
463
|
-
return {
|
|
464
|
-
hasOptional: schemas.some((item) => isKeyword(item, schemaKeywords.optional)),
|
|
465
|
-
hasNullable: schemas.some((item) => isKeyword(item, schemaKeywords.nullable)),
|
|
466
|
-
hasNullish: schemas.some((item) => isKeyword(item, schemaKeywords.nullish)),
|
|
467
|
-
defaultValue: defaultSchema?.args,
|
|
468
|
-
isBigInt
|
|
469
|
-
};
|
|
470
|
-
}
|
|
471
|
-
/**
|
|
472
|
-
* Filters out modifier keywords from schemas for mini mode base schema parsing
|
|
473
|
-
* This can be reused by other parsers (e.g., valibot) that need similar functionality
|
|
474
|
-
*/
|
|
475
|
-
function filterMiniModifiers(schemas) {
|
|
476
|
-
return schemas.filter((item) => !miniModifierKeywords.some((keyword) => isKeyword(item, keyword)));
|
|
477
|
-
}
|
|
478
|
-
/**
|
|
479
|
-
* Wraps an output string with Zod Mini functional modifiers
|
|
480
|
-
* Order: default (innermost) -> nullable -> optional (outermost)
|
|
481
|
-
* OR: default -> nullish
|
|
482
|
-
* Note: describe is not supported in Zod Mini and is skipped
|
|
483
|
-
*/
|
|
484
|
-
function wrapWithMiniModifiers(output, modifiers) {
|
|
485
|
-
let result = output;
|
|
486
|
-
if (modifiers.defaultValue !== void 0) result = zodKeywordMapper.default(modifiers.defaultValue, result, true, modifiers.isBigInt);
|
|
487
|
-
if (modifiers.hasNullish) result = zodKeywordMapper.nullish(result);
|
|
488
|
-
else {
|
|
489
|
-
if (modifiers.hasNullable) result = zodKeywordMapper.nullable(result);
|
|
490
|
-
if (modifiers.hasOptional) result = zodKeywordMapper.optional(result);
|
|
491
|
-
}
|
|
492
|
-
return result;
|
|
493
|
-
}
|
|
494
|
-
const shouldCoerce = (coercion, type) => {
|
|
495
|
-
if (coercion === void 0) return false;
|
|
496
|
-
if (typeof coercion === "boolean") return coercion;
|
|
497
|
-
return !!coercion[type];
|
|
498
|
-
};
|
|
499
|
-
const parse = createParser({
|
|
500
|
-
mapper: zodKeywordMapper,
|
|
501
|
-
handlers: {
|
|
502
|
-
union(tree, options) {
|
|
503
|
-
const { current, schema, parent, name, siblings } = tree;
|
|
504
|
-
if (Array.isArray(current.args) && current.args.length === 1) return this.parse({
|
|
505
|
-
schema,
|
|
506
|
-
parent,
|
|
507
|
-
name,
|
|
508
|
-
current: current.args[0],
|
|
509
|
-
siblings
|
|
510
|
-
}, options);
|
|
511
|
-
if (Array.isArray(current.args) && !current.args.length) return "";
|
|
512
|
-
return zodKeywordMapper.union(sort(current.args).map((it, _index, siblings) => this.parse({
|
|
513
|
-
schema,
|
|
514
|
-
parent: current,
|
|
515
|
-
name,
|
|
516
|
-
current: it,
|
|
517
|
-
siblings
|
|
518
|
-
}, options)).filter(Boolean));
|
|
519
|
-
},
|
|
520
|
-
and(tree, options) {
|
|
521
|
-
const { current, schema, name } = tree;
|
|
522
|
-
const items = sort(current.args).filter((schema) => {
|
|
523
|
-
return ![schemaKeywords.optional, schemaKeywords.describe].includes(schema.keyword);
|
|
524
|
-
}).map((it, _index, siblings) => this.parse({
|
|
525
|
-
schema,
|
|
526
|
-
parent: current,
|
|
527
|
-
name,
|
|
528
|
-
current: it,
|
|
529
|
-
siblings
|
|
530
|
-
}, options)).filter(Boolean);
|
|
531
|
-
return `${items.slice(0, 1)}${zodKeywordMapper.and(items.slice(1), options.mini)}`;
|
|
532
|
-
},
|
|
533
|
-
array(tree, options) {
|
|
534
|
-
const { current, schema, name } = tree;
|
|
535
|
-
return zodKeywordMapper.array(sort(current.args.items).map((it, _index, siblings) => {
|
|
536
|
-
return this.parse({
|
|
537
|
-
schema,
|
|
538
|
-
parent: current,
|
|
539
|
-
name,
|
|
540
|
-
current: it,
|
|
541
|
-
siblings
|
|
542
|
-
}, options);
|
|
543
|
-
}).filter(Boolean), current.args.min, current.args.max, current.args.unique, options.mini);
|
|
544
|
-
},
|
|
545
|
-
enum(tree, options) {
|
|
546
|
-
const { current, schema, name } = tree;
|
|
547
|
-
if (current.args.asConst) {
|
|
548
|
-
if (current.args.items.length === 1) {
|
|
549
|
-
const child = {
|
|
550
|
-
keyword: schemaKeywords.const,
|
|
551
|
-
args: current.args.items[0]
|
|
552
|
-
};
|
|
553
|
-
return this.parse({
|
|
554
|
-
schema,
|
|
555
|
-
parent: current,
|
|
556
|
-
name,
|
|
557
|
-
current: child,
|
|
558
|
-
siblings: [child]
|
|
559
|
-
}, options);
|
|
560
|
-
}
|
|
561
|
-
return zodKeywordMapper.union(current.args.items.map((schema) => ({
|
|
562
|
-
keyword: schemaKeywords.const,
|
|
563
|
-
args: schema
|
|
564
|
-
})).map((it, _index, siblings) => {
|
|
565
|
-
return this.parse({
|
|
566
|
-
schema,
|
|
567
|
-
parent: current,
|
|
568
|
-
name,
|
|
569
|
-
current: it,
|
|
570
|
-
siblings
|
|
571
|
-
}, options);
|
|
572
|
-
}).filter(Boolean));
|
|
573
|
-
}
|
|
574
|
-
return zodKeywordMapper.enum(current.args.items.map((schema) => {
|
|
575
|
-
if (schema.format === "boolean") return stringify(schema.value);
|
|
576
|
-
if (schema.format === "number") return stringify(schema.value);
|
|
577
|
-
return stringify(schema.value);
|
|
578
|
-
}));
|
|
579
|
-
},
|
|
580
|
-
ref(tree, options) {
|
|
581
|
-
const { current } = tree;
|
|
582
|
-
if (options.skipLazyForRefs) return current.args?.name;
|
|
583
|
-
return zodKeywordMapper.ref(current.args?.name);
|
|
584
|
-
},
|
|
585
|
-
object(tree, options) {
|
|
586
|
-
const { current, schema, name } = tree;
|
|
587
|
-
const properties = Object.entries(current.args?.properties || {}).filter((item) => {
|
|
588
|
-
const schema = item[1];
|
|
589
|
-
return schema && typeof schema.map === "function";
|
|
590
|
-
}).map(([propertyName, schemas]) => {
|
|
591
|
-
const nameSchema = schemas.find((it) => it.keyword === schemaKeywords.name);
|
|
592
|
-
const isNullable = schemas.some((it) => isKeyword(it, schemaKeywords.nullable));
|
|
593
|
-
const isNullish = schemas.some((it) => isKeyword(it, schemaKeywords.nullish));
|
|
594
|
-
const isOptional = schemas.some((it) => isKeyword(it, schemaKeywords.optional));
|
|
595
|
-
const hasRef = !!SchemaGenerator.find(schemas, schemaKeywords.ref);
|
|
596
|
-
const mappedName = nameSchema?.args || propertyName;
|
|
597
|
-
if (options.mapper && Object.hasOwn(options.mapper, mappedName)) return `"${propertyName}": ${options.mapper?.[mappedName]}`;
|
|
598
|
-
const baseSchemaOutput = sort(schemas).filter((schema) => {
|
|
599
|
-
return !isKeyword(schema, schemaKeywords.optional) && !isKeyword(schema, schemaKeywords.nullable) && !isKeyword(schema, schemaKeywords.nullish);
|
|
600
|
-
}).map((it) => {
|
|
601
|
-
const skipLazyForRefs = options.version === "4" && hasRef;
|
|
602
|
-
return this.parse({
|
|
603
|
-
schema,
|
|
604
|
-
parent: current,
|
|
605
|
-
name,
|
|
606
|
-
current: it,
|
|
607
|
-
siblings: schemas
|
|
608
|
-
}, {
|
|
609
|
-
...options,
|
|
610
|
-
skipLazyForRefs
|
|
611
|
-
});
|
|
612
|
-
}).filter(Boolean).join("");
|
|
613
|
-
const objectValue = options.wrapOutput ? options.wrapOutput({
|
|
614
|
-
output: baseSchemaOutput,
|
|
615
|
-
schema: schema?.properties?.[propertyName]
|
|
616
|
-
}) || baseSchemaOutput : baseSchemaOutput;
|
|
617
|
-
if (options.version === "4" && hasRef) {
|
|
618
|
-
if (options.mini) {
|
|
619
|
-
if (isNullish) return `get "${propertyName}"(){
|
|
620
|
-
return ${zodKeywordMapper.nullish(objectValue)}
|
|
621
|
-
}`;
|
|
622
|
-
if (isOptional) return `get "${propertyName}"(){
|
|
623
|
-
return ${zodKeywordMapper.optional(objectValue)}
|
|
624
|
-
}`;
|
|
625
|
-
if (isNullable) return `get "${propertyName}"(){
|
|
626
|
-
return ${zodKeywordMapper.nullable(objectValue)}
|
|
627
|
-
}`;
|
|
628
|
-
return `get "${propertyName}"(){
|
|
629
|
-
return ${objectValue}
|
|
630
|
-
}`;
|
|
631
|
-
}
|
|
632
|
-
if (isNullish) return `get "${propertyName}"(){
|
|
633
|
-
return ${objectValue}${zodKeywordMapper.nullish()}
|
|
634
|
-
}`;
|
|
635
|
-
if (isOptional) return `get "${propertyName}"(){
|
|
636
|
-
return ${objectValue}${zodKeywordMapper.optional()}
|
|
637
|
-
}`;
|
|
638
|
-
if (isNullable) return `get "${propertyName}"(){
|
|
639
|
-
return ${objectValue}${zodKeywordMapper.nullable()}
|
|
640
|
-
}`;
|
|
641
|
-
return `get "${propertyName}"(){
|
|
642
|
-
return ${objectValue}
|
|
643
|
-
}`;
|
|
644
|
-
}
|
|
645
|
-
if (isNullish && options.mini) return `"${propertyName}": ${zodKeywordMapper.nullish(objectValue)}`;
|
|
646
|
-
if (isNullish && !options.mini) return `"${propertyName}": ${objectValue}${zodKeywordMapper.nullish()}`;
|
|
647
|
-
if (isOptional) return `"${propertyName}": ${zodKeywordMapper.optional(objectValue)}`;
|
|
648
|
-
if (isNullable) return `"${propertyName}": ${zodKeywordMapper.nullable(objectValue)}`;
|
|
649
|
-
return `"${propertyName}": ${objectValue}`;
|
|
650
|
-
}).join(",\n");
|
|
651
|
-
const additionalProperties = current.args?.additionalProperties?.length ? current.args.additionalProperties.map((it, _index, siblings) => this.parse({
|
|
652
|
-
schema,
|
|
653
|
-
parent: current,
|
|
654
|
-
name,
|
|
655
|
-
current: it,
|
|
656
|
-
siblings
|
|
657
|
-
}, options)).filter(Boolean).join("") : void 0;
|
|
658
|
-
return [zodKeywordMapper.object(properties, current.args?.strict, options.version), additionalProperties ? zodKeywordMapper.catchall(additionalProperties, options.mini) : void 0].filter(Boolean).join("");
|
|
659
|
-
},
|
|
660
|
-
tuple(tree, options) {
|
|
661
|
-
const { current, schema, name } = tree;
|
|
662
|
-
return zodKeywordMapper.tuple(current.args.items.map((it, _index, siblings) => this.parse({
|
|
663
|
-
schema,
|
|
664
|
-
parent: current,
|
|
665
|
-
name,
|
|
666
|
-
current: it,
|
|
667
|
-
siblings
|
|
668
|
-
}, options)).filter(Boolean));
|
|
669
|
-
},
|
|
670
|
-
const(tree, _options) {
|
|
671
|
-
const { current } = tree;
|
|
672
|
-
if (current.args.format === "number" && current.args.value !== void 0) return zodKeywordMapper.const(Number(current.args.value));
|
|
673
|
-
if (current.args.format === "boolean" && current.args.value !== void 0) return zodKeywordMapper.const(typeof current.args.value === "boolean" ? current.args.value : void 0);
|
|
674
|
-
return zodKeywordMapper.const(stringify(current.args.value));
|
|
675
|
-
},
|
|
676
|
-
matches(tree, options) {
|
|
677
|
-
const { current, siblings } = tree;
|
|
678
|
-
if (siblings.some((it) => isKeyword(it, schemaKeywords.ref))) return;
|
|
679
|
-
const minSchema = findSchemaKeyword(siblings, "min");
|
|
680
|
-
const maxSchema = findSchemaKeyword(siblings, "max");
|
|
681
|
-
if (current.args) return zodKeywordMapper.matches(toRegExpString(current.args, null), shouldCoerce(options.coercion, "strings"), options.mini, minSchema?.args, maxSchema?.args);
|
|
682
|
-
},
|
|
683
|
-
default(tree, options) {
|
|
684
|
-
const { current, siblings } = tree;
|
|
685
|
-
if (options.mini) return;
|
|
686
|
-
const isBigInt = siblings.some((it) => isKeyword(it, schemaKeywords.bigint));
|
|
687
|
-
if (current.args !== void 0) return zodKeywordMapper.default(current.args, void 0, void 0, isBigInt);
|
|
688
|
-
return zodKeywordMapper.default();
|
|
689
|
-
},
|
|
690
|
-
describe(tree, options) {
|
|
691
|
-
const { current } = tree;
|
|
692
|
-
if (current.args) return zodKeywordMapper.describe(stringify(current.args.toString()), void 0, options.mini);
|
|
693
|
-
},
|
|
694
|
-
string(tree, options) {
|
|
695
|
-
const { siblings } = tree;
|
|
696
|
-
const minSchema = findSchemaKeyword(siblings, "min");
|
|
697
|
-
const maxSchema = findSchemaKeyword(siblings, "max");
|
|
698
|
-
return zodKeywordMapper.string(shouldCoerce(options.coercion, "strings"), minSchema?.args, maxSchema?.args, options.mini);
|
|
699
|
-
},
|
|
700
|
-
uuid(tree, options) {
|
|
701
|
-
const { siblings } = tree;
|
|
702
|
-
const minSchema = findSchemaKeyword(siblings, "min");
|
|
703
|
-
const maxSchema = findSchemaKeyword(siblings, "max");
|
|
704
|
-
return zodKeywordMapper.uuid({
|
|
705
|
-
coercion: shouldCoerce(options.coercion, "strings"),
|
|
706
|
-
version: options.version,
|
|
707
|
-
guidType: options.guidType,
|
|
708
|
-
min: minSchema?.args,
|
|
709
|
-
max: maxSchema?.args,
|
|
710
|
-
mini: options.mini
|
|
711
|
-
});
|
|
712
|
-
},
|
|
713
|
-
email(tree, options) {
|
|
714
|
-
const { siblings } = tree;
|
|
715
|
-
const minSchema = findSchemaKeyword(siblings, "min");
|
|
716
|
-
const maxSchema = findSchemaKeyword(siblings, "max");
|
|
717
|
-
return zodKeywordMapper.email(shouldCoerce(options.coercion, "strings"), options.version, minSchema?.args, maxSchema?.args, options.mini);
|
|
718
|
-
},
|
|
719
|
-
url(tree, options) {
|
|
720
|
-
const { siblings } = tree;
|
|
721
|
-
const minSchema = findSchemaKeyword(siblings, "min");
|
|
722
|
-
const maxSchema = findSchemaKeyword(siblings, "max");
|
|
723
|
-
return zodKeywordMapper.url(shouldCoerce(options.coercion, "strings"), options.version, minSchema?.args, maxSchema?.args, options.mini);
|
|
724
|
-
},
|
|
725
|
-
number(tree, options) {
|
|
726
|
-
const { siblings } = tree;
|
|
727
|
-
const minSchema = findSchemaKeyword(siblings, "min");
|
|
728
|
-
const maxSchema = findSchemaKeyword(siblings, "max");
|
|
729
|
-
const exclusiveMinimumSchema = findSchemaKeyword(siblings, "exclusiveMinimum");
|
|
730
|
-
const exclusiveMaximumSchema = findSchemaKeyword(siblings, "exclusiveMaximum");
|
|
731
|
-
return zodKeywordMapper.number(shouldCoerce(options.coercion, "numbers"), minSchema?.args, maxSchema?.args, exclusiveMinimumSchema?.args, exclusiveMaximumSchema?.args, options.mini);
|
|
732
|
-
},
|
|
733
|
-
integer(tree, options) {
|
|
734
|
-
const { siblings } = tree;
|
|
735
|
-
const minSchema = findSchemaKeyword(siblings, "min");
|
|
736
|
-
const maxSchema = findSchemaKeyword(siblings, "max");
|
|
737
|
-
const exclusiveMinimumSchema = findSchemaKeyword(siblings, "exclusiveMinimum");
|
|
738
|
-
const exclusiveMaximumSchema = findSchemaKeyword(siblings, "exclusiveMaximum");
|
|
739
|
-
return zodKeywordMapper.integer(shouldCoerce(options.coercion, "numbers"), minSchema?.args, maxSchema?.args, options.version, exclusiveMinimumSchema?.args, exclusiveMaximumSchema?.args, options.mini);
|
|
740
|
-
},
|
|
741
|
-
bigint(_tree, options) {
|
|
742
|
-
return zodKeywordMapper.bigint(shouldCoerce(options.coercion, "numbers"));
|
|
743
|
-
},
|
|
744
|
-
datetime(tree, options) {
|
|
745
|
-
const { current } = tree;
|
|
746
|
-
return zodKeywordMapper.datetime(current.args.offset, current.args.local, options.version, options.mini);
|
|
747
|
-
},
|
|
748
|
-
date(tree, options) {
|
|
749
|
-
const { current } = tree;
|
|
750
|
-
return zodKeywordMapper.date(current.args.type, shouldCoerce(options.coercion, "dates"), options.version);
|
|
751
|
-
},
|
|
752
|
-
time(tree, options) {
|
|
753
|
-
const { current } = tree;
|
|
754
|
-
return zodKeywordMapper.time(current.args.type, shouldCoerce(options.coercion, "dates"), options.version);
|
|
755
|
-
}
|
|
756
|
-
}
|
|
757
|
-
});
|
|
758
|
-
//#endregion
|
|
759
|
-
//#region src/components/Zod.tsx
|
|
760
|
-
function Zod({ name, typeName, tree, schema, inferTypeName, mapper, coercion, keysToOmit, description, wrapOutput, version, guidType, emptySchemaType, mini = false }) {
|
|
761
|
-
const hasTuple = !!SchemaGenerator.find(tree, schemaKeywords.tuple);
|
|
762
|
-
const schemas = sort(tree).filter((item) => {
|
|
763
|
-
if (hasTuple && (isKeyword(item, schemaKeywords.min) || isKeyword(item, schemaKeywords.max))) return false;
|
|
764
|
-
return true;
|
|
765
|
-
});
|
|
766
|
-
const baseSchemas = mini ? filterMiniModifiers(schemas) : schemas;
|
|
767
|
-
const output = baseSchemas.map((schemaKeyword, index) => {
|
|
768
|
-
return parse({
|
|
769
|
-
schema,
|
|
770
|
-
parent: void 0,
|
|
771
|
-
current: schemaKeyword,
|
|
772
|
-
siblings: baseSchemas.filter((_, i) => i !== index),
|
|
773
|
-
name
|
|
774
|
-
}, {
|
|
775
|
-
mapper,
|
|
776
|
-
coercion,
|
|
777
|
-
wrapOutput,
|
|
778
|
-
version,
|
|
779
|
-
guidType,
|
|
780
|
-
mini
|
|
781
|
-
});
|
|
782
|
-
}).filter(Boolean).join("");
|
|
783
|
-
let suffix = "";
|
|
784
|
-
const firstSchema = schemas.at(0);
|
|
785
|
-
const lastSchema = schemas.at(-1);
|
|
786
|
-
if (!mini && lastSchema && isKeyword(lastSchema, schemaKeywords.nullable)) if (firstSchema && isKeyword(firstSchema, schemaKeywords.ref)) if (version === "3") suffix = ".unwrap().schema.unwrap()";
|
|
787
|
-
else suffix = ".unwrap().unwrap()";
|
|
788
|
-
else suffix = ".unwrap()";
|
|
789
|
-
else if (!mini) {
|
|
790
|
-
if (firstSchema && isKeyword(firstSchema, schemaKeywords.ref)) if (version === "3") suffix = ".schema";
|
|
791
|
-
else suffix = ".unwrap()";
|
|
792
|
-
}
|
|
793
|
-
const emptyValue = parse({
|
|
794
|
-
schema,
|
|
795
|
-
parent: void 0,
|
|
796
|
-
current: { keyword: schemaKeywords[emptySchemaType] },
|
|
797
|
-
siblings: []
|
|
798
|
-
}, {
|
|
799
|
-
mapper,
|
|
800
|
-
coercion,
|
|
801
|
-
wrapOutput,
|
|
802
|
-
version,
|
|
803
|
-
guidType,
|
|
804
|
-
mini
|
|
805
|
-
});
|
|
806
|
-
let baseSchemaOutput = [output, keysToOmit?.length ? `${suffix}.omit({ ${keysToOmit.map((key) => `'${key}': true`).join(",")} })` : void 0].filter(Boolean).join("") || emptyValue || "";
|
|
807
|
-
if (mini) baseSchemaOutput = wrapWithMiniModifiers(baseSchemaOutput, extractMiniModifiers(schemas));
|
|
808
|
-
const wrappedSchemaOutput = wrapOutput ? wrapOutput({
|
|
809
|
-
output: baseSchemaOutput,
|
|
810
|
-
schema
|
|
811
|
-
}) || baseSchemaOutput : baseSchemaOutput;
|
|
812
|
-
const finalOutput = typeName ? `${wrappedSchemaOutput} as unknown as ${version === "4" ? "z.ZodType" : "ToZod"}<${typeName}>` : wrappedSchemaOutput;
|
|
813
|
-
return /* @__PURE__ */ jsxs(Fragment, { children: [/* @__PURE__ */ jsx(File.Source, {
|
|
814
|
-
name,
|
|
815
|
-
isExportable: true,
|
|
816
|
-
isIndexable: true,
|
|
817
|
-
children: /* @__PURE__ */ jsx(Const, {
|
|
818
|
-
export: true,
|
|
819
|
-
name,
|
|
820
|
-
JSDoc: { comments: [description ? `@description ${jsStringEscape(description)}` : void 0].filter(Boolean) },
|
|
821
|
-
children: finalOutput
|
|
822
|
-
})
|
|
823
|
-
}), inferTypeName && /* @__PURE__ */ jsxs(File.Source, {
|
|
824
|
-
name: inferTypeName,
|
|
825
|
-
isExportable: true,
|
|
826
|
-
isIndexable: true,
|
|
827
|
-
isTypeOnly: true,
|
|
828
|
-
children: [typeName && /* @__PURE__ */ jsx(Type, {
|
|
829
|
-
export: true,
|
|
830
|
-
name: inferTypeName,
|
|
831
|
-
children: typeName
|
|
832
|
-
}), !typeName && /* @__PURE__ */ jsx(Type, {
|
|
833
|
-
export: true,
|
|
834
|
-
name: inferTypeName,
|
|
835
|
-
children: `z.infer<typeof ${name}>`
|
|
836
|
-
})]
|
|
837
|
-
})] });
|
|
838
|
-
}
|
|
839
|
-
//#endregion
|
|
840
|
-
export { Operations as n, Zod as t };
|
|
841
|
-
|
|
842
|
-
//# sourceMappingURL=components-eECfXVou.js.map
|