@daydreamui/figma 0.0.0-stage → 0.3.0
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/README.md +206 -2
- package/dist/bridge.js +4613 -0
- package/dist/index.js +1545 -0
- package/icon.svg +10 -0
- package/manifest.json +35 -0
- package/package.json +15 -4
package/dist/bridge.js
ADDED
|
@@ -0,0 +1,4613 @@
|
|
|
1
|
+
import { lstat, mkdir, writeFile } from "node:fs/promises";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
var ENTITIES = {
|
|
4
|
+
""": "\"",
|
|
5
|
+
"'": "'",
|
|
6
|
+
"<": "<",
|
|
7
|
+
">": ">",
|
|
8
|
+
"&": "&"
|
|
9
|
+
};
|
|
10
|
+
var unescapeXml = (text) => text.replace(/&(?:quot|apos|lt|gt|amp);|&#(\d+);|&#x([0-9a-fA-F]+);/g, (match, dec, hex) => dec !== void 0 ? String.fromCodePoint(Number(dec)) : hex !== void 0 ? String.fromCodePoint(parseInt(hex, 16)) : ENTITIES[match] ?? match);
|
|
11
|
+
var sizeOf = (value) => {
|
|
12
|
+
if (value === void 0) return null;
|
|
13
|
+
const n = Number(value);
|
|
14
|
+
return Number.isFinite(n) && n > 0 ? n : null;
|
|
15
|
+
};
|
|
16
|
+
function frameOf(xml) {
|
|
17
|
+
const root = /^\s*<([A-Za-z][\w-]*)\b([^>]*?)\/?>/.exec(xml);
|
|
18
|
+
if (root === null) return listedFrame(xml);
|
|
19
|
+
const attributes = new Map();
|
|
20
|
+
for (const match of root[2].matchAll(/(?:^|\s)([\w:-]+)\s*=\s*"([^"]*)"/g)) attributes.set(match[1], unescapeXml(match[2]));
|
|
21
|
+
const id = attributes.get("id");
|
|
22
|
+
const name = (attributes.get("name") ?? "").replace(/\p{Cc}+/gu, " ").trim().slice(0, 80).trimEnd();
|
|
23
|
+
return {
|
|
24
|
+
id: id === void 0 || id === "" ? null : id,
|
|
25
|
+
name: name === "" ? null : name,
|
|
26
|
+
width: sizeOf(attributes.get("width")),
|
|
27
|
+
height: sizeOf(attributes.get("height"))
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
var listedFrame = (text) => selectionOf(text)[0] ?? null;
|
|
31
|
+
function selectionOf(text) {
|
|
32
|
+
if (!/^\s*Currently selected nodes\b/.test(text)) return [];
|
|
33
|
+
const frames = [];
|
|
34
|
+
for (const line of text.matchAll(/^\s*-\s+([\w:;-]+):[ \t]*(.*)$/gm)) {
|
|
35
|
+
const name = line[2].replace(/\p{Cc}+/gu, " ").trim().slice(0, 80).trimEnd();
|
|
36
|
+
frames.push({
|
|
37
|
+
id: line[1],
|
|
38
|
+
name: name === "" ? null : name,
|
|
39
|
+
width: null,
|
|
40
|
+
height: null
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
return frames;
|
|
44
|
+
}
|
|
45
|
+
var NODE_ID = /^\d+[:-]\d+$/;
|
|
46
|
+
function nodeIdFromUrl(urlOrId) {
|
|
47
|
+
const given = urlOrId.trim();
|
|
48
|
+
if (NODE_ID.test(given)) return given.replace("-", ":");
|
|
49
|
+
let url;
|
|
50
|
+
try {
|
|
51
|
+
url = new URL(given);
|
|
52
|
+
} catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
const id = url.searchParams.get("node-id");
|
|
56
|
+
return id !== null && NODE_ID.test(id) ? id.replace("-", ":") : null;
|
|
57
|
+
}
|
|
58
|
+
var NOT_REACHABLE = "Figma is not reachable: open Figma desktop and enable the Dev Mode MCP server in its preferences";
|
|
59
|
+
var TimeoutError = class extends Error {
|
|
60
|
+
constructor(timeoutMs) {
|
|
61
|
+
super(`Figma did not answer within ${Math.round(timeoutMs / 1e3)} s`);
|
|
62
|
+
this.name = "TimeoutError";
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
var UnreachableError = class extends Error {
|
|
66
|
+
constructor(message, cause) {
|
|
67
|
+
super(message, { cause });
|
|
68
|
+
this.name = "UnreachableError";
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
var MAX_STREAM_BYTES = 33554432;
|
|
72
|
+
var OversizeError = class extends Error {
|
|
73
|
+
constructor() {
|
|
74
|
+
super(`Figma streamed more than ${MAX_STREAM_BYTES / 1048576} MB without answering; the answer was dropped`);
|
|
75
|
+
this.name = "OversizeError";
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
async function post(options, message, wantId, unreachable) {
|
|
79
|
+
const headers = {
|
|
80
|
+
"content-type": "application/json",
|
|
81
|
+
accept: "application/json, text/event-stream"
|
|
82
|
+
};
|
|
83
|
+
if (options.session !== null) headers["mcp-session-id"] = options.session;
|
|
84
|
+
if (options.protocolVersion !== null) headers["MCP-Protocol-Version"] = options.protocolVersion;
|
|
85
|
+
const signal = AbortSignal.timeout(options.timeoutMs);
|
|
86
|
+
let response;
|
|
87
|
+
try {
|
|
88
|
+
response = await options.fetch(options.url, {
|
|
89
|
+
method: "POST",
|
|
90
|
+
headers,
|
|
91
|
+
body: JSON.stringify(message),
|
|
92
|
+
signal
|
|
93
|
+
});
|
|
94
|
+
} catch (error) {
|
|
95
|
+
throw signal.aborted ? new TimeoutError(options.timeoutMs) : new UnreachableError(unreachable, error);
|
|
96
|
+
}
|
|
97
|
+
const session = response.headers.get("mcp-session-id");
|
|
98
|
+
const type = response.headers.get("content-type") ?? "";
|
|
99
|
+
let messages = [];
|
|
100
|
+
try {
|
|
101
|
+
if (type.startsWith("text/event-stream") && response.body !== null) messages = await readEvents(response.body, wantId);
|
|
102
|
+
else if (response.status !== 202 && response.status !== 204) messages = parseMessages(await response.text());
|
|
103
|
+
} catch (error) {
|
|
104
|
+
if (signal.aborted) throw new TimeoutError(options.timeoutMs);
|
|
105
|
+
if (error instanceof OversizeError) throw error;
|
|
106
|
+
throw new UnreachableError(unreachable, error);
|
|
107
|
+
}
|
|
108
|
+
return {
|
|
109
|
+
status: response.status,
|
|
110
|
+
session,
|
|
111
|
+
messages
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
function parseMessages(text) {
|
|
115
|
+
let parsed;
|
|
116
|
+
try {
|
|
117
|
+
parsed = JSON.parse(text);
|
|
118
|
+
} catch {
|
|
119
|
+
return [];
|
|
120
|
+
}
|
|
121
|
+
return (Array.isArray(parsed) ? parsed : [parsed]).filter(isResponse);
|
|
122
|
+
}
|
|
123
|
+
var isResponse = (value) => typeof value === "object" && value !== null && value.jsonrpc === "2.0" && "id" in value && !("method" in value) && ("result" in value || "error" in value);
|
|
124
|
+
function dataOf(event) {
|
|
125
|
+
const lines = event.split("\n").filter((line) => line.startsWith("data:")).map((line) => line.slice(5).replace(/^ /, ""));
|
|
126
|
+
return lines.length === 0 ? null : lines.join("\n");
|
|
127
|
+
}
|
|
128
|
+
async function readEvents(body, wantId) {
|
|
129
|
+
const reader = body.getReader();
|
|
130
|
+
const decoder = new TextDecoder();
|
|
131
|
+
const messages = [];
|
|
132
|
+
let buffered = "";
|
|
133
|
+
let searched = 0;
|
|
134
|
+
let received = 0;
|
|
135
|
+
let heldCr = false;
|
|
136
|
+
const take = (event) => {
|
|
137
|
+
const data = dataOf(event);
|
|
138
|
+
if (data === null) return false;
|
|
139
|
+
const read = parseMessages(data);
|
|
140
|
+
messages.push(...read);
|
|
141
|
+
return wantId !== null && read.some((m) => m.id === wantId);
|
|
142
|
+
};
|
|
143
|
+
try {
|
|
144
|
+
for (;;) {
|
|
145
|
+
const { value, done } = await reader.read();
|
|
146
|
+
received += value?.byteLength ?? 0;
|
|
147
|
+
if (received > 33554432) {
|
|
148
|
+
await reader.cancel().catch(() => {});
|
|
149
|
+
throw new OversizeError();
|
|
150
|
+
}
|
|
151
|
+
let text = (heldCr ? "\r" : "") + decoder.decode(value, { stream: !done });
|
|
152
|
+
heldCr = !done && text.endsWith("\r");
|
|
153
|
+
if (heldCr) text = text.slice(0, -1);
|
|
154
|
+
buffered += text.replace(/\r\n?/g, "\n");
|
|
155
|
+
let end;
|
|
156
|
+
while ((end = buffered.indexOf("\n\n", searched)) !== -1) {
|
|
157
|
+
const event = buffered.slice(0, end);
|
|
158
|
+
buffered = buffered.slice(end + 2);
|
|
159
|
+
searched = 0;
|
|
160
|
+
if (take(event)) {
|
|
161
|
+
await reader.cancel().catch(() => {});
|
|
162
|
+
return messages;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
searched = Math.max(0, buffered.length - 1);
|
|
166
|
+
if (done) {
|
|
167
|
+
if (buffered.trim() !== "") take(buffered);
|
|
168
|
+
return messages;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
} finally {
|
|
172
|
+
reader.releaseLock();
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
var PROTOCOL_VERSION = "2025-06-18";
|
|
176
|
+
var DEFAULT_TIMEOUT_MS = 2e4;
|
|
177
|
+
var DEFAULT_BUDGET_MS = 45e3;
|
|
178
|
+
var isContentItem = (value) => typeof value === "object" && value !== null && typeof value.type === "string";
|
|
179
|
+
var textsOf = (content) => content.flatMap((item) => item.type === "text" && typeof item.text === "string" ? [item.text] : []);
|
|
180
|
+
var imageOf = (content) => {
|
|
181
|
+
for (const item of content) {
|
|
182
|
+
if (item.type !== "image") continue;
|
|
183
|
+
const { data, mimeType } = item;
|
|
184
|
+
if (typeof data === "string") return {
|
|
185
|
+
mimeType: typeof mimeType === "string" ? mimeType : "image/png",
|
|
186
|
+
base64: data
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
return null;
|
|
190
|
+
};
|
|
191
|
+
var textResult = (content) => {
|
|
192
|
+
const [text = "", ...notes] = textsOf(content);
|
|
193
|
+
return {
|
|
194
|
+
text,
|
|
195
|
+
notes
|
|
196
|
+
};
|
|
197
|
+
};
|
|
198
|
+
var xmlResult = (content) => {
|
|
199
|
+
const texts = textsOf(content);
|
|
200
|
+
const at = texts.findIndex((text) => /^\s*</.test(text));
|
|
201
|
+
if (at === -1) return textResult(content);
|
|
202
|
+
return {
|
|
203
|
+
text: texts[at],
|
|
204
|
+
notes: texts.filter((_, i) => i !== at)
|
|
205
|
+
};
|
|
206
|
+
};
|
|
207
|
+
var acknowledged = (status) => status >= 200 && status < 300;
|
|
208
|
+
var failedRead = (error) => error instanceof TimeoutError ? { kind: "late" } : {
|
|
209
|
+
kind: "unreachable",
|
|
210
|
+
said: error instanceof Error ? error.message : String(error)
|
|
211
|
+
};
|
|
212
|
+
function connectFigma(options = {}) {
|
|
213
|
+
const settings = {
|
|
214
|
+
fetch: options.fetch ?? fetch,
|
|
215
|
+
url: options.url ?? "http://127.0.0.1:3845/mcp",
|
|
216
|
+
timeoutMs: options.timeoutMs ?? DEFAULT_TIMEOUT_MS,
|
|
217
|
+
budgetMs: options.budgetMs ?? DEFAULT_BUDGET_MS
|
|
218
|
+
};
|
|
219
|
+
const client = {
|
|
220
|
+
clientLanguages: options.clientLanguages ?? "html,css",
|
|
221
|
+
clientFrameworks: options.clientFrameworks ?? "unknown"
|
|
222
|
+
};
|
|
223
|
+
let nextId = 0;
|
|
224
|
+
let session = null;
|
|
225
|
+
const request = (method, params) => ({
|
|
226
|
+
jsonrpc: "2.0",
|
|
227
|
+
id: ++nextId,
|
|
228
|
+
method,
|
|
229
|
+
params
|
|
230
|
+
});
|
|
231
|
+
const statusError = (answer, method) => new Error(answer.messages.find((m) => m.error !== void 0)?.error?.message ?? `Figma's Dev Mode server answered ${method} with HTTP ${answer.status}`);
|
|
232
|
+
function resultOf(answer, id, method) {
|
|
233
|
+
const response = answer.messages.find((m) => m.id === id);
|
|
234
|
+
if (response?.error !== void 0) throw new Error(response.error.message);
|
|
235
|
+
if (response !== void 0 && answer.status === 200) return response.result;
|
|
236
|
+
throw statusError(answer, method);
|
|
237
|
+
}
|
|
238
|
+
const budget = (ms = settings.budgetMs) => ({
|
|
239
|
+
deadline: Date.now() + ms,
|
|
240
|
+
ms
|
|
241
|
+
});
|
|
242
|
+
function bound(budget) {
|
|
243
|
+
const left = budget.deadline - Date.now();
|
|
244
|
+
if (left <= 0) throw new TimeoutError(budget.ms);
|
|
245
|
+
return Math.min(settings.timeoutMs, left);
|
|
246
|
+
}
|
|
247
|
+
function within(pending, budget) {
|
|
248
|
+
const left = bound(budget);
|
|
249
|
+
let timer;
|
|
250
|
+
const late = new Promise((_resolve, reject) => {
|
|
251
|
+
timer = setTimeout(() => reject(new TimeoutError(budget.ms)), left);
|
|
252
|
+
});
|
|
253
|
+
return Promise.race([pending, late]).finally(() => clearTimeout(timer));
|
|
254
|
+
}
|
|
255
|
+
async function initialize(budget) {
|
|
256
|
+
const hello = request("initialize", {
|
|
257
|
+
protocolVersion: PROTOCOL_VERSION,
|
|
258
|
+
capabilities: {},
|
|
259
|
+
clientInfo: {
|
|
260
|
+
name: "daydream-figma",
|
|
261
|
+
version: "0.1.0"
|
|
262
|
+
}
|
|
263
|
+
});
|
|
264
|
+
const answer = await post({
|
|
265
|
+
...settings,
|
|
266
|
+
session: null,
|
|
267
|
+
protocolVersion: null,
|
|
268
|
+
timeoutMs: bound(budget)
|
|
269
|
+
}, hello, hello.id, NOT_REACHABLE);
|
|
270
|
+
const agreed = resultOf(answer, hello.id, "initialize")?.protocolVersion;
|
|
271
|
+
const opened = {
|
|
272
|
+
id: answer.session,
|
|
273
|
+
protocol: typeof agreed === "string" && agreed !== "" ? agreed : PROTOCOL_VERSION
|
|
274
|
+
};
|
|
275
|
+
const ready = await post({
|
|
276
|
+
...settings,
|
|
277
|
+
session: opened.id,
|
|
278
|
+
protocolVersion: opened.protocol,
|
|
279
|
+
timeoutMs: bound(budget)
|
|
280
|
+
}, {
|
|
281
|
+
jsonrpc: "2.0",
|
|
282
|
+
method: "notifications/initialized"
|
|
283
|
+
}, null, NOT_REACHABLE);
|
|
284
|
+
if (!acknowledged(ready.status)) throw statusError(ready, "notifications/initialized");
|
|
285
|
+
return opened;
|
|
286
|
+
}
|
|
287
|
+
function ensureSession(budget) {
|
|
288
|
+
if (session === null) {
|
|
289
|
+
const opening = initialize(budget);
|
|
290
|
+
session = opening;
|
|
291
|
+
opening.catch(() => {
|
|
292
|
+
if (session === opening) session = null;
|
|
293
|
+
});
|
|
294
|
+
}
|
|
295
|
+
return within(session, budget);
|
|
296
|
+
}
|
|
297
|
+
const currentSession = () => session;
|
|
298
|
+
async function call(method, params, budget, retried = false) {
|
|
299
|
+
const sessionPromise = ensureSession(budget);
|
|
300
|
+
const rode = currentSession();
|
|
301
|
+
const { id, protocol } = await sessionPromise;
|
|
302
|
+
const message = request(method, params);
|
|
303
|
+
const answer = await post({
|
|
304
|
+
...settings,
|
|
305
|
+
session: id,
|
|
306
|
+
protocolVersion: protocol,
|
|
307
|
+
timeoutMs: bound(budget)
|
|
308
|
+
}, message, message.id, NOT_REACHABLE);
|
|
309
|
+
if (answer.status === 404 && id !== null && !retried) {
|
|
310
|
+
if (session === rode) session = null;
|
|
311
|
+
return call(method, params, budget, true);
|
|
312
|
+
}
|
|
313
|
+
return resultOf(answer, message.id, method);
|
|
314
|
+
}
|
|
315
|
+
async function tool(name, args, under = budget()) {
|
|
316
|
+
const result = await call("tools/call", {
|
|
317
|
+
name,
|
|
318
|
+
arguments: args
|
|
319
|
+
}, under);
|
|
320
|
+
const content = Array.isArray(result?.content) ? result.content.filter(isContentItem) : [];
|
|
321
|
+
if (result?.isError === true) {
|
|
322
|
+
const said = textsOf(content).join(" ").trim();
|
|
323
|
+
throw new Error(said === "" ? `Figma could not answer ${name}` : said);
|
|
324
|
+
}
|
|
325
|
+
return content;
|
|
326
|
+
}
|
|
327
|
+
function nodeArgs(node) {
|
|
328
|
+
if (node === void 0 || node.trim() === "") return {};
|
|
329
|
+
const nodeId = nodeIdFromUrl(node);
|
|
330
|
+
if (nodeId === null) throw new Error(`not a Figma node id or link: ${node}`);
|
|
331
|
+
return { nodeId };
|
|
332
|
+
}
|
|
333
|
+
return {
|
|
334
|
+
async reachable() {
|
|
335
|
+
try {
|
|
336
|
+
const under = budget();
|
|
337
|
+
if (session === null) await ensureSession(under);
|
|
338
|
+
else await call("ping", {}, under);
|
|
339
|
+
return true;
|
|
340
|
+
} catch {
|
|
341
|
+
return false;
|
|
342
|
+
}
|
|
343
|
+
},
|
|
344
|
+
async variableDefs(node) {
|
|
345
|
+
return textResult(await tool("get_variable_defs", {
|
|
346
|
+
...nodeArgs(node),
|
|
347
|
+
...client
|
|
348
|
+
}));
|
|
349
|
+
},
|
|
350
|
+
async designContext(node, options = {}) {
|
|
351
|
+
const content = await tool("get_design_context", {
|
|
352
|
+
...nodeArgs(node),
|
|
353
|
+
...client,
|
|
354
|
+
...options.excludeScreenshot === void 0 ? {} : { excludeScreenshot: options.excludeScreenshot },
|
|
355
|
+
...options.forceCode === void 0 ? {} : { forceCode: options.forceCode }
|
|
356
|
+
});
|
|
357
|
+
return {
|
|
358
|
+
...textResult(content),
|
|
359
|
+
screenshot: imageOf(content)
|
|
360
|
+
};
|
|
361
|
+
},
|
|
362
|
+
async metadata(node) {
|
|
363
|
+
return xmlResult(await tool("get_metadata", {
|
|
364
|
+
...nodeArgs(node),
|
|
365
|
+
...client
|
|
366
|
+
}));
|
|
367
|
+
},
|
|
368
|
+
async frame(node, options = {}) {
|
|
369
|
+
try {
|
|
370
|
+
const frame = frameOf(xmlResult(await tool("get_metadata", {
|
|
371
|
+
...nodeArgs(node),
|
|
372
|
+
...client
|
|
373
|
+
}, budget(options.timeoutMs ?? 3e3))).text);
|
|
374
|
+
return frame === null ? { kind: "none" } : {
|
|
375
|
+
kind: "frame",
|
|
376
|
+
frame
|
|
377
|
+
};
|
|
378
|
+
} catch (error) {
|
|
379
|
+
return failedRead(error);
|
|
380
|
+
}
|
|
381
|
+
},
|
|
382
|
+
async selection(options = {}) {
|
|
383
|
+
try {
|
|
384
|
+
const listing = textsOf(await tool("get_metadata", { ...client }, budget(options.timeoutMs ?? 5e3))).find((text) => /^\s*Currently selected nodes\b/.test(text));
|
|
385
|
+
const frames = listing === void 0 ? [] : selectionOf(listing);
|
|
386
|
+
return frames.length === 0 ? { kind: "none" } : {
|
|
387
|
+
kind: "frames",
|
|
388
|
+
frames
|
|
389
|
+
};
|
|
390
|
+
} catch (error) {
|
|
391
|
+
return failedRead(error);
|
|
392
|
+
}
|
|
393
|
+
},
|
|
394
|
+
async screenshot(node, options = {}) {
|
|
395
|
+
const content = await tool("get_screenshot", {
|
|
396
|
+
...nodeArgs(node),
|
|
397
|
+
...options.contentsOnly === void 0 ? {} : { contentsOnly: options.contentsOnly }
|
|
398
|
+
});
|
|
399
|
+
const image = imageOf(content);
|
|
400
|
+
if (image === null) {
|
|
401
|
+
const said = textsOf(content).join(" ").trim();
|
|
402
|
+
throw new Error(said === "" ? "Figma answered no image" : said);
|
|
403
|
+
}
|
|
404
|
+
return image;
|
|
405
|
+
},
|
|
406
|
+
async close() {
|
|
407
|
+
const opened = session;
|
|
408
|
+
session = null;
|
|
409
|
+
if (opened === null) return;
|
|
410
|
+
let id;
|
|
411
|
+
try {
|
|
412
|
+
id = (await opened).id;
|
|
413
|
+
} catch {
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if (id === null) return;
|
|
417
|
+
try {
|
|
418
|
+
await settings.fetch(settings.url, {
|
|
419
|
+
method: "DELETE",
|
|
420
|
+
headers: { "mcp-session-id": id },
|
|
421
|
+
signal: AbortSignal.timeout(settings.timeoutMs)
|
|
422
|
+
});
|
|
423
|
+
} catch {}
|
|
424
|
+
}
|
|
425
|
+
};
|
|
426
|
+
}
|
|
427
|
+
var NODE_SHAPE = "Give a Figma link or a node id, or nothing for Figma's selection.";
|
|
428
|
+
function nodeInput(input) {
|
|
429
|
+
if (input === void 0 || input === null) return void 0;
|
|
430
|
+
if (typeof input !== "object") throw new Error(NODE_SHAPE);
|
|
431
|
+
const node = input.node;
|
|
432
|
+
if (node === void 0 || node === null) return void 0;
|
|
433
|
+
if (typeof node !== "string" || node.length > 2048) throw new Error(NODE_SHAPE);
|
|
434
|
+
return node.trim() === "" ? void 0 : node;
|
|
435
|
+
}
|
|
436
|
+
function joinValues(array, separator = "|") {
|
|
437
|
+
return array.map((val) => stringifyPrimitive(val)).join(separator);
|
|
438
|
+
}
|
|
439
|
+
function jsonStringifyReplacer(_, value) {
|
|
440
|
+
if (typeof value === "bigint") return value.toString();
|
|
441
|
+
return value;
|
|
442
|
+
}
|
|
443
|
+
function nullish(input) {
|
|
444
|
+
return input === null || input === void 0;
|
|
445
|
+
}
|
|
446
|
+
function cleanRegex(source) {
|
|
447
|
+
const start = source.startsWith("^") ? 1 : 0;
|
|
448
|
+
const end = source.endsWith("$") ? source.length - 1 : source.length;
|
|
449
|
+
return source.slice(start, end);
|
|
450
|
+
}
|
|
451
|
+
function assignProp(target, prop, value) {
|
|
452
|
+
Object.defineProperty(target, prop, {
|
|
453
|
+
value,
|
|
454
|
+
writable: true,
|
|
455
|
+
enumerable: true,
|
|
456
|
+
configurable: true
|
|
457
|
+
});
|
|
458
|
+
}
|
|
459
|
+
function rawShape(def) {
|
|
460
|
+
const desc = Object.getOwnPropertyDescriptor(def, "shape");
|
|
461
|
+
return desc?.get ? desc.get.raw : desc?.value;
|
|
462
|
+
}
|
|
463
|
+
function mergeDefs(...defs) {
|
|
464
|
+
const mergedDescriptors = {};
|
|
465
|
+
for (const def of defs) {
|
|
466
|
+
const descriptors = Object.getOwnPropertyDescriptors(def);
|
|
467
|
+
Object.assign(mergedDescriptors, descriptors);
|
|
468
|
+
}
|
|
469
|
+
return Object.defineProperties({}, mergedDescriptors);
|
|
470
|
+
}
|
|
471
|
+
function slugify(input) {
|
|
472
|
+
return input.toLowerCase().trim().replace(/[^\w\s-]/g, "").replace(/[\s_-]+/g, "-").replace(/^-+|-+$/g, "");
|
|
473
|
+
}
|
|
474
|
+
var captureStackTrace = "captureStackTrace" in Error ? Error.captureStackTrace : (..._args) => {};
|
|
475
|
+
function isObject(data) {
|
|
476
|
+
return typeof data === "object" && data !== null && !Array.isArray(data);
|
|
477
|
+
}
|
|
478
|
+
function isPlainObject(o) {
|
|
479
|
+
if (isObject(o) === false) return false;
|
|
480
|
+
const ctor = o.constructor;
|
|
481
|
+
if (ctor === void 0) return true;
|
|
482
|
+
if (typeof ctor !== "function") return true;
|
|
483
|
+
const prot = ctor.prototype;
|
|
484
|
+
if (isObject(prot) === false) return false;
|
|
485
|
+
if (Object.prototype.hasOwnProperty.call(prot, "isPrototypeOf") === false) return false;
|
|
486
|
+
return true;
|
|
487
|
+
}
|
|
488
|
+
function shallowClone(o) {
|
|
489
|
+
if (isPlainObject(o)) return { ...o };
|
|
490
|
+
if (Array.isArray(o)) return [...o];
|
|
491
|
+
if (o instanceof Map) return new Map(o);
|
|
492
|
+
if (o instanceof Set) return new Set(o);
|
|
493
|
+
return o;
|
|
494
|
+
}
|
|
495
|
+
function escapeRegex(str) {
|
|
496
|
+
return str.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
497
|
+
}
|
|
498
|
+
function clone(inst, def, params) {
|
|
499
|
+
const cl = new inst._zod.constr(def ?? inst._zod.def);
|
|
500
|
+
if (!def || params?.parent) cl._zod.parent = inst;
|
|
501
|
+
return cl;
|
|
502
|
+
}
|
|
503
|
+
function normalizeParams(_params) {
|
|
504
|
+
const params = _params;
|
|
505
|
+
if (!params) return {};
|
|
506
|
+
if (typeof params === "string") return { error: () => params };
|
|
507
|
+
if (params?.message !== void 0) {
|
|
508
|
+
if (params?.error !== void 0) throw new Error("Cannot specify both `message` and `error` params");
|
|
509
|
+
params.error = params.message;
|
|
510
|
+
}
|
|
511
|
+
delete params.message;
|
|
512
|
+
if (typeof params.error === "string") return {
|
|
513
|
+
...params,
|
|
514
|
+
error: () => params.error
|
|
515
|
+
};
|
|
516
|
+
return params;
|
|
517
|
+
}
|
|
518
|
+
function stringifyPrimitive(value) {
|
|
519
|
+
if (typeof value === "bigint") return value.toString() + "n";
|
|
520
|
+
if (typeof value === "string") return `"${value}"`;
|
|
521
|
+
return `${value}`;
|
|
522
|
+
}
|
|
523
|
+
var NUMBER_FORMAT_RANGES = (() => ({
|
|
524
|
+
safeint: [Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER],
|
|
525
|
+
int32: [-2147483648, 2147483647],
|
|
526
|
+
uint32: [0, 4294967295],
|
|
527
|
+
float32: [-34028234663852886e22, 34028234663852886e22],
|
|
528
|
+
float64: [-Number.MAX_VALUE, Number.MAX_VALUE]
|
|
529
|
+
}))();
|
|
530
|
+
var BIGINT_FORMAT_RANGES = {
|
|
531
|
+
int64: [BigInt("-9223372036854775808"), BigInt("9223372036854775807")],
|
|
532
|
+
uint64: [BigInt(0), BigInt("18446744073709551615")]
|
|
533
|
+
};
|
|
534
|
+
function aborted(x, startIndex = 0) {
|
|
535
|
+
if (x.aborted === true) return true;
|
|
536
|
+
for (let i = startIndex; i < x.issues.length; i++) if (x.issues[i]?.continue !== true) return true;
|
|
537
|
+
return false;
|
|
538
|
+
}
|
|
539
|
+
function explicitlyAborted(x, startIndex = 0) {
|
|
540
|
+
if (x.aborted === true) return true;
|
|
541
|
+
for (let i = startIndex; i < x.issues.length; i++) if (x.issues[i]?.continue === false) return true;
|
|
542
|
+
return false;
|
|
543
|
+
}
|
|
544
|
+
function prefixIssues(path, issues) {
|
|
545
|
+
return issues.map((iss) => {
|
|
546
|
+
var _a;
|
|
547
|
+
(_a = iss).path ?? (_a.path = []);
|
|
548
|
+
iss.path.unshift(path);
|
|
549
|
+
return iss;
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
function unwrapMessage(message) {
|
|
553
|
+
return typeof message === "string" ? message : message?.message;
|
|
554
|
+
}
|
|
555
|
+
function attachSchema(issues, start, inst) {
|
|
556
|
+
var _a;
|
|
557
|
+
for (let i = start; i < issues.length; i++) (_a = issues[i]).schema ?? (_a.schema = inst);
|
|
558
|
+
}
|
|
559
|
+
function finalizeIssue(iss, ctx, config) {
|
|
560
|
+
var _a;
|
|
561
|
+
const traits = iss.inst?._zod?.traits;
|
|
562
|
+
if (traits?.has("$ZodType")) {
|
|
563
|
+
if (traits.has("$ZodCheck")) (_a = iss).schema ?? (_a.schema = iss.inst);
|
|
564
|
+
else iss.schema = iss.inst;
|
|
565
|
+
}
|
|
566
|
+
const schemaError = iss.schema !== iss.inst ? iss.schema?._zod.def?.error : void 0;
|
|
567
|
+
const message = iss.message ? iss.message : unwrapMessage(iss.inst?._zod.def?.error?.(iss)) ?? unwrapMessage(schemaError?.(iss)) ?? unwrapMessage(ctx?.error?.(iss)) ?? unwrapMessage(config.customError?.(iss)) ?? unwrapMessage(config.localeError?.(iss)) ?? "Invalid input";
|
|
568
|
+
const full = {};
|
|
569
|
+
for (const k of Object.keys(iss)) {
|
|
570
|
+
if (k === "inst" || k === "schema" || k === "continue" || k === "input" || k === "__proto__") continue;
|
|
571
|
+
full[k] = iss[k];
|
|
572
|
+
}
|
|
573
|
+
full.path ?? (full.path = []);
|
|
574
|
+
full.message = message;
|
|
575
|
+
if (ctx?.reportInput) full.input = iss.input;
|
|
576
|
+
return full;
|
|
577
|
+
}
|
|
578
|
+
var highSurrogate = /[\uD800-\uDBFF]/;
|
|
579
|
+
function codePointLength(str) {
|
|
580
|
+
const units = str.length;
|
|
581
|
+
if (!highSurrogate.test(str)) return units;
|
|
582
|
+
let count = units;
|
|
583
|
+
for (let i = 0; i < units - 1; i++) if ((str.charCodeAt(i) & 64512) === 55296 && (str.charCodeAt(i + 1) & 64512) === 56320) {
|
|
584
|
+
count--;
|
|
585
|
+
i++;
|
|
586
|
+
}
|
|
587
|
+
return count;
|
|
588
|
+
}
|
|
589
|
+
function getLengthableOrigin(input) {
|
|
590
|
+
if (Array.isArray(input)) return "array";
|
|
591
|
+
if (typeof input === "string") return "string";
|
|
592
|
+
return "unknown";
|
|
593
|
+
}
|
|
594
|
+
function parsedType(data) {
|
|
595
|
+
const t = typeof data;
|
|
596
|
+
switch (t) {
|
|
597
|
+
case "number": return Number.isNaN(data) ? "nan" : "number";
|
|
598
|
+
case "object": {
|
|
599
|
+
if (data === null) return "null";
|
|
600
|
+
if (Array.isArray(data)) return "array";
|
|
601
|
+
const obj = data;
|
|
602
|
+
if (obj && Object.getPrototypeOf(obj) !== Object.prototype && "constructor" in obj && obj.constructor) return obj.constructor.name;
|
|
603
|
+
}
|
|
604
|
+
}
|
|
605
|
+
return t;
|
|
606
|
+
}
|
|
607
|
+
function issue(...args) {
|
|
608
|
+
const [iss, input, inst] = args;
|
|
609
|
+
if (typeof iss === "string") return {
|
|
610
|
+
message: iss,
|
|
611
|
+
code: "custom",
|
|
612
|
+
input,
|
|
613
|
+
inst
|
|
614
|
+
};
|
|
615
|
+
return { ...iss };
|
|
616
|
+
}
|
|
617
|
+
function members(proto, table) {
|
|
618
|
+
for (const key in table) {
|
|
619
|
+
const desc = Object.getOwnPropertyDescriptor(table, key);
|
|
620
|
+
if (desc.get) Object.defineProperty(proto, key, {
|
|
621
|
+
...desc,
|
|
622
|
+
enumerable: false
|
|
623
|
+
});
|
|
624
|
+
else defineBound(proto, key, desc.value);
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
function own(inst, key, value, enumerable = true) {
|
|
628
|
+
Object.defineProperty(inst, key, {
|
|
629
|
+
configurable: true,
|
|
630
|
+
writable: true,
|
|
631
|
+
enumerable,
|
|
632
|
+
value
|
|
633
|
+
});
|
|
634
|
+
return value;
|
|
635
|
+
}
|
|
636
|
+
function hide(inst, key, value) {
|
|
637
|
+
return own(inst, key, value, false);
|
|
638
|
+
}
|
|
639
|
+
function derived(computes, table) {
|
|
640
|
+
for (const key in computes) {
|
|
641
|
+
const compute = computes[key];
|
|
642
|
+
Object.defineProperty(table, key, {
|
|
643
|
+
configurable: true,
|
|
644
|
+
enumerable: true,
|
|
645
|
+
get() {
|
|
646
|
+
return own(this, key, compute(this));
|
|
647
|
+
},
|
|
648
|
+
set(value) {
|
|
649
|
+
own(this, key, value);
|
|
650
|
+
}
|
|
651
|
+
});
|
|
652
|
+
}
|
|
653
|
+
return table;
|
|
654
|
+
}
|
|
655
|
+
function defineBound(proto, key, fn) {
|
|
656
|
+
Object.defineProperty(proto, key, {
|
|
657
|
+
configurable: true,
|
|
658
|
+
get() {
|
|
659
|
+
return this == null ? fn : own(this, key, fn.bind(this));
|
|
660
|
+
},
|
|
661
|
+
set(value) {
|
|
662
|
+
own(this, key, value);
|
|
663
|
+
}
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
var installing;
|
|
667
|
+
var broke = false;
|
|
668
|
+
var breaker = {
|
|
669
|
+
configurable: true,
|
|
670
|
+
get() {
|
|
671
|
+
broke = true;
|
|
672
|
+
}
|
|
673
|
+
};
|
|
674
|
+
function defineLazyInternal(inst, key, compute) {
|
|
675
|
+
const proto = Object.getPrototypeOf(inst._zod);
|
|
676
|
+
if (key in proto && installing !== inst._zod) {
|
|
677
|
+
installing = void 0;
|
|
678
|
+
return;
|
|
679
|
+
}
|
|
680
|
+
installing = inst._zod;
|
|
681
|
+
Object.defineProperty(proto, key, {
|
|
682
|
+
configurable: true,
|
|
683
|
+
get() {
|
|
684
|
+
Object.defineProperty(this, key, breaker);
|
|
685
|
+
const outer = broke;
|
|
686
|
+
broke = false;
|
|
687
|
+
try {
|
|
688
|
+
const value = compute(this);
|
|
689
|
+
if (broke) delete this[key];
|
|
690
|
+
else Object.defineProperty(this, key, {
|
|
691
|
+
configurable: true,
|
|
692
|
+
writable: true,
|
|
693
|
+
value
|
|
694
|
+
});
|
|
695
|
+
broke = broke || outer;
|
|
696
|
+
return value;
|
|
697
|
+
} catch (err) {
|
|
698
|
+
delete this[key];
|
|
699
|
+
broke = broke || outer;
|
|
700
|
+
throw err;
|
|
701
|
+
}
|
|
702
|
+
},
|
|
703
|
+
set(value) {
|
|
704
|
+
Object.defineProperty(this, key, {
|
|
705
|
+
configurable: true,
|
|
706
|
+
writable: true,
|
|
707
|
+
value
|
|
708
|
+
});
|
|
709
|
+
}
|
|
710
|
+
});
|
|
711
|
+
}
|
|
712
|
+
var CONSTANT_CATCH = "~constantCatch";
|
|
713
|
+
function constantCatch(value) {
|
|
714
|
+
const fn = () => value;
|
|
715
|
+
fn[CONSTANT_CATCH] = true;
|
|
716
|
+
return fn;
|
|
717
|
+
}
|
|
718
|
+
var _a$1;
|
|
719
|
+
var _zodDesc = {
|
|
720
|
+
value: void 0,
|
|
721
|
+
enumerable: false
|
|
722
|
+
};
|
|
723
|
+
var _E = "captureStackTrace" in Error ? Error : null;
|
|
724
|
+
function newError(Definition) {
|
|
725
|
+
const E = _E;
|
|
726
|
+
if (E) {
|
|
727
|
+
const saved = E.stackTraceLimit;
|
|
728
|
+
if (typeof saved === "number") {
|
|
729
|
+
try {
|
|
730
|
+
E.stackTraceLimit = 0;
|
|
731
|
+
} catch {
|
|
732
|
+
_E = null;
|
|
733
|
+
return new Definition();
|
|
734
|
+
}
|
|
735
|
+
try {
|
|
736
|
+
return new Definition();
|
|
737
|
+
} finally {
|
|
738
|
+
E.stackTraceLimit = saved;
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
return new Definition();
|
|
743
|
+
}
|
|
744
|
+
function $constructor(name, initializer, proto, params) {
|
|
745
|
+
const zodProto = {};
|
|
746
|
+
function Internals(def) {
|
|
747
|
+
this.def = def;
|
|
748
|
+
this.constr = _;
|
|
749
|
+
this.traits = new Set();
|
|
750
|
+
}
|
|
751
|
+
Internals.prototype = zodProto;
|
|
752
|
+
const protoMembers = proto;
|
|
753
|
+
const initialized = protoMembers && new WeakSet();
|
|
754
|
+
function init(inst, def) {
|
|
755
|
+
if (!inst._zod) {
|
|
756
|
+
_zodDesc.value = new Internals(def);
|
|
757
|
+
try {
|
|
758
|
+
Object.defineProperty(inst, "_zod", _zodDesc);
|
|
759
|
+
} finally {
|
|
760
|
+
_zodDesc.value = void 0;
|
|
761
|
+
}
|
|
762
|
+
} else if (inst._zod.traits.has(name)) return;
|
|
763
|
+
inst._zod.traits.add(name);
|
|
764
|
+
initializer(inst, def);
|
|
765
|
+
if (initialized) {
|
|
766
|
+
const own = Object.getPrototypeOf(inst);
|
|
767
|
+
const ctorProto = inst._zod.constr.prototype;
|
|
768
|
+
let up = own;
|
|
769
|
+
while (up && up !== ctorProto) up = Object.getPrototypeOf(up);
|
|
770
|
+
const target = up ?? own;
|
|
771
|
+
if (!initialized.has(target)) {
|
|
772
|
+
initialized.add(target);
|
|
773
|
+
members(target, protoMembers);
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
const proto = _.prototype;
|
|
777
|
+
for (const k in proto) {
|
|
778
|
+
if (!Object.prototype.hasOwnProperty.call(proto, k)) continue;
|
|
779
|
+
if (!(k in inst)) inst[k] = proto[k].bind(inst);
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
const Parent = params?.Parent ?? Object;
|
|
783
|
+
class Definition extends Parent {}
|
|
784
|
+
Object.defineProperty(Definition, "name", { value: name });
|
|
785
|
+
function _(def) {
|
|
786
|
+
const inst = params?.Parent ? newError(Definition) : this;
|
|
787
|
+
init(inst, def);
|
|
788
|
+
const deferred = inst._zod.deferred;
|
|
789
|
+
if (deferred) {
|
|
790
|
+
for (const fn of deferred) fn();
|
|
791
|
+
inst._zod.deferred = void 0;
|
|
792
|
+
}
|
|
793
|
+
const pp = globalThis.__zod_globalConfig?.postProcessor;
|
|
794
|
+
if (pp) pp(inst);
|
|
795
|
+
return inst;
|
|
796
|
+
}
|
|
797
|
+
Object.defineProperty(_, "init", { value: init });
|
|
798
|
+
Object.defineProperty(_, Symbol.hasInstance, { value: (inst) => {
|
|
799
|
+
if (params?.Parent && inst instanceof params.Parent) return true;
|
|
800
|
+
return inst?._zod?.traits?.has(name);
|
|
801
|
+
} });
|
|
802
|
+
Object.defineProperty(_, "name", { value: name });
|
|
803
|
+
return _;
|
|
804
|
+
}
|
|
805
|
+
var $ZodAsyncError = class extends Error {
|
|
806
|
+
constructor() {
|
|
807
|
+
super(`Encountered Promise during synchronous parse. Use .parseAsync() instead.`);
|
|
808
|
+
}
|
|
809
|
+
};
|
|
810
|
+
var $ZodEncodeError = class extends Error {
|
|
811
|
+
constructor(name) {
|
|
812
|
+
super(`Encountered unidirectional transform during encode: ${name}`);
|
|
813
|
+
this.name = "ZodEncodeError";
|
|
814
|
+
}
|
|
815
|
+
};
|
|
816
|
+
(_a$1 = globalThis).__zod_globalConfig ?? (_a$1.__zod_globalConfig = {});
|
|
817
|
+
var globalConfig = globalThis.__zod_globalConfig;
|
|
818
|
+
function config(newConfig) {
|
|
819
|
+
if (newConfig) Object.assign(globalConfig, newConfig);
|
|
820
|
+
return globalConfig;
|
|
821
|
+
}
|
|
822
|
+
function _getMessage() {
|
|
823
|
+
const internals = this._zod;
|
|
824
|
+
internals.message ?? (internals.message = JSON.stringify(internals.def, jsonStringifyReplacer, 2));
|
|
825
|
+
return internals.message;
|
|
826
|
+
}
|
|
827
|
+
function _setMessage(value) {
|
|
828
|
+
this._zod.message = value;
|
|
829
|
+
}
|
|
830
|
+
var _messageDesc = {
|
|
831
|
+
get: _getMessage,
|
|
832
|
+
set: _setMessage,
|
|
833
|
+
enumerable: true,
|
|
834
|
+
configurable: true
|
|
835
|
+
};
|
|
836
|
+
var _issuesDesc = {
|
|
837
|
+
value: void 0,
|
|
838
|
+
enumerable: false
|
|
839
|
+
};
|
|
840
|
+
var _installedToString = new WeakSet([Object.prototype, Error.prototype]);
|
|
841
|
+
var initializer$1 = (inst, def) => {
|
|
842
|
+
inst.name = "$ZodError";
|
|
843
|
+
_issuesDesc.value = def;
|
|
844
|
+
Object.defineProperty(inst, "issues", _issuesDesc);
|
|
845
|
+
_issuesDesc.value = void 0;
|
|
846
|
+
Object.defineProperty(inst, "message", _messageDesc);
|
|
847
|
+
const proto = Object.getPrototypeOf(inst);
|
|
848
|
+
if (!_installedToString.has(proto)) {
|
|
849
|
+
_installedToString.add(proto);
|
|
850
|
+
Object.defineProperty(proto, "toString", {
|
|
851
|
+
configurable: true,
|
|
852
|
+
enumerable: false,
|
|
853
|
+
get() {
|
|
854
|
+
const value = () => this.message;
|
|
855
|
+
Object.defineProperty(this, "toString", {
|
|
856
|
+
value,
|
|
857
|
+
configurable: true,
|
|
858
|
+
writable: true
|
|
859
|
+
});
|
|
860
|
+
return value;
|
|
861
|
+
},
|
|
862
|
+
set(value) {
|
|
863
|
+
Object.defineProperty(this, "toString", {
|
|
864
|
+
value,
|
|
865
|
+
configurable: true,
|
|
866
|
+
writable: true
|
|
867
|
+
});
|
|
868
|
+
}
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
};
|
|
872
|
+
var $ZodError = $constructor("$ZodError", initializer$1);
|
|
873
|
+
$constructor("$ZodError", initializer$1, void 0, { Parent: Error });
|
|
874
|
+
function node(obj, key, make) {
|
|
875
|
+
if (!Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
876
|
+
if (key === "__proto__") Object.defineProperty(obj, key, {
|
|
877
|
+
value: make(),
|
|
878
|
+
writable: true,
|
|
879
|
+
enumerable: true,
|
|
880
|
+
configurable: true
|
|
881
|
+
});
|
|
882
|
+
else obj[key] = make();
|
|
883
|
+
}
|
|
884
|
+
return obj[key];
|
|
885
|
+
}
|
|
886
|
+
function flattenError(error, mapper = (issue) => issue.message) {
|
|
887
|
+
const fieldErrors = {};
|
|
888
|
+
const formErrors = [];
|
|
889
|
+
for (const sub of error.issues) if (sub.path.length > 0) node(fieldErrors, sub.path[0], () => []).push(mapper(sub));
|
|
890
|
+
else formErrors.push(mapper(sub));
|
|
891
|
+
return {
|
|
892
|
+
formErrors,
|
|
893
|
+
fieldErrors
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
function formatError(error, mapper = (issue) => issue.message) {
|
|
897
|
+
const fieldErrors = { _errors: [] };
|
|
898
|
+
const processError = (error, path = []) => {
|
|
899
|
+
for (const issue of error.issues) if (issue.code === "invalid_union" && issue.errors.length) issue.errors.map((issues) => processError({ issues }, [...path, ...issue.path]));
|
|
900
|
+
else if (issue.code === "invalid_key") processError({ issues: issue.issues }, [...path, ...issue.path]);
|
|
901
|
+
else if (issue.code === "invalid_element") processError({ issues: issue.issues }, [...path, ...issue.path]);
|
|
902
|
+
else {
|
|
903
|
+
const fullpath = [...path, ...issue.path];
|
|
904
|
+
if (fullpath.length === 0) fieldErrors._errors.push(mapper(issue));
|
|
905
|
+
else {
|
|
906
|
+
let curr = fieldErrors;
|
|
907
|
+
let i = 0;
|
|
908
|
+
while (i < fullpath.length) {
|
|
909
|
+
const el = fullpath[i];
|
|
910
|
+
const terminal = i === fullpath.length - 1;
|
|
911
|
+
if (el === "_errors") {
|
|
912
|
+
if (terminal) curr._errors.push(mapper(issue));
|
|
913
|
+
i++;
|
|
914
|
+
continue;
|
|
915
|
+
}
|
|
916
|
+
if (!Object.prototype.hasOwnProperty.call(curr, el)) Object.defineProperty(curr, el, {
|
|
917
|
+
value: { _errors: [] },
|
|
918
|
+
enumerable: true,
|
|
919
|
+
writable: true,
|
|
920
|
+
configurable: true
|
|
921
|
+
});
|
|
922
|
+
const node = curr[el];
|
|
923
|
+
if (terminal) node._errors.push(mapper(issue));
|
|
924
|
+
curr = node;
|
|
925
|
+
i++;
|
|
926
|
+
}
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
};
|
|
930
|
+
processError(error);
|
|
931
|
+
return fieldErrors;
|
|
932
|
+
}
|
|
933
|
+
function finalizeParams(callee, params) {
|
|
934
|
+
return {
|
|
935
|
+
callee: params?.callee ?? callee,
|
|
936
|
+
Err: params?.Err
|
|
937
|
+
};
|
|
938
|
+
}
|
|
939
|
+
var _parse = (_Err) => {
|
|
940
|
+
const fn = (schema, value, _ctx, _params) => {
|
|
941
|
+
const ctx = _ctx ? {
|
|
942
|
+
..._ctx,
|
|
943
|
+
async: false
|
|
944
|
+
} : { async: false };
|
|
945
|
+
const result = schema._zod.run({
|
|
946
|
+
value,
|
|
947
|
+
issues: []
|
|
948
|
+
}, ctx);
|
|
949
|
+
if (result instanceof Promise) throw new $ZodAsyncError();
|
|
950
|
+
if (result.issues.length) {
|
|
951
|
+
const e = new ((_params?.Err) ?? _Err)(result.issues.map((iss) => finalizeIssue(iss, ctx, config())));
|
|
952
|
+
captureStackTrace(e, _params?.callee ?? fn);
|
|
953
|
+
throw e;
|
|
954
|
+
}
|
|
955
|
+
return result.value;
|
|
956
|
+
};
|
|
957
|
+
return fn;
|
|
958
|
+
};
|
|
959
|
+
var _parseAsync = (_Err) => {
|
|
960
|
+
const fn = async (schema, value, _ctx, params) => {
|
|
961
|
+
const ctx = _ctx ? {
|
|
962
|
+
..._ctx,
|
|
963
|
+
async: true
|
|
964
|
+
} : { async: true };
|
|
965
|
+
let result = schema._zod.run({
|
|
966
|
+
value,
|
|
967
|
+
issues: []
|
|
968
|
+
}, ctx);
|
|
969
|
+
if (result instanceof Promise) result = await result;
|
|
970
|
+
if (result.issues.length) {
|
|
971
|
+
const e = new ((params?.Err) ?? _Err)(result.issues.map((iss) => finalizeIssue(iss, ctx, config())));
|
|
972
|
+
captureStackTrace(e, params?.callee ?? fn);
|
|
973
|
+
throw e;
|
|
974
|
+
}
|
|
975
|
+
return result.value;
|
|
976
|
+
};
|
|
977
|
+
return fn;
|
|
978
|
+
};
|
|
979
|
+
var _safeParse = (_Err) => (schema, value, _ctx) => {
|
|
980
|
+
const ctx = _ctx ? {
|
|
981
|
+
..._ctx,
|
|
982
|
+
async: false
|
|
983
|
+
} : { async: false };
|
|
984
|
+
const result = schema._zod.run({
|
|
985
|
+
value,
|
|
986
|
+
issues: []
|
|
987
|
+
}, ctx);
|
|
988
|
+
if (result instanceof Promise) throw new $ZodAsyncError();
|
|
989
|
+
return result.issues.length ? failure(_Err, result.issues, ctx) : {
|
|
990
|
+
success: true,
|
|
991
|
+
data: result.value
|
|
992
|
+
};
|
|
993
|
+
};
|
|
994
|
+
function failure(Err, issues, ctx) {
|
|
995
|
+
let error;
|
|
996
|
+
return {
|
|
997
|
+
success: false,
|
|
998
|
+
get error() {
|
|
999
|
+
if (!error) {
|
|
1000
|
+
error = new Err(issues.map((iss) => finalizeIssue(iss, ctx, config())));
|
|
1001
|
+
issues = void 0;
|
|
1002
|
+
ctx = void 0;
|
|
1003
|
+
}
|
|
1004
|
+
return error;
|
|
1005
|
+
},
|
|
1006
|
+
set error(e) {
|
|
1007
|
+
error = e;
|
|
1008
|
+
issues = void 0;
|
|
1009
|
+
ctx = void 0;
|
|
1010
|
+
}
|
|
1011
|
+
};
|
|
1012
|
+
}
|
|
1013
|
+
var _safeParseAsync = (_Err) => async (schema, value, _ctx) => {
|
|
1014
|
+
const ctx = _ctx ? {
|
|
1015
|
+
..._ctx,
|
|
1016
|
+
async: true
|
|
1017
|
+
} : { async: true };
|
|
1018
|
+
let result = schema._zod.run({
|
|
1019
|
+
value,
|
|
1020
|
+
issues: []
|
|
1021
|
+
}, ctx);
|
|
1022
|
+
if (result instanceof Promise) result = await result;
|
|
1023
|
+
return result.issues.length ? failure(_Err, result.issues, ctx) : {
|
|
1024
|
+
success: true,
|
|
1025
|
+
data: result.value
|
|
1026
|
+
};
|
|
1027
|
+
};
|
|
1028
|
+
var COMPILE_INVALID = Symbol.for("zod.compile.invalid");
|
|
1029
|
+
var COMPILE_FALLBACK = Symbol.for("zod.compile.fallback");
|
|
1030
|
+
var validate = ((schema, value, _ctx) => {
|
|
1031
|
+
const validator = schema._zod.bag.validator;
|
|
1032
|
+
if (validator !== void 0) {
|
|
1033
|
+
if (validator(value) !== COMPILE_INVALID) return true;
|
|
1034
|
+
if (validator.definite === true && _ctx === void 0) return false;
|
|
1035
|
+
}
|
|
1036
|
+
return validateFallback(schema, value, _ctx);
|
|
1037
|
+
});
|
|
1038
|
+
function validateFallback(schema, value, _ctx) {
|
|
1039
|
+
const ctx = _ctx ? {
|
|
1040
|
+
..._ctx,
|
|
1041
|
+
async: false,
|
|
1042
|
+
abortEarly: true
|
|
1043
|
+
} : {
|
|
1044
|
+
async: false,
|
|
1045
|
+
abortEarly: true
|
|
1046
|
+
};
|
|
1047
|
+
const fallbackRun = schema._zod.bag.fallbackRun;
|
|
1048
|
+
let result;
|
|
1049
|
+
if (fallbackRun) {
|
|
1050
|
+
ctx[COMPILE_FALLBACK] = true;
|
|
1051
|
+
result = fallbackRun({
|
|
1052
|
+
value,
|
|
1053
|
+
issues: []
|
|
1054
|
+
}, ctx);
|
|
1055
|
+
} else result = schema._zod.run({
|
|
1056
|
+
value,
|
|
1057
|
+
issues: []
|
|
1058
|
+
}, ctx);
|
|
1059
|
+
if (result instanceof Promise) throw new $ZodAsyncError();
|
|
1060
|
+
return result.issues.length === 0;
|
|
1061
|
+
}
|
|
1062
|
+
var validateAsync$1 = async (schema, value, _ctx) => {
|
|
1063
|
+
const ctx = _ctx ? {
|
|
1064
|
+
..._ctx,
|
|
1065
|
+
async: true,
|
|
1066
|
+
abortEarly: true
|
|
1067
|
+
} : {
|
|
1068
|
+
async: true,
|
|
1069
|
+
abortEarly: true
|
|
1070
|
+
};
|
|
1071
|
+
let result = schema._zod.run({
|
|
1072
|
+
value,
|
|
1073
|
+
issues: []
|
|
1074
|
+
}, ctx);
|
|
1075
|
+
if (result instanceof Promise) result = await result;
|
|
1076
|
+
return result.issues.length === 0;
|
|
1077
|
+
};
|
|
1078
|
+
var _encode = (_Err) => {
|
|
1079
|
+
const parse = _parse(_Err);
|
|
1080
|
+
const fn = (schema, value, _ctx, _params) => {
|
|
1081
|
+
const ctx = _ctx ? {
|
|
1082
|
+
..._ctx,
|
|
1083
|
+
direction: "backward"
|
|
1084
|
+
} : { direction: "backward" };
|
|
1085
|
+
return parse(schema, value, ctx, finalizeParams(fn, _params));
|
|
1086
|
+
};
|
|
1087
|
+
return fn;
|
|
1088
|
+
};
|
|
1089
|
+
var _decode = (_Err) => {
|
|
1090
|
+
const parse = _parse(_Err);
|
|
1091
|
+
const fn = (schema, value, _ctx, _params) => {
|
|
1092
|
+
return parse(schema, value, _ctx, finalizeParams(fn, _params));
|
|
1093
|
+
};
|
|
1094
|
+
return fn;
|
|
1095
|
+
};
|
|
1096
|
+
var _encodeAsync = (_Err) => {
|
|
1097
|
+
const parseAsync = _parseAsync(_Err);
|
|
1098
|
+
const fn = async (schema, value, _ctx, _params) => {
|
|
1099
|
+
const ctx = _ctx ? {
|
|
1100
|
+
..._ctx,
|
|
1101
|
+
direction: "backward"
|
|
1102
|
+
} : { direction: "backward" };
|
|
1103
|
+
return await parseAsync(schema, value, ctx, finalizeParams(fn, _params));
|
|
1104
|
+
};
|
|
1105
|
+
return fn;
|
|
1106
|
+
};
|
|
1107
|
+
var _decodeAsync = (_Err) => {
|
|
1108
|
+
const parseAsync = _parseAsync(_Err);
|
|
1109
|
+
const fn = async (schema, value, _ctx, _params) => {
|
|
1110
|
+
return await parseAsync(schema, value, _ctx, finalizeParams(fn, _params));
|
|
1111
|
+
};
|
|
1112
|
+
return fn;
|
|
1113
|
+
};
|
|
1114
|
+
var _safeEncode = (_Err) => (schema, value, _ctx) => {
|
|
1115
|
+
const ctx = _ctx ? {
|
|
1116
|
+
..._ctx,
|
|
1117
|
+
direction: "backward"
|
|
1118
|
+
} : { direction: "backward" };
|
|
1119
|
+
return _safeParse(_Err)(schema, value, ctx);
|
|
1120
|
+
};
|
|
1121
|
+
var _safeDecode = (_Err) => (schema, value, _ctx) => {
|
|
1122
|
+
return _safeParse(_Err)(schema, value, _ctx);
|
|
1123
|
+
};
|
|
1124
|
+
var _safeEncodeAsync = (_Err) => async (schema, value, _ctx) => {
|
|
1125
|
+
const ctx = _ctx ? {
|
|
1126
|
+
..._ctx,
|
|
1127
|
+
direction: "backward"
|
|
1128
|
+
} : { direction: "backward" };
|
|
1129
|
+
return _safeParseAsync(_Err)(schema, value, ctx);
|
|
1130
|
+
};
|
|
1131
|
+
var _safeDecodeAsync = (_Err) => async (schema, value, _ctx) => {
|
|
1132
|
+
return _safeParseAsync(_Err)(schema, value, _ctx);
|
|
1133
|
+
};
|
|
1134
|
+
var cuid = /^[cC][0-9a-z]{6,}$/;
|
|
1135
|
+
var cuid2 = /^[0-9a-z]+$/;
|
|
1136
|
+
var ulid = /^[0-7][0-9A-HJKMNP-TV-Za-hjkmnp-tv-z]{25}$/;
|
|
1137
|
+
var xid = /^[0-9a-vA-V]{20}$/;
|
|
1138
|
+
var ksuid = /^[A-Za-z0-9]{27}$/;
|
|
1139
|
+
var nanoid = /^[a-zA-Z0-9_-]{21}$/;
|
|
1140
|
+
function nanoidOfLength(length) {
|
|
1141
|
+
return new RegExp(`^[a-zA-Z0-9_-]{${length}}$`);
|
|
1142
|
+
}
|
|
1143
|
+
var duration = /^P(?:(\d+W)|(?!.*W)(?=\d|T\d)(\d+Y)?(\d+M)?(\d+D)?(T(?=\d)(\d+H)?(\d+M)?(\d+([.,]\d+)?S)?)?)$/;
|
|
1144
|
+
var guid = /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})$/;
|
|
1145
|
+
var uuid = (version) => {
|
|
1146
|
+
if (!version) return /^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-8][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000|ffffffff-ffff-ffff-ffff-ffffffffffff)$/;
|
|
1147
|
+
return new RegExp(`^([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-${version}[0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12})$`);
|
|
1148
|
+
};
|
|
1149
|
+
var email = /^(?:[A-Za-z0-9_'+\-]+\.)*[A-Za-z0-9_'+\-]*[A-Za-z0-9_+-]@(?:[A-Za-z0-9][A-Za-z0-9\-]*\.)+[A-Za-z]{2,}$/;
|
|
1150
|
+
var _emoji$1 = `^(?=[\\s\\S]*[\\p{Extended_Pictographic}\\p{Regional_Indicator}\\u20E3])[\\p{Extended_Pictographic}\\p{Emoji_Component}]+$`;
|
|
1151
|
+
function emoji() {
|
|
1152
|
+
return new RegExp(_emoji$1, "u");
|
|
1153
|
+
}
|
|
1154
|
+
var ipv4 = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
|
|
1155
|
+
var ipv6 = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))$/;
|
|
1156
|
+
var cidrv4 = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/([0-9]|[1-2][0-9]|3[0-2])$/;
|
|
1157
|
+
var cidrv6 = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/;
|
|
1158
|
+
var base64 = /^$|^(?:[0-9a-zA-Z+/]{4})*(?:(?:[0-9a-zA-Z+/]{2}==)|(?:[0-9a-zA-Z+/]{3}=))?$/;
|
|
1159
|
+
var base64url = /^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2,3})?$/;
|
|
1160
|
+
var httpProtocol = /^https?$/;
|
|
1161
|
+
var e164 = /^\+[1-9]\d{6,14}$/;
|
|
1162
|
+
var dateSource = `(?:(?:\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-(?:(?:0[13578]|1[02])-(?:0[1-9]|[12]\\d|3[01])|(?:0[469]|11)-(?:0[1-9]|[12]\\d|30)|(?:02)-(?:0[1-9]|1\\d|2[0-8])))`;
|
|
1163
|
+
function anchor(source) {
|
|
1164
|
+
return new RegExp(`^${source}$`);
|
|
1165
|
+
}
|
|
1166
|
+
var date = anchor(dateSource);
|
|
1167
|
+
function timeSource(args) {
|
|
1168
|
+
const hhmm = `(?:[01]\\d|2[0-3]):[0-5]\\d`;
|
|
1169
|
+
return typeof args.precision === "number" ? args.precision === -1 ? `${hhmm}` : args.precision === 0 ? `${hhmm}:[0-5]\\d` : `${hhmm}:[0-5]\\d\\.\\d{${args.precision}}` : args.seconds ? `${hhmm}:[0-5]\\d(?:\\.\\d+)?` : `${hhmm}(?::[0-5]\\d(?:\\.\\d+)?)?`;
|
|
1170
|
+
}
|
|
1171
|
+
function time(args) {
|
|
1172
|
+
return new RegExp(`^${timeSource(args)}$`);
|
|
1173
|
+
}
|
|
1174
|
+
function datetime(args) {
|
|
1175
|
+
const opts = ["Z"];
|
|
1176
|
+
if (args.offset) opts.push(`([+-](?:[01]\\d|2[0-3]):[0-5]\\d)`);
|
|
1177
|
+
const qualified = `${timeSource({
|
|
1178
|
+
precision: args.precision,
|
|
1179
|
+
seconds: true
|
|
1180
|
+
})}(?:${opts.join("|")})`;
|
|
1181
|
+
const timeRegex = args.local ? `${qualified}|${timeSource({ precision: args.precision })}` : qualified;
|
|
1182
|
+
return new RegExp(`^${dateSource}T(?:${timeRegex})$`);
|
|
1183
|
+
}
|
|
1184
|
+
var anyString = /^[\s\S]{0,}$/;
|
|
1185
|
+
var boolean$1 = /^(?:true|false)$/i;
|
|
1186
|
+
var lowercase = /^[^A-Z]*$/;
|
|
1187
|
+
var uppercase = /^[^a-z]*$/;
|
|
1188
|
+
var $ZodCheck = $constructor("$ZodCheck", (inst, def) => {
|
|
1189
|
+
var _a;
|
|
1190
|
+
inst._zod ?? (inst._zod = {});
|
|
1191
|
+
inst._zod.def = def;
|
|
1192
|
+
(_a = inst._zod).onattach ?? (_a.onattach = []);
|
|
1193
|
+
});
|
|
1194
|
+
var _whenHasLength = (payload) => {
|
|
1195
|
+
const val = payload.value;
|
|
1196
|
+
return !nullish(val) && val.length !== void 0;
|
|
1197
|
+
};
|
|
1198
|
+
var $ZodCheckMaxLength = $constructor("$ZodCheckMaxLength", (inst, def) => {
|
|
1199
|
+
var _a;
|
|
1200
|
+
$ZodCheck.init(inst, def);
|
|
1201
|
+
(_a = inst._zod.def).when ?? (_a.when = _whenHasLength);
|
|
1202
|
+
inst._zod.check = (payload) => {
|
|
1203
|
+
const input = payload.value;
|
|
1204
|
+
const units = input.length;
|
|
1205
|
+
if ((typeof input === "string" && units > def.maximum ? codePointLength(input) : units) <= def.maximum) return;
|
|
1206
|
+
const origin = getLengthableOrigin(input);
|
|
1207
|
+
payload.issues.push({
|
|
1208
|
+
origin,
|
|
1209
|
+
code: "too_big",
|
|
1210
|
+
maximum: def.maximum,
|
|
1211
|
+
inclusive: true,
|
|
1212
|
+
input,
|
|
1213
|
+
inst,
|
|
1214
|
+
continue: !def.abort
|
|
1215
|
+
});
|
|
1216
|
+
};
|
|
1217
|
+
});
|
|
1218
|
+
var $ZodCheckMinLength = $constructor("$ZodCheckMinLength", (inst, def) => {
|
|
1219
|
+
var _a;
|
|
1220
|
+
$ZodCheck.init(inst, def);
|
|
1221
|
+
(_a = inst._zod.def).when ?? (_a.when = _whenHasLength);
|
|
1222
|
+
inst._zod.check = (payload) => {
|
|
1223
|
+
const input = payload.value;
|
|
1224
|
+
const units = input.length;
|
|
1225
|
+
if ((typeof input === "string" && units >= def.minimum && units < def.minimum * 2 ? codePointLength(input) : units) >= def.minimum) return;
|
|
1226
|
+
const origin = getLengthableOrigin(input);
|
|
1227
|
+
payload.issues.push({
|
|
1228
|
+
origin,
|
|
1229
|
+
code: "too_small",
|
|
1230
|
+
minimum: def.minimum,
|
|
1231
|
+
inclusive: true,
|
|
1232
|
+
input,
|
|
1233
|
+
inst,
|
|
1234
|
+
continue: !def.abort
|
|
1235
|
+
});
|
|
1236
|
+
};
|
|
1237
|
+
});
|
|
1238
|
+
var $ZodCheckLengthEquals = $constructor("$ZodCheckLengthEquals", (inst, def) => {
|
|
1239
|
+
var _a;
|
|
1240
|
+
$ZodCheck.init(inst, def);
|
|
1241
|
+
(_a = inst._zod.def).when ?? (_a.when = _whenHasLength);
|
|
1242
|
+
inst._zod.check = (payload) => {
|
|
1243
|
+
const input = payload.value;
|
|
1244
|
+
const units = input.length;
|
|
1245
|
+
const length = typeof input === "string" && units >= def.length && units <= def.length * 2 ? codePointLength(input) : units;
|
|
1246
|
+
if (length === def.length) return;
|
|
1247
|
+
const origin = getLengthableOrigin(input);
|
|
1248
|
+
const tooBig = length > def.length;
|
|
1249
|
+
payload.issues.push({
|
|
1250
|
+
origin,
|
|
1251
|
+
...tooBig ? {
|
|
1252
|
+
code: "too_big",
|
|
1253
|
+
maximum: def.length
|
|
1254
|
+
} : {
|
|
1255
|
+
code: "too_small",
|
|
1256
|
+
minimum: def.length
|
|
1257
|
+
},
|
|
1258
|
+
inclusive: true,
|
|
1259
|
+
exact: true,
|
|
1260
|
+
input: payload.value,
|
|
1261
|
+
inst,
|
|
1262
|
+
continue: !def.abort
|
|
1263
|
+
});
|
|
1264
|
+
};
|
|
1265
|
+
});
|
|
1266
|
+
var $ZodCheckStringFormat = $constructor("$ZodCheckStringFormat", (inst, def) => {
|
|
1267
|
+
var _a, _b;
|
|
1268
|
+
$ZodCheck.init(inst, def);
|
|
1269
|
+
if (def.pattern) (_a = inst._zod).check ?? (_a.check = (payload) => {
|
|
1270
|
+
def.pattern.lastIndex = 0;
|
|
1271
|
+
if (def.pattern.test(payload.value)) return;
|
|
1272
|
+
payload.issues.push({
|
|
1273
|
+
origin: "string",
|
|
1274
|
+
code: "invalid_format",
|
|
1275
|
+
format: def.format,
|
|
1276
|
+
input: payload.value,
|
|
1277
|
+
...def.pattern ? { pattern: def.pattern.toString() } : {},
|
|
1278
|
+
inst,
|
|
1279
|
+
continue: !def.abort
|
|
1280
|
+
});
|
|
1281
|
+
});
|
|
1282
|
+
else (_b = inst._zod).check ?? (_b.check = () => {});
|
|
1283
|
+
});
|
|
1284
|
+
var $ZodCheckRegex = $constructor("$ZodCheckRegex", (inst, def) => {
|
|
1285
|
+
$ZodCheckStringFormat.init(inst, def);
|
|
1286
|
+
inst._zod.check = (payload) => {
|
|
1287
|
+
def.pattern.lastIndex = 0;
|
|
1288
|
+
if (def.pattern.test(payload.value)) return;
|
|
1289
|
+
payload.issues.push({
|
|
1290
|
+
origin: "string",
|
|
1291
|
+
code: "invalid_format",
|
|
1292
|
+
format: "regex",
|
|
1293
|
+
input: payload.value,
|
|
1294
|
+
pattern: def.pattern.toString(),
|
|
1295
|
+
inst,
|
|
1296
|
+
continue: !def.abort
|
|
1297
|
+
});
|
|
1298
|
+
};
|
|
1299
|
+
});
|
|
1300
|
+
var $ZodCheckLowerCase = $constructor("$ZodCheckLowerCase", (inst, def) => {
|
|
1301
|
+
def.pattern ?? (def.pattern = lowercase);
|
|
1302
|
+
$ZodCheckStringFormat.init(inst, def);
|
|
1303
|
+
});
|
|
1304
|
+
var $ZodCheckUpperCase = $constructor("$ZodCheckUpperCase", (inst, def) => {
|
|
1305
|
+
def.pattern ?? (def.pattern = uppercase);
|
|
1306
|
+
$ZodCheckStringFormat.init(inst, def);
|
|
1307
|
+
});
|
|
1308
|
+
var $ZodCheckIncludes = $constructor("$ZodCheckIncludes", (inst, def) => {
|
|
1309
|
+
$ZodCheck.init(inst, def);
|
|
1310
|
+
const escapedRegex = escapeRegex(def.includes);
|
|
1311
|
+
def.pattern = new RegExp(typeof def.position === "number" ? `^.{${def.position},}${escapedRegex}` : escapedRegex);
|
|
1312
|
+
inst._zod.check = (payload) => {
|
|
1313
|
+
if (payload.value.includes(def.includes, def.position)) return;
|
|
1314
|
+
payload.issues.push({
|
|
1315
|
+
origin: "string",
|
|
1316
|
+
code: "invalid_format",
|
|
1317
|
+
format: "includes",
|
|
1318
|
+
includes: def.includes,
|
|
1319
|
+
input: payload.value,
|
|
1320
|
+
inst,
|
|
1321
|
+
continue: !def.abort
|
|
1322
|
+
});
|
|
1323
|
+
};
|
|
1324
|
+
});
|
|
1325
|
+
var $ZodCheckStartsWith = $constructor("$ZodCheckStartsWith", (inst, def) => {
|
|
1326
|
+
$ZodCheck.init(inst, def);
|
|
1327
|
+
const pattern = new RegExp(`^${escapeRegex(def.prefix)}.*`);
|
|
1328
|
+
def.pattern ?? (def.pattern = pattern);
|
|
1329
|
+
inst._zod.check = (payload) => {
|
|
1330
|
+
if (payload.value.startsWith(def.prefix)) return;
|
|
1331
|
+
payload.issues.push({
|
|
1332
|
+
origin: "string",
|
|
1333
|
+
code: "invalid_format",
|
|
1334
|
+
format: "starts_with",
|
|
1335
|
+
prefix: def.prefix,
|
|
1336
|
+
input: payload.value,
|
|
1337
|
+
inst,
|
|
1338
|
+
continue: !def.abort
|
|
1339
|
+
});
|
|
1340
|
+
};
|
|
1341
|
+
});
|
|
1342
|
+
var $ZodCheckEndsWith = $constructor("$ZodCheckEndsWith", (inst, def) => {
|
|
1343
|
+
$ZodCheck.init(inst, def);
|
|
1344
|
+
const pattern = new RegExp(`.*${escapeRegex(def.suffix)}$`);
|
|
1345
|
+
def.pattern ?? (def.pattern = pattern);
|
|
1346
|
+
inst._zod.check = (payload) => {
|
|
1347
|
+
if (payload.value.endsWith(def.suffix)) return;
|
|
1348
|
+
payload.issues.push({
|
|
1349
|
+
origin: "string",
|
|
1350
|
+
code: "invalid_format",
|
|
1351
|
+
format: "ends_with",
|
|
1352
|
+
suffix: def.suffix,
|
|
1353
|
+
input: payload.value,
|
|
1354
|
+
inst,
|
|
1355
|
+
continue: !def.abort
|
|
1356
|
+
});
|
|
1357
|
+
};
|
|
1358
|
+
});
|
|
1359
|
+
var $ZodCheckOverwrite = $constructor("$ZodCheckOverwrite", (inst, def) => {
|
|
1360
|
+
$ZodCheck.init(inst, def);
|
|
1361
|
+
inst._zod.check = (payload) => {
|
|
1362
|
+
payload.value = def.tx(payload.value);
|
|
1363
|
+
};
|
|
1364
|
+
});
|
|
1365
|
+
var version = {
|
|
1366
|
+
major: 4,
|
|
1367
|
+
minor: 6,
|
|
1368
|
+
patch: 5
|
|
1369
|
+
};
|
|
1370
|
+
var $ZodType = $constructor("$ZodType", (inst, def) => {
|
|
1371
|
+
var _a;
|
|
1372
|
+
inst ?? (inst = {});
|
|
1373
|
+
inst._zod.def = def;
|
|
1374
|
+
inst._zod.bag = inst._zod.bag || {};
|
|
1375
|
+
inst._zod.version = version;
|
|
1376
|
+
const defChecks = inst._zod.def.checks;
|
|
1377
|
+
const checks = inst._zod.traits.has("$ZodCheck") ? [inst, ...defChecks ?? []] : defChecks?.length ? [...defChecks] : [];
|
|
1378
|
+
for (const ch of checks) for (const fn of ch._zod.onattach) fn(inst);
|
|
1379
|
+
if (checks.length === 0) {
|
|
1380
|
+
(_a = inst._zod).deferred ?? (_a.deferred = []);
|
|
1381
|
+
inst._zod.deferred?.push(() => {
|
|
1382
|
+
inst._zod.run = inst._zod.parse;
|
|
1383
|
+
});
|
|
1384
|
+
} else {
|
|
1385
|
+
const runChecks = (payload, checks, ctx) => {
|
|
1386
|
+
if (payload.memo) return payload;
|
|
1387
|
+
let isAborted = aborted(payload);
|
|
1388
|
+
let asyncResult;
|
|
1389
|
+
for (const ch of checks) {
|
|
1390
|
+
if (ch._zod.def.when) {
|
|
1391
|
+
if (explicitlyAborted(payload)) continue;
|
|
1392
|
+
if (!ch._zod.def.when(payload)) continue;
|
|
1393
|
+
} else if (isAborted) continue;
|
|
1394
|
+
const currLen = payload.issues.length;
|
|
1395
|
+
const _ = ch._zod.check(payload);
|
|
1396
|
+
if (_ instanceof Promise && ctx?.async === false) throw new $ZodAsyncError();
|
|
1397
|
+
if (asyncResult || _ instanceof Promise) asyncResult = (asyncResult ?? Promise.resolve()).then(async () => {
|
|
1398
|
+
await _;
|
|
1399
|
+
if (payload.issues.length === currLen) return;
|
|
1400
|
+
attachSchema(payload.issues, currLen, inst);
|
|
1401
|
+
if (!isAborted) isAborted = aborted(payload, currLen);
|
|
1402
|
+
});
|
|
1403
|
+
else {
|
|
1404
|
+
if (payload.issues.length === currLen) continue;
|
|
1405
|
+
attachSchema(payload.issues, currLen, inst);
|
|
1406
|
+
if (!isAborted) isAborted = aborted(payload, currLen);
|
|
1407
|
+
}
|
|
1408
|
+
}
|
|
1409
|
+
if (asyncResult) return asyncResult.then(() => {
|
|
1410
|
+
return payload;
|
|
1411
|
+
});
|
|
1412
|
+
return payload;
|
|
1413
|
+
};
|
|
1414
|
+
const handleCanaryResult = (canary, payload, ctx) => {
|
|
1415
|
+
if (aborted(canary)) {
|
|
1416
|
+
canary.aborted = true;
|
|
1417
|
+
return canary;
|
|
1418
|
+
}
|
|
1419
|
+
const checkResult = runChecks(payload, checks, ctx);
|
|
1420
|
+
if (checkResult instanceof Promise) {
|
|
1421
|
+
if (ctx.async === false) throw new $ZodAsyncError();
|
|
1422
|
+
return checkResult.then((checkResult) => inst._zod.parse(checkResult, ctx));
|
|
1423
|
+
}
|
|
1424
|
+
return inst._zod.parse(checkResult, ctx);
|
|
1425
|
+
};
|
|
1426
|
+
inst._zod.run = (payload, ctx) => {
|
|
1427
|
+
if (ctx.skipChecks) return inst._zod.parse(payload, ctx);
|
|
1428
|
+
if (ctx.direction === "backward") {
|
|
1429
|
+
const canary = inst._zod.parse({
|
|
1430
|
+
value: payload.value,
|
|
1431
|
+
issues: []
|
|
1432
|
+
}, {
|
|
1433
|
+
...ctx,
|
|
1434
|
+
skipChecks: true
|
|
1435
|
+
});
|
|
1436
|
+
if (canary instanceof Promise) return canary.then((canary) => {
|
|
1437
|
+
return handleCanaryResult(canary, payload, ctx);
|
|
1438
|
+
});
|
|
1439
|
+
return handleCanaryResult(canary, payload, ctx);
|
|
1440
|
+
}
|
|
1441
|
+
const result = inst._zod.parse(payload, ctx);
|
|
1442
|
+
if (result instanceof Promise) {
|
|
1443
|
+
if (ctx.async === false) throw new $ZodAsyncError();
|
|
1444
|
+
return result.then((result) => runChecks(result, checks, ctx));
|
|
1445
|
+
}
|
|
1446
|
+
return runChecks(result, checks, ctx);
|
|
1447
|
+
};
|
|
1448
|
+
}
|
|
1449
|
+
}, {
|
|
1450
|
+
get "~standard"() {
|
|
1451
|
+
return hide(this, "~standard", standardProps(this));
|
|
1452
|
+
},
|
|
1453
|
+
set "~standard"(value) {
|
|
1454
|
+
own(this, "~standard", value);
|
|
1455
|
+
}
|
|
1456
|
+
});
|
|
1457
|
+
var toStandardResult = (r, ctx) => r.issues.length ? { issues: r.issues.map((iss) => finalizeIssue(iss, ctx, config())) } : { value: r.value };
|
|
1458
|
+
async function validateAsync(inst, value) {
|
|
1459
|
+
const ctx = { async: true };
|
|
1460
|
+
return toStandardResult(await inst._zod.run({
|
|
1461
|
+
value,
|
|
1462
|
+
issues: []
|
|
1463
|
+
}, ctx), ctx);
|
|
1464
|
+
}
|
|
1465
|
+
function standardProps(inst) {
|
|
1466
|
+
return {
|
|
1467
|
+
validate: (value) => {
|
|
1468
|
+
const ctx = { async: false };
|
|
1469
|
+
try {
|
|
1470
|
+
const r = inst._zod.run({
|
|
1471
|
+
value,
|
|
1472
|
+
issues: []
|
|
1473
|
+
}, ctx);
|
|
1474
|
+
if (!(r instanceof Promise)) return toStandardResult(r, ctx);
|
|
1475
|
+
} catch (_) {}
|
|
1476
|
+
return validateAsync(inst, value);
|
|
1477
|
+
},
|
|
1478
|
+
vendor: "zod",
|
|
1479
|
+
version: 1
|
|
1480
|
+
};
|
|
1481
|
+
}
|
|
1482
|
+
var $ZodString = $constructor("$ZodString", (inst, def) => {
|
|
1483
|
+
$ZodType.init(inst, def);
|
|
1484
|
+
inst._zod.pattern = def.pattern ?? anyString;
|
|
1485
|
+
inst._zod.parse = (payload, _) => {
|
|
1486
|
+
if (def.coerce) try {
|
|
1487
|
+
payload.value = String(payload.value);
|
|
1488
|
+
} catch (_) {}
|
|
1489
|
+
if (typeof payload.value === "string") return payload;
|
|
1490
|
+
payload.issues.push({
|
|
1491
|
+
expected: "string",
|
|
1492
|
+
code: "invalid_type",
|
|
1493
|
+
input: payload.value,
|
|
1494
|
+
inst
|
|
1495
|
+
});
|
|
1496
|
+
return payload;
|
|
1497
|
+
};
|
|
1498
|
+
});
|
|
1499
|
+
var $ZodStringFormat = $constructor("$ZodStringFormat", (inst, def) => {
|
|
1500
|
+
$ZodCheckStringFormat.init(inst, def);
|
|
1501
|
+
$ZodString.init(inst, def);
|
|
1502
|
+
});
|
|
1503
|
+
var $ZodGUID = $constructor("$ZodGUID", (inst, def) => {
|
|
1504
|
+
def.pattern ?? (def.pattern = guid);
|
|
1505
|
+
$ZodStringFormat.init(inst, def);
|
|
1506
|
+
});
|
|
1507
|
+
var $ZodUUID = $constructor("$ZodUUID", (inst, def) => {
|
|
1508
|
+
if (def.version) {
|
|
1509
|
+
const v = {
|
|
1510
|
+
v1: 1,
|
|
1511
|
+
v2: 2,
|
|
1512
|
+
v3: 3,
|
|
1513
|
+
v4: 4,
|
|
1514
|
+
v5: 5,
|
|
1515
|
+
v6: 6,
|
|
1516
|
+
v7: 7,
|
|
1517
|
+
v8: 8
|
|
1518
|
+
}[def.version];
|
|
1519
|
+
if (v === void 0) throw new Error(`Invalid UUID version: "${def.version}"`);
|
|
1520
|
+
def.pattern ?? (def.pattern = uuid(v));
|
|
1521
|
+
} else def.pattern ?? (def.pattern = uuid());
|
|
1522
|
+
$ZodStringFormat.init(inst, def);
|
|
1523
|
+
});
|
|
1524
|
+
var $ZodEmail = $constructor("$ZodEmail", (inst, def) => {
|
|
1525
|
+
def.pattern ?? (def.pattern = email);
|
|
1526
|
+
$ZodStringFormat.init(inst, def);
|
|
1527
|
+
});
|
|
1528
|
+
function canParseURL(input) {
|
|
1529
|
+
try {
|
|
1530
|
+
if (typeof URL !== "undefined" && typeof URL.canParse === "function") return URL.canParse(input);
|
|
1531
|
+
new URL(input);
|
|
1532
|
+
return true;
|
|
1533
|
+
} catch {
|
|
1534
|
+
return false;
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
function validateURL(trimmed, def) {
|
|
1538
|
+
if (!("normalize" in def) && !("hostname" in def) && !("protocol" in def)) return canParseURL(trimmed) || 2;
|
|
1539
|
+
return parseURLObject(trimmed, def);
|
|
1540
|
+
}
|
|
1541
|
+
function parseURLObject(trimmed, def) {
|
|
1542
|
+
if (!def.normalize && def.protocol?.source === httpProtocol.source && !/^https?:\/\//i.test(trimmed)) return 1;
|
|
1543
|
+
try {
|
|
1544
|
+
if (typeof URL !== "undefined") {
|
|
1545
|
+
const URLStatic = URL;
|
|
1546
|
+
if (typeof URLStatic.parse === "function") return URLStatic.parse(trimmed) ?? 2;
|
|
1547
|
+
}
|
|
1548
|
+
return new URL(trimmed);
|
|
1549
|
+
} catch {
|
|
1550
|
+
return 2;
|
|
1551
|
+
}
|
|
1552
|
+
}
|
|
1553
|
+
var asciiTabOrNewline = /[\t\n\r]/g;
|
|
1554
|
+
function stripTabAndNewline(value) {
|
|
1555
|
+
return value.replace(asciiTabOrNewline, "");
|
|
1556
|
+
}
|
|
1557
|
+
function urlHostnameOk(url, hostname) {
|
|
1558
|
+
hostname.lastIndex = 0;
|
|
1559
|
+
return hostname.test(url.hostname);
|
|
1560
|
+
}
|
|
1561
|
+
function urlProtocolOk(url, protocol) {
|
|
1562
|
+
protocol.lastIndex = 0;
|
|
1563
|
+
return protocol.test(url.protocol.endsWith(":") ? url.protocol.slice(0, -1) : url.protocol);
|
|
1564
|
+
}
|
|
1565
|
+
var $ZodURL = $constructor("$ZodURL", (inst, def) => {
|
|
1566
|
+
$ZodStringFormat.init(inst, def);
|
|
1567
|
+
inst._zod.check = (payload) => {
|
|
1568
|
+
try {
|
|
1569
|
+
const trimmed = payload.value.trim();
|
|
1570
|
+
const url = validateURL(trimmed, def);
|
|
1571
|
+
if (url === 1) {
|
|
1572
|
+
payload.issues.push({
|
|
1573
|
+
code: "invalid_format",
|
|
1574
|
+
format: "url",
|
|
1575
|
+
note: "Invalid URL format",
|
|
1576
|
+
input: payload.value,
|
|
1577
|
+
inst,
|
|
1578
|
+
continue: !def.abort
|
|
1579
|
+
});
|
|
1580
|
+
return;
|
|
1581
|
+
}
|
|
1582
|
+
if (url === 2) {
|
|
1583
|
+
payload.issues.push({
|
|
1584
|
+
code: "invalid_format",
|
|
1585
|
+
format: "url",
|
|
1586
|
+
input: payload.value,
|
|
1587
|
+
inst,
|
|
1588
|
+
continue: !def.abort
|
|
1589
|
+
});
|
|
1590
|
+
return;
|
|
1591
|
+
}
|
|
1592
|
+
if (url === true) {
|
|
1593
|
+
payload.value = stripTabAndNewline(trimmed);
|
|
1594
|
+
return;
|
|
1595
|
+
}
|
|
1596
|
+
if (def.hostname && !urlHostnameOk(url, def.hostname)) payload.issues.push({
|
|
1597
|
+
code: "invalid_format",
|
|
1598
|
+
format: "url",
|
|
1599
|
+
note: "Invalid hostname",
|
|
1600
|
+
pattern: def.hostname.source,
|
|
1601
|
+
input: payload.value,
|
|
1602
|
+
inst,
|
|
1603
|
+
continue: !def.abort
|
|
1604
|
+
});
|
|
1605
|
+
if (def.protocol && !urlProtocolOk(url, def.protocol)) payload.issues.push({
|
|
1606
|
+
code: "invalid_format",
|
|
1607
|
+
format: "url",
|
|
1608
|
+
note: "Invalid protocol",
|
|
1609
|
+
pattern: def.protocol.source,
|
|
1610
|
+
input: payload.value,
|
|
1611
|
+
inst,
|
|
1612
|
+
continue: !def.abort
|
|
1613
|
+
});
|
|
1614
|
+
payload.value = def.normalize ? url.href : stripTabAndNewline(trimmed);
|
|
1615
|
+
return;
|
|
1616
|
+
} catch (_) {
|
|
1617
|
+
payload.issues.push({
|
|
1618
|
+
code: "invalid_format",
|
|
1619
|
+
format: "url",
|
|
1620
|
+
input: payload.value,
|
|
1621
|
+
inst,
|
|
1622
|
+
continue: !def.abort
|
|
1623
|
+
});
|
|
1624
|
+
}
|
|
1625
|
+
};
|
|
1626
|
+
});
|
|
1627
|
+
var $ZodEmoji = $constructor("$ZodEmoji", (inst, def) => {
|
|
1628
|
+
def.pattern ?? (def.pattern = emoji());
|
|
1629
|
+
$ZodStringFormat.init(inst, def);
|
|
1630
|
+
});
|
|
1631
|
+
var $ZodNanoID = $constructor("$ZodNanoID", (inst, def) => {
|
|
1632
|
+
if (def.length !== void 0 && (!Number.isInteger(def.length) || def.length < 1)) throw new Error(`Invalid nanoid length: ${def.length}`);
|
|
1633
|
+
def.pattern ?? (def.pattern = def.length === void 0 ? nanoid : nanoidOfLength(def.length));
|
|
1634
|
+
$ZodStringFormat.init(inst, def);
|
|
1635
|
+
});
|
|
1636
|
+
var $ZodCUID = $constructor("$ZodCUID", (inst, def) => {
|
|
1637
|
+
def.pattern ?? (def.pattern = cuid);
|
|
1638
|
+
$ZodStringFormat.init(inst, def);
|
|
1639
|
+
});
|
|
1640
|
+
var $ZodCUID2 = $constructor("$ZodCUID2", (inst, def) => {
|
|
1641
|
+
def.pattern ?? (def.pattern = cuid2);
|
|
1642
|
+
$ZodStringFormat.init(inst, def);
|
|
1643
|
+
});
|
|
1644
|
+
var $ZodULID = $constructor("$ZodULID", (inst, def) => {
|
|
1645
|
+
def.pattern ?? (def.pattern = ulid);
|
|
1646
|
+
$ZodStringFormat.init(inst, def);
|
|
1647
|
+
});
|
|
1648
|
+
var $ZodXID = $constructor("$ZodXID", (inst, def) => {
|
|
1649
|
+
def.pattern ?? (def.pattern = xid);
|
|
1650
|
+
$ZodStringFormat.init(inst, def);
|
|
1651
|
+
});
|
|
1652
|
+
var $ZodKSUID = $constructor("$ZodKSUID", (inst, def) => {
|
|
1653
|
+
def.pattern ?? (def.pattern = ksuid);
|
|
1654
|
+
$ZodStringFormat.init(inst, def);
|
|
1655
|
+
});
|
|
1656
|
+
var $ZodISODateTime = $constructor("$ZodISODateTime", (inst, def) => {
|
|
1657
|
+
def.pattern ?? (def.pattern = datetime(def));
|
|
1658
|
+
$ZodStringFormat.init(inst, def);
|
|
1659
|
+
});
|
|
1660
|
+
var $ZodISODate = $constructor("$ZodISODate", (inst, def) => {
|
|
1661
|
+
def.pattern ?? (def.pattern = date);
|
|
1662
|
+
$ZodStringFormat.init(inst, def);
|
|
1663
|
+
});
|
|
1664
|
+
var $ZodISOTime = $constructor("$ZodISOTime", (inst, def) => {
|
|
1665
|
+
def.pattern ?? (def.pattern = time(def));
|
|
1666
|
+
$ZodStringFormat.init(inst, def);
|
|
1667
|
+
});
|
|
1668
|
+
var $ZodISODuration = $constructor("$ZodISODuration", (inst, def) => {
|
|
1669
|
+
def.pattern ?? (def.pattern = duration);
|
|
1670
|
+
$ZodStringFormat.init(inst, def);
|
|
1671
|
+
});
|
|
1672
|
+
var $ZodIPv4 = $constructor("$ZodIPv4", (inst, def) => {
|
|
1673
|
+
def.pattern ?? (def.pattern = ipv4);
|
|
1674
|
+
$ZodStringFormat.init(inst, def);
|
|
1675
|
+
});
|
|
1676
|
+
var ipv6Alphabet = /^[0-9a-fA-F:.]+$/;
|
|
1677
|
+
function isValidIPv6(value) {
|
|
1678
|
+
if (!ipv6Alphabet.test(value)) return false;
|
|
1679
|
+
return canParseURL(`http://[${value}]`);
|
|
1680
|
+
}
|
|
1681
|
+
var $ZodIPv6 = $constructor("$ZodIPv6", (inst, def) => {
|
|
1682
|
+
def.pattern ?? (def.pattern = ipv6);
|
|
1683
|
+
$ZodStringFormat.init(inst, def);
|
|
1684
|
+
inst._zod.check = (payload) => {
|
|
1685
|
+
if (!isValidIPv6(payload.value)) payload.issues.push({
|
|
1686
|
+
code: "invalid_format",
|
|
1687
|
+
format: "ipv6",
|
|
1688
|
+
input: payload.value,
|
|
1689
|
+
inst,
|
|
1690
|
+
continue: !def.abort
|
|
1691
|
+
});
|
|
1692
|
+
};
|
|
1693
|
+
});
|
|
1694
|
+
var $ZodCIDRv4 = $constructor("$ZodCIDRv4", (inst, def) => {
|
|
1695
|
+
def.pattern ?? (def.pattern = cidrv4);
|
|
1696
|
+
$ZodStringFormat.init(inst, def);
|
|
1697
|
+
});
|
|
1698
|
+
function isValidCIDRv6(value) {
|
|
1699
|
+
const parts = value.split("/");
|
|
1700
|
+
if (parts.length !== 2) return false;
|
|
1701
|
+
const [address, prefix] = parts;
|
|
1702
|
+
if (!prefix) return false;
|
|
1703
|
+
const prefixNum = Number(prefix);
|
|
1704
|
+
if (`${prefixNum}` !== prefix) return false;
|
|
1705
|
+
if (prefixNum < 0 || prefixNum > 128) return false;
|
|
1706
|
+
return isValidIPv6(address);
|
|
1707
|
+
}
|
|
1708
|
+
var $ZodCIDRv6 = $constructor("$ZodCIDRv6", (inst, def) => {
|
|
1709
|
+
def.pattern ?? (def.pattern = cidrv6);
|
|
1710
|
+
$ZodStringFormat.init(inst, def);
|
|
1711
|
+
inst._zod.check = (payload) => {
|
|
1712
|
+
if (!isValidCIDRv6(payload.value)) payload.issues.push({
|
|
1713
|
+
code: "invalid_format",
|
|
1714
|
+
format: "cidrv6",
|
|
1715
|
+
input: payload.value,
|
|
1716
|
+
inst,
|
|
1717
|
+
continue: !def.abort
|
|
1718
|
+
});
|
|
1719
|
+
};
|
|
1720
|
+
});
|
|
1721
|
+
function isValidBase64(data) {
|
|
1722
|
+
if (data === "") return true;
|
|
1723
|
+
if (/\s/.test(data)) return false;
|
|
1724
|
+
if (data.length % 4 !== 0) return false;
|
|
1725
|
+
try {
|
|
1726
|
+
atob(data);
|
|
1727
|
+
return true;
|
|
1728
|
+
} catch {
|
|
1729
|
+
return false;
|
|
1730
|
+
}
|
|
1731
|
+
}
|
|
1732
|
+
var base64Charset = /^[0-9a-zA-Z+/]*={0,2}$/;
|
|
1733
|
+
var $ZodBase64 = $constructor("$ZodBase64", (inst, def) => {
|
|
1734
|
+
def.pattern ?? (def.pattern = base64Charset);
|
|
1735
|
+
$ZodStringFormat.init(inst, def);
|
|
1736
|
+
inst._zod.check = (payload) => {
|
|
1737
|
+
if (isValidBase64(payload.value)) return;
|
|
1738
|
+
payload.issues.push({
|
|
1739
|
+
code: "invalid_format",
|
|
1740
|
+
format: "base64",
|
|
1741
|
+
input: payload.value,
|
|
1742
|
+
inst,
|
|
1743
|
+
continue: !def.abort
|
|
1744
|
+
});
|
|
1745
|
+
};
|
|
1746
|
+
});
|
|
1747
|
+
var base64urlCharset = /^[A-Za-z0-9_-]*$/;
|
|
1748
|
+
function isValidBase64URL(data) {
|
|
1749
|
+
if (!base64urlCharset.test(data)) return false;
|
|
1750
|
+
const base64 = data.replace(/[-_]/g, (c) => c === "-" ? "+" : "/");
|
|
1751
|
+
return isValidBase64(base64.padEnd(Math.ceil(base64.length / 4) * 4, "="));
|
|
1752
|
+
}
|
|
1753
|
+
var $ZodBase64URL = $constructor("$ZodBase64URL", (inst, def) => {
|
|
1754
|
+
def.pattern ?? (def.pattern = base64urlCharset);
|
|
1755
|
+
$ZodStringFormat.init(inst, def);
|
|
1756
|
+
inst._zod.check = (payload) => {
|
|
1757
|
+
if (isValidBase64URL(payload.value)) return;
|
|
1758
|
+
payload.issues.push({
|
|
1759
|
+
code: "invalid_format",
|
|
1760
|
+
format: "base64url",
|
|
1761
|
+
input: payload.value,
|
|
1762
|
+
inst,
|
|
1763
|
+
continue: !def.abort
|
|
1764
|
+
});
|
|
1765
|
+
};
|
|
1766
|
+
});
|
|
1767
|
+
var $ZodE164 = $constructor("$ZodE164", (inst, def) => {
|
|
1768
|
+
def.pattern ?? (def.pattern = e164);
|
|
1769
|
+
$ZodStringFormat.init(inst, def);
|
|
1770
|
+
});
|
|
1771
|
+
function isValidJWT(token, algorithm = null) {
|
|
1772
|
+
try {
|
|
1773
|
+
const tokensParts = token.split(".");
|
|
1774
|
+
if (tokensParts.length !== 3) return false;
|
|
1775
|
+
const [header] = tokensParts;
|
|
1776
|
+
if (!header) return false;
|
|
1777
|
+
const parsedHeader = JSON.parse(atob(header));
|
|
1778
|
+
if ("typ" in parsedHeader && parsedHeader?.typ !== "JWT") return false;
|
|
1779
|
+
if (!parsedHeader.alg) return false;
|
|
1780
|
+
if (algorithm && (!("alg" in parsedHeader) || parsedHeader.alg !== algorithm)) return false;
|
|
1781
|
+
return true;
|
|
1782
|
+
} catch {
|
|
1783
|
+
return false;
|
|
1784
|
+
}
|
|
1785
|
+
}
|
|
1786
|
+
var $ZodJWT = $constructor("$ZodJWT", (inst, def) => {
|
|
1787
|
+
$ZodStringFormat.init(inst, def);
|
|
1788
|
+
inst._zod.check = (payload) => {
|
|
1789
|
+
if (isValidJWT(payload.value, def.alg)) return;
|
|
1790
|
+
payload.issues.push({
|
|
1791
|
+
code: "invalid_format",
|
|
1792
|
+
format: "jwt",
|
|
1793
|
+
input: payload.value,
|
|
1794
|
+
inst,
|
|
1795
|
+
continue: !def.abort
|
|
1796
|
+
});
|
|
1797
|
+
};
|
|
1798
|
+
});
|
|
1799
|
+
var $ZodBoolean = $constructor("$ZodBoolean", (inst, def) => {
|
|
1800
|
+
$ZodType.init(inst, def);
|
|
1801
|
+
inst._zod.pattern = boolean$1;
|
|
1802
|
+
inst._zod.parse = (payload, _ctx) => {
|
|
1803
|
+
if (def.coerce) try {
|
|
1804
|
+
payload.value = Boolean(payload.value);
|
|
1805
|
+
} catch (_) {}
|
|
1806
|
+
const input = payload.value;
|
|
1807
|
+
if (typeof input === "boolean") return payload;
|
|
1808
|
+
payload.issues.push({
|
|
1809
|
+
expected: "boolean",
|
|
1810
|
+
code: "invalid_type",
|
|
1811
|
+
input,
|
|
1812
|
+
inst
|
|
1813
|
+
});
|
|
1814
|
+
return payload;
|
|
1815
|
+
};
|
|
1816
|
+
});
|
|
1817
|
+
function handleArrayResult(result, final, index) {
|
|
1818
|
+
if (result.issues.length) final.issues.push(...prefixIssues(index, result.issues));
|
|
1819
|
+
final.value[index] = result.value;
|
|
1820
|
+
}
|
|
1821
|
+
var $ZodArray = $constructor("$ZodArray", (inst, def) => {
|
|
1822
|
+
$ZodType.init(inst, def);
|
|
1823
|
+
const memo = globalConfig.memoizer;
|
|
1824
|
+
memo?.attach(inst);
|
|
1825
|
+
inst._zod.parse = (payload, ctx) => {
|
|
1826
|
+
const input = payload.value;
|
|
1827
|
+
if (!Array.isArray(input)) {
|
|
1828
|
+
payload.issues.push({
|
|
1829
|
+
expected: "array",
|
|
1830
|
+
code: "invalid_type",
|
|
1831
|
+
input,
|
|
1832
|
+
inst
|
|
1833
|
+
});
|
|
1834
|
+
return payload;
|
|
1835
|
+
}
|
|
1836
|
+
payload.value = memo ? memo.alloc(inst, payload, Array(input.length), ctx) : Array(input.length);
|
|
1837
|
+
const proms = [];
|
|
1838
|
+
const abortEarly = ctx?.abortEarly;
|
|
1839
|
+
for (let i = 0; i < input.length; i++) {
|
|
1840
|
+
const item = input[i];
|
|
1841
|
+
const result = def.element._zod.run({
|
|
1842
|
+
value: item,
|
|
1843
|
+
issues: []
|
|
1844
|
+
}, ctx);
|
|
1845
|
+
if (result instanceof Promise) proms.push(result.then((result) => handleArrayResult(result, payload, i)));
|
|
1846
|
+
else {
|
|
1847
|
+
handleArrayResult(result, payload, i);
|
|
1848
|
+
if (abortEarly && result.issues.length !== 0 && aborted(result)) break;
|
|
1849
|
+
}
|
|
1850
|
+
}
|
|
1851
|
+
if (proms.length) return Promise.all(proms).then(() => payload);
|
|
1852
|
+
return payload;
|
|
1853
|
+
};
|
|
1854
|
+
});
|
|
1855
|
+
function handleUnionResults(results, final, inst, ctx) {
|
|
1856
|
+
for (const result of results) if (result.issues.length === 0) {
|
|
1857
|
+
final.value = result.value;
|
|
1858
|
+
return final;
|
|
1859
|
+
}
|
|
1860
|
+
const nonaborted = results.filter((r) => !aborted(r));
|
|
1861
|
+
if (nonaborted.length === 1) {
|
|
1862
|
+
final.value = nonaborted[0].value;
|
|
1863
|
+
return nonaborted[0];
|
|
1864
|
+
}
|
|
1865
|
+
final.issues.push({
|
|
1866
|
+
code: "invalid_union",
|
|
1867
|
+
input: final.value,
|
|
1868
|
+
inst,
|
|
1869
|
+
errors: results.map((result) => result.issues.map((iss) => finalizeIssue(iss, ctx, config())))
|
|
1870
|
+
});
|
|
1871
|
+
return final;
|
|
1872
|
+
}
|
|
1873
|
+
var $ZodUnion = $constructor("$ZodUnion", (inst, def) => {
|
|
1874
|
+
$ZodType.init(inst, def);
|
|
1875
|
+
defineLazyInternal(inst, "optin", (zod) => zod.def.options.some((o) => o._zod.optin === "defaulted") ? "defaulted" : zod.def.options.some((o) => o._zod.optin !== void 0) ? "optional" : void 0);
|
|
1876
|
+
defineLazyInternal(inst, "optout", (zod) => zod.def.options.some((o) => o._zod.optout === "optional") ? "optional" : void 0);
|
|
1877
|
+
defineLazyInternal(inst, "values", (zod) => {
|
|
1878
|
+
if (zod.def.options.every((o) => o._zod.values)) return new Set(zod.def.options.flatMap((option) => Array.from(option._zod.values)));
|
|
1879
|
+
});
|
|
1880
|
+
defineLazyInternal(inst, "pattern", (zod) => {
|
|
1881
|
+
if (zod.def.options.every((o) => o._zod.pattern)) {
|
|
1882
|
+
const patterns = zod.def.options.map((o) => o._zod.pattern);
|
|
1883
|
+
return new RegExp(`^(${patterns.map((p) => cleanRegex(p.source)).join("|")})$`);
|
|
1884
|
+
}
|
|
1885
|
+
});
|
|
1886
|
+
const first = def.options.length === 1 ? def.options[0]._zod.run : null;
|
|
1887
|
+
inst._zod.parse = (payload, ctx) => {
|
|
1888
|
+
if (first) return first(payload, ctx);
|
|
1889
|
+
let async = false;
|
|
1890
|
+
const results = [];
|
|
1891
|
+
for (const option of def.options) {
|
|
1892
|
+
const result = option._zod.run({
|
|
1893
|
+
value: payload.value,
|
|
1894
|
+
issues: []
|
|
1895
|
+
}, ctx);
|
|
1896
|
+
if (result instanceof Promise) {
|
|
1897
|
+
results.push(result);
|
|
1898
|
+
async = true;
|
|
1899
|
+
} else {
|
|
1900
|
+
if (result.issues.length === 0) return result;
|
|
1901
|
+
results.push(result);
|
|
1902
|
+
}
|
|
1903
|
+
}
|
|
1904
|
+
if (!async) return handleUnionResults(results, payload, inst, ctx);
|
|
1905
|
+
return Promise.all(results).then((results) => {
|
|
1906
|
+
return handleUnionResults(results, payload, inst, ctx);
|
|
1907
|
+
});
|
|
1908
|
+
};
|
|
1909
|
+
});
|
|
1910
|
+
var $ZodIntersection = $constructor("$ZodIntersection", (inst, def) => {
|
|
1911
|
+
$ZodType.init(inst, def);
|
|
1912
|
+
inst._zod.parse = (payload, ctx) => {
|
|
1913
|
+
const input = payload.value;
|
|
1914
|
+
const left = def.left._zod.run({
|
|
1915
|
+
value: input,
|
|
1916
|
+
issues: []
|
|
1917
|
+
}, ctx);
|
|
1918
|
+
const right = def.right._zod.run({
|
|
1919
|
+
value: input,
|
|
1920
|
+
issues: []
|
|
1921
|
+
}, ctx);
|
|
1922
|
+
if (left instanceof Promise || right instanceof Promise) return Promise.all([left, right]).then(([left, right]) => {
|
|
1923
|
+
return handleIntersectionResults(payload, left, right);
|
|
1924
|
+
});
|
|
1925
|
+
return handleIntersectionResults(payload, left, right);
|
|
1926
|
+
};
|
|
1927
|
+
});
|
|
1928
|
+
function mergeValues(a, b) {
|
|
1929
|
+
if (a === b) return {
|
|
1930
|
+
valid: true,
|
|
1931
|
+
data: a
|
|
1932
|
+
};
|
|
1933
|
+
if (a instanceof Date && b instanceof Date && +a === +b) return {
|
|
1934
|
+
valid: true,
|
|
1935
|
+
data: a
|
|
1936
|
+
};
|
|
1937
|
+
if (isPlainObject(a) && isPlainObject(b)) {
|
|
1938
|
+
const bKeys = Object.keys(b);
|
|
1939
|
+
const sharedKeys = Object.keys(a).filter((key) => bKeys.indexOf(key) !== -1);
|
|
1940
|
+
const newObj = {
|
|
1941
|
+
...a,
|
|
1942
|
+
...b
|
|
1943
|
+
};
|
|
1944
|
+
if (Object.prototype.hasOwnProperty.call(newObj, "__proto__")) delete newObj.__proto__;
|
|
1945
|
+
for (const key of sharedKeys) {
|
|
1946
|
+
if (key === "__proto__") continue;
|
|
1947
|
+
const sharedValue = mergeValues(a[key], b[key]);
|
|
1948
|
+
if (!sharedValue.valid) return {
|
|
1949
|
+
valid: false,
|
|
1950
|
+
mergeErrorPath: [key, ...sharedValue.mergeErrorPath]
|
|
1951
|
+
};
|
|
1952
|
+
newObj[key] = sharedValue.data;
|
|
1953
|
+
}
|
|
1954
|
+
return {
|
|
1955
|
+
valid: true,
|
|
1956
|
+
data: newObj
|
|
1957
|
+
};
|
|
1958
|
+
}
|
|
1959
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
1960
|
+
if (a.length !== b.length) return {
|
|
1961
|
+
valid: false,
|
|
1962
|
+
mergeErrorPath: []
|
|
1963
|
+
};
|
|
1964
|
+
const newArray = [];
|
|
1965
|
+
for (let index = 0; index < a.length; index++) {
|
|
1966
|
+
const itemA = a[index];
|
|
1967
|
+
const itemB = b[index];
|
|
1968
|
+
const sharedValue = mergeValues(itemA, itemB);
|
|
1969
|
+
if (!sharedValue.valid) return {
|
|
1970
|
+
valid: false,
|
|
1971
|
+
mergeErrorPath: [index, ...sharedValue.mergeErrorPath]
|
|
1972
|
+
};
|
|
1973
|
+
newArray.push(sharedValue.data);
|
|
1974
|
+
}
|
|
1975
|
+
return {
|
|
1976
|
+
valid: true,
|
|
1977
|
+
data: newArray
|
|
1978
|
+
};
|
|
1979
|
+
}
|
|
1980
|
+
return {
|
|
1981
|
+
valid: false,
|
|
1982
|
+
mergeErrorPath: []
|
|
1983
|
+
};
|
|
1984
|
+
}
|
|
1985
|
+
function handleIntersectionResults(result, left, right) {
|
|
1986
|
+
const unrecKeys = new Map();
|
|
1987
|
+
let unrecIssue;
|
|
1988
|
+
const keyIssues = new Map();
|
|
1989
|
+
const collect = (iss, side) => {
|
|
1990
|
+
let keys;
|
|
1991
|
+
if (iss.code === "unrecognized_keys" && !iss.path?.length) {
|
|
1992
|
+
unrecIssue ?? (unrecIssue = iss);
|
|
1993
|
+
keys = iss.keys;
|
|
1994
|
+
} else if (iss.code === "invalid_key" && iss.origin === "record" && iss.path?.length === 1) {
|
|
1995
|
+
const k = String(iss.path[0]);
|
|
1996
|
+
if (!keyIssues.has(k)) keyIssues.set(k, iss);
|
|
1997
|
+
keys = [k];
|
|
1998
|
+
} else return false;
|
|
1999
|
+
for (const k of keys) {
|
|
2000
|
+
if (!unrecKeys.has(k)) unrecKeys.set(k, {});
|
|
2001
|
+
unrecKeys.get(k)[side] = true;
|
|
2002
|
+
}
|
|
2003
|
+
return true;
|
|
2004
|
+
};
|
|
2005
|
+
for (const iss of left.issues) if (!collect(iss, "l")) result.issues.push(iss);
|
|
2006
|
+
for (const iss of right.issues) if (!collect(iss, "r")) result.issues.push(iss);
|
|
2007
|
+
const bothKeys = [...unrecKeys].filter(([, f]) => f.l && f.r).map(([k]) => k);
|
|
2008
|
+
if (bothKeys.length) {
|
|
2009
|
+
const aggregated = unrecIssue ? bothKeys.filter((k) => unrecIssue.keys.includes(k)) : [];
|
|
2010
|
+
if (aggregated.length) result.issues.push({
|
|
2011
|
+
...unrecIssue,
|
|
2012
|
+
keys: aggregated
|
|
2013
|
+
});
|
|
2014
|
+
for (const k of bothKeys) if (!aggregated.includes(k) && keyIssues.has(k)) result.issues.push(keyIssues.get(k));
|
|
2015
|
+
}
|
|
2016
|
+
const merged = mergeValues(left.value, right.value);
|
|
2017
|
+
if (!merged.valid) {
|
|
2018
|
+
if (aborted(result)) return result;
|
|
2019
|
+
throw new Error(`Unmergable intersection. Error path: ${JSON.stringify(merged.mergeErrorPath)}`);
|
|
2020
|
+
}
|
|
2021
|
+
result.value = merged.data;
|
|
2022
|
+
return result;
|
|
2023
|
+
}
|
|
2024
|
+
var $ZodTransform = $constructor("$ZodTransform", (inst, def) => {
|
|
2025
|
+
$ZodType.init(inst, def);
|
|
2026
|
+
inst._zod.optin = "optional";
|
|
2027
|
+
globalConfig.memoizer?.guard(inst);
|
|
2028
|
+
inst._zod.parse = (payload, ctx) => {
|
|
2029
|
+
if (ctx.direction === "backward") throw new $ZodEncodeError(inst.constructor.name);
|
|
2030
|
+
const _out = def.transform(payload.value, payload);
|
|
2031
|
+
if (ctx.async) return (_out instanceof Promise ? _out : Promise.resolve(_out)).then((output) => {
|
|
2032
|
+
payload.value = output;
|
|
2033
|
+
return payload;
|
|
2034
|
+
});
|
|
2035
|
+
if (_out instanceof Promise) throw new $ZodAsyncError();
|
|
2036
|
+
payload.value = _out;
|
|
2037
|
+
return payload;
|
|
2038
|
+
};
|
|
2039
|
+
});
|
|
2040
|
+
function handleOptionalResult(payload, result) {
|
|
2041
|
+
payload.value = result.issues.length ? void 0 : result.value;
|
|
2042
|
+
return payload;
|
|
2043
|
+
}
|
|
2044
|
+
var $ZodOptional = $constructor("$ZodOptional", (inst, def) => {
|
|
2045
|
+
$ZodType.init(inst, def);
|
|
2046
|
+
defineLazyInternal(inst, "optin", (zod) => zod.def.innerType._zod.optin === "defaulted" ? "defaulted" : "optional");
|
|
2047
|
+
inst._zod.optout = "optional";
|
|
2048
|
+
defineLazyInternal(inst, "values", (zod) => {
|
|
2049
|
+
const values = zod.def.innerType._zod.values;
|
|
2050
|
+
return values ? new Set([...values, void 0]) : void 0;
|
|
2051
|
+
});
|
|
2052
|
+
defineLazyInternal(inst, "pattern", (zod) => {
|
|
2053
|
+
const pattern = zod.def.innerType._zod.pattern;
|
|
2054
|
+
return pattern ? new RegExp(`^(${cleanRegex(pattern.source)})?$`) : void 0;
|
|
2055
|
+
});
|
|
2056
|
+
inst._zod.parse = (payload, ctx) => {
|
|
2057
|
+
if (payload.value === void 0) {
|
|
2058
|
+
if (def.innerType._zod.optin !== "defaulted") return payload;
|
|
2059
|
+
const result = def.innerType._zod.run({
|
|
2060
|
+
value: payload.value,
|
|
2061
|
+
issues: []
|
|
2062
|
+
}, ctx);
|
|
2063
|
+
if (result instanceof Promise) return result.then((result) => handleOptionalResult(payload, result));
|
|
2064
|
+
return handleOptionalResult(payload, result);
|
|
2065
|
+
}
|
|
2066
|
+
return def.innerType._zod.run(payload, ctx);
|
|
2067
|
+
};
|
|
2068
|
+
});
|
|
2069
|
+
var $ZodExactOptional = $constructor("$ZodExactOptional", (inst, def) => {
|
|
2070
|
+
$ZodOptional.init(inst, def);
|
|
2071
|
+
defineLazyInternal(inst, "values", (zod) => zod.def.innerType._zod.values);
|
|
2072
|
+
defineLazyInternal(inst, "pattern", (zod) => zod.def.innerType._zod.pattern);
|
|
2073
|
+
inst._zod.parse = (payload, ctx) => {
|
|
2074
|
+
return def.innerType._zod.run(payload, ctx);
|
|
2075
|
+
};
|
|
2076
|
+
});
|
|
2077
|
+
var $ZodNullable = $constructor("$ZodNullable", (inst, def) => {
|
|
2078
|
+
$ZodType.init(inst, def);
|
|
2079
|
+
defineLazyInternal(inst, "optin", (zod) => zod.def.innerType._zod.optin);
|
|
2080
|
+
defineLazyInternal(inst, "optout", (zod) => zod.def.innerType._zod.optout);
|
|
2081
|
+
defineLazyInternal(inst, "pattern", (zod) => {
|
|
2082
|
+
const pattern = zod.def.innerType._zod.pattern;
|
|
2083
|
+
return pattern ? new RegExp(`^(${cleanRegex(pattern.source)}|null)$`) : void 0;
|
|
2084
|
+
});
|
|
2085
|
+
defineLazyInternal(inst, "values", (zod) => {
|
|
2086
|
+
return zod.def.innerType._zod.values ? new Set([...zod.def.innerType._zod.values, null]) : void 0;
|
|
2087
|
+
});
|
|
2088
|
+
inst._zod.parse = (payload, ctx) => {
|
|
2089
|
+
if (payload.value === null) return payload;
|
|
2090
|
+
return def.innerType._zod.run(payload, ctx);
|
|
2091
|
+
};
|
|
2092
|
+
});
|
|
2093
|
+
var $ZodDefault = $constructor("$ZodDefault", (inst, def) => {
|
|
2094
|
+
$ZodType.init(inst, def);
|
|
2095
|
+
inst._zod.optin = "defaulted";
|
|
2096
|
+
defineLazyInternal(inst, "values", (zod) => zod.def.innerType._zod.values);
|
|
2097
|
+
inst._zod.parse = (payload, ctx) => {
|
|
2098
|
+
if (ctx.direction === "backward") return def.innerType._zod.run(payload, ctx);
|
|
2099
|
+
if (payload.value === void 0) {
|
|
2100
|
+
payload.value = def.defaultValue;
|
|
2101
|
+
return payload;
|
|
2102
|
+
}
|
|
2103
|
+
const result = def.innerType._zod.run(payload, ctx);
|
|
2104
|
+
if (result instanceof Promise) return result.then((result) => handleDefaultResult(result, def));
|
|
2105
|
+
return handleDefaultResult(result, def);
|
|
2106
|
+
};
|
|
2107
|
+
});
|
|
2108
|
+
function handleDefaultResult(payload, def) {
|
|
2109
|
+
if (payload.value === void 0) payload.value = def.defaultValue;
|
|
2110
|
+
return payload;
|
|
2111
|
+
}
|
|
2112
|
+
var $ZodPrefault = $constructor("$ZodPrefault", (inst, def) => {
|
|
2113
|
+
$ZodType.init(inst, def);
|
|
2114
|
+
inst._zod.optin = "defaulted";
|
|
2115
|
+
defineLazyInternal(inst, "values", (zod) => zod.def.innerType._zod.values);
|
|
2116
|
+
inst._zod.parse = (payload, ctx) => {
|
|
2117
|
+
if (ctx.direction === "backward") return def.innerType._zod.run(payload, ctx);
|
|
2118
|
+
if (payload.value === void 0) payload.value = def.defaultValue;
|
|
2119
|
+
return def.innerType._zod.run(payload, ctx);
|
|
2120
|
+
};
|
|
2121
|
+
});
|
|
2122
|
+
var $ZodNonOptional = $constructor("$ZodNonOptional", (inst, def) => {
|
|
2123
|
+
$ZodType.init(inst, def);
|
|
2124
|
+
defineLazyInternal(inst, "values", (zod) => {
|
|
2125
|
+
const v = zod.def.innerType._zod.values;
|
|
2126
|
+
return v ? new Set([...v].filter((x) => x !== void 0)) : void 0;
|
|
2127
|
+
});
|
|
2128
|
+
inst._zod.parse = (payload, ctx) => {
|
|
2129
|
+
const result = def.innerType._zod.run(payload, ctx);
|
|
2130
|
+
if (result instanceof Promise) return result.then((result) => handleNonOptionalResult(result, inst));
|
|
2131
|
+
return handleNonOptionalResult(result, inst);
|
|
2132
|
+
};
|
|
2133
|
+
});
|
|
2134
|
+
function handleNonOptionalResult(payload, inst) {
|
|
2135
|
+
if (!payload.issues.length && payload.value === void 0) payload.issues.push({
|
|
2136
|
+
code: "invalid_type",
|
|
2137
|
+
expected: "nonoptional",
|
|
2138
|
+
input: payload.value,
|
|
2139
|
+
inst
|
|
2140
|
+
});
|
|
2141
|
+
return payload;
|
|
2142
|
+
}
|
|
2143
|
+
function handleCatchResult(payload, result, def, ctx) {
|
|
2144
|
+
if (!result.issues.length) {
|
|
2145
|
+
payload.value = result.value;
|
|
2146
|
+
if (result.memo) payload.memo = true;
|
|
2147
|
+
return payload;
|
|
2148
|
+
}
|
|
2149
|
+
payload.value = def.catchValue({
|
|
2150
|
+
...result,
|
|
2151
|
+
value: payload.value,
|
|
2152
|
+
error: { issues: result.issues.map((iss) => finalizeIssue(iss, ctx, config())) },
|
|
2153
|
+
input: payload.value
|
|
2154
|
+
});
|
|
2155
|
+
return payload;
|
|
2156
|
+
}
|
|
2157
|
+
var $ZodCatch = $constructor("$ZodCatch", (inst, def) => {
|
|
2158
|
+
$ZodType.init(inst, def);
|
|
2159
|
+
defineLazyInternal(inst, "optin", (zod) => zod.def.innerType._zod.optin === "defaulted" ? "defaulted" : "optional");
|
|
2160
|
+
defineLazyInternal(inst, "optout", (zod) => zod.def.innerType._zod.optout);
|
|
2161
|
+
defineLazyInternal(inst, "values", (zod) => zod.def.innerType._zod.values);
|
|
2162
|
+
inst._zod.parse = (payload, ctx) => {
|
|
2163
|
+
if (ctx.direction === "backward") return def.innerType._zod.run(payload, ctx);
|
|
2164
|
+
const result = def.innerType._zod.run({
|
|
2165
|
+
value: payload.value,
|
|
2166
|
+
issues: []
|
|
2167
|
+
}, ctx);
|
|
2168
|
+
if (result instanceof Promise) return result.then((result) => handleCatchResult(payload, result, def, ctx));
|
|
2169
|
+
return handleCatchResult(payload, result, def, ctx);
|
|
2170
|
+
};
|
|
2171
|
+
});
|
|
2172
|
+
var $ZodPipe = $constructor("$ZodPipe", (inst, def) => {
|
|
2173
|
+
$ZodType.init(inst, def);
|
|
2174
|
+
defineLazyInternal(inst, "values", (zod) => zod.def.in._zod.values);
|
|
2175
|
+
defineLazyInternal(inst, "optin", (zod) => zod.def.in._zod.optin);
|
|
2176
|
+
defineLazyInternal(inst, "optout", (zod) => zod.def.out._zod.optout);
|
|
2177
|
+
defineLazyInternal(inst, "propValues", (zod) => zod.def.in._zod.propValues);
|
|
2178
|
+
inst._zod.parse = (payload, ctx) => {
|
|
2179
|
+
if (ctx.direction === "backward") {
|
|
2180
|
+
const right = def.out._zod.run(payload, ctx);
|
|
2181
|
+
if (right instanceof Promise) return right.then((right) => handlePipeResult(right, def.in, ctx));
|
|
2182
|
+
return handlePipeResult(right, def.in, ctx);
|
|
2183
|
+
}
|
|
2184
|
+
const left = def.in._zod.run(payload, ctx);
|
|
2185
|
+
if (left instanceof Promise) return left.then((left) => handlePipeResult(left, def.out, ctx));
|
|
2186
|
+
return handlePipeResult(left, def.out, ctx);
|
|
2187
|
+
};
|
|
2188
|
+
});
|
|
2189
|
+
function handlePipeResult(left, next, ctx) {
|
|
2190
|
+
if (left.issues.some((iss) => iss.code !== "unrecognized_keys")) {
|
|
2191
|
+
left.aborted = true;
|
|
2192
|
+
return left;
|
|
2193
|
+
}
|
|
2194
|
+
return next._zod.run({
|
|
2195
|
+
value: left.value,
|
|
2196
|
+
issues: left.issues
|
|
2197
|
+
}, ctx);
|
|
2198
|
+
}
|
|
2199
|
+
var $ZodReadonly = $constructor("$ZodReadonly", (inst, def) => {
|
|
2200
|
+
$ZodType.init(inst, def);
|
|
2201
|
+
defineLazyInternal(inst, "propValues", (zod) => zod.def.innerType._zod.propValues);
|
|
2202
|
+
defineLazyInternal(inst, "values", (zod) => zod.def.innerType._zod.values);
|
|
2203
|
+
defineLazyInternal(inst, "optin", (zod) => zod.def.innerType?._zod?.optin);
|
|
2204
|
+
defineLazyInternal(inst, "optout", (zod) => zod.def.innerType?._zod?.optout);
|
|
2205
|
+
inst._zod.parse = (payload, ctx) => {
|
|
2206
|
+
if (ctx.direction === "backward") return def.innerType._zod.run(payload, ctx);
|
|
2207
|
+
const result = def.innerType._zod.run(payload, ctx);
|
|
2208
|
+
if (result instanceof Promise) return result.then(handleReadonlyResult);
|
|
2209
|
+
return handleReadonlyResult(result);
|
|
2210
|
+
};
|
|
2211
|
+
});
|
|
2212
|
+
function handleReadonlyResult(payload) {
|
|
2213
|
+
if (!payload.memo) payload.value = Object.freeze(payload.value);
|
|
2214
|
+
return payload;
|
|
2215
|
+
}
|
|
2216
|
+
var $ZodCustom = $constructor("$ZodCustom", (inst, def) => {
|
|
2217
|
+
$ZodCheck.init(inst, def);
|
|
2218
|
+
$ZodType.init(inst, def);
|
|
2219
|
+
inst._zod.parse = (payload, _) => {
|
|
2220
|
+
return payload;
|
|
2221
|
+
};
|
|
2222
|
+
inst._zod.check = (payload) => {
|
|
2223
|
+
const input = payload.value;
|
|
2224
|
+
const r = def.fn(input);
|
|
2225
|
+
if (r instanceof Promise) return r.then((r) => handleRefineResult(r, payload, input, inst));
|
|
2226
|
+
handleRefineResult(r, payload, input, inst);
|
|
2227
|
+
};
|
|
2228
|
+
});
|
|
2229
|
+
function handleRefineResult(result, payload, input, inst) {
|
|
2230
|
+
if (!result) {
|
|
2231
|
+
const _iss = {
|
|
2232
|
+
code: "custom",
|
|
2233
|
+
input,
|
|
2234
|
+
inst,
|
|
2235
|
+
path: [...inst._zod.def.path ?? []],
|
|
2236
|
+
continue: !inst._zod.def.abort
|
|
2237
|
+
};
|
|
2238
|
+
if (inst._zod.def.params) _iss.params = inst._zod.def.params;
|
|
2239
|
+
payload.issues.push(issue(_iss));
|
|
2240
|
+
}
|
|
2241
|
+
}
|
|
2242
|
+
var $ZodCyclicError = class extends Error {
|
|
2243
|
+
constructor() {
|
|
2244
|
+
super(`Cannot parse a reference cycle that closes through a transform`);
|
|
2245
|
+
this.name = "ZodCyclicError";
|
|
2246
|
+
}
|
|
2247
|
+
};
|
|
2248
|
+
var STATE = "~memo";
|
|
2249
|
+
var NO_ISSUES = [];
|
|
2250
|
+
function isRef(value) {
|
|
2251
|
+
return value !== null && typeof value === "object";
|
|
2252
|
+
}
|
|
2253
|
+
function cloneIssues(issues) {
|
|
2254
|
+
return issues.map((iss) => iss.path ? {
|
|
2255
|
+
...iss,
|
|
2256
|
+
path: iss.path.slice()
|
|
2257
|
+
} : { ...iss });
|
|
2258
|
+
}
|
|
2259
|
+
var recursive = new WeakMap();
|
|
2260
|
+
var NONE = 0;
|
|
2261
|
+
var ASSUMED = 1;
|
|
2262
|
+
var PROVEN = 2;
|
|
2263
|
+
function isRecursive(inst, stack, resolve) {
|
|
2264
|
+
const cached = recursive.get(inst);
|
|
2265
|
+
if (cached !== void 0) return cached ? PROVEN : NONE;
|
|
2266
|
+
if (stack.has(inst)) return PROVEN;
|
|
2267
|
+
stack.add(inst);
|
|
2268
|
+
let result = NONE;
|
|
2269
|
+
const check = (child) => {
|
|
2270
|
+
if (result !== PROVEN && child?._zod) {
|
|
2271
|
+
const answer = isRecursive(child, stack, resolve);
|
|
2272
|
+
if (answer > result) result = answer;
|
|
2273
|
+
}
|
|
2274
|
+
};
|
|
2275
|
+
const shape = (sh, spread) => {
|
|
2276
|
+
let answer = NONE;
|
|
2277
|
+
for (const key of Reflect.ownKeys(sh)) {
|
|
2278
|
+
const desc = Object.getOwnPropertyDescriptor(sh, key);
|
|
2279
|
+
if (spread && !desc.enumerable) continue;
|
|
2280
|
+
const child = desc.get ? ASSUMED : desc.value?._zod ? isRecursive(desc.value, stack, resolve) : NONE;
|
|
2281
|
+
if (child > answer) answer = child;
|
|
2282
|
+
}
|
|
2283
|
+
return answer;
|
|
2284
|
+
};
|
|
2285
|
+
const merge = (answer) => {
|
|
2286
|
+
if (answer > result) result = answer;
|
|
2287
|
+
};
|
|
2288
|
+
const def = inst._zod.def;
|
|
2289
|
+
switch (def.type) {
|
|
2290
|
+
case "object": {
|
|
2291
|
+
const raw = rawShape(def);
|
|
2292
|
+
merge(raw ? shape(raw, true) : ASSUMED);
|
|
2293
|
+
check(def.catchall);
|
|
2294
|
+
break;
|
|
2295
|
+
}
|
|
2296
|
+
case "array":
|
|
2297
|
+
check(def.element);
|
|
2298
|
+
break;
|
|
2299
|
+
case "tuple":
|
|
2300
|
+
for (const el of def.items) check(el);
|
|
2301
|
+
check(def.rest);
|
|
2302
|
+
break;
|
|
2303
|
+
case "record":
|
|
2304
|
+
case "map":
|
|
2305
|
+
check(def.keyType);
|
|
2306
|
+
check(def.valueType);
|
|
2307
|
+
break;
|
|
2308
|
+
case "set":
|
|
2309
|
+
check(def.valueType);
|
|
2310
|
+
break;
|
|
2311
|
+
case "union":
|
|
2312
|
+
for (const el of def.options) check(el);
|
|
2313
|
+
break;
|
|
2314
|
+
case "intersection":
|
|
2315
|
+
check(def.left);
|
|
2316
|
+
check(def.right);
|
|
2317
|
+
break;
|
|
2318
|
+
case "optional":
|
|
2319
|
+
case "nullable":
|
|
2320
|
+
case "default":
|
|
2321
|
+
case "prefault":
|
|
2322
|
+
case "catch":
|
|
2323
|
+
case "readonly":
|
|
2324
|
+
case "nonoptional":
|
|
2325
|
+
case "promise":
|
|
2326
|
+
case "success":
|
|
2327
|
+
check(def.innerType);
|
|
2328
|
+
break;
|
|
2329
|
+
case "pipe":
|
|
2330
|
+
check(def.in);
|
|
2331
|
+
check(def.out);
|
|
2332
|
+
break;
|
|
2333
|
+
case "function":
|
|
2334
|
+
check(def.input);
|
|
2335
|
+
check(def.output);
|
|
2336
|
+
break;
|
|
2337
|
+
case "lazy": {
|
|
2338
|
+
const inner = def._cachedInner ?? (resolve ? inst._zod.innerType : void 0);
|
|
2339
|
+
merge(inner ? isRecursive(inner, stack, false) : ASSUMED);
|
|
2340
|
+
break;
|
|
2341
|
+
}
|
|
2342
|
+
case "template_literal":
|
|
2343
|
+
case "string":
|
|
2344
|
+
case "number":
|
|
2345
|
+
case "int":
|
|
2346
|
+
case "boolean":
|
|
2347
|
+
case "bigint":
|
|
2348
|
+
case "symbol":
|
|
2349
|
+
case "undefined":
|
|
2350
|
+
case "null":
|
|
2351
|
+
case "void":
|
|
2352
|
+
case "never":
|
|
2353
|
+
case "any":
|
|
2354
|
+
case "unknown":
|
|
2355
|
+
case "date":
|
|
2356
|
+
case "nan":
|
|
2357
|
+
case "enum":
|
|
2358
|
+
case "literal":
|
|
2359
|
+
case "file":
|
|
2360
|
+
case "transform":
|
|
2361
|
+
case "custom": break;
|
|
2362
|
+
default: for (const key in def) {
|
|
2363
|
+
const desc = Object.getOwnPropertyDescriptor(def, key);
|
|
2364
|
+
if (!desc || desc.get) continue;
|
|
2365
|
+
const value = desc.value;
|
|
2366
|
+
if (!value || typeof value !== "object") continue;
|
|
2367
|
+
if (value._zod) check(value);
|
|
2368
|
+
else if (Array.isArray(value)) for (const el of value) check(el);
|
|
2369
|
+
}
|
|
2370
|
+
}
|
|
2371
|
+
stack.delete(inst);
|
|
2372
|
+
return settle(inst, result);
|
|
2373
|
+
}
|
|
2374
|
+
function settle(inst, answer) {
|
|
2375
|
+
if (answer !== ASSUMED) recursive.set(inst, answer === PROVEN);
|
|
2376
|
+
return answer;
|
|
2377
|
+
}
|
|
2378
|
+
function bucketFor(state, inst) {
|
|
2379
|
+
let bucket = state.buckets.get(inst);
|
|
2380
|
+
if (!bucket) {
|
|
2381
|
+
bucket = new WeakMap();
|
|
2382
|
+
state.buckets.set(inst, bucket);
|
|
2383
|
+
}
|
|
2384
|
+
return bucket;
|
|
2385
|
+
}
|
|
2386
|
+
var handoff;
|
|
2387
|
+
var open = [];
|
|
2388
|
+
var memo = {
|
|
2389
|
+
alloc(_inst, payload, empty) {
|
|
2390
|
+
const bucket = handoff;
|
|
2391
|
+
if (!bucket) return empty;
|
|
2392
|
+
handoff = void 0;
|
|
2393
|
+
const entry = {
|
|
2394
|
+
value: empty,
|
|
2395
|
+
issues: null
|
|
2396
|
+
};
|
|
2397
|
+
bucket.set(payload.value, entry);
|
|
2398
|
+
open.push(entry);
|
|
2399
|
+
return empty;
|
|
2400
|
+
},
|
|
2401
|
+
guard(inst) {
|
|
2402
|
+
var _a;
|
|
2403
|
+
(_a = inst._zod).deferred ?? (_a.deferred = []);
|
|
2404
|
+
inst._zod.deferred.push(() => {
|
|
2405
|
+
const base = inst._zod.parse;
|
|
2406
|
+
const wrapped = (payload, ctx) => {
|
|
2407
|
+
if (ctx.direction !== "backward" && isBackEdge(ctx, payload.value)) throw new $ZodCyclicError();
|
|
2408
|
+
return base(payload, ctx);
|
|
2409
|
+
};
|
|
2410
|
+
inst._zod.parse = wrapped;
|
|
2411
|
+
if (inst._zod.run === base) inst._zod.run = wrapped;
|
|
2412
|
+
});
|
|
2413
|
+
},
|
|
2414
|
+
attach(inst) {
|
|
2415
|
+
var _a;
|
|
2416
|
+
let isRecursiveInst;
|
|
2417
|
+
let rechecked = false;
|
|
2418
|
+
let lastCtx;
|
|
2419
|
+
let lastBucket;
|
|
2420
|
+
(_a = inst._zod).deferred ?? (_a.deferred = []);
|
|
2421
|
+
inst._zod.deferred.push(() => {
|
|
2422
|
+
const base = inst._zod.parse;
|
|
2423
|
+
const wrapped = (payload, ctx) => {
|
|
2424
|
+
if (isRecursiveInst === void 0) {
|
|
2425
|
+
const walked = isRecursive(inst, new Set(), false);
|
|
2426
|
+
if (walked === NONE) {
|
|
2427
|
+
inst._zod.parse = base;
|
|
2428
|
+
if (inst._zod.run === wrapped) inst._zod.run = base;
|
|
2429
|
+
return base(payload, ctx);
|
|
2430
|
+
}
|
|
2431
|
+
if (walked === PROVEN || rechecked) isRecursiveInst = true;
|
|
2432
|
+
else rechecked = true;
|
|
2433
|
+
}
|
|
2434
|
+
const input = payload.value;
|
|
2435
|
+
if (!isRef(input)) return base(payload, ctx);
|
|
2436
|
+
let state = ctx[STATE];
|
|
2437
|
+
if (!state) {
|
|
2438
|
+
state = {
|
|
2439
|
+
buckets: new WeakMap(),
|
|
2440
|
+
backEdges: void 0
|
|
2441
|
+
};
|
|
2442
|
+
ctx[STATE] = state;
|
|
2443
|
+
}
|
|
2444
|
+
let bucket;
|
|
2445
|
+
if (lastCtx === ctx) bucket = lastBucket;
|
|
2446
|
+
else {
|
|
2447
|
+
bucket = bucketFor(state, inst);
|
|
2448
|
+
lastCtx = ctx;
|
|
2449
|
+
lastBucket = bucket;
|
|
2450
|
+
}
|
|
2451
|
+
const hit = bucket.get(input);
|
|
2452
|
+
if (hit) {
|
|
2453
|
+
payload.value = hit.value;
|
|
2454
|
+
if (hit.issues) {
|
|
2455
|
+
if (hit.issues.length) payload.issues.push(...cloneIssues(hit.issues));
|
|
2456
|
+
} else {
|
|
2457
|
+
payload.memo = true;
|
|
2458
|
+
state.backEdges ?? (state.backEdges = new WeakSet());
|
|
2459
|
+
state.backEdges.add(hit.value);
|
|
2460
|
+
}
|
|
2461
|
+
return payload;
|
|
2462
|
+
}
|
|
2463
|
+
handoff = bucket;
|
|
2464
|
+
const depth = open.length;
|
|
2465
|
+
const result = base(payload, ctx);
|
|
2466
|
+
handoff = void 0;
|
|
2467
|
+
const entry = open.length > depth ? open.pop() : void 0;
|
|
2468
|
+
if (result instanceof Promise) return result.then((r) => {
|
|
2469
|
+
if (entry) entry.issues = r.issues.length ? cloneIssues(r.issues) : NO_ISSUES;
|
|
2470
|
+
return r;
|
|
2471
|
+
});
|
|
2472
|
+
if (entry) entry.issues = result.issues.length ? cloneIssues(result.issues) : NO_ISSUES;
|
|
2473
|
+
return result;
|
|
2474
|
+
};
|
|
2475
|
+
inst._zod.parse = wrapped;
|
|
2476
|
+
if (inst._zod.run === base) inst._zod.run = wrapped;
|
|
2477
|
+
});
|
|
2478
|
+
}
|
|
2479
|
+
};
|
|
2480
|
+
function memoizer() {
|
|
2481
|
+
return memo;
|
|
2482
|
+
}
|
|
2483
|
+
function isBackEdge(ctx, value) {
|
|
2484
|
+
const backEdges = ctx[STATE]?.backEdges;
|
|
2485
|
+
return backEdges !== void 0 && isRef(value) && backEdges.has(value);
|
|
2486
|
+
}
|
|
2487
|
+
var error = () => {
|
|
2488
|
+
const Sizable = {
|
|
2489
|
+
string: {
|
|
2490
|
+
unit: "characters",
|
|
2491
|
+
verb: "to have"
|
|
2492
|
+
},
|
|
2493
|
+
file: {
|
|
2494
|
+
unit: "bytes",
|
|
2495
|
+
verb: "to have"
|
|
2496
|
+
},
|
|
2497
|
+
array: {
|
|
2498
|
+
unit: "items",
|
|
2499
|
+
verb: "to have"
|
|
2500
|
+
},
|
|
2501
|
+
set: {
|
|
2502
|
+
unit: "items",
|
|
2503
|
+
verb: "to have"
|
|
2504
|
+
},
|
|
2505
|
+
map: {
|
|
2506
|
+
unit: "entries",
|
|
2507
|
+
verb: "to have"
|
|
2508
|
+
}
|
|
2509
|
+
};
|
|
2510
|
+
function getSizing(origin) {
|
|
2511
|
+
return Sizable[origin] ?? null;
|
|
2512
|
+
}
|
|
2513
|
+
const FormatDictionary = {
|
|
2514
|
+
regex: "input",
|
|
2515
|
+
email: "email address",
|
|
2516
|
+
url: "URL",
|
|
2517
|
+
emoji: "emoji",
|
|
2518
|
+
uuid: "UUID",
|
|
2519
|
+
uuidv4: "UUIDv4",
|
|
2520
|
+
uuidv6: "UUIDv6",
|
|
2521
|
+
nanoid: "nanoid",
|
|
2522
|
+
guid: "GUID",
|
|
2523
|
+
cuid: "cuid",
|
|
2524
|
+
cuid2: "cuid2",
|
|
2525
|
+
ulid: "ULID",
|
|
2526
|
+
xid: "XID",
|
|
2527
|
+
ksuid: "KSUID",
|
|
2528
|
+
datetime: "ISO datetime",
|
|
2529
|
+
date: "ISO date",
|
|
2530
|
+
time: "ISO time",
|
|
2531
|
+
duration: "ISO duration",
|
|
2532
|
+
ipv4: "IPv4 address",
|
|
2533
|
+
ipv6: "IPv6 address",
|
|
2534
|
+
mac: "MAC address",
|
|
2535
|
+
cidrv4: "IPv4 range",
|
|
2536
|
+
cidrv6: "IPv6 range",
|
|
2537
|
+
base64: "base64-encoded string",
|
|
2538
|
+
base64url: "base64url-encoded string",
|
|
2539
|
+
json_string: "JSON string",
|
|
2540
|
+
e164: "E.164 number",
|
|
2541
|
+
currency_code: "currency code",
|
|
2542
|
+
credit_card: "credit card number",
|
|
2543
|
+
iban: "IBAN",
|
|
2544
|
+
jwt: "JWT",
|
|
2545
|
+
template_literal: "input"
|
|
2546
|
+
};
|
|
2547
|
+
const TypeDictionary = { nan: "NaN" };
|
|
2548
|
+
function getTypeName(type, input) {
|
|
2549
|
+
if (type === "number" && typeof input === "number" && !Number.isFinite(input)) return String(input);
|
|
2550
|
+
return TypeDictionary[type] ?? type;
|
|
2551
|
+
}
|
|
2552
|
+
return (issue) => {
|
|
2553
|
+
switch (issue.code) {
|
|
2554
|
+
case "invalid_type": return `Invalid input: expected ${getTypeName(issue.expected)}, received ${getTypeName(parsedType(issue.input), issue.input)}`;
|
|
2555
|
+
case "invalid_value":
|
|
2556
|
+
if (issue.values.length === 1) return `Invalid input: expected ${stringifyPrimitive(issue.values[0])}`;
|
|
2557
|
+
return `Invalid option: expected one of ${joinValues(issue.values, "|")}`;
|
|
2558
|
+
case "too_big": {
|
|
2559
|
+
const adj = issue.exact ? "exactly " : issue.inclusive ? "<=" : "<";
|
|
2560
|
+
const sizing = getSizing(issue.origin);
|
|
2561
|
+
if (sizing) return `Too big: expected ${issue.origin ?? "value"} to have ${adj}${issue.maximum.toString()} ${sizing.unit ?? "elements"}`;
|
|
2562
|
+
return `Too big: expected ${issue.origin ?? "value"} to be ${adj}${issue.maximum.toString()}`;
|
|
2563
|
+
}
|
|
2564
|
+
case "too_small": {
|
|
2565
|
+
const adj = issue.exact ? "exactly " : issue.inclusive ? ">=" : ">";
|
|
2566
|
+
const sizing = getSizing(issue.origin);
|
|
2567
|
+
if (sizing) return `Too small: expected ${issue.origin} to have ${adj}${issue.minimum.toString()} ${sizing.unit}`;
|
|
2568
|
+
return `Too small: expected ${issue.origin} to be ${adj}${issue.minimum.toString()}`;
|
|
2569
|
+
}
|
|
2570
|
+
case "invalid_format": {
|
|
2571
|
+
const _issue = issue;
|
|
2572
|
+
if (_issue.format === "starts_with") return `Invalid string: must start with "${_issue.prefix}"`;
|
|
2573
|
+
if (_issue.format === "ends_with") return `Invalid string: must end with "${_issue.suffix}"`;
|
|
2574
|
+
if (_issue.format === "includes") return `Invalid string: must include "${_issue.includes}"`;
|
|
2575
|
+
if (_issue.format === "regex") return `Invalid string: must match pattern ${_issue.pattern}`;
|
|
2576
|
+
return `Invalid ${FormatDictionary[_issue.format] ?? issue.format}`;
|
|
2577
|
+
}
|
|
2578
|
+
case "not_multiple_of": return `Invalid number: must be a multiple of ${issue.divisor}`;
|
|
2579
|
+
case "unrecognized_keys": return `Unrecognized key${issue.keys.length > 1 ? "s" : ""}: ${joinValues(issue.keys, ", ")}`;
|
|
2580
|
+
case "invalid_key": return `Invalid key in ${issue.origin}`;
|
|
2581
|
+
case "invalid_union":
|
|
2582
|
+
if (issue.options && Array.isArray(issue.options) && issue.options.length > 0) return `Invalid discriminator value. Expected ${issue.options.map((o) => `'${o}'`).join(" | ")}`;
|
|
2583
|
+
if (issue.inclusive === false) return "Invalid input: more than one option matched";
|
|
2584
|
+
return "Invalid input";
|
|
2585
|
+
case "invalid_element": return `Invalid value in ${issue.origin}`;
|
|
2586
|
+
default: return `Invalid input`;
|
|
2587
|
+
}
|
|
2588
|
+
};
|
|
2589
|
+
};
|
|
2590
|
+
function en_default() {
|
|
2591
|
+
return { localeError: error() };
|
|
2592
|
+
}
|
|
2593
|
+
var _a;
|
|
2594
|
+
var $ZodRegistry = class {
|
|
2595
|
+
constructor() {
|
|
2596
|
+
this._map = new WeakMap();
|
|
2597
|
+
this._idmap = new Map();
|
|
2598
|
+
}
|
|
2599
|
+
add(schema, ..._meta) {
|
|
2600
|
+
const meta = _meta[0];
|
|
2601
|
+
this._map.set(schema, meta);
|
|
2602
|
+
if (meta && typeof meta === "object" && "id" in meta) this._idmap.set(meta.id, schema);
|
|
2603
|
+
return this;
|
|
2604
|
+
}
|
|
2605
|
+
clear() {
|
|
2606
|
+
this._map = new WeakMap();
|
|
2607
|
+
this._idmap = new Map();
|
|
2608
|
+
return this;
|
|
2609
|
+
}
|
|
2610
|
+
remove(schema) {
|
|
2611
|
+
const meta = this._map.get(schema);
|
|
2612
|
+
if (meta && typeof meta === "object" && "id" in meta) this._idmap.delete(meta.id);
|
|
2613
|
+
this._map.delete(schema);
|
|
2614
|
+
return this;
|
|
2615
|
+
}
|
|
2616
|
+
get(schema) {
|
|
2617
|
+
const p = schema._zod.parent;
|
|
2618
|
+
if (p) {
|
|
2619
|
+
const pm = { ...this.get(p) ?? {} };
|
|
2620
|
+
delete pm.id;
|
|
2621
|
+
const f = {
|
|
2622
|
+
...pm,
|
|
2623
|
+
...this._map.get(schema)
|
|
2624
|
+
};
|
|
2625
|
+
return Object.keys(f).length ? f : void 0;
|
|
2626
|
+
}
|
|
2627
|
+
return this._map.get(schema);
|
|
2628
|
+
}
|
|
2629
|
+
has(schema) {
|
|
2630
|
+
return this._map.has(schema);
|
|
2631
|
+
}
|
|
2632
|
+
};
|
|
2633
|
+
function registry() {
|
|
2634
|
+
return new $ZodRegistry();
|
|
2635
|
+
}
|
|
2636
|
+
(_a = globalThis).__zod_globalRegistry ?? (_a.__zod_globalRegistry = registry());
|
|
2637
|
+
var globalRegistry = globalThis.__zod_globalRegistry;
|
|
2638
|
+
function snapshotChecks(def) {
|
|
2639
|
+
if (def.checks) def.checks = [...def.checks];
|
|
2640
|
+
return def;
|
|
2641
|
+
}
|
|
2642
|
+
function _string(Class, params) {
|
|
2643
|
+
return new Class(snapshotChecks({
|
|
2644
|
+
type: "string",
|
|
2645
|
+
...normalizeParams(params)
|
|
2646
|
+
}));
|
|
2647
|
+
}
|
|
2648
|
+
function _email(Class, params) {
|
|
2649
|
+
return new Class({
|
|
2650
|
+
type: "string",
|
|
2651
|
+
format: "email",
|
|
2652
|
+
check: "string_format",
|
|
2653
|
+
abort: false,
|
|
2654
|
+
...normalizeParams(params)
|
|
2655
|
+
});
|
|
2656
|
+
}
|
|
2657
|
+
function _guid(Class, params) {
|
|
2658
|
+
return new Class({
|
|
2659
|
+
type: "string",
|
|
2660
|
+
format: "guid",
|
|
2661
|
+
check: "string_format",
|
|
2662
|
+
abort: false,
|
|
2663
|
+
...normalizeParams(params)
|
|
2664
|
+
});
|
|
2665
|
+
}
|
|
2666
|
+
function _uuid(Class, params) {
|
|
2667
|
+
return new Class({
|
|
2668
|
+
type: "string",
|
|
2669
|
+
format: "uuid",
|
|
2670
|
+
check: "string_format",
|
|
2671
|
+
abort: false,
|
|
2672
|
+
...normalizeParams(params)
|
|
2673
|
+
});
|
|
2674
|
+
}
|
|
2675
|
+
function _uuidv4(Class, params) {
|
|
2676
|
+
return new Class({
|
|
2677
|
+
type: "string",
|
|
2678
|
+
format: "uuid",
|
|
2679
|
+
check: "string_format",
|
|
2680
|
+
abort: false,
|
|
2681
|
+
version: "v4",
|
|
2682
|
+
...normalizeParams(params)
|
|
2683
|
+
});
|
|
2684
|
+
}
|
|
2685
|
+
function _uuidv6(Class, params) {
|
|
2686
|
+
return new Class({
|
|
2687
|
+
type: "string",
|
|
2688
|
+
format: "uuid",
|
|
2689
|
+
check: "string_format",
|
|
2690
|
+
abort: false,
|
|
2691
|
+
version: "v6",
|
|
2692
|
+
...normalizeParams(params)
|
|
2693
|
+
});
|
|
2694
|
+
}
|
|
2695
|
+
function _uuidv7(Class, params) {
|
|
2696
|
+
return new Class({
|
|
2697
|
+
type: "string",
|
|
2698
|
+
format: "uuid",
|
|
2699
|
+
check: "string_format",
|
|
2700
|
+
abort: false,
|
|
2701
|
+
version: "v7",
|
|
2702
|
+
...normalizeParams(params)
|
|
2703
|
+
});
|
|
2704
|
+
}
|
|
2705
|
+
function _url(Class, params) {
|
|
2706
|
+
return new Class({
|
|
2707
|
+
type: "string",
|
|
2708
|
+
format: "url",
|
|
2709
|
+
check: "string_format",
|
|
2710
|
+
abort: false,
|
|
2711
|
+
...normalizeParams(params)
|
|
2712
|
+
});
|
|
2713
|
+
}
|
|
2714
|
+
function _emoji(Class, params) {
|
|
2715
|
+
return new Class({
|
|
2716
|
+
type: "string",
|
|
2717
|
+
format: "emoji",
|
|
2718
|
+
check: "string_format",
|
|
2719
|
+
abort: false,
|
|
2720
|
+
...normalizeParams(params)
|
|
2721
|
+
});
|
|
2722
|
+
}
|
|
2723
|
+
function _nanoid(Class, params) {
|
|
2724
|
+
return new Class({
|
|
2725
|
+
type: "string",
|
|
2726
|
+
format: "nanoid",
|
|
2727
|
+
check: "string_format",
|
|
2728
|
+
abort: false,
|
|
2729
|
+
...normalizeParams(params)
|
|
2730
|
+
});
|
|
2731
|
+
}
|
|
2732
|
+
function _cuid(Class, params) {
|
|
2733
|
+
return new Class({
|
|
2734
|
+
type: "string",
|
|
2735
|
+
format: "cuid",
|
|
2736
|
+
check: "string_format",
|
|
2737
|
+
abort: false,
|
|
2738
|
+
...normalizeParams(params)
|
|
2739
|
+
});
|
|
2740
|
+
}
|
|
2741
|
+
function _cuid2(Class, params) {
|
|
2742
|
+
return new Class({
|
|
2743
|
+
type: "string",
|
|
2744
|
+
format: "cuid2",
|
|
2745
|
+
check: "string_format",
|
|
2746
|
+
abort: false,
|
|
2747
|
+
...normalizeParams(params)
|
|
2748
|
+
});
|
|
2749
|
+
}
|
|
2750
|
+
function _ulid(Class, params) {
|
|
2751
|
+
return new Class({
|
|
2752
|
+
type: "string",
|
|
2753
|
+
format: "ulid",
|
|
2754
|
+
check: "string_format",
|
|
2755
|
+
abort: false,
|
|
2756
|
+
...normalizeParams(params)
|
|
2757
|
+
});
|
|
2758
|
+
}
|
|
2759
|
+
function _xid(Class, params) {
|
|
2760
|
+
return new Class({
|
|
2761
|
+
type: "string",
|
|
2762
|
+
format: "xid",
|
|
2763
|
+
check: "string_format",
|
|
2764
|
+
abort: false,
|
|
2765
|
+
...normalizeParams(params)
|
|
2766
|
+
});
|
|
2767
|
+
}
|
|
2768
|
+
function _ksuid(Class, params) {
|
|
2769
|
+
return new Class({
|
|
2770
|
+
type: "string",
|
|
2771
|
+
format: "ksuid",
|
|
2772
|
+
check: "string_format",
|
|
2773
|
+
abort: false,
|
|
2774
|
+
...normalizeParams(params)
|
|
2775
|
+
});
|
|
2776
|
+
}
|
|
2777
|
+
function _ipv4(Class, params) {
|
|
2778
|
+
return new Class({
|
|
2779
|
+
type: "string",
|
|
2780
|
+
format: "ipv4",
|
|
2781
|
+
check: "string_format",
|
|
2782
|
+
abort: false,
|
|
2783
|
+
...normalizeParams(params)
|
|
2784
|
+
});
|
|
2785
|
+
}
|
|
2786
|
+
function _ipv6(Class, params) {
|
|
2787
|
+
return new Class({
|
|
2788
|
+
type: "string",
|
|
2789
|
+
format: "ipv6",
|
|
2790
|
+
check: "string_format",
|
|
2791
|
+
abort: false,
|
|
2792
|
+
...normalizeParams(params)
|
|
2793
|
+
});
|
|
2794
|
+
}
|
|
2795
|
+
function _cidrv4(Class, params) {
|
|
2796
|
+
return new Class({
|
|
2797
|
+
type: "string",
|
|
2798
|
+
format: "cidrv4",
|
|
2799
|
+
check: "string_format",
|
|
2800
|
+
abort: false,
|
|
2801
|
+
...normalizeParams(params)
|
|
2802
|
+
});
|
|
2803
|
+
}
|
|
2804
|
+
function _cidrv6(Class, params) {
|
|
2805
|
+
return new Class({
|
|
2806
|
+
type: "string",
|
|
2807
|
+
format: "cidrv6",
|
|
2808
|
+
check: "string_format",
|
|
2809
|
+
abort: false,
|
|
2810
|
+
...normalizeParams(params)
|
|
2811
|
+
});
|
|
2812
|
+
}
|
|
2813
|
+
function _base64(Class, params) {
|
|
2814
|
+
return new Class({
|
|
2815
|
+
type: "string",
|
|
2816
|
+
format: "base64",
|
|
2817
|
+
check: "string_format",
|
|
2818
|
+
abort: false,
|
|
2819
|
+
...normalizeParams(params)
|
|
2820
|
+
});
|
|
2821
|
+
}
|
|
2822
|
+
function _base64url(Class, params) {
|
|
2823
|
+
return new Class({
|
|
2824
|
+
type: "string",
|
|
2825
|
+
format: "base64url",
|
|
2826
|
+
check: "string_format",
|
|
2827
|
+
abort: false,
|
|
2828
|
+
...normalizeParams(params)
|
|
2829
|
+
});
|
|
2830
|
+
}
|
|
2831
|
+
function _e164(Class, params) {
|
|
2832
|
+
return new Class({
|
|
2833
|
+
type: "string",
|
|
2834
|
+
format: "e164",
|
|
2835
|
+
check: "string_format",
|
|
2836
|
+
abort: false,
|
|
2837
|
+
...normalizeParams(params)
|
|
2838
|
+
});
|
|
2839
|
+
}
|
|
2840
|
+
function _jwt(Class, params) {
|
|
2841
|
+
return new Class({
|
|
2842
|
+
type: "string",
|
|
2843
|
+
format: "jwt",
|
|
2844
|
+
check: "string_format",
|
|
2845
|
+
abort: false,
|
|
2846
|
+
...normalizeParams(params)
|
|
2847
|
+
});
|
|
2848
|
+
}
|
|
2849
|
+
function _isoDateTime(Class, params) {
|
|
2850
|
+
return new Class({
|
|
2851
|
+
type: "string",
|
|
2852
|
+
format: "datetime",
|
|
2853
|
+
check: "string_format",
|
|
2854
|
+
offset: false,
|
|
2855
|
+
local: false,
|
|
2856
|
+
precision: null,
|
|
2857
|
+
...normalizeParams(params)
|
|
2858
|
+
});
|
|
2859
|
+
}
|
|
2860
|
+
function _isoDate(Class, params) {
|
|
2861
|
+
return new Class({
|
|
2862
|
+
type: "string",
|
|
2863
|
+
format: "date",
|
|
2864
|
+
check: "string_format",
|
|
2865
|
+
...normalizeParams(params)
|
|
2866
|
+
});
|
|
2867
|
+
}
|
|
2868
|
+
function _isoTime(Class, params) {
|
|
2869
|
+
return new Class({
|
|
2870
|
+
type: "string",
|
|
2871
|
+
format: "time",
|
|
2872
|
+
check: "string_format",
|
|
2873
|
+
precision: null,
|
|
2874
|
+
...normalizeParams(params)
|
|
2875
|
+
});
|
|
2876
|
+
}
|
|
2877
|
+
function _isoDuration(Class, params) {
|
|
2878
|
+
return new Class({
|
|
2879
|
+
type: "string",
|
|
2880
|
+
format: "duration",
|
|
2881
|
+
check: "string_format",
|
|
2882
|
+
...normalizeParams(params)
|
|
2883
|
+
});
|
|
2884
|
+
}
|
|
2885
|
+
function _boolean(Class, params) {
|
|
2886
|
+
return new Class({
|
|
2887
|
+
type: "boolean",
|
|
2888
|
+
...normalizeParams(params)
|
|
2889
|
+
});
|
|
2890
|
+
}
|
|
2891
|
+
function _maxLength(maximum, params) {
|
|
2892
|
+
return new $ZodCheckMaxLength({
|
|
2893
|
+
check: "max_length",
|
|
2894
|
+
...normalizeParams(params),
|
|
2895
|
+
maximum
|
|
2896
|
+
});
|
|
2897
|
+
}
|
|
2898
|
+
function _minLength(minimum, params) {
|
|
2899
|
+
return new $ZodCheckMinLength({
|
|
2900
|
+
check: "min_length",
|
|
2901
|
+
...normalizeParams(params),
|
|
2902
|
+
minimum
|
|
2903
|
+
});
|
|
2904
|
+
}
|
|
2905
|
+
function _length(length, params) {
|
|
2906
|
+
return new $ZodCheckLengthEquals({
|
|
2907
|
+
check: "length_equals",
|
|
2908
|
+
...normalizeParams(params),
|
|
2909
|
+
length
|
|
2910
|
+
});
|
|
2911
|
+
}
|
|
2912
|
+
function _regex(pattern, params) {
|
|
2913
|
+
return new $ZodCheckRegex({
|
|
2914
|
+
check: "string_format",
|
|
2915
|
+
format: "regex",
|
|
2916
|
+
...normalizeParams(params),
|
|
2917
|
+
pattern
|
|
2918
|
+
});
|
|
2919
|
+
}
|
|
2920
|
+
function _lowercase(params) {
|
|
2921
|
+
return new $ZodCheckLowerCase({
|
|
2922
|
+
check: "string_format",
|
|
2923
|
+
format: "lowercase",
|
|
2924
|
+
...normalizeParams(params)
|
|
2925
|
+
});
|
|
2926
|
+
}
|
|
2927
|
+
function _uppercase(params) {
|
|
2928
|
+
return new $ZodCheckUpperCase({
|
|
2929
|
+
check: "string_format",
|
|
2930
|
+
format: "uppercase",
|
|
2931
|
+
...normalizeParams(params)
|
|
2932
|
+
});
|
|
2933
|
+
}
|
|
2934
|
+
function _includes(includes, params) {
|
|
2935
|
+
return new $ZodCheckIncludes({
|
|
2936
|
+
check: "string_format",
|
|
2937
|
+
format: "includes",
|
|
2938
|
+
...normalizeParams(params),
|
|
2939
|
+
includes
|
|
2940
|
+
});
|
|
2941
|
+
}
|
|
2942
|
+
function _startsWith(prefix, params) {
|
|
2943
|
+
return new $ZodCheckStartsWith({
|
|
2944
|
+
check: "string_format",
|
|
2945
|
+
format: "starts_with",
|
|
2946
|
+
...normalizeParams(params),
|
|
2947
|
+
prefix
|
|
2948
|
+
});
|
|
2949
|
+
}
|
|
2950
|
+
function _endsWith(suffix, params) {
|
|
2951
|
+
return new $ZodCheckEndsWith({
|
|
2952
|
+
check: "string_format",
|
|
2953
|
+
format: "ends_with",
|
|
2954
|
+
...normalizeParams(params),
|
|
2955
|
+
suffix
|
|
2956
|
+
});
|
|
2957
|
+
}
|
|
2958
|
+
function _overwrite(tx) {
|
|
2959
|
+
return new $ZodCheckOverwrite({
|
|
2960
|
+
check: "overwrite",
|
|
2961
|
+
tx
|
|
2962
|
+
});
|
|
2963
|
+
}
|
|
2964
|
+
function _normalize(form) {
|
|
2965
|
+
return _overwrite((input) => input.normalize(form));
|
|
2966
|
+
}
|
|
2967
|
+
function _trim() {
|
|
2968
|
+
return _overwrite((input) => input.trim());
|
|
2969
|
+
}
|
|
2970
|
+
function _toLowerCase() {
|
|
2971
|
+
return _overwrite((input) => input.toLowerCase());
|
|
2972
|
+
}
|
|
2973
|
+
function _toUpperCase() {
|
|
2974
|
+
return _overwrite((input) => input.toUpperCase());
|
|
2975
|
+
}
|
|
2976
|
+
function _slugify() {
|
|
2977
|
+
return _overwrite((input) => slugify(input));
|
|
2978
|
+
}
|
|
2979
|
+
function _array(Class, element, params) {
|
|
2980
|
+
return new Class({
|
|
2981
|
+
type: "array",
|
|
2982
|
+
element,
|
|
2983
|
+
...normalizeParams(params)
|
|
2984
|
+
});
|
|
2985
|
+
}
|
|
2986
|
+
function _refine(Class, fn, _params) {
|
|
2987
|
+
return new Class({
|
|
2988
|
+
type: "custom",
|
|
2989
|
+
check: "custom",
|
|
2990
|
+
fn,
|
|
2991
|
+
...normalizeParams(_params)
|
|
2992
|
+
});
|
|
2993
|
+
}
|
|
2994
|
+
function _superRefine(fn, params) {
|
|
2995
|
+
const ch = _check((payload) => {
|
|
2996
|
+
payload.addIssue = (issue$2) => {
|
|
2997
|
+
if (typeof issue$2 === "string") payload.issues.push(issue(issue$2, payload.value, ch._zod.def));
|
|
2998
|
+
else {
|
|
2999
|
+
const _issue = issue$2;
|
|
3000
|
+
if (_issue.fatal) _issue.continue = false;
|
|
3001
|
+
_issue.code ?? (_issue.code = "custom");
|
|
3002
|
+
if (!("input" in _issue)) _issue.input = payload.value;
|
|
3003
|
+
_issue.inst ?? (_issue.inst = ch);
|
|
3004
|
+
_issue.continue ?? (_issue.continue = !ch._zod.def.abort);
|
|
3005
|
+
payload.issues.push(issue(_issue));
|
|
3006
|
+
}
|
|
3007
|
+
};
|
|
3008
|
+
return fn(payload.value, payload);
|
|
3009
|
+
}, params);
|
|
3010
|
+
return ch;
|
|
3011
|
+
}
|
|
3012
|
+
function _check(fn, params) {
|
|
3013
|
+
const ch = new $ZodCheck({
|
|
3014
|
+
check: "custom",
|
|
3015
|
+
...normalizeParams(params)
|
|
3016
|
+
});
|
|
3017
|
+
ch._zod.check = fn;
|
|
3018
|
+
return ch;
|
|
3019
|
+
}
|
|
3020
|
+
function assignProps(target, ...sources) {
|
|
3021
|
+
for (const source of sources) for (const key of Reflect.ownKeys(source)) if (Object.prototype.propertyIsEnumerable.call(source, key)) assignProp(target, key, source[key]);
|
|
3022
|
+
return target;
|
|
3023
|
+
}
|
|
3024
|
+
function initializeContext(params) {
|
|
3025
|
+
let target = params?.target ?? "draft-2020-12";
|
|
3026
|
+
if (target === "draft-4") target = "draft-04";
|
|
3027
|
+
if (target === "draft-7") target = "draft-07";
|
|
3028
|
+
return {
|
|
3029
|
+
processors: params.processors ?? {},
|
|
3030
|
+
metadataRegistry: params?.metadata ?? globalRegistry,
|
|
3031
|
+
target,
|
|
3032
|
+
unrepresentable: params?.unrepresentable ?? "throw",
|
|
3033
|
+
override: params?.override ?? (() => {}),
|
|
3034
|
+
io: params?.io ?? "output",
|
|
3035
|
+
counter: 0,
|
|
3036
|
+
seen: new Map(),
|
|
3037
|
+
sharedDefsExtractedFor: void 0,
|
|
3038
|
+
sharedEmitDoneFor: void 0,
|
|
3039
|
+
cycles: params?.cycles ?? "ref",
|
|
3040
|
+
reused: params?.reused ?? "inline",
|
|
3041
|
+
intersections: [],
|
|
3042
|
+
deferred: [],
|
|
3043
|
+
external: params?.external ?? void 0
|
|
3044
|
+
};
|
|
3045
|
+
}
|
|
3046
|
+
function handleUnrepresentable(schema, ctx, json, params, message) {
|
|
3047
|
+
const result = typeof ctx.unrepresentable === "function" ? ctx.unrepresentable({
|
|
3048
|
+
zodSchema: schema,
|
|
3049
|
+
path: params.path,
|
|
3050
|
+
message
|
|
3051
|
+
}) : ctx.unrepresentable;
|
|
3052
|
+
if (result === "any") return false;
|
|
3053
|
+
if (result === void 0 || result === "throw") throw new Error(message);
|
|
3054
|
+
Object.assign(json, result);
|
|
3055
|
+
return true;
|
|
3056
|
+
}
|
|
3057
|
+
function processSchema(schema, ctx, _params = {
|
|
3058
|
+
path: [],
|
|
3059
|
+
schemaPath: []
|
|
3060
|
+
}) {
|
|
3061
|
+
var _a;
|
|
3062
|
+
const def = schema._zod.def;
|
|
3063
|
+
const seen = ctx.seen.get(schema);
|
|
3064
|
+
if (seen) {
|
|
3065
|
+
seen.count++;
|
|
3066
|
+
if (_params.schemaPath.includes(schema)) seen.cycle = _params.path;
|
|
3067
|
+
return seen.schema;
|
|
3068
|
+
}
|
|
3069
|
+
const result = {
|
|
3070
|
+
schema: {},
|
|
3071
|
+
count: 1,
|
|
3072
|
+
cycle: void 0,
|
|
3073
|
+
path: _params.path
|
|
3074
|
+
};
|
|
3075
|
+
ctx.seen.set(schema, result);
|
|
3076
|
+
ctx.sharedDefsExtractedFor = void 0;
|
|
3077
|
+
ctx.sharedEmitDoneFor = void 0;
|
|
3078
|
+
const overrideSchema = schema._zod.toJSONSchema?.();
|
|
3079
|
+
if (overrideSchema) result.schema = overrideSchema;
|
|
3080
|
+
else {
|
|
3081
|
+
const params = {
|
|
3082
|
+
..._params,
|
|
3083
|
+
schemaPath: [..._params.schemaPath, schema],
|
|
3084
|
+
path: _params.path
|
|
3085
|
+
};
|
|
3086
|
+
if (schema._zod.processJSONSchema) schema._zod.processJSONSchema(ctx, result.schema, params);
|
|
3087
|
+
else {
|
|
3088
|
+
const _json = result.schema;
|
|
3089
|
+
const processor = ctx.processors[def.type];
|
|
3090
|
+
if (!processor) throw new Error(`[toJSONSchema]: Non-representable type encountered: ${def.type}`);
|
|
3091
|
+
processor(schema, ctx, _json, params);
|
|
3092
|
+
}
|
|
3093
|
+
const parent = schema._zod.parent;
|
|
3094
|
+
if (parent) {
|
|
3095
|
+
if (!result.ref) result.ref = parent;
|
|
3096
|
+
processSchema(parent, ctx, params);
|
|
3097
|
+
ctx.seen.get(parent).isParent = true;
|
|
3098
|
+
}
|
|
3099
|
+
}
|
|
3100
|
+
const meta = ctx.metadataRegistry.get(schema);
|
|
3101
|
+
if (meta) assignProps(result.schema, meta);
|
|
3102
|
+
if (ctx.io === "input" && isTransforming(schema)) {
|
|
3103
|
+
delete result.schema.examples;
|
|
3104
|
+
delete result.schema.default;
|
|
3105
|
+
}
|
|
3106
|
+
if (ctx.io === "input" && "_prefault" in result.schema) (_a = result.schema).default ?? (_a.default = result.schema._prefault);
|
|
3107
|
+
delete result.schema._prefault;
|
|
3108
|
+
return ctx.seen.get(schema).schema;
|
|
3109
|
+
}
|
|
3110
|
+
function encodeJSONPointerSegment(segment) {
|
|
3111
|
+
return segment.replace(/~/g, "~0").replace(/\//g, "~1");
|
|
3112
|
+
}
|
|
3113
|
+
function extractDefs(ctx, schema) {
|
|
3114
|
+
const root = ctx.seen.get(schema);
|
|
3115
|
+
if (!root) throw new Error("Unprocessed schema. This is a bug in Zod.");
|
|
3116
|
+
if (ctx.external && ctx.sharedDefsExtractedFor === ctx.external) return;
|
|
3117
|
+
const idToSchema = new Map();
|
|
3118
|
+
for (const entry of ctx.seen.entries()) {
|
|
3119
|
+
const id = ctx.metadataRegistry.get(entry[0])?.id;
|
|
3120
|
+
if (id) {
|
|
3121
|
+
const existing = idToSchema.get(id);
|
|
3122
|
+
if (existing && existing !== entry[0]) throw new Error(`Duplicate schema id "${id}" detected during JSON Schema conversion. Two different schemas cannot share the same id when converted together.`);
|
|
3123
|
+
idToSchema.set(id, entry[0]);
|
|
3124
|
+
}
|
|
3125
|
+
}
|
|
3126
|
+
const makeURI = (entry) => {
|
|
3127
|
+
const defsSegment = ctx.target === "draft-2020-12" ? "$defs" : "definitions";
|
|
3128
|
+
if (ctx.external) {
|
|
3129
|
+
const externalId = ctx.external.registry.get(entry[0])?.id;
|
|
3130
|
+
const uriGenerator = ctx.external.uri ?? ((id) => id);
|
|
3131
|
+
if (externalId) return { ref: uriGenerator(externalId) };
|
|
3132
|
+
const id = entry[1].defId ?? entry[1].schema.id ?? `schema${ctx.counter++}`;
|
|
3133
|
+
entry[1].defId = id;
|
|
3134
|
+
return {
|
|
3135
|
+
defId: id,
|
|
3136
|
+
ref: `${uriGenerator("__shared")}#/${defsSegment}/${encodeJSONPointerSegment(id)}`
|
|
3137
|
+
};
|
|
3138
|
+
}
|
|
3139
|
+
const uriPrefix = `#`;
|
|
3140
|
+
const defUriPrefix = `${uriPrefix}/${defsSegment}/`;
|
|
3141
|
+
if (entry[1] === root && !entry[1].schema.id) return { ref: uriPrefix };
|
|
3142
|
+
const defId = entry[1].schema.id ?? `__schema${ctx.counter++}`;
|
|
3143
|
+
return {
|
|
3144
|
+
defId,
|
|
3145
|
+
ref: defUriPrefix + encodeJSONPointerSegment(defId)
|
|
3146
|
+
};
|
|
3147
|
+
};
|
|
3148
|
+
const extractToDef = (entry) => {
|
|
3149
|
+
if (entry[1].schema.$ref) return;
|
|
3150
|
+
const seen = entry[1];
|
|
3151
|
+
const { ref, defId } = makeURI(entry);
|
|
3152
|
+
seen.def = { ...seen.schema };
|
|
3153
|
+
if (defId) seen.defId = defId;
|
|
3154
|
+
const schema = seen.schema;
|
|
3155
|
+
for (const key in schema) delete schema[key];
|
|
3156
|
+
schema.$ref = ref;
|
|
3157
|
+
};
|
|
3158
|
+
if (ctx.cycles === "throw") for (const entry of ctx.seen.entries()) {
|
|
3159
|
+
const seen = entry[1];
|
|
3160
|
+
if (seen.cycle) throw new Error(`Cycle detected: #/${seen.cycle?.join("/")}/<root>
|
|
3161
|
+
|
|
3162
|
+
Set the \`cycles\` parameter to \`"ref"\` to resolve cyclical schemas with defs.`);
|
|
3163
|
+
}
|
|
3164
|
+
for (const entry of ctx.seen.entries()) {
|
|
3165
|
+
const seen = entry[1];
|
|
3166
|
+
if (schema === entry[0]) {
|
|
3167
|
+
extractToDef(entry);
|
|
3168
|
+
continue;
|
|
3169
|
+
}
|
|
3170
|
+
if (ctx.external) {
|
|
3171
|
+
const ext = ctx.external.registry.get(entry[0])?.id;
|
|
3172
|
+
if (schema !== entry[0] && ext) {
|
|
3173
|
+
extractToDef(entry);
|
|
3174
|
+
continue;
|
|
3175
|
+
}
|
|
3176
|
+
}
|
|
3177
|
+
if (ctx.metadataRegistry.get(entry[0])?.id) {
|
|
3178
|
+
extractToDef(entry);
|
|
3179
|
+
continue;
|
|
3180
|
+
}
|
|
3181
|
+
if (seen.cycle) {
|
|
3182
|
+
extractToDef(entry);
|
|
3183
|
+
continue;
|
|
3184
|
+
}
|
|
3185
|
+
if (seen.count > 1) {
|
|
3186
|
+
if (ctx.reused === "ref") extractToDef(entry);
|
|
3187
|
+
}
|
|
3188
|
+
}
|
|
3189
|
+
if (ctx.external) ctx.sharedDefsExtractedFor = ctx.external;
|
|
3190
|
+
}
|
|
3191
|
+
function compactTypeUnion(schema) {
|
|
3192
|
+
const options = schema.anyOf;
|
|
3193
|
+
if (!Array.isArray(options) || options.length === 0 || schema.type !== void 0) return;
|
|
3194
|
+
const types = [];
|
|
3195
|
+
for (const option of options) {
|
|
3196
|
+
if (!option || typeof option !== "object") return;
|
|
3197
|
+
compactTypeUnion(option);
|
|
3198
|
+
const keys = Object.keys(option);
|
|
3199
|
+
if (keys.length !== 1 || keys[0] !== "type") return;
|
|
3200
|
+
const type = option.type;
|
|
3201
|
+
for (const member of Array.isArray(type) ? type : [type]) {
|
|
3202
|
+
if (typeof member !== "string") return;
|
|
3203
|
+
if (!types.includes(member)) types.push(member);
|
|
3204
|
+
}
|
|
3205
|
+
}
|
|
3206
|
+
delete schema.anyOf;
|
|
3207
|
+
schema.type = types.length === 1 ? types[0] : types;
|
|
3208
|
+
}
|
|
3209
|
+
var FOLDABLE_KEYS = new Set([
|
|
3210
|
+
"type",
|
|
3211
|
+
"properties",
|
|
3212
|
+
"required",
|
|
3213
|
+
"additionalProperties"
|
|
3214
|
+
]);
|
|
3215
|
+
var UNION_KEYS = ["oneOf", "anyOf"];
|
|
3216
|
+
function undeclaredConstraint(member) {
|
|
3217
|
+
const extra = member.additionalProperties;
|
|
3218
|
+
if (extra === void 0 || extra === false || typeof extra !== "object" || extra === null) return null;
|
|
3219
|
+
return Object.keys(extra).length ? extra : null;
|
|
3220
|
+
}
|
|
3221
|
+
function foldObjects(members) {
|
|
3222
|
+
const objects = [];
|
|
3223
|
+
for (const member of members) {
|
|
3224
|
+
if (typeof member !== "object" || member.type !== "object") return null;
|
|
3225
|
+
for (const key in member) if (!FOLDABLE_KEYS.has(key)) return null;
|
|
3226
|
+
objects.push(member);
|
|
3227
|
+
}
|
|
3228
|
+
const properties = {};
|
|
3229
|
+
const required = new Set();
|
|
3230
|
+
for (const object of objects) {
|
|
3231
|
+
for (const key in object.properties) {
|
|
3232
|
+
if (Object.prototype.hasOwnProperty.call(properties, key)) continue;
|
|
3233
|
+
const parts = [];
|
|
3234
|
+
for (const other of objects) {
|
|
3235
|
+
const part = other.properties?.[key] ?? undeclaredConstraint(other);
|
|
3236
|
+
if (part === null || part === void 0) continue;
|
|
3237
|
+
if (!parts.some((seen) => JSON.stringify(seen) === JSON.stringify(part))) parts.push(part);
|
|
3238
|
+
}
|
|
3239
|
+
assignProp(properties, key, parts.length === 1 ? parts[0] : foldObjects(parts) ?? { allOf: parts });
|
|
3240
|
+
}
|
|
3241
|
+
for (const key of object.required ?? []) required.add(key);
|
|
3242
|
+
}
|
|
3243
|
+
const folded = {
|
|
3244
|
+
type: "object",
|
|
3245
|
+
properties
|
|
3246
|
+
};
|
|
3247
|
+
if (required.size) folded.required = [...required];
|
|
3248
|
+
if (objects.every((object) => object.additionalProperties === false)) folded.additionalProperties = false;
|
|
3249
|
+
else {
|
|
3250
|
+
const constraints = [];
|
|
3251
|
+
for (const object of objects) {
|
|
3252
|
+
const constraint = undeclaredConstraint(object);
|
|
3253
|
+
if (constraint && !constraints.some((seen) => JSON.stringify(seen) === JSON.stringify(constraint))) constraints.push(constraint);
|
|
3254
|
+
}
|
|
3255
|
+
if (constraints.length === 1) folded.additionalProperties = constraints[0];
|
|
3256
|
+
else if (constraints.length > 1) folded.additionalProperties = { allOf: constraints };
|
|
3257
|
+
}
|
|
3258
|
+
return folded;
|
|
3259
|
+
}
|
|
3260
|
+
function foldIntersection(json) {
|
|
3261
|
+
const allOf = json.allOf;
|
|
3262
|
+
if (!Array.isArray(allOf) || allOf.length < 2) return;
|
|
3263
|
+
for (const key of FOLDABLE_KEYS) if (key in json) return;
|
|
3264
|
+
const unions = allOf.filter((m) => UNION_KEYS.some((k) => Array.isArray(m[k])));
|
|
3265
|
+
let folded = null;
|
|
3266
|
+
if (!unions.length) folded = foldObjects(allOf);
|
|
3267
|
+
else {
|
|
3268
|
+
const union = unions[0];
|
|
3269
|
+
const keyword = UNION_KEYS.find((k) => Array.isArray(union[k]));
|
|
3270
|
+
if (Object.keys(union).length !== 1) return;
|
|
3271
|
+
const rest = allOf.filter((m) => m !== union);
|
|
3272
|
+
const branches = union[keyword].map((branch) => foldObjects([...rest, branch]));
|
|
3273
|
+
if (branches.some((b) => !b)) return;
|
|
3274
|
+
folded = { [keyword]: branches };
|
|
3275
|
+
}
|
|
3276
|
+
if (!folded) return;
|
|
3277
|
+
delete json.allOf;
|
|
3278
|
+
assignProps(json, folded);
|
|
3279
|
+
}
|
|
3280
|
+
function finalize(ctx, schema) {
|
|
3281
|
+
const root = ctx.seen.get(schema);
|
|
3282
|
+
if (!root) throw new Error("Unprocessed schema. This is a bug in Zod.");
|
|
3283
|
+
const flattenRef = (zodSchema) => {
|
|
3284
|
+
const seen = ctx.seen.get(zodSchema);
|
|
3285
|
+
if (seen.ref === null) return;
|
|
3286
|
+
const schema = seen.def ?? seen.schema;
|
|
3287
|
+
const _cached = { ...schema };
|
|
3288
|
+
const ref = seen.ref;
|
|
3289
|
+
seen.ref = null;
|
|
3290
|
+
if (ref) {
|
|
3291
|
+
flattenRef(ref);
|
|
3292
|
+
const refSeen = ctx.seen.get(ref);
|
|
3293
|
+
const refSchema = refSeen.schema;
|
|
3294
|
+
if (refSchema.$ref && (ctx.target === "draft-07" || ctx.target === "draft-04" || ctx.target === "openapi-3.0")) {
|
|
3295
|
+
schema.allOf = schema.allOf ?? [];
|
|
3296
|
+
schema.allOf.push(refSchema);
|
|
3297
|
+
} else assignProps(schema, refSchema);
|
|
3298
|
+
assignProps(schema, _cached);
|
|
3299
|
+
if (zodSchema._zod.parent === ref) for (const key in schema) {
|
|
3300
|
+
if (key === "$ref" || key === "allOf") continue;
|
|
3301
|
+
if (!(key in _cached)) delete schema[key];
|
|
3302
|
+
}
|
|
3303
|
+
if (refSchema.$ref && refSeen.def) for (const key in schema) {
|
|
3304
|
+
if (key === "$ref" || key === "allOf") continue;
|
|
3305
|
+
if (key in refSeen.def && JSON.stringify(schema[key]) === JSON.stringify(refSeen.def[key])) delete schema[key];
|
|
3306
|
+
}
|
|
3307
|
+
}
|
|
3308
|
+
const parent = zodSchema._zod.parent;
|
|
3309
|
+
if (parent && parent !== ref) {
|
|
3310
|
+
flattenRef(parent);
|
|
3311
|
+
const parentSeen = ctx.seen.get(parent);
|
|
3312
|
+
if (parentSeen?.schema.$ref) {
|
|
3313
|
+
schema.$ref = parentSeen.schema.$ref;
|
|
3314
|
+
if (parentSeen.def) for (const key in schema) {
|
|
3315
|
+
if (key === "$ref" || key === "allOf") continue;
|
|
3316
|
+
if (key in parentSeen.def && JSON.stringify(schema[key]) === JSON.stringify(parentSeen.def[key])) delete schema[key];
|
|
3317
|
+
}
|
|
3318
|
+
}
|
|
3319
|
+
}
|
|
3320
|
+
ctx.override({
|
|
3321
|
+
zodSchema,
|
|
3322
|
+
jsonSchema: schema,
|
|
3323
|
+
path: seen.path ?? []
|
|
3324
|
+
});
|
|
3325
|
+
};
|
|
3326
|
+
if (!ctx.external || ctx.sharedEmitDoneFor !== ctx.external) {
|
|
3327
|
+
for (const entry of [...ctx.seen.entries()].reverse()) flattenRef(entry[0]);
|
|
3328
|
+
if (ctx.target !== "openapi-3.0") for (const entry of ctx.seen.entries()) compactTypeUnion(entry[1].def ?? entry[1].schema);
|
|
3329
|
+
for (const rewrite of ctx.deferred) rewrite();
|
|
3330
|
+
if (ctx.intersections.length) {
|
|
3331
|
+
const carriers = new Map();
|
|
3332
|
+
for (const seen of ctx.seen.values()) for (const json of [seen.schema, seen.def]) {
|
|
3333
|
+
const allOf = json?.allOf;
|
|
3334
|
+
if (!Array.isArray(allOf)) continue;
|
|
3335
|
+
const existing = carriers.get(allOf);
|
|
3336
|
+
if (existing) existing.push(json);
|
|
3337
|
+
else carriers.set(allOf, [json]);
|
|
3338
|
+
}
|
|
3339
|
+
for (const allOf of ctx.intersections) for (const json of carriers.get(allOf) ?? []) foldIntersection(json);
|
|
3340
|
+
}
|
|
3341
|
+
}
|
|
3342
|
+
const result = {};
|
|
3343
|
+
if (ctx.target === "draft-2020-12") result.$schema = "https://json-schema.org/draft/2020-12/schema";
|
|
3344
|
+
else if (ctx.target === "draft-07") result.$schema = "http://json-schema.org/draft-07/schema#";
|
|
3345
|
+
else if (ctx.target === "draft-04") result.$schema = "http://json-schema.org/draft-04/schema#";
|
|
3346
|
+
else if (ctx.target === "openapi-3.0") {}
|
|
3347
|
+
if (ctx.external?.uri) {
|
|
3348
|
+
const id = ctx.external.registry.get(schema)?.id;
|
|
3349
|
+
if (!id) throw new Error("Schema is missing an `id` property");
|
|
3350
|
+
result.$id = ctx.external.uri(id);
|
|
3351
|
+
}
|
|
3352
|
+
assignProps(result, root.defId ? root.schema : root.def ?? root.schema);
|
|
3353
|
+
const rootMetaId = ctx.metadataRegistry.get(schema)?.id;
|
|
3354
|
+
if (rootMetaId !== void 0 && result.id === rootMetaId) delete result.id;
|
|
3355
|
+
const defs = ctx.external?.defs ?? {};
|
|
3356
|
+
if (!ctx.external || ctx.sharedEmitDoneFor !== ctx.external) for (const entry of ctx.seen.entries()) {
|
|
3357
|
+
const seen = entry[1];
|
|
3358
|
+
if (seen.def && seen.defId) {
|
|
3359
|
+
if (seen.def.id === seen.defId) delete seen.def.id;
|
|
3360
|
+
assignProp(defs, seen.defId, seen.def);
|
|
3361
|
+
}
|
|
3362
|
+
}
|
|
3363
|
+
if (ctx.external) ctx.sharedEmitDoneFor = ctx.external;
|
|
3364
|
+
if (ctx.external) {} else if (Object.keys(defs).length > 0) {
|
|
3365
|
+
if (ctx.target === "draft-2020-12") result.$defs = defs;
|
|
3366
|
+
else result.definitions = defs;
|
|
3367
|
+
}
|
|
3368
|
+
try {
|
|
3369
|
+
const finalized = JSON.parse(JSON.stringify(result));
|
|
3370
|
+
Object.defineProperty(finalized, "~standard", {
|
|
3371
|
+
value: {
|
|
3372
|
+
...schema["~standard"],
|
|
3373
|
+
jsonSchema: {
|
|
3374
|
+
input: createStandardJSONSchemaMethod(schema, "input", ctx.processors),
|
|
3375
|
+
output: createStandardJSONSchemaMethod(schema, "output", ctx.processors)
|
|
3376
|
+
}
|
|
3377
|
+
},
|
|
3378
|
+
enumerable: false,
|
|
3379
|
+
writable: false
|
|
3380
|
+
});
|
|
3381
|
+
return finalized;
|
|
3382
|
+
} catch (_err) {
|
|
3383
|
+
throw new Error("Error converting schema to JSON.");
|
|
3384
|
+
}
|
|
3385
|
+
}
|
|
3386
|
+
function isTransforming(_schema, _ctx) {
|
|
3387
|
+
const ctx = _ctx ?? { seen: new Set() };
|
|
3388
|
+
if (ctx.seen.has(_schema)) return false;
|
|
3389
|
+
ctx.seen.add(_schema);
|
|
3390
|
+
const def = _schema._zod.def;
|
|
3391
|
+
if (def.type === "transform") return true;
|
|
3392
|
+
if (def.type === "array") return isTransforming(def.element, ctx);
|
|
3393
|
+
if (def.type === "set") return isTransforming(def.valueType, ctx);
|
|
3394
|
+
if (def.type === "lazy") return isTransforming(def.getter(), ctx);
|
|
3395
|
+
if (def.type === "promise" || def.type === "optional" || def.type === "nonoptional" || def.type === "nullable" || def.type === "readonly" || def.type === "default" || def.type === "prefault" || def.type === "catch") return isTransforming(def.innerType, ctx);
|
|
3396
|
+
if (def.type === "intersection") return isTransforming(def.left, ctx) || isTransforming(def.right, ctx);
|
|
3397
|
+
if (def.type === "record" || def.type === "map") return isTransforming(def.keyType, ctx) || isTransforming(def.valueType, ctx);
|
|
3398
|
+
if (def.type === "pipe") {
|
|
3399
|
+
if (_schema._zod.traits.has("$ZodCodec")) return true;
|
|
3400
|
+
return isTransforming(def.in, ctx) || isTransforming(def.out, ctx);
|
|
3401
|
+
}
|
|
3402
|
+
if (def.type === "object") {
|
|
3403
|
+
for (const key in def.shape) if (isTransforming(def.shape[key], ctx)) return true;
|
|
3404
|
+
return false;
|
|
3405
|
+
}
|
|
3406
|
+
if (def.type === "union") {
|
|
3407
|
+
for (const option of def.options) if (isTransforming(option, ctx)) return true;
|
|
3408
|
+
return false;
|
|
3409
|
+
}
|
|
3410
|
+
if (def.type === "tuple") {
|
|
3411
|
+
for (const item of def.items) if (isTransforming(item, ctx)) return true;
|
|
3412
|
+
if (def.rest && isTransforming(def.rest, ctx)) return true;
|
|
3413
|
+
return false;
|
|
3414
|
+
}
|
|
3415
|
+
return false;
|
|
3416
|
+
}
|
|
3417
|
+
var createToJSONSchemaMethod = (schema, processors = {}) => (params) => {
|
|
3418
|
+
const ctx = initializeContext({
|
|
3419
|
+
...params,
|
|
3420
|
+
processors
|
|
3421
|
+
});
|
|
3422
|
+
processSchema(schema, ctx);
|
|
3423
|
+
extractDefs(ctx, schema);
|
|
3424
|
+
return finalize(ctx, schema);
|
|
3425
|
+
};
|
|
3426
|
+
var createStandardJSONSchemaMethod = (schema, io, processors = {}) => (params) => {
|
|
3427
|
+
const { libraryOptions, target } = params ?? {};
|
|
3428
|
+
const ctx = initializeContext({
|
|
3429
|
+
...libraryOptions ?? {},
|
|
3430
|
+
target,
|
|
3431
|
+
io,
|
|
3432
|
+
processors
|
|
3433
|
+
});
|
|
3434
|
+
processSchema(schema, ctx);
|
|
3435
|
+
extractDefs(ctx, schema);
|
|
3436
|
+
return finalize(ctx, schema);
|
|
3437
|
+
};
|
|
3438
|
+
var narrowMin = (agg, key, value) => {
|
|
3439
|
+
if (agg[key] === void 0 || value > agg[key]) agg[key] = value;
|
|
3440
|
+
};
|
|
3441
|
+
var narrowMax = (agg, key, value) => {
|
|
3442
|
+
if (agg[key] === void 0 || value < agg[key]) agg[key] = value;
|
|
3443
|
+
};
|
|
3444
|
+
var narrowBoth = (agg, value) => {
|
|
3445
|
+
narrowMin(agg, "minimum", value);
|
|
3446
|
+
narrowMax(agg, "maximum", value);
|
|
3447
|
+
};
|
|
3448
|
+
var addDivisor = (agg, value) => {
|
|
3449
|
+
agg.multipleOf ?? (agg.multipleOf = []);
|
|
3450
|
+
if (!agg.multipleOf.includes(value)) agg.multipleOf.push(value);
|
|
3451
|
+
};
|
|
3452
|
+
var addPattern = (agg, pattern) => {
|
|
3453
|
+
agg.patterns ?? (agg.patterns = new Set());
|
|
3454
|
+
agg.patterns.add(pattern);
|
|
3455
|
+
};
|
|
3456
|
+
var intersectMime = (agg, mime) => {
|
|
3457
|
+
agg.mime = agg.mime ? agg.mime.filter((m) => mime.includes(m)) : [...mime];
|
|
3458
|
+
};
|
|
3459
|
+
var setFormat = (agg, format) => {
|
|
3460
|
+
agg.format = format;
|
|
3461
|
+
if (format.includes("int")) agg.isInt = true;
|
|
3462
|
+
};
|
|
3463
|
+
var minContributor = (agg, def) => narrowMin(agg, "minimum", def.minimum);
|
|
3464
|
+
var maxContributor = (agg, def) => narrowMax(agg, "maximum", def.maximum);
|
|
3465
|
+
var formatContributor = (ranges) => (agg, def) => {
|
|
3466
|
+
setFormat(agg, def.format);
|
|
3467
|
+
const [minimum, maximum] = ranges[def.format];
|
|
3468
|
+
narrowMin(agg, "minimum", minimum);
|
|
3469
|
+
narrowMax(agg, "maximum", maximum);
|
|
3470
|
+
};
|
|
3471
|
+
var contributors = {
|
|
3472
|
+
greater_than: (agg, def) => narrowMin(agg, def.inclusive ? "minimum" : "exclusiveMinimum", def.value),
|
|
3473
|
+
less_than: (agg, def) => narrowMax(agg, def.inclusive ? "maximum" : "exclusiveMaximum", def.value),
|
|
3474
|
+
multiple_of: (agg, def) => addDivisor(agg, def.value),
|
|
3475
|
+
number_format: formatContributor(NUMBER_FORMAT_RANGES),
|
|
3476
|
+
bigint_format: formatContributor(BIGINT_FORMAT_RANGES),
|
|
3477
|
+
min_length: minContributor,
|
|
3478
|
+
max_length: maxContributor,
|
|
3479
|
+
length_equals: (agg, def) => narrowBoth(agg, def.length),
|
|
3480
|
+
min_size: minContributor,
|
|
3481
|
+
max_size: maxContributor,
|
|
3482
|
+
size_equals: (agg, def) => narrowBoth(agg, def.size),
|
|
3483
|
+
string_format: (agg, def) => {
|
|
3484
|
+
setFormat(agg, def.format);
|
|
3485
|
+
if (def.pattern) addPattern(agg, def.pattern);
|
|
3486
|
+
if (def.format === "base64" || def.format === "base64url") agg.contentEncoding = def.format;
|
|
3487
|
+
if (def.local || def.precision === -1) agg.laxFormat = true;
|
|
3488
|
+
},
|
|
3489
|
+
mime_type: (agg, def) => intersectMime(agg, def.mime)
|
|
3490
|
+
};
|
|
3491
|
+
function aggregateChecks(schema) {
|
|
3492
|
+
const agg = {};
|
|
3493
|
+
const def = schema._zod.def;
|
|
3494
|
+
const list = schema._zod.traits.has("$ZodCheck") ? [schema, ...def.checks ?? []] : def.checks ?? [];
|
|
3495
|
+
for (const ch of list) contributors[ch._zod.def.check]?.(agg, ch._zod.def);
|
|
3496
|
+
const bag = schema._zod.bag;
|
|
3497
|
+
if (bag.minimum !== void 0) narrowMin(agg, "minimum", bag.minimum);
|
|
3498
|
+
if (bag.exclusiveMinimum !== void 0) narrowMin(agg, "exclusiveMinimum", bag.exclusiveMinimum);
|
|
3499
|
+
if (bag.maximum !== void 0) narrowMax(agg, "maximum", bag.maximum);
|
|
3500
|
+
if (bag.exclusiveMaximum !== void 0) narrowMax(agg, "exclusiveMaximum", bag.exclusiveMaximum);
|
|
3501
|
+
if (bag.multipleOf !== void 0) addDivisor(agg, bag.multipleOf);
|
|
3502
|
+
if (bag.format !== void 0) {
|
|
3503
|
+
agg.format ?? (agg.format = bag.format);
|
|
3504
|
+
if (bag.format.includes("int")) agg.isInt = true;
|
|
3505
|
+
}
|
|
3506
|
+
if (bag.mime) intersectMime(agg, bag.mime);
|
|
3507
|
+
for (const pattern of bag.patterns ?? []) addPattern(agg, pattern);
|
|
3508
|
+
return agg;
|
|
3509
|
+
}
|
|
3510
|
+
var formatMap = {
|
|
3511
|
+
guid: "uuid",
|
|
3512
|
+
url: "uri",
|
|
3513
|
+
datetime: "date-time",
|
|
3514
|
+
json_string: "json-string",
|
|
3515
|
+
regex: ""
|
|
3516
|
+
};
|
|
3517
|
+
var exactPatterns = new Map([[base64Charset, base64], [base64urlCharset, base64url]]);
|
|
3518
|
+
var exactPattern = (p) => exactPatterns.get(p) ?? p;
|
|
3519
|
+
var stringProcessor = (schema, ctx, _json, _params) => {
|
|
3520
|
+
const json = _json;
|
|
3521
|
+
json.type = "string";
|
|
3522
|
+
const { minimum, maximum, format, patterns, contentEncoding, laxFormat } = aggregateChecks(schema);
|
|
3523
|
+
if (typeof minimum === "number") json.minLength = minimum;
|
|
3524
|
+
if (typeof maximum === "number") json.maxLength = maximum;
|
|
3525
|
+
if (format) {
|
|
3526
|
+
json.format = formatMap[format] ?? format;
|
|
3527
|
+
if (json.format === "") delete json.format;
|
|
3528
|
+
if (format === "time" || laxFormat) delete json.format;
|
|
3529
|
+
}
|
|
3530
|
+
if (contentEncoding) json.contentEncoding = contentEncoding;
|
|
3531
|
+
if (patterns && patterns.size > 0) {
|
|
3532
|
+
const patternList = [...patterns].map(exactPattern);
|
|
3533
|
+
if (patternList.length === 1) json.pattern = patternList[0].source;
|
|
3534
|
+
else if (patternList.length > 1) json.allOf = [...patternList.map((regex) => ({
|
|
3535
|
+
...ctx.target === "draft-07" || ctx.target === "draft-04" || ctx.target === "openapi-3.0" ? { type: "string" } : {},
|
|
3536
|
+
pattern: regex.source
|
|
3537
|
+
}))];
|
|
3538
|
+
}
|
|
3539
|
+
};
|
|
3540
|
+
var booleanProcessor = (_schema, _ctx, json, _params) => {
|
|
3541
|
+
json.type = "boolean";
|
|
3542
|
+
};
|
|
3543
|
+
var customProcessor = (schema, ctx, json, params) => {
|
|
3544
|
+
handleUnrepresentable(schema, ctx, json, params, "Custom types cannot be represented in JSON Schema");
|
|
3545
|
+
};
|
|
3546
|
+
var transformProcessor = (schema, ctx, json, params) => {
|
|
3547
|
+
handleUnrepresentable(schema, ctx, json, params, "Transforms cannot be represented in JSON Schema");
|
|
3548
|
+
};
|
|
3549
|
+
var arrayProcessor = (schema, ctx, _json, params) => {
|
|
3550
|
+
const json = _json;
|
|
3551
|
+
const def = schema._zod.def;
|
|
3552
|
+
const { minimum, maximum } = aggregateChecks(schema);
|
|
3553
|
+
if (typeof minimum === "number") json.minItems = minimum;
|
|
3554
|
+
if (typeof maximum === "number") json.maxItems = maximum;
|
|
3555
|
+
json.type = "array";
|
|
3556
|
+
json.items = processSchema(def.element, ctx, {
|
|
3557
|
+
...params,
|
|
3558
|
+
path: [...params.path, "items"]
|
|
3559
|
+
});
|
|
3560
|
+
};
|
|
3561
|
+
var unionProcessor = (schema, ctx, json, params) => {
|
|
3562
|
+
const def = schema._zod.def;
|
|
3563
|
+
const isExclusive = def.inclusive === false;
|
|
3564
|
+
const options = def.options.map((x, i) => processSchema(x, ctx, {
|
|
3565
|
+
...params,
|
|
3566
|
+
path: [
|
|
3567
|
+
...params.path,
|
|
3568
|
+
isExclusive ? "oneOf" : "anyOf",
|
|
3569
|
+
i
|
|
3570
|
+
]
|
|
3571
|
+
}));
|
|
3572
|
+
if (isExclusive) json.oneOf = options;
|
|
3573
|
+
else json.anyOf = options;
|
|
3574
|
+
};
|
|
3575
|
+
var intersectionProcessor = (schema, ctx, json, params) => {
|
|
3576
|
+
const def = schema._zod.def;
|
|
3577
|
+
const a = processSchema(def.left, ctx, {
|
|
3578
|
+
...params,
|
|
3579
|
+
path: [
|
|
3580
|
+
...params.path,
|
|
3581
|
+
"allOf",
|
|
3582
|
+
0
|
|
3583
|
+
]
|
|
3584
|
+
});
|
|
3585
|
+
const b = processSchema(def.right, ctx, {
|
|
3586
|
+
...params,
|
|
3587
|
+
path: [
|
|
3588
|
+
...params.path,
|
|
3589
|
+
"allOf",
|
|
3590
|
+
1
|
|
3591
|
+
]
|
|
3592
|
+
});
|
|
3593
|
+
const isSimpleIntersection = (val) => "allOf" in val && Object.keys(val).length === 1;
|
|
3594
|
+
const allOf = [...isSimpleIntersection(a) ? a.allOf : [a], ...isSimpleIntersection(b) ? b.allOf : [b]];
|
|
3595
|
+
json.allOf = allOf;
|
|
3596
|
+
ctx.intersections.push(allOf);
|
|
3597
|
+
};
|
|
3598
|
+
var nullableProcessor = (schema, ctx, json, params) => {
|
|
3599
|
+
const def = schema._zod.def;
|
|
3600
|
+
const inner = processSchema(def.innerType, ctx, params);
|
|
3601
|
+
const seen = ctx.seen.get(schema);
|
|
3602
|
+
if (ctx.target === "openapi-3.0") {
|
|
3603
|
+
seen.ref = def.innerType;
|
|
3604
|
+
json.nullable = true;
|
|
3605
|
+
} else json.anyOf = [inner, { type: "null" }];
|
|
3606
|
+
};
|
|
3607
|
+
var nonoptionalProcessor = (schema, ctx, _json, params) => {
|
|
3608
|
+
const def = schema._zod.def;
|
|
3609
|
+
processSchema(def.innerType, ctx, params);
|
|
3610
|
+
const seen = ctx.seen.get(schema);
|
|
3611
|
+
seen.ref = def.innerType;
|
|
3612
|
+
};
|
|
3613
|
+
var UNREPRESENTABLE_DEFAULT = Symbol();
|
|
3614
|
+
function serializeDefaultValue(value, schema, ctx, json, params) {
|
|
3615
|
+
let unrepresentable = false;
|
|
3616
|
+
const serialized = JSON.stringify(value, (_, val) => {
|
|
3617
|
+
if (typeof val !== "bigint") return val;
|
|
3618
|
+
unrepresentable = true;
|
|
3619
|
+
return null;
|
|
3620
|
+
});
|
|
3621
|
+
if (!unrepresentable) return JSON.parse(serialized);
|
|
3622
|
+
handleUnrepresentable(schema, ctx, json, params, "BigInt defaults cannot be represented in JSON Schema");
|
|
3623
|
+
return UNREPRESENTABLE_DEFAULT;
|
|
3624
|
+
}
|
|
3625
|
+
var defaultProcessor = (schema, ctx, json, params) => {
|
|
3626
|
+
const def = schema._zod.def;
|
|
3627
|
+
processSchema(def.innerType, ctx, params);
|
|
3628
|
+
const seen = ctx.seen.get(schema);
|
|
3629
|
+
seen.ref = def.innerType;
|
|
3630
|
+
const value = serializeDefaultValue(def.defaultValue, schema, ctx, json, params);
|
|
3631
|
+
if (value !== UNREPRESENTABLE_DEFAULT) json.default = value;
|
|
3632
|
+
};
|
|
3633
|
+
var prefaultProcessor = (schema, ctx, json, params) => {
|
|
3634
|
+
const def = schema._zod.def;
|
|
3635
|
+
processSchema(def.innerType, ctx, params);
|
|
3636
|
+
const seen = ctx.seen.get(schema);
|
|
3637
|
+
seen.ref = def.innerType;
|
|
3638
|
+
if (ctx.io !== "input") return;
|
|
3639
|
+
const value = serializeDefaultValue(def.defaultValue, schema, ctx, json, params);
|
|
3640
|
+
if (value !== UNREPRESENTABLE_DEFAULT) json._prefault = value;
|
|
3641
|
+
};
|
|
3642
|
+
var catchProcessor = (schema, ctx, json, params) => {
|
|
3643
|
+
const def = schema._zod.def;
|
|
3644
|
+
processSchema(def.innerType, ctx, params);
|
|
3645
|
+
const seen = ctx.seen.get(schema);
|
|
3646
|
+
seen.ref = def.innerType;
|
|
3647
|
+
let catchValue;
|
|
3648
|
+
try {
|
|
3649
|
+
catchValue = def.catchValue(void 0);
|
|
3650
|
+
} catch {
|
|
3651
|
+
handleUnrepresentable(schema, ctx, json, params, "Dynamic catch values are not supported in JSON Schema");
|
|
3652
|
+
return;
|
|
3653
|
+
}
|
|
3654
|
+
json.default = catchValue;
|
|
3655
|
+
};
|
|
3656
|
+
var pipeProcessor = (schema, ctx, _json, params) => {
|
|
3657
|
+
const def = schema._zod.def;
|
|
3658
|
+
const inIsTransform = def.in._zod.traits.has("$ZodTransform");
|
|
3659
|
+
const innerType = ctx.io === "input" ? inIsTransform ? def.out : def.in : def.out;
|
|
3660
|
+
processSchema(innerType, ctx, params);
|
|
3661
|
+
const seen = ctx.seen.get(schema);
|
|
3662
|
+
seen.ref = innerType;
|
|
3663
|
+
};
|
|
3664
|
+
var readonlyProcessor = (schema, ctx, json, params) => {
|
|
3665
|
+
const def = schema._zod.def;
|
|
3666
|
+
processSchema(def.innerType, ctx, params);
|
|
3667
|
+
const seen = ctx.seen.get(schema);
|
|
3668
|
+
seen.ref = def.innerType;
|
|
3669
|
+
json.readOnly = true;
|
|
3670
|
+
};
|
|
3671
|
+
var optionalProcessor = (schema, ctx, _json, params) => {
|
|
3672
|
+
const def = schema._zod.def;
|
|
3673
|
+
processSchema(def.innerType, ctx, params);
|
|
3674
|
+
const seen = ctx.seen.get(schema);
|
|
3675
|
+
seen.ref = def.innerType;
|
|
3676
|
+
};
|
|
3677
|
+
var _installedErrorProtos = new WeakSet([Object.prototype, Error.prototype]);
|
|
3678
|
+
function _lazyMethod(proto, key, make) {
|
|
3679
|
+
Object.defineProperty(proto, key, {
|
|
3680
|
+
configurable: true,
|
|
3681
|
+
enumerable: false,
|
|
3682
|
+
get() {
|
|
3683
|
+
const value = make(this);
|
|
3684
|
+
Object.defineProperty(this, key, {
|
|
3685
|
+
value,
|
|
3686
|
+
configurable: true,
|
|
3687
|
+
writable: true
|
|
3688
|
+
});
|
|
3689
|
+
return value;
|
|
3690
|
+
},
|
|
3691
|
+
set(value) {
|
|
3692
|
+
Object.defineProperty(this, key, {
|
|
3693
|
+
value,
|
|
3694
|
+
configurable: true,
|
|
3695
|
+
writable: true
|
|
3696
|
+
});
|
|
3697
|
+
}
|
|
3698
|
+
});
|
|
3699
|
+
}
|
|
3700
|
+
var initializer = (inst, issues) => {
|
|
3701
|
+
$ZodError.init(inst, issues);
|
|
3702
|
+
inst.name = "ZodError";
|
|
3703
|
+
const proto = Object.getPrototypeOf(inst);
|
|
3704
|
+
if (_installedErrorProtos.has(proto)) return;
|
|
3705
|
+
_installedErrorProtos.add(proto);
|
|
3706
|
+
_lazyMethod(proto, "format", (self) => (mapper) => formatError(self, mapper));
|
|
3707
|
+
_lazyMethod(proto, "flatten", (self) => (mapper) => flattenError(self, mapper));
|
|
3708
|
+
_lazyMethod(proto, "addIssue", (self) => (issue) => {
|
|
3709
|
+
self.issues.push(issue);
|
|
3710
|
+
self.message = JSON.stringify(self.issues, jsonStringifyReplacer, 2);
|
|
3711
|
+
});
|
|
3712
|
+
_lazyMethod(proto, "addIssues", (self) => (issues) => {
|
|
3713
|
+
self.issues.push(...issues);
|
|
3714
|
+
self.message = JSON.stringify(self.issues, jsonStringifyReplacer, 2);
|
|
3715
|
+
});
|
|
3716
|
+
Object.defineProperty(proto, "isEmpty", {
|
|
3717
|
+
configurable: true,
|
|
3718
|
+
enumerable: false,
|
|
3719
|
+
get() {
|
|
3720
|
+
return this.issues.length === 0;
|
|
3721
|
+
}
|
|
3722
|
+
});
|
|
3723
|
+
};
|
|
3724
|
+
var ZodRealError = $constructor("ZodError", initializer, void 0, { Parent: Error });
|
|
3725
|
+
var parse = _parse(ZodRealError);
|
|
3726
|
+
var parseAsync = _parseAsync(ZodRealError);
|
|
3727
|
+
var safeParse = _safeParse(ZodRealError);
|
|
3728
|
+
var safeParseAsync = _safeParseAsync(ZodRealError);
|
|
3729
|
+
var encode = _encode(ZodRealError);
|
|
3730
|
+
var decode = _decode(ZodRealError);
|
|
3731
|
+
var encodeAsync = _encodeAsync(ZodRealError);
|
|
3732
|
+
var decodeAsync = _decodeAsync(ZodRealError);
|
|
3733
|
+
var safeEncode = _safeEncode(ZodRealError);
|
|
3734
|
+
var safeDecode = _safeDecode(ZodRealError);
|
|
3735
|
+
var safeEncodeAsync = _safeEncodeAsync(ZodRealError);
|
|
3736
|
+
var safeDecodeAsync = _safeDecodeAsync(ZodRealError);
|
|
3737
|
+
function _ensureDefaultLocale() {
|
|
3738
|
+
if (!globalConfig.localeError) config(en_default());
|
|
3739
|
+
}
|
|
3740
|
+
function _ensureDefaultMemoizer() {
|
|
3741
|
+
if (!globalConfig.memoizer) config({ memoizer: memoizer() });
|
|
3742
|
+
}
|
|
3743
|
+
var ZodType = $constructor("ZodType", (inst, def) => {
|
|
3744
|
+
_ensureDefaultLocale();
|
|
3745
|
+
$ZodType.init(inst, def);
|
|
3746
|
+
inst.def = def;
|
|
3747
|
+
inst.type = def.type;
|
|
3748
|
+
return inst;
|
|
3749
|
+
}, {
|
|
3750
|
+
check(...chks) {
|
|
3751
|
+
const def = this.def;
|
|
3752
|
+
return this.clone(mergeDefs(def, { checks: [...def.checks ?? [], ...chks.map((ch) => typeof ch === "function" ? { _zod: {
|
|
3753
|
+
check: ch,
|
|
3754
|
+
def: { check: "custom" },
|
|
3755
|
+
onattach: []
|
|
3756
|
+
} } : ch)] }), { parent: true });
|
|
3757
|
+
},
|
|
3758
|
+
with(...chks) {
|
|
3759
|
+
return this.check(...chks);
|
|
3760
|
+
},
|
|
3761
|
+
clone(def, params) {
|
|
3762
|
+
return clone(this, def, params);
|
|
3763
|
+
},
|
|
3764
|
+
brand() {
|
|
3765
|
+
return this;
|
|
3766
|
+
},
|
|
3767
|
+
register(reg, meta) {
|
|
3768
|
+
reg.add(this, meta);
|
|
3769
|
+
return this;
|
|
3770
|
+
},
|
|
3771
|
+
refine(check, params) {
|
|
3772
|
+
return this.check(refine(check, params));
|
|
3773
|
+
},
|
|
3774
|
+
superRefine(refinement, params) {
|
|
3775
|
+
return this.check(superRefine(refinement, params));
|
|
3776
|
+
},
|
|
3777
|
+
overwrite(fn) {
|
|
3778
|
+
return this.check(_overwrite(fn));
|
|
3779
|
+
},
|
|
3780
|
+
optional() {
|
|
3781
|
+
return optional(this);
|
|
3782
|
+
},
|
|
3783
|
+
exactOptional() {
|
|
3784
|
+
return exactOptional(this);
|
|
3785
|
+
},
|
|
3786
|
+
nullable() {
|
|
3787
|
+
return nullable(this);
|
|
3788
|
+
},
|
|
3789
|
+
nullish() {
|
|
3790
|
+
return optional(nullable(this));
|
|
3791
|
+
},
|
|
3792
|
+
nonoptional(params) {
|
|
3793
|
+
return nonoptional(this, params);
|
|
3794
|
+
},
|
|
3795
|
+
array() {
|
|
3796
|
+
return array(this);
|
|
3797
|
+
},
|
|
3798
|
+
or(arg) {
|
|
3799
|
+
return union([this, arg]);
|
|
3800
|
+
},
|
|
3801
|
+
and(arg) {
|
|
3802
|
+
return intersection(this, arg);
|
|
3803
|
+
},
|
|
3804
|
+
transform(tx) {
|
|
3805
|
+
return pipe(this, transform(tx));
|
|
3806
|
+
},
|
|
3807
|
+
default(d) {
|
|
3808
|
+
return _default(this, d);
|
|
3809
|
+
},
|
|
3810
|
+
prefault(d) {
|
|
3811
|
+
return prefault(this, d);
|
|
3812
|
+
},
|
|
3813
|
+
catch(params) {
|
|
3814
|
+
return _catch(this, params);
|
|
3815
|
+
},
|
|
3816
|
+
pipe(target) {
|
|
3817
|
+
return pipe(this, target);
|
|
3818
|
+
},
|
|
3819
|
+
readonly() {
|
|
3820
|
+
return readonly(this);
|
|
3821
|
+
},
|
|
3822
|
+
describe(description) {
|
|
3823
|
+
const cl = this.clone();
|
|
3824
|
+
globalRegistry.add(cl, { description });
|
|
3825
|
+
return cl;
|
|
3826
|
+
},
|
|
3827
|
+
meta(...args) {
|
|
3828
|
+
if (args.length === 0) return globalRegistry.get(this);
|
|
3829
|
+
const cl = this.clone();
|
|
3830
|
+
globalRegistry.add(cl, args[0]);
|
|
3831
|
+
return cl;
|
|
3832
|
+
},
|
|
3833
|
+
isOptional() {
|
|
3834
|
+
return this.safeParse(void 0).success;
|
|
3835
|
+
},
|
|
3836
|
+
isNullable() {
|
|
3837
|
+
return this.safeParse(null).success;
|
|
3838
|
+
},
|
|
3839
|
+
apply(fn, ...args) {
|
|
3840
|
+
return args.length === 0 ? fn(this) : fn(this, ...args);
|
|
3841
|
+
},
|
|
3842
|
+
get "~standard"() {
|
|
3843
|
+
return hide(this, "~standard", {
|
|
3844
|
+
...standardProps(this),
|
|
3845
|
+
jsonSchema: {
|
|
3846
|
+
input: createStandardJSONSchemaMethod(this, "input"),
|
|
3847
|
+
output: createStandardJSONSchemaMethod(this, "output")
|
|
3848
|
+
}
|
|
3849
|
+
});
|
|
3850
|
+
},
|
|
3851
|
+
set "~standard"(value) {
|
|
3852
|
+
own(this, "~standard", value);
|
|
3853
|
+
},
|
|
3854
|
+
parse: function _parse(data, params) {
|
|
3855
|
+
return parse(this, data, params, { callee: _parse });
|
|
3856
|
+
},
|
|
3857
|
+
parseAsync: async function _parseAsync(data, params) {
|
|
3858
|
+
return await parseAsync(this, data, params, { callee: _parseAsync });
|
|
3859
|
+
},
|
|
3860
|
+
safeParse(data, params) {
|
|
3861
|
+
return safeParse(this, data, params);
|
|
3862
|
+
},
|
|
3863
|
+
async safeParseAsync(data, params) {
|
|
3864
|
+
return safeParseAsync(this, data, params);
|
|
3865
|
+
},
|
|
3866
|
+
get spa() {
|
|
3867
|
+
return this?.safeParseAsync;
|
|
3868
|
+
},
|
|
3869
|
+
set spa(value) {
|
|
3870
|
+
own(this, "spa", value);
|
|
3871
|
+
},
|
|
3872
|
+
validate(data, params) {
|
|
3873
|
+
return validate(this, data, params);
|
|
3874
|
+
},
|
|
3875
|
+
validateAsync(data, params) {
|
|
3876
|
+
return validateAsync$1(this, data, params);
|
|
3877
|
+
},
|
|
3878
|
+
encode: function _encode(data, params) {
|
|
3879
|
+
return encode(this, data, params, { callee: _encode });
|
|
3880
|
+
},
|
|
3881
|
+
decode: function _decode(data, params) {
|
|
3882
|
+
return decode(this, data, params, { callee: _decode });
|
|
3883
|
+
},
|
|
3884
|
+
encodeAsync: async function _encodeAsync(data, params) {
|
|
3885
|
+
return await encodeAsync(this, data, params, { callee: _encodeAsync });
|
|
3886
|
+
},
|
|
3887
|
+
decodeAsync: async function _decodeAsync(data, params) {
|
|
3888
|
+
return await decodeAsync(this, data, params, { callee: _decodeAsync });
|
|
3889
|
+
},
|
|
3890
|
+
safeEncode(data, params) {
|
|
3891
|
+
return safeEncode(this, data, params);
|
|
3892
|
+
},
|
|
3893
|
+
safeDecode(data, params) {
|
|
3894
|
+
return safeDecode(this, data, params);
|
|
3895
|
+
},
|
|
3896
|
+
async safeEncodeAsync(data, params) {
|
|
3897
|
+
return safeEncodeAsync(this, data, params);
|
|
3898
|
+
},
|
|
3899
|
+
async safeDecodeAsync(data, params) {
|
|
3900
|
+
return safeDecodeAsync(this, data, params);
|
|
3901
|
+
},
|
|
3902
|
+
toJSONSchema(params) {
|
|
3903
|
+
return createToJSONSchemaMethod(this, {})(params);
|
|
3904
|
+
},
|
|
3905
|
+
get description() {
|
|
3906
|
+
return globalRegistry.get(this)?.description;
|
|
3907
|
+
},
|
|
3908
|
+
get _def() {
|
|
3909
|
+
return this._zod.def;
|
|
3910
|
+
}
|
|
3911
|
+
});
|
|
3912
|
+
var _ZodString = $constructor("_ZodString", (inst, def) => {
|
|
3913
|
+
$ZodString.init(inst, def);
|
|
3914
|
+
ZodType.init(inst, def);
|
|
3915
|
+
inst._zod.processJSONSchema = (ctx, json, params) => stringProcessor(inst, ctx, json, params);
|
|
3916
|
+
}, derived({
|
|
3917
|
+
format: (inst) => aggregateChecks(inst).format ?? null,
|
|
3918
|
+
minLength: (inst) => aggregateChecks(inst).minimum ?? null,
|
|
3919
|
+
maxLength: (inst) => aggregateChecks(inst).maximum ?? null
|
|
3920
|
+
}, {
|
|
3921
|
+
regex(...args) {
|
|
3922
|
+
return this.check(_regex(...args));
|
|
3923
|
+
},
|
|
3924
|
+
includes(...args) {
|
|
3925
|
+
return this.check(_includes(...args));
|
|
3926
|
+
},
|
|
3927
|
+
startsWith(...args) {
|
|
3928
|
+
return this.check(_startsWith(...args));
|
|
3929
|
+
},
|
|
3930
|
+
endsWith(...args) {
|
|
3931
|
+
return this.check(_endsWith(...args));
|
|
3932
|
+
},
|
|
3933
|
+
min(...args) {
|
|
3934
|
+
return this.check(_minLength(...args));
|
|
3935
|
+
},
|
|
3936
|
+
max(...args) {
|
|
3937
|
+
return this.check(_maxLength(...args));
|
|
3938
|
+
},
|
|
3939
|
+
length(...args) {
|
|
3940
|
+
return this.check(_length(...args));
|
|
3941
|
+
},
|
|
3942
|
+
nonempty(...args) {
|
|
3943
|
+
return this.check(_minLength(1, ...args));
|
|
3944
|
+
},
|
|
3945
|
+
lowercase(params) {
|
|
3946
|
+
return this.check(_lowercase(params));
|
|
3947
|
+
},
|
|
3948
|
+
uppercase(params) {
|
|
3949
|
+
return this.check(_uppercase(params));
|
|
3950
|
+
},
|
|
3951
|
+
trim() {
|
|
3952
|
+
return this.check(_trim());
|
|
3953
|
+
},
|
|
3954
|
+
normalize(...args) {
|
|
3955
|
+
return this.check(_normalize(...args));
|
|
3956
|
+
},
|
|
3957
|
+
toLowerCase() {
|
|
3958
|
+
return this.check(_toLowerCase());
|
|
3959
|
+
},
|
|
3960
|
+
toUpperCase() {
|
|
3961
|
+
return this.check(_toUpperCase());
|
|
3962
|
+
},
|
|
3963
|
+
slugify() {
|
|
3964
|
+
return this.check(_slugify());
|
|
3965
|
+
}
|
|
3966
|
+
}));
|
|
3967
|
+
var ZodString = $constructor("ZodString", (inst, def) => {
|
|
3968
|
+
$ZodString.init(inst, def);
|
|
3969
|
+
_ZodString.init(inst, def);
|
|
3970
|
+
}, {
|
|
3971
|
+
email(params) {
|
|
3972
|
+
return this.check(_email(ZodEmail, params));
|
|
3973
|
+
},
|
|
3974
|
+
url(params) {
|
|
3975
|
+
return this.check(_url(ZodURL, params));
|
|
3976
|
+
},
|
|
3977
|
+
jwt(params) {
|
|
3978
|
+
return this.check(_jwt(ZodJWT, params));
|
|
3979
|
+
},
|
|
3980
|
+
emoji(params) {
|
|
3981
|
+
return this.check(_emoji(ZodEmoji, params));
|
|
3982
|
+
},
|
|
3983
|
+
guid(params) {
|
|
3984
|
+
return this.check(_guid(ZodGUID, params));
|
|
3985
|
+
},
|
|
3986
|
+
uuid(params) {
|
|
3987
|
+
return this.check(_uuid(ZodUUID, params));
|
|
3988
|
+
},
|
|
3989
|
+
uuidv4(params) {
|
|
3990
|
+
return this.check(_uuidv4(ZodUUID, params));
|
|
3991
|
+
},
|
|
3992
|
+
uuidv6(params) {
|
|
3993
|
+
return this.check(_uuidv6(ZodUUID, params));
|
|
3994
|
+
},
|
|
3995
|
+
uuidv7(params) {
|
|
3996
|
+
return this.check(_uuidv7(ZodUUID, params));
|
|
3997
|
+
},
|
|
3998
|
+
nanoid(params) {
|
|
3999
|
+
return this.check(_nanoid(ZodNanoID, params));
|
|
4000
|
+
},
|
|
4001
|
+
cuid(params) {
|
|
4002
|
+
return this.check(_cuid(ZodCUID, params));
|
|
4003
|
+
},
|
|
4004
|
+
cuid2(params) {
|
|
4005
|
+
return this.check(_cuid2(ZodCUID2, params));
|
|
4006
|
+
},
|
|
4007
|
+
ulid(params) {
|
|
4008
|
+
return this.check(_ulid(ZodULID, params));
|
|
4009
|
+
},
|
|
4010
|
+
base64(params) {
|
|
4011
|
+
return this.check(_base64(ZodBase64, params));
|
|
4012
|
+
},
|
|
4013
|
+
base64url(params) {
|
|
4014
|
+
return this.check(_base64url(ZodBase64URL, params));
|
|
4015
|
+
},
|
|
4016
|
+
xid(params) {
|
|
4017
|
+
return this.check(_xid(ZodXID, params));
|
|
4018
|
+
},
|
|
4019
|
+
ksuid(params) {
|
|
4020
|
+
return this.check(_ksuid(ZodKSUID, params));
|
|
4021
|
+
},
|
|
4022
|
+
ipv4(params) {
|
|
4023
|
+
return this.check(_ipv4(ZodIPv4, params));
|
|
4024
|
+
},
|
|
4025
|
+
ipv6(params) {
|
|
4026
|
+
return this.check(_ipv6(ZodIPv6, params));
|
|
4027
|
+
},
|
|
4028
|
+
cidrv4(params) {
|
|
4029
|
+
return this.check(_cidrv4(ZodCIDRv4, params));
|
|
4030
|
+
},
|
|
4031
|
+
cidrv6(params) {
|
|
4032
|
+
return this.check(_cidrv6(ZodCIDRv6, params));
|
|
4033
|
+
},
|
|
4034
|
+
e164(params) {
|
|
4035
|
+
return this.check(_e164(ZodE164, params));
|
|
4036
|
+
},
|
|
4037
|
+
datetime(params) {
|
|
4038
|
+
return this.check(_isoDateTime(ZodISODateTime, params));
|
|
4039
|
+
},
|
|
4040
|
+
date(params) {
|
|
4041
|
+
return this.check(_isoDate(ZodISODate, params));
|
|
4042
|
+
},
|
|
4043
|
+
time(params) {
|
|
4044
|
+
return this.check(_isoTime(ZodISOTime, params));
|
|
4045
|
+
},
|
|
4046
|
+
duration(params) {
|
|
4047
|
+
return this.check(_isoDuration(ZodISODuration, params));
|
|
4048
|
+
}
|
|
4049
|
+
});
|
|
4050
|
+
function string(params) {
|
|
4051
|
+
return _string(ZodString, params);
|
|
4052
|
+
}
|
|
4053
|
+
var ZodStringFormat = $constructor("ZodStringFormat", (inst, def) => {
|
|
4054
|
+
$ZodStringFormat.init(inst, def);
|
|
4055
|
+
_ZodString.init(inst, def);
|
|
4056
|
+
});
|
|
4057
|
+
var ZodISODateTime = $constructor("ZodISODateTime", (inst, def) => {
|
|
4058
|
+
$ZodISODateTime.init(inst, def);
|
|
4059
|
+
ZodStringFormat.init(inst, def);
|
|
4060
|
+
});
|
|
4061
|
+
var ZodISODate = $constructor("ZodISODate", (inst, def) => {
|
|
4062
|
+
$ZodISODate.init(inst, def);
|
|
4063
|
+
ZodStringFormat.init(inst, def);
|
|
4064
|
+
});
|
|
4065
|
+
var ZodISOTime = $constructor("ZodISOTime", (inst, def) => {
|
|
4066
|
+
$ZodISOTime.init(inst, def);
|
|
4067
|
+
ZodStringFormat.init(inst, def);
|
|
4068
|
+
});
|
|
4069
|
+
var ZodISODuration = $constructor("ZodISODuration", (inst, def) => {
|
|
4070
|
+
$ZodISODuration.init(inst, def);
|
|
4071
|
+
ZodStringFormat.init(inst, def);
|
|
4072
|
+
});
|
|
4073
|
+
var ZodEmail = $constructor("ZodEmail", (inst, def) => {
|
|
4074
|
+
$ZodEmail.init(inst, def);
|
|
4075
|
+
ZodStringFormat.init(inst, def);
|
|
4076
|
+
});
|
|
4077
|
+
var ZodGUID = $constructor("ZodGUID", (inst, def) => {
|
|
4078
|
+
$ZodGUID.init(inst, def);
|
|
4079
|
+
ZodStringFormat.init(inst, def);
|
|
4080
|
+
});
|
|
4081
|
+
var ZodUUID = $constructor("ZodUUID", (inst, def) => {
|
|
4082
|
+
$ZodUUID.init(inst, def);
|
|
4083
|
+
ZodStringFormat.init(inst, def);
|
|
4084
|
+
});
|
|
4085
|
+
var ZodURL = $constructor("ZodURL", (inst, def) => {
|
|
4086
|
+
$ZodURL.init(inst, def);
|
|
4087
|
+
ZodStringFormat.init(inst, def);
|
|
4088
|
+
});
|
|
4089
|
+
var ZodEmoji = $constructor("ZodEmoji", (inst, def) => {
|
|
4090
|
+
$ZodEmoji.init(inst, def);
|
|
4091
|
+
ZodStringFormat.init(inst, def);
|
|
4092
|
+
});
|
|
4093
|
+
var ZodNanoID = $constructor("ZodNanoID", (inst, def) => {
|
|
4094
|
+
$ZodNanoID.init(inst, def);
|
|
4095
|
+
ZodStringFormat.init(inst, def);
|
|
4096
|
+
});
|
|
4097
|
+
var ZodCUID = $constructor("ZodCUID", (inst, def) => {
|
|
4098
|
+
$ZodCUID.init(inst, def);
|
|
4099
|
+
ZodStringFormat.init(inst, def);
|
|
4100
|
+
});
|
|
4101
|
+
var ZodCUID2 = $constructor("ZodCUID2", (inst, def) => {
|
|
4102
|
+
$ZodCUID2.init(inst, def);
|
|
4103
|
+
ZodStringFormat.init(inst, def);
|
|
4104
|
+
});
|
|
4105
|
+
var ZodULID = $constructor("ZodULID", (inst, def) => {
|
|
4106
|
+
$ZodULID.init(inst, def);
|
|
4107
|
+
ZodStringFormat.init(inst, def);
|
|
4108
|
+
});
|
|
4109
|
+
var ZodXID = $constructor("ZodXID", (inst, def) => {
|
|
4110
|
+
$ZodXID.init(inst, def);
|
|
4111
|
+
ZodStringFormat.init(inst, def);
|
|
4112
|
+
});
|
|
4113
|
+
var ZodKSUID = $constructor("ZodKSUID", (inst, def) => {
|
|
4114
|
+
$ZodKSUID.init(inst, def);
|
|
4115
|
+
ZodStringFormat.init(inst, def);
|
|
4116
|
+
});
|
|
4117
|
+
var ZodIPv4 = $constructor("ZodIPv4", (inst, def) => {
|
|
4118
|
+
$ZodIPv4.init(inst, def);
|
|
4119
|
+
ZodStringFormat.init(inst, def);
|
|
4120
|
+
});
|
|
4121
|
+
var ZodIPv6 = $constructor("ZodIPv6", (inst, def) => {
|
|
4122
|
+
$ZodIPv6.init(inst, def);
|
|
4123
|
+
ZodStringFormat.init(inst, def);
|
|
4124
|
+
});
|
|
4125
|
+
var ZodCIDRv4 = $constructor("ZodCIDRv4", (inst, def) => {
|
|
4126
|
+
$ZodCIDRv4.init(inst, def);
|
|
4127
|
+
ZodStringFormat.init(inst, def);
|
|
4128
|
+
});
|
|
4129
|
+
var ZodCIDRv6 = $constructor("ZodCIDRv6", (inst, def) => {
|
|
4130
|
+
$ZodCIDRv6.init(inst, def);
|
|
4131
|
+
ZodStringFormat.init(inst, def);
|
|
4132
|
+
});
|
|
4133
|
+
var ZodBase64 = $constructor("ZodBase64", (inst, def) => {
|
|
4134
|
+
$ZodBase64.init(inst, def);
|
|
4135
|
+
ZodStringFormat.init(inst, def);
|
|
4136
|
+
});
|
|
4137
|
+
var ZodBase64URL = $constructor("ZodBase64URL", (inst, def) => {
|
|
4138
|
+
$ZodBase64URL.init(inst, def);
|
|
4139
|
+
ZodStringFormat.init(inst, def);
|
|
4140
|
+
});
|
|
4141
|
+
var ZodE164 = $constructor("ZodE164", (inst, def) => {
|
|
4142
|
+
$ZodE164.init(inst, def);
|
|
4143
|
+
ZodStringFormat.init(inst, def);
|
|
4144
|
+
});
|
|
4145
|
+
var ZodJWT = $constructor("ZodJWT", (inst, def) => {
|
|
4146
|
+
$ZodJWT.init(inst, def);
|
|
4147
|
+
ZodStringFormat.init(inst, def);
|
|
4148
|
+
});
|
|
4149
|
+
var ZodBoolean = $constructor("ZodBoolean", (inst, def) => {
|
|
4150
|
+
$ZodBoolean.init(inst, def);
|
|
4151
|
+
ZodType.init(inst, def);
|
|
4152
|
+
inst._zod.processJSONSchema = (ctx, json, params) => booleanProcessor(inst, ctx, json, params);
|
|
4153
|
+
});
|
|
4154
|
+
function boolean(params) {
|
|
4155
|
+
return _boolean(ZodBoolean, params);
|
|
4156
|
+
}
|
|
4157
|
+
var ZodArray = $constructor("ZodArray", (inst, def) => {
|
|
4158
|
+
_ensureDefaultMemoizer();
|
|
4159
|
+
$ZodArray.init(inst, def);
|
|
4160
|
+
ZodType.init(inst, def);
|
|
4161
|
+
inst._zod.processJSONSchema = (ctx, json, params) => arrayProcessor(inst, ctx, json, params);
|
|
4162
|
+
inst.element = def.element;
|
|
4163
|
+
}, {
|
|
4164
|
+
min(n, params) {
|
|
4165
|
+
return this.check(_minLength(n, params));
|
|
4166
|
+
},
|
|
4167
|
+
nonempty(params) {
|
|
4168
|
+
return this.check(_minLength(1, params));
|
|
4169
|
+
},
|
|
4170
|
+
max(n, params) {
|
|
4171
|
+
return this.check(_maxLength(n, params));
|
|
4172
|
+
},
|
|
4173
|
+
length(n, params) {
|
|
4174
|
+
return this.check(_length(n, params));
|
|
4175
|
+
},
|
|
4176
|
+
unwrap() {
|
|
4177
|
+
return this.element;
|
|
4178
|
+
}
|
|
4179
|
+
});
|
|
4180
|
+
function array(element, params) {
|
|
4181
|
+
return _array(ZodArray, element, params);
|
|
4182
|
+
}
|
|
4183
|
+
var ZodUnion = $constructor("ZodUnion", (inst, def) => {
|
|
4184
|
+
$ZodUnion.init(inst, def);
|
|
4185
|
+
ZodType.init(inst, def);
|
|
4186
|
+
inst._zod.processJSONSchema = (ctx, json, params) => unionProcessor(inst, ctx, json, params);
|
|
4187
|
+
inst.options = def.options;
|
|
4188
|
+
});
|
|
4189
|
+
function union(options, params) {
|
|
4190
|
+
return new ZodUnion({
|
|
4191
|
+
type: "union",
|
|
4192
|
+
options,
|
|
4193
|
+
...normalizeParams(params)
|
|
4194
|
+
});
|
|
4195
|
+
}
|
|
4196
|
+
var ZodIntersection = $constructor("ZodIntersection", (inst, def) => {
|
|
4197
|
+
$ZodIntersection.init(inst, def);
|
|
4198
|
+
ZodType.init(inst, def);
|
|
4199
|
+
inst._zod.processJSONSchema = (ctx, json, params) => intersectionProcessor(inst, ctx, json, params);
|
|
4200
|
+
});
|
|
4201
|
+
function intersection(left, right) {
|
|
4202
|
+
return new ZodIntersection({
|
|
4203
|
+
type: "intersection",
|
|
4204
|
+
left,
|
|
4205
|
+
right
|
|
4206
|
+
});
|
|
4207
|
+
}
|
|
4208
|
+
var ZodTransform = $constructor("ZodTransform", (inst, def) => {
|
|
4209
|
+
_ensureDefaultMemoizer();
|
|
4210
|
+
$ZodTransform.init(inst, def);
|
|
4211
|
+
ZodType.init(inst, def);
|
|
4212
|
+
inst._zod.processJSONSchema = (ctx, json, params) => transformProcessor(inst, ctx, json, params);
|
|
4213
|
+
inst._zod.parse = (payload, _ctx) => {
|
|
4214
|
+
if (_ctx.direction === "backward") throw new $ZodEncodeError(inst.constructor.name);
|
|
4215
|
+
payload.addIssue = (issue$1) => {
|
|
4216
|
+
if (typeof issue$1 === "string") payload.issues.push(issue(issue$1, payload.value, def));
|
|
4217
|
+
else {
|
|
4218
|
+
const _issue = issue$1;
|
|
4219
|
+
if (_issue.fatal) _issue.continue = false;
|
|
4220
|
+
_issue.code ?? (_issue.code = "custom");
|
|
4221
|
+
if (!("input" in _issue)) _issue.input = payload.value;
|
|
4222
|
+
_issue.inst ?? (_issue.inst = inst);
|
|
4223
|
+
payload.issues.push(issue(_issue));
|
|
4224
|
+
}
|
|
4225
|
+
};
|
|
4226
|
+
const output = def.transform(payload.value, payload);
|
|
4227
|
+
if (output instanceof Promise) return output.then((output) => {
|
|
4228
|
+
payload.value = output;
|
|
4229
|
+
return payload;
|
|
4230
|
+
});
|
|
4231
|
+
payload.value = output;
|
|
4232
|
+
return payload;
|
|
4233
|
+
};
|
|
4234
|
+
});
|
|
4235
|
+
function transform(fn) {
|
|
4236
|
+
return new ZodTransform({
|
|
4237
|
+
type: "transform",
|
|
4238
|
+
transform: fn
|
|
4239
|
+
});
|
|
4240
|
+
}
|
|
4241
|
+
var ZodOptional = $constructor("ZodOptional", (inst, def) => {
|
|
4242
|
+
$ZodOptional.init(inst, def);
|
|
4243
|
+
ZodType.init(inst, def);
|
|
4244
|
+
inst._zod.processJSONSchema = (ctx, json, params) => optionalProcessor(inst, ctx, json, params);
|
|
4245
|
+
inst.unwrap = () => inst._zod.def.innerType;
|
|
4246
|
+
});
|
|
4247
|
+
function optional(innerType) {
|
|
4248
|
+
return new ZodOptional({
|
|
4249
|
+
type: "optional",
|
|
4250
|
+
innerType
|
|
4251
|
+
});
|
|
4252
|
+
}
|
|
4253
|
+
var ZodExactOptional = $constructor("ZodExactOptional", (inst, def) => {
|
|
4254
|
+
$ZodExactOptional.init(inst, def);
|
|
4255
|
+
ZodType.init(inst, def);
|
|
4256
|
+
inst._zod.processJSONSchema = (ctx, json, params) => optionalProcessor(inst, ctx, json, params);
|
|
4257
|
+
inst.unwrap = () => inst._zod.def.innerType;
|
|
4258
|
+
});
|
|
4259
|
+
function exactOptional(innerType) {
|
|
4260
|
+
return new ZodExactOptional({
|
|
4261
|
+
type: "optional",
|
|
4262
|
+
innerType
|
|
4263
|
+
});
|
|
4264
|
+
}
|
|
4265
|
+
var ZodNullable = $constructor("ZodNullable", (inst, def) => {
|
|
4266
|
+
$ZodNullable.init(inst, def);
|
|
4267
|
+
ZodType.init(inst, def);
|
|
4268
|
+
inst._zod.processJSONSchema = (ctx, json, params) => nullableProcessor(inst, ctx, json, params);
|
|
4269
|
+
inst.unwrap = () => inst._zod.def.innerType;
|
|
4270
|
+
});
|
|
4271
|
+
function nullable(innerType) {
|
|
4272
|
+
return new ZodNullable({
|
|
4273
|
+
type: "nullable",
|
|
4274
|
+
innerType
|
|
4275
|
+
});
|
|
4276
|
+
}
|
|
4277
|
+
var ZodDefault = $constructor("ZodDefault", (inst, def) => {
|
|
4278
|
+
$ZodDefault.init(inst, def);
|
|
4279
|
+
ZodType.init(inst, def);
|
|
4280
|
+
inst._zod.processJSONSchema = (ctx, json, params) => defaultProcessor(inst, ctx, json, params);
|
|
4281
|
+
inst.unwrap = () => inst._zod.def.innerType;
|
|
4282
|
+
inst.removeDefault = inst.unwrap;
|
|
4283
|
+
});
|
|
4284
|
+
function _default(innerType, defaultValue) {
|
|
4285
|
+
return new ZodDefault({
|
|
4286
|
+
type: "default",
|
|
4287
|
+
innerType,
|
|
4288
|
+
get defaultValue() {
|
|
4289
|
+
return typeof defaultValue === "function" ? defaultValue() : shallowClone(defaultValue);
|
|
4290
|
+
}
|
|
4291
|
+
});
|
|
4292
|
+
}
|
|
4293
|
+
var ZodPrefault = $constructor("ZodPrefault", (inst, def) => {
|
|
4294
|
+
$ZodPrefault.init(inst, def);
|
|
4295
|
+
ZodType.init(inst, def);
|
|
4296
|
+
inst._zod.processJSONSchema = (ctx, json, params) => prefaultProcessor(inst, ctx, json, params);
|
|
4297
|
+
inst.unwrap = () => inst._zod.def.innerType;
|
|
4298
|
+
});
|
|
4299
|
+
function prefault(innerType, defaultValue) {
|
|
4300
|
+
return new ZodPrefault({
|
|
4301
|
+
type: "prefault",
|
|
4302
|
+
innerType,
|
|
4303
|
+
get defaultValue() {
|
|
4304
|
+
return typeof defaultValue === "function" ? defaultValue() : shallowClone(defaultValue);
|
|
4305
|
+
}
|
|
4306
|
+
});
|
|
4307
|
+
}
|
|
4308
|
+
var ZodNonOptional = $constructor("ZodNonOptional", (inst, def) => {
|
|
4309
|
+
$ZodNonOptional.init(inst, def);
|
|
4310
|
+
ZodType.init(inst, def);
|
|
4311
|
+
inst._zod.processJSONSchema = (ctx, json, params) => nonoptionalProcessor(inst, ctx, json, params);
|
|
4312
|
+
inst.unwrap = () => inst._zod.def.innerType;
|
|
4313
|
+
});
|
|
4314
|
+
function nonoptional(innerType, params) {
|
|
4315
|
+
return new ZodNonOptional({
|
|
4316
|
+
type: "nonoptional",
|
|
4317
|
+
innerType,
|
|
4318
|
+
...normalizeParams(params)
|
|
4319
|
+
});
|
|
4320
|
+
}
|
|
4321
|
+
var ZodCatch = $constructor("ZodCatch", (inst, def) => {
|
|
4322
|
+
$ZodCatch.init(inst, def);
|
|
4323
|
+
ZodType.init(inst, def);
|
|
4324
|
+
inst._zod.processJSONSchema = (ctx, json, params) => catchProcessor(inst, ctx, json, params);
|
|
4325
|
+
inst.unwrap = () => inst._zod.def.innerType;
|
|
4326
|
+
inst.removeCatch = inst.unwrap;
|
|
4327
|
+
});
|
|
4328
|
+
function _catch(innerType, catchValue) {
|
|
4329
|
+
return new ZodCatch({
|
|
4330
|
+
type: "catch",
|
|
4331
|
+
innerType,
|
|
4332
|
+
catchValue: typeof catchValue === "function" ? catchValue : constantCatch(catchValue)
|
|
4333
|
+
});
|
|
4334
|
+
}
|
|
4335
|
+
var ZodPipe = $constructor("ZodPipe", (inst, def) => {
|
|
4336
|
+
$ZodPipe.init(inst, def);
|
|
4337
|
+
ZodType.init(inst, def);
|
|
4338
|
+
inst._zod.processJSONSchema = (ctx, json, params) => pipeProcessor(inst, ctx, json, params);
|
|
4339
|
+
inst.in = def.in;
|
|
4340
|
+
inst.out = def.out;
|
|
4341
|
+
});
|
|
4342
|
+
function pipe(in_, out) {
|
|
4343
|
+
return new ZodPipe({
|
|
4344
|
+
type: "pipe",
|
|
4345
|
+
in: in_,
|
|
4346
|
+
out
|
|
4347
|
+
});
|
|
4348
|
+
}
|
|
4349
|
+
var ZodReadonly = $constructor("ZodReadonly", (inst, def) => {
|
|
4350
|
+
$ZodReadonly.init(inst, def);
|
|
4351
|
+
ZodType.init(inst, def);
|
|
4352
|
+
inst._zod.processJSONSchema = (ctx, json, params) => readonlyProcessor(inst, ctx, json, params);
|
|
4353
|
+
inst.unwrap = () => inst._zod.def.innerType;
|
|
4354
|
+
});
|
|
4355
|
+
function readonly(innerType) {
|
|
4356
|
+
return new ZodReadonly({
|
|
4357
|
+
type: "readonly",
|
|
4358
|
+
innerType
|
|
4359
|
+
});
|
|
4360
|
+
}
|
|
4361
|
+
var ZodCustom = $constructor("ZodCustom", (inst, def) => {
|
|
4362
|
+
$ZodCustom.init(inst, def);
|
|
4363
|
+
ZodType.init(inst, def);
|
|
4364
|
+
inst._zod.processJSONSchema = (ctx, json, params) => customProcessor(inst, ctx, json, params);
|
|
4365
|
+
});
|
|
4366
|
+
function refine(fn, _params = {}) {
|
|
4367
|
+
return _refine(ZodCustom, fn, _params);
|
|
4368
|
+
}
|
|
4369
|
+
function superRefine(fn, params) {
|
|
4370
|
+
return _superRefine(fn, params);
|
|
4371
|
+
}
|
|
4372
|
+
var messageOf = (error) => error instanceof Error ? error.message : String(error);
|
|
4373
|
+
var TOOL_NAMES = [
|
|
4374
|
+
"figma_metadata",
|
|
4375
|
+
"figma_design_context",
|
|
4376
|
+
"figma_screenshot",
|
|
4377
|
+
"figma_variables"
|
|
4378
|
+
];
|
|
4379
|
+
var NO_PROJECT = "open a project in Daydream first: a screenshot is written into its .daydream/figma/ folder";
|
|
4380
|
+
var PROJECT_CHANGED = "another project was opened while Figma answered; nothing was written";
|
|
4381
|
+
var CLIENT = Symbol.for("daydream.figma-devmode.client");
|
|
4382
|
+
function processClient(holder = globalThis, connect = () => connectFigma()) {
|
|
4383
|
+
return holder[CLIENT] ??= connect();
|
|
4384
|
+
}
|
|
4385
|
+
function screenshotFileName(node) {
|
|
4386
|
+
const id = node === void 0 || node === "" ? null : nodeIdFromUrl(node) ?? node;
|
|
4387
|
+
const stem = id === null ? "selection" : id.replace(/[^A-Za-z0-9_-]+/g, "-").slice(0, 64);
|
|
4388
|
+
return `${stem === "" ? "node" : stem}.png`;
|
|
4389
|
+
}
|
|
4390
|
+
var DOT_DAYDREAM_GITIGNORE = "plugin-data/\nattachments/\npublished*/\n";
|
|
4391
|
+
var FIGMA_GITIGNORE = "*\n";
|
|
4392
|
+
function refuseUnlessFileName(name) {
|
|
4393
|
+
if (name === "" || name === "." || name === ".." || /[/\\\0]/.test(name)) throw new Error(`${JSON.stringify(name)} is not a file name: a screenshot is written under .daydream/figma/ only`);
|
|
4394
|
+
}
|
|
4395
|
+
async function refuseLink(file) {
|
|
4396
|
+
try {
|
|
4397
|
+
if ((await lstat(file)).isSymbolicLink()) throw new Error(`${file} is a link, which is never written through`);
|
|
4398
|
+
} catch (error) {
|
|
4399
|
+
if (error.code === "ENOENT") return;
|
|
4400
|
+
throw error;
|
|
4401
|
+
}
|
|
4402
|
+
}
|
|
4403
|
+
async function makeFolder(dir) {
|
|
4404
|
+
try {
|
|
4405
|
+
await mkdir(dir);
|
|
4406
|
+
return true;
|
|
4407
|
+
} catch (error) {
|
|
4408
|
+
if (error.code === "EEXIST") return false;
|
|
4409
|
+
throw error;
|
|
4410
|
+
}
|
|
4411
|
+
}
|
|
4412
|
+
async function writeOnce(file, text) {
|
|
4413
|
+
try {
|
|
4414
|
+
await writeFile(file, text, { flag: "wx" });
|
|
4415
|
+
} catch (error) {
|
|
4416
|
+
if (error.code !== "EEXIST") throw error;
|
|
4417
|
+
}
|
|
4418
|
+
}
|
|
4419
|
+
async function saveImage(root, name, bytes) {
|
|
4420
|
+
refuseUnlessFileName(name);
|
|
4421
|
+
const scratch = path.join(root, ".daydream");
|
|
4422
|
+
const dir = path.join(scratch, "figma");
|
|
4423
|
+
const file = path.join(dir, name);
|
|
4424
|
+
await refuseLink(scratch);
|
|
4425
|
+
await refuseLink(dir);
|
|
4426
|
+
if (await makeFolder(scratch)) await writeOnce(path.join(scratch, ".gitignore"), DOT_DAYDREAM_GITIGNORE);
|
|
4427
|
+
await makeFolder(dir);
|
|
4428
|
+
await writeOnce(path.join(dir, ".gitignore"), FIGMA_GITIGNORE);
|
|
4429
|
+
await refuseLink(file);
|
|
4430
|
+
await writeFile(file, bytes);
|
|
4431
|
+
return file;
|
|
4432
|
+
}
|
|
4433
|
+
var askedId = (node) => node === void 0 || node.trim() === "" ? null : nodeIdFromUrl(node);
|
|
4434
|
+
var OLDER_CONNECTION = "Daydream's host loaded an older Figma connection than this plugin; quit and reopen Daydream, then try again.";
|
|
4435
|
+
var NEEDED = [
|
|
4436
|
+
"reachable",
|
|
4437
|
+
"frame",
|
|
4438
|
+
"selection",
|
|
4439
|
+
"screenshot",
|
|
4440
|
+
"variableDefs",
|
|
4441
|
+
"metadata",
|
|
4442
|
+
"designContext"
|
|
4443
|
+
];
|
|
4444
|
+
function olderConnection(client) {
|
|
4445
|
+
return NEEDED.some((name) => typeof client?.[name] !== "function");
|
|
4446
|
+
}
|
|
4447
|
+
function createFigmaPart(given = {}) {
|
|
4448
|
+
const client = given.client ?? processClient(given.holder, given.connect);
|
|
4449
|
+
const save = given.saveImage ?? saveImage;
|
|
4450
|
+
if (olderConnection(client)) {
|
|
4451
|
+
const older = async () => {
|
|
4452
|
+
throw new Error(OLDER_CONNECTION);
|
|
4453
|
+
};
|
|
4454
|
+
return {
|
|
4455
|
+
reachable: () => typeof client.reachable === "function" ? client.reachable() : Promise.resolve(false),
|
|
4456
|
+
screenshot: older,
|
|
4457
|
+
frame: older,
|
|
4458
|
+
selection: older,
|
|
4459
|
+
metadata: older,
|
|
4460
|
+
designContext: older,
|
|
4461
|
+
variables: older,
|
|
4462
|
+
saveScreenshot: older
|
|
4463
|
+
};
|
|
4464
|
+
}
|
|
4465
|
+
const frame = async (node) => {
|
|
4466
|
+
const read = await client.frame(node);
|
|
4467
|
+
return read.kind === "frame" ? read.frame : {
|
|
4468
|
+
id: askedId(node),
|
|
4469
|
+
name: null,
|
|
4470
|
+
width: null,
|
|
4471
|
+
height: null
|
|
4472
|
+
};
|
|
4473
|
+
};
|
|
4474
|
+
return {
|
|
4475
|
+
reachable: () => client.reachable(),
|
|
4476
|
+
async screenshot(node) {
|
|
4477
|
+
const [image, named] = await Promise.all([client.screenshot(node), frame(node)]);
|
|
4478
|
+
return {
|
|
4479
|
+
...image,
|
|
4480
|
+
name: named.name,
|
|
4481
|
+
nodeId: named.id
|
|
4482
|
+
};
|
|
4483
|
+
},
|
|
4484
|
+
frame,
|
|
4485
|
+
selection: () => client.selection(),
|
|
4486
|
+
metadata: (node) => client.metadata(node),
|
|
4487
|
+
designContext: (node, options = {}) => client.designContext(node, { excludeScreenshot: options.screenshot !== true }),
|
|
4488
|
+
variables: (node) => client.variableDefs(node),
|
|
4489
|
+
saveScreenshot: (root, node, image) => save(root, screenshotFileName(node), Buffer.from(image.base64, "base64"))
|
|
4490
|
+
};
|
|
4491
|
+
}
|
|
4492
|
+
var projectRootOf = (dataFile) => dataFile === null ? null : path.resolve(dataFile, "..", "..", "..");
|
|
4493
|
+
var NODE_ARG = { node: string().optional().describe("A Figma node id (1:7) or a Figma link with a node-id; none for what is selected in Figma") };
|
|
4494
|
+
var labelOf = (image) => image.name !== null ? `${image.name}${image.nodeId === null ? "" : ` (${image.nodeId})`}` : image.nodeId === null ? "Figma's selection" : `Figma node ${image.nodeId}`;
|
|
4495
|
+
var withNotes = (result) => result.notes.length === 0 ? result.text : `${result.text}\n\n${result.notes.join("\n\n")}`;
|
|
4496
|
+
function registerFigmaPart(host, part) {
|
|
4497
|
+
host.browserPart.handle("reachable", () => part.reachable());
|
|
4498
|
+
host.browserPart.handle("figma-screenshot", async (input) => part.screenshot(nodeInput(input)));
|
|
4499
|
+
host.browserPart.handle("figma-frame", async (input) => part.frame(nodeInput(input)));
|
|
4500
|
+
host.browserPart.handle("figma-selection", () => part.selection());
|
|
4501
|
+
const answering = (run) => async () => {
|
|
4502
|
+
try {
|
|
4503
|
+
return await run();
|
|
4504
|
+
} catch (error) {
|
|
4505
|
+
return {
|
|
4506
|
+
text: messageOf(error),
|
|
4507
|
+
isError: true
|
|
4508
|
+
};
|
|
4509
|
+
}
|
|
4510
|
+
};
|
|
4511
|
+
const openRoot = () => {
|
|
4512
|
+
const root = projectRootOf(host.plugin.dataFile);
|
|
4513
|
+
if (root === null) throw new Error(NO_PROJECT);
|
|
4514
|
+
return root;
|
|
4515
|
+
};
|
|
4516
|
+
const saved = async (root, node, image) => {
|
|
4517
|
+
if (projectRootOf(host.plugin.dataFile) !== root) throw new Error(PROJECT_CHANGED);
|
|
4518
|
+
const file = await part.saveScreenshot(root, node, image);
|
|
4519
|
+
const bytes = Buffer.byteLength(image.base64, "base64");
|
|
4520
|
+
return {
|
|
4521
|
+
file,
|
|
4522
|
+
sentence: `The screenshot (${image.mimeType}, ${bytes} bytes) is written at ${file}; read it with your file tools.`
|
|
4523
|
+
};
|
|
4524
|
+
};
|
|
4525
|
+
const [METADATA, DESIGN_CONTEXT, SCREENSHOT, VARIABLES] = TOOL_NAMES;
|
|
4526
|
+
host.registerTool({
|
|
4527
|
+
name: METADATA,
|
|
4528
|
+
title: "Figma metadata",
|
|
4529
|
+
description: "The tree of a node in the file open in Figma desktop, as Figma's Dev Mode server answers get_metadata: XML of ids, layer types, names, positions and sizes, with the server's notes after it. With no node, Figma's current selection — or, with nothing selected, the document's pages. Figma desktop must be open with its Dev Mode MCP server on.",
|
|
4530
|
+
inputSchema: NODE_ARG,
|
|
4531
|
+
annotations: { readOnlyHint: true },
|
|
4532
|
+
run: ({ node }) => answering(async () => {
|
|
4533
|
+
const result = await part.metadata(node);
|
|
4534
|
+
return {
|
|
4535
|
+
text: withNotes(result),
|
|
4536
|
+
structured: {
|
|
4537
|
+
text: result.text,
|
|
4538
|
+
notes: result.notes
|
|
4539
|
+
}
|
|
4540
|
+
};
|
|
4541
|
+
})()
|
|
4542
|
+
});
|
|
4543
|
+
host.registerTool({
|
|
4544
|
+
name: DESIGN_CONTEXT,
|
|
4545
|
+
title: "Figma design context",
|
|
4546
|
+
description: "A node of the file open in Figma desktop as code, as Figma's Dev Mode server answers get_design_context: React and Tailwind with data-node-id attributes — Figma's rendering of the design, to transcribe into the project's own HTML and CSS, never to ship as it is — with the server's notes; the images it names are served at http://localhost:3845/assets/ only while Figma runs. With screenshot true, the node's PNG is written into the open project's .daydream/figma/ and its path answered. With no node, Figma's current selection.",
|
|
4547
|
+
inputSchema: {
|
|
4548
|
+
...NODE_ARG,
|
|
4549
|
+
screenshot: boolean().optional().describe("Also write the node's screenshot into the project and answer its path")
|
|
4550
|
+
},
|
|
4551
|
+
annotations: { readOnlyHint: false },
|
|
4552
|
+
run: ({ node, screenshot }) => answering(async () => {
|
|
4553
|
+
const root = screenshot === true ? openRoot() : null;
|
|
4554
|
+
const result = await part.designContext(node, { screenshot: screenshot === true });
|
|
4555
|
+
const structured = {
|
|
4556
|
+
text: result.text,
|
|
4557
|
+
notes: result.notes
|
|
4558
|
+
};
|
|
4559
|
+
let text = withNotes(result);
|
|
4560
|
+
if (root !== null && result.screenshot !== null) {
|
|
4561
|
+
const written = await saved(root, node, result.screenshot);
|
|
4562
|
+
structured["screenshot"] = written.file;
|
|
4563
|
+
text = `${text}\n\n${written.sentence}`;
|
|
4564
|
+
}
|
|
4565
|
+
return {
|
|
4566
|
+
text,
|
|
4567
|
+
structured
|
|
4568
|
+
};
|
|
4569
|
+
})()
|
|
4570
|
+
});
|
|
4571
|
+
host.registerTool({
|
|
4572
|
+
name: SCREENSHOT,
|
|
4573
|
+
title: "Figma screenshot",
|
|
4574
|
+
description: "A node of the file open in Figma desktop rendered as a PNG, as Figma's Dev Mode server answers get_screenshot, written into the open project's .daydream/figma/<node>.png; the answer is that path, to read with your file tools. With no node, Figma's current selection.",
|
|
4575
|
+
inputSchema: NODE_ARG,
|
|
4576
|
+
annotations: { readOnlyHint: false },
|
|
4577
|
+
run: ({ node }) => answering(async () => {
|
|
4578
|
+
const root = openRoot();
|
|
4579
|
+
const image = await part.screenshot(node);
|
|
4580
|
+
const written = await saved(root, node, image);
|
|
4581
|
+
return {
|
|
4582
|
+
text: `${labelOf(image)}: ${written.sentence}`,
|
|
4583
|
+
structured: {
|
|
4584
|
+
path: written.file,
|
|
4585
|
+
mimeType: image.mimeType,
|
|
4586
|
+
name: image.name,
|
|
4587
|
+
nodeId: image.nodeId
|
|
4588
|
+
}
|
|
4589
|
+
};
|
|
4590
|
+
})()
|
|
4591
|
+
});
|
|
4592
|
+
host.registerTool({
|
|
4593
|
+
name: VARIABLES,
|
|
4594
|
+
title: "Figma variables",
|
|
4595
|
+
description: "The variables and styles a node of the file open in Figma desktop uses, as Figma's Dev Mode server answers get_variable_defs (names to values). With no node, Figma's current selection.",
|
|
4596
|
+
inputSchema: NODE_ARG,
|
|
4597
|
+
annotations: { readOnlyHint: true },
|
|
4598
|
+
run: ({ node }) => answering(async () => {
|
|
4599
|
+
const result = await part.variables(node);
|
|
4600
|
+
return {
|
|
4601
|
+
text: withNotes(result),
|
|
4602
|
+
structured: {
|
|
4603
|
+
text: result.text,
|
|
4604
|
+
notes: result.notes
|
|
4605
|
+
}
|
|
4606
|
+
};
|
|
4607
|
+
})()
|
|
4608
|
+
});
|
|
4609
|
+
}
|
|
4610
|
+
function activate(host) {
|
|
4611
|
+
registerFigmaPart(host, createFigmaPart());
|
|
4612
|
+
}
|
|
4613
|
+
export { NO_PROJECT, OLDER_CONNECTION, PROJECT_CHANGED, TOOL_NAMES, createFigmaPart, activate as default, olderConnection, processClient, projectRootOf, registerFigmaPart, saveImage, screenshotFileName };
|