@statelyai/agent 2.0.0-next.3 → 2.0.0-next.5
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/.changeset/calm-beans-talk.md +5 -0
- package/.changeset/long-guests-explode.md +5 -0
- package/.changeset/nice-pants-rule.md +10 -0
- package/.changeset/odd-kiwis-compare.md +5 -0
- package/.changeset/pre.json +4 -0
- package/CHANGELOG.md +23 -0
- package/architecture.tldr +797 -0
- package/dist/index.d.mts +198 -140
- package/dist/index.d.ts +198 -140
- package/dist/index.js +4397 -148
- package/dist/index.mjs +4397 -149
- package/examples/chatbot.ts +9 -5
- package/examples/cot.ts +2 -4
- package/examples/jugs.ts +2 -2
- package/examples/learn-from-feedback.ts +7 -7
- package/examples/newspaper.ts +1 -1
- package/examples/rewoo.ts +62 -0
- package/examples/river-crossing.ts +2 -2
- package/examples/serverless.ts +71 -0
- package/examples/simple.ts +1 -1
- package/examples/ticTacToe.ts +6 -2
- package/examples/wiki.ts +2 -2
- package/package.json +14 -12
- package/readme.md +57 -0
- package/src/agent.test.ts +387 -30
- package/src/agent.ts +177 -64
- package/src/decide.test.ts +24 -2
- package/src/decide.ts +34 -78
- package/src/index.ts +1 -0
- package/src/{strategies/chainOfThought.ts → policies/chainOfThoughtPolicy.ts} +7 -9
- package/src/policies/index.ts +3 -0
- package/src/{strategies/shortestPath.test.ts → policies/shortestPathPolicy.test.ts} +2 -2
- package/src/{strategies/shortestPath.ts → policies/shortestPathPolicy.ts} +8 -8
- package/src/{strategies/simple.ts → policies/toolPolicy.ts} +27 -26
- package/src/text.ts +17 -22
- package/src/types.ts +162 -166
- package/src/agent-experimental.ts +0 -221
package/dist/index.js
CHANGED
|
@@ -28,19 +28,22 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
29
|
|
|
30
30
|
// src/index.ts
|
|
31
|
-
var
|
|
32
|
-
__export(
|
|
31
|
+
var index_exports = {};
|
|
32
|
+
__export(index_exports, {
|
|
33
|
+
chainOfThoughtPolicy: () => chainOfThoughtPolicy,
|
|
33
34
|
createAgent: () => createAgent,
|
|
35
|
+
experimental_shortestPathPolicy: () => experimental_shortestPathPolicy,
|
|
34
36
|
fromDecision: () => fromDecision,
|
|
35
37
|
fromText: () => fromText,
|
|
36
|
-
fromTextStream: () => fromTextStream
|
|
38
|
+
fromTextStream: () => fromTextStream,
|
|
39
|
+
toolPolicy: () => toolPolicy
|
|
37
40
|
});
|
|
38
|
-
module.exports = __toCommonJS(
|
|
41
|
+
module.exports = __toCommonJS(index_exports);
|
|
39
42
|
|
|
40
43
|
// src/agent.ts
|
|
41
44
|
var import_xstate4 = require("xstate");
|
|
42
45
|
|
|
43
|
-
// src/
|
|
46
|
+
// src/policies/toolPolicy.ts
|
|
44
47
|
var import_ai3 = require("ai");
|
|
45
48
|
|
|
46
49
|
// src/utils.ts
|
|
@@ -49,10 +52,10 @@ function getAllTransitions(state) {
|
|
|
49
52
|
const nodes = state._nodes;
|
|
50
53
|
const transitions = nodes.map((node) => [...node.transitions.values()]).map((nodeTransitions) => {
|
|
51
54
|
return nodeTransitions.map((nodeEventTransitions) => {
|
|
52
|
-
return nodeEventTransitions.map((
|
|
55
|
+
return nodeEventTransitions.map((transition2) => {
|
|
53
56
|
return {
|
|
54
|
-
...
|
|
55
|
-
guard: typeof
|
|
57
|
+
...transition2,
|
|
58
|
+
guard: typeof transition2.guard === "string" ? { type: transition2.guard } : transition2.guard
|
|
56
59
|
// TODO: fix
|
|
57
60
|
};
|
|
58
61
|
});
|
|
@@ -60,22 +63,6 @@ function getAllTransitions(state) {
|
|
|
60
63
|
}).flat(2);
|
|
61
64
|
return transitions;
|
|
62
65
|
}
|
|
63
|
-
function getAllMachineTransitions(stateNode) {
|
|
64
|
-
const transitions = [...stateNode.transitions.values()].map((nodeTransitions) => {
|
|
65
|
-
return nodeTransitions.map((transition) => {
|
|
66
|
-
return {
|
|
67
|
-
...transition,
|
|
68
|
-
guard: typeof transition.guard === "string" ? { type: transition.guard } : transition.guard
|
|
69
|
-
// TODO: fix
|
|
70
|
-
};
|
|
71
|
-
});
|
|
72
|
-
}).flat(2);
|
|
73
|
-
for (const s of Object.values(stateNode.states)) {
|
|
74
|
-
const stateTransitions = getAllMachineTransitions(s);
|
|
75
|
-
transitions.push(...stateTransitions);
|
|
76
|
-
}
|
|
77
|
-
return transitions;
|
|
78
|
-
}
|
|
79
66
|
function wrapInXml(tagName, content) {
|
|
80
67
|
return `<${tagName}>${content}</${tagName}>`;
|
|
81
68
|
}
|
|
@@ -93,14 +80,6 @@ function randomId(prefix) {
|
|
|
93
80
|
const random = Math.random().toString(36).substring(2, 9);
|
|
94
81
|
return `${prefix || ""}${timestamp}${random}`;
|
|
95
82
|
}
|
|
96
|
-
var machineHashes = /* @__PURE__ */ new WeakMap();
|
|
97
|
-
function getMachineHash(machine) {
|
|
98
|
-
if (machineHashes.has(machine)) return machineHashes.get(machine);
|
|
99
|
-
const transitions = getAllMachineTransitions(machine.root);
|
|
100
|
-
const machineHash = (0, import_object_hash.default)(transitions);
|
|
101
|
-
machineHashes.set(machine, machineHash);
|
|
102
|
-
return machineHash;
|
|
103
|
-
}
|
|
104
83
|
function isActorRef(actorRefLike) {
|
|
105
84
|
return "src" in actorRefLike && "system" in actorRefLike && "sessionId" in actorRefLike;
|
|
106
85
|
}
|
|
@@ -119,7 +98,7 @@ function isMachineActor(actor) {
|
|
|
119
98
|
return "src" in actor && typeof actor.src === "object" && actor.src !== null && "definition" in actor.src;
|
|
120
99
|
}
|
|
121
100
|
|
|
122
|
-
// src/
|
|
101
|
+
// src/policies/toolPolicy.ts
|
|
123
102
|
var import_xstate3 = require("xstate");
|
|
124
103
|
|
|
125
104
|
// src/text.ts
|
|
@@ -140,18 +119,11 @@ ${data.goal}
|
|
|
140
119
|
|
|
141
120
|
// src/text.ts
|
|
142
121
|
var import_xstate = require("xstate");
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
if (typeof options.messages === "function") {
|
|
146
|
-
messages = await options.messages(agent);
|
|
147
|
-
} else if (options.messages) {
|
|
148
|
-
messages = options.messages;
|
|
149
|
-
}
|
|
150
|
-
messages = messages.concat({
|
|
122
|
+
function combinePromptAndMessages(prompt, messages) {
|
|
123
|
+
return (messages ?? []).concat({
|
|
151
124
|
role: "user",
|
|
152
125
|
content: prompt
|
|
153
126
|
});
|
|
154
|
-
return messages;
|
|
155
127
|
}
|
|
156
128
|
function fromTextStream(agent, options) {
|
|
157
129
|
const template = options?.template ?? defaultTextTemplate;
|
|
@@ -164,7 +136,10 @@ function fromTextStream(agent, options) {
|
|
|
164
136
|
goal,
|
|
165
137
|
context: input.context
|
|
166
138
|
});
|
|
167
|
-
const messages =
|
|
139
|
+
const messages = combinePromptAndMessages(
|
|
140
|
+
promptWithContext,
|
|
141
|
+
input.messages
|
|
142
|
+
);
|
|
168
143
|
const result = await (0, import_ai.streamText)({
|
|
169
144
|
...options,
|
|
170
145
|
...input,
|
|
@@ -196,7 +171,6 @@ function fromTextStream(agent, options) {
|
|
|
196
171
|
}
|
|
197
172
|
function fromText(agent, options) {
|
|
198
173
|
const resolvedOptions = {
|
|
199
|
-
...agent.defaultOptions,
|
|
200
174
|
...options
|
|
201
175
|
};
|
|
202
176
|
const template = resolvedOptions.template ?? defaultTextTemplate;
|
|
@@ -206,7 +180,10 @@ function fromText(agent, options) {
|
|
|
206
180
|
goal,
|
|
207
181
|
context: input.context
|
|
208
182
|
});
|
|
209
|
-
const messages =
|
|
183
|
+
const messages = combinePromptAndMessages(
|
|
184
|
+
promptWithContext,
|
|
185
|
+
input.messages
|
|
186
|
+
);
|
|
210
187
|
const model = input.model ? agent.wrap(input.model) : agent.model;
|
|
211
188
|
return await (0, import_ai.generateText)({
|
|
212
189
|
...input,
|
|
@@ -221,53 +198,6 @@ function fromText(agent, options) {
|
|
|
221
198
|
// src/decide.ts
|
|
222
199
|
var import_xstate2 = require("xstate");
|
|
223
200
|
var import_ai2 = require("ai");
|
|
224
|
-
async function agentDecide(agent, options) {
|
|
225
|
-
const resolvedOptions = {
|
|
226
|
-
...agent.defaultOptions,
|
|
227
|
-
...options
|
|
228
|
-
};
|
|
229
|
-
const {
|
|
230
|
-
strategy = agent.strategy,
|
|
231
|
-
goal,
|
|
232
|
-
allowedEvents,
|
|
233
|
-
events = agent.events,
|
|
234
|
-
state,
|
|
235
|
-
machine,
|
|
236
|
-
model = agent.model,
|
|
237
|
-
messages,
|
|
238
|
-
...otherDecideInput
|
|
239
|
-
} = resolvedOptions;
|
|
240
|
-
const filteredEventSchemas = allowedEvents ? Object.fromEntries(
|
|
241
|
-
Object.entries(events).filter(([key]) => {
|
|
242
|
-
return allowedEvents.includes(key);
|
|
243
|
-
})
|
|
244
|
-
) : events;
|
|
245
|
-
let attempts = 0;
|
|
246
|
-
const maxAttempts = resolvedOptions.maxAttempts ?? 2;
|
|
247
|
-
let decision;
|
|
248
|
-
const minimalState = {
|
|
249
|
-
value: state.value,
|
|
250
|
-
context: state.context
|
|
251
|
-
};
|
|
252
|
-
while (attempts++ < maxAttempts) {
|
|
253
|
-
decision = await strategy(agent, {
|
|
254
|
-
model,
|
|
255
|
-
goal,
|
|
256
|
-
events: filteredEventSchemas,
|
|
257
|
-
state: minimalState,
|
|
258
|
-
machine,
|
|
259
|
-
messages,
|
|
260
|
-
// TODO: fix UIMessage thing
|
|
261
|
-
...otherDecideInput
|
|
262
|
-
});
|
|
263
|
-
if (decision?.nextEvent) {
|
|
264
|
-
agent.addDecision(decision);
|
|
265
|
-
await resolvedOptions.execute?.(decision.nextEvent);
|
|
266
|
-
break;
|
|
267
|
-
}
|
|
268
|
-
}
|
|
269
|
-
return decision;
|
|
270
|
-
}
|
|
271
201
|
function fromDecision(agent, defaultInput) {
|
|
272
202
|
return (0, import_xstate2.fromPromise)(async ({ input, self }) => {
|
|
273
203
|
const parentRef = self._parent;
|
|
@@ -280,25 +210,27 @@ function fromDecision(agent, defaultInput) {
|
|
|
280
210
|
...defaultInput,
|
|
281
211
|
...inputObject
|
|
282
212
|
};
|
|
283
|
-
const decision = await
|
|
213
|
+
const decision = await agent.decide({
|
|
284
214
|
machine: parentRef.logic,
|
|
285
215
|
state: snapshot,
|
|
286
|
-
|
|
287
|
-
parentRef.send(event);
|
|
288
|
-
},
|
|
216
|
+
allowedEvents: resolvedInput.allowedEvents,
|
|
289
217
|
...resolvedInput,
|
|
290
218
|
// @ts-ignore
|
|
291
219
|
messages: resolvedInput.messages
|
|
292
220
|
});
|
|
221
|
+
if (decision?.nextEvent) {
|
|
222
|
+
parentRef.send(decision.nextEvent);
|
|
223
|
+
}
|
|
293
224
|
return decision;
|
|
294
225
|
});
|
|
295
226
|
}
|
|
296
|
-
function getToolMap(
|
|
297
|
-
const
|
|
227
|
+
function getToolMap(agent, input) {
|
|
228
|
+
const events = input.events ?? agent.events;
|
|
229
|
+
const transitions = input.machine ? getTransitions(input.state, input.machine) : Object.entries(events).map(([eventType, { description }]) => ({
|
|
298
230
|
eventType,
|
|
299
231
|
description
|
|
300
232
|
}));
|
|
301
|
-
const filter = (eventType) => Object.keys(
|
|
233
|
+
const filter = (eventType) => Object.keys(events).includes(eventType);
|
|
302
234
|
const functionNameMapping = {};
|
|
303
235
|
const toolTransitions = transitions.filter((t) => {
|
|
304
236
|
return filter(t.eventType);
|
|
@@ -336,25 +268,25 @@ function getToolMap(_agent, input) {
|
|
|
336
268
|
return toolMap;
|
|
337
269
|
}
|
|
338
270
|
|
|
339
|
-
// src/
|
|
340
|
-
var
|
|
271
|
+
// src/policies/toolPolicy.ts
|
|
272
|
+
var toolPolicyPromptTemplate = (data) => {
|
|
341
273
|
return `
|
|
342
274
|
${convertToXml(data)}
|
|
343
275
|
|
|
344
276
|
Make at most one tool call to achieve the above goal. If the goal cannot be achieved with any tool calls, do not make any tool call.
|
|
345
277
|
`.trim();
|
|
346
278
|
};
|
|
347
|
-
async function
|
|
279
|
+
async function toolPolicy(agent, input) {
|
|
348
280
|
const toolMap = getToolMap(agent, input);
|
|
349
281
|
if (!toolMap) {
|
|
350
282
|
return void 0;
|
|
351
283
|
}
|
|
352
|
-
const prompt =
|
|
284
|
+
const prompt = toolPolicyPromptTemplate({
|
|
353
285
|
stateValue: input.state.value,
|
|
354
286
|
context: input.context ?? input.state.context,
|
|
355
287
|
goal: input.goal
|
|
356
288
|
});
|
|
357
|
-
const messages =
|
|
289
|
+
const messages = combinePromptAndMessages(prompt, input.messages);
|
|
358
290
|
const model = input.model ? agent.wrap(input.model) : agent.model;
|
|
359
291
|
const { state, machine, events, goal, model: _, ...rest } = input;
|
|
360
292
|
const machineState = input.machine && input.state ? input.machine.resolveState({
|
|
@@ -370,33 +302,30 @@ async function simpleStrategy(agent, input) {
|
|
|
370
302
|
toolChoice: input.toolChoice ?? "required"
|
|
371
303
|
});
|
|
372
304
|
result.response.messages.forEach((m) => {
|
|
373
|
-
|
|
374
|
-
agent.addMessage({
|
|
375
|
-
...message,
|
|
376
|
-
id: randomId(),
|
|
377
|
-
timestamp: Date.now()
|
|
378
|
-
});
|
|
305
|
+
agent.addMessage(m);
|
|
379
306
|
});
|
|
380
307
|
const singleResult = result.toolResults[0];
|
|
381
308
|
if (!singleResult) {
|
|
382
309
|
console.warn("No tool call results returned");
|
|
383
310
|
return void 0;
|
|
384
311
|
}
|
|
312
|
+
const nextEvent = singleResult.result;
|
|
385
313
|
return {
|
|
386
314
|
id: randomId(),
|
|
387
|
-
|
|
315
|
+
decisionId: input.decisionId ?? null,
|
|
316
|
+
policy: "simple",
|
|
388
317
|
goal: input.goal,
|
|
389
318
|
goalState: input.state,
|
|
390
|
-
nextEvent
|
|
391
|
-
episodeId: agent.episodeId,
|
|
319
|
+
nextEvent,
|
|
320
|
+
episodeId: input.episodeId ?? agent.episodeId,
|
|
392
321
|
timestamp: Date.now(),
|
|
393
322
|
paths: [
|
|
394
323
|
{
|
|
395
|
-
state:
|
|
324
|
+
state: null,
|
|
396
325
|
steps: [
|
|
397
326
|
{
|
|
398
|
-
event:
|
|
399
|
-
state: machine && machineState ? (0, import_xstate3.
|
|
327
|
+
event: nextEvent,
|
|
328
|
+
state: machine && machineState ? (0, import_xstate3.transition)(machine, machineState, nextEvent)[0] : null
|
|
400
329
|
}
|
|
401
330
|
]
|
|
402
331
|
}
|
|
@@ -478,7 +407,6 @@ var agentLogic = (0, import_xstate4.fromTransition)(
|
|
|
478
407
|
state.feedback.push(event.feedback);
|
|
479
408
|
emit({
|
|
480
409
|
type: "feedback",
|
|
481
|
-
// @ts-ignore TODO: fix types in XState
|
|
482
410
|
feedback: event.feedback
|
|
483
411
|
});
|
|
484
412
|
break;
|
|
@@ -487,7 +415,6 @@ var agentLogic = (0, import_xstate4.fromTransition)(
|
|
|
487
415
|
state.observations.push(event.observation);
|
|
488
416
|
emit({
|
|
489
417
|
type: "observation",
|
|
490
|
-
// @ts-ignore TODO: fix types in XState
|
|
491
418
|
observation: event.observation
|
|
492
419
|
});
|
|
493
420
|
break;
|
|
@@ -496,7 +423,6 @@ var agentLogic = (0, import_xstate4.fromTransition)(
|
|
|
496
423
|
state.messages.push(event.message);
|
|
497
424
|
emit({
|
|
498
425
|
type: "message",
|
|
499
|
-
// @ts-ignore TODO: fix types in XState
|
|
500
426
|
message: event.message
|
|
501
427
|
});
|
|
502
428
|
break;
|
|
@@ -509,6 +435,14 @@ var agentLogic = (0, import_xstate4.fromTransition)(
|
|
|
509
435
|
});
|
|
510
436
|
break;
|
|
511
437
|
}
|
|
438
|
+
case "agent.insight": {
|
|
439
|
+
state.insights.push(event.insight);
|
|
440
|
+
emit({
|
|
441
|
+
type: "insight",
|
|
442
|
+
insight: event.insight
|
|
443
|
+
});
|
|
444
|
+
break;
|
|
445
|
+
}
|
|
512
446
|
default: {
|
|
513
447
|
console.warn("Unrecognized event", event);
|
|
514
448
|
break;
|
|
@@ -520,7 +454,8 @@ var agentLogic = (0, import_xstate4.fromTransition)(
|
|
|
520
454
|
feedback: [],
|
|
521
455
|
messages: [],
|
|
522
456
|
observations: [],
|
|
523
|
-
decisions: []
|
|
457
|
+
decisions: [],
|
|
458
|
+
insights: []
|
|
524
459
|
})
|
|
525
460
|
);
|
|
526
461
|
function createAgent({
|
|
@@ -530,7 +465,7 @@ function createAgent({
|
|
|
530
465
|
events,
|
|
531
466
|
context,
|
|
532
467
|
episodeId,
|
|
533
|
-
|
|
468
|
+
policy = toolPolicy,
|
|
534
469
|
logic = agentLogic
|
|
535
470
|
}) {
|
|
536
471
|
return new Agent({
|
|
@@ -538,14 +473,13 @@ function createAgent({
|
|
|
538
473
|
context,
|
|
539
474
|
events,
|
|
540
475
|
description,
|
|
541
|
-
|
|
476
|
+
policy,
|
|
542
477
|
model,
|
|
543
478
|
logic,
|
|
544
479
|
episodeId
|
|
545
480
|
});
|
|
546
481
|
}
|
|
547
482
|
var Agent = class extends import_xstate4.Actor {
|
|
548
|
-
// todo
|
|
549
483
|
constructor({
|
|
550
484
|
logic = agentLogic,
|
|
551
485
|
id,
|
|
@@ -555,7 +489,7 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
555
489
|
events,
|
|
556
490
|
context,
|
|
557
491
|
episodeId,
|
|
558
|
-
|
|
492
|
+
policy = toolPolicy
|
|
559
493
|
}) {
|
|
560
494
|
super(logic);
|
|
561
495
|
this.model = model;
|
|
@@ -564,24 +498,36 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
564
498
|
this.description = description;
|
|
565
499
|
this.events = events;
|
|
566
500
|
this.context = context;
|
|
567
|
-
this.
|
|
501
|
+
this.policy = policy;
|
|
568
502
|
this.id = id ?? randomId();
|
|
569
503
|
this.start();
|
|
570
504
|
}
|
|
571
505
|
/**
|
|
572
|
-
* Called whenever the agent
|
|
506
|
+
* Called whenever the agent detects that a message was sent from the human, assistant, or system.
|
|
573
507
|
*/
|
|
574
508
|
onMessage(fn) {
|
|
575
509
|
return this.on("message", (ev) => fn(ev.message));
|
|
576
510
|
}
|
|
577
511
|
/**
|
|
578
|
-
* Called whenever the agent
|
|
512
|
+
* Called whenever the agent receives some feedback.
|
|
579
513
|
*/
|
|
580
514
|
onFeedback(fn) {
|
|
581
515
|
return this.on("feedback", (ev) => fn(ev.feedback));
|
|
582
516
|
}
|
|
583
517
|
/**
|
|
584
|
-
*
|
|
518
|
+
* Called whenever the agent receives an observation.
|
|
519
|
+
*/
|
|
520
|
+
onObservation(fn) {
|
|
521
|
+
return this.on("observation", (ev) => fn(ev.observation));
|
|
522
|
+
}
|
|
523
|
+
/**
|
|
524
|
+
* Called whenever the agent makes a decision.
|
|
525
|
+
*/
|
|
526
|
+
onDecision(fn) {
|
|
527
|
+
return this.on("decision", (ev) => fn(ev.decision));
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* Adds a message to the agent's short-term (local) memory.
|
|
585
531
|
*/
|
|
586
532
|
addMessage(messageInput) {
|
|
587
533
|
const message = {
|
|
@@ -602,10 +548,11 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
602
548
|
addFeedback(feedbackInput) {
|
|
603
549
|
const feedback = {
|
|
604
550
|
...feedbackInput,
|
|
551
|
+
id: feedbackInput.id ?? randomId(),
|
|
605
552
|
comment: feedbackInput.comment ?? void 0,
|
|
606
553
|
attributes: { ...feedbackInput.attributes },
|
|
607
554
|
timestamp: feedbackInput.timestamp ?? Date.now(),
|
|
608
|
-
episodeId: this.episodeId
|
|
555
|
+
episodeId: feedbackInput.episodeId ?? this.episodeId
|
|
609
556
|
};
|
|
610
557
|
this.send({
|
|
611
558
|
type: "agent.feedback",
|
|
@@ -626,9 +573,12 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
626
573
|
event,
|
|
627
574
|
state,
|
|
628
575
|
id: observationInput.id ?? randomId(),
|
|
629
|
-
episodeId: this.episodeId,
|
|
576
|
+
episodeId: observationInput.episodeId ?? this.episodeId,
|
|
630
577
|
timestamp: observationInput.timestamp ?? Date.now(),
|
|
631
|
-
|
|
578
|
+
decisionId: observationInput.decisionId
|
|
579
|
+
// machineHash: observationInput.machine
|
|
580
|
+
// ? getMachineHash(observationInput.machine)
|
|
581
|
+
// : undefined,
|
|
632
582
|
};
|
|
633
583
|
this.send({
|
|
634
584
|
type: "agent.observe",
|
|
@@ -642,10 +592,36 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
642
592
|
getObservations() {
|
|
643
593
|
return this.getSnapshot().context.observations;
|
|
644
594
|
}
|
|
645
|
-
|
|
595
|
+
addInsight(insightInput) {
|
|
596
|
+
const insight = {
|
|
597
|
+
...insightInput,
|
|
598
|
+
episodeId: insightInput.episodeId ?? this.episodeId,
|
|
599
|
+
id: insightInput.id ?? randomId(),
|
|
600
|
+
timestamp: insightInput.timestamp ?? Date.now()
|
|
601
|
+
};
|
|
602
|
+
this.send({
|
|
603
|
+
type: "agent.insight",
|
|
604
|
+
insight
|
|
605
|
+
});
|
|
606
|
+
return insight;
|
|
607
|
+
}
|
|
608
|
+
getInsights() {
|
|
609
|
+
return this.getSnapshot().context.insights;
|
|
610
|
+
}
|
|
611
|
+
addDecision(input) {
|
|
646
612
|
this.send({
|
|
647
613
|
type: "agent.decision",
|
|
648
|
-
decision
|
|
614
|
+
decision: {
|
|
615
|
+
id: input.id ?? randomId(),
|
|
616
|
+
episodeId: input.episodeId ?? this.episodeId,
|
|
617
|
+
timestamp: input.timestamp ?? Date.now(),
|
|
618
|
+
decisionId: input.decisionId ?? null,
|
|
619
|
+
policy: input.policy ?? null,
|
|
620
|
+
goalState: input.goalState ?? null,
|
|
621
|
+
nextEvent: input.nextEvent ?? null,
|
|
622
|
+
paths: input.paths ?? [],
|
|
623
|
+
...input
|
|
624
|
+
}
|
|
649
625
|
});
|
|
650
626
|
}
|
|
651
627
|
/**
|
|
@@ -660,11 +636,11 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
660
636
|
let prevState = void 0;
|
|
661
637
|
let subscribed = true;
|
|
662
638
|
const agent = this;
|
|
663
|
-
async
|
|
639
|
+
const handleObservation = async (observationInput) => {
|
|
664
640
|
const observation = agent.addObservation(observationInput);
|
|
665
641
|
const interactInput = getInput?.(observation);
|
|
666
642
|
if (interactInput) {
|
|
667
|
-
const decision = await
|
|
643
|
+
const decision = await this.decide({
|
|
668
644
|
machine,
|
|
669
645
|
state: observation.state,
|
|
670
646
|
...interactInput
|
|
@@ -675,31 +651,31 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
675
651
|
}
|
|
676
652
|
}
|
|
677
653
|
prevState = observationInput.state;
|
|
678
|
-
}
|
|
654
|
+
};
|
|
679
655
|
const sub = actorRefCheck ? actorRef.system.inspect({
|
|
680
656
|
next: async (inspEvent) => {
|
|
681
657
|
if (!subscribed || inspEvent.actorRef !== actorRef || inspEvent.type !== "@xstate.snapshot") {
|
|
682
658
|
return;
|
|
683
659
|
}
|
|
684
660
|
const decisionId = inspEvent.event["_decision"];
|
|
685
|
-
const
|
|
661
|
+
const decisions = agent.getDecisions();
|
|
662
|
+
const decision = decisionId ? decisions.find((d) => d.id === decisionId) : void 0;
|
|
686
663
|
const observationInput = {
|
|
687
664
|
event: inspEvent.event,
|
|
688
665
|
prevState,
|
|
689
666
|
state: inspEvent.snapshot,
|
|
690
|
-
|
|
691
|
-
|
|
667
|
+
goal: decision?.goal,
|
|
668
|
+
decisionId
|
|
692
669
|
};
|
|
693
670
|
await handleObservation(observationInput);
|
|
694
671
|
}
|
|
695
672
|
}) : void 0;
|
|
696
673
|
if (actorRef._processingStatus === 1) {
|
|
697
674
|
handleObservation({
|
|
675
|
+
decisionId: void 0,
|
|
698
676
|
prevState: void 0,
|
|
699
677
|
event: void 0,
|
|
700
|
-
state: actorRef.getSnapshot()
|
|
701
|
-
machine: actorRef.src,
|
|
702
|
-
goal: void 0
|
|
678
|
+
state: actorRef.getSnapshot()
|
|
703
679
|
});
|
|
704
680
|
}
|
|
705
681
|
return {
|
|
@@ -718,12 +694,13 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
718
694
|
return;
|
|
719
695
|
}
|
|
720
696
|
const decisionId = inspEvent.event["_decision"];
|
|
721
|
-
const
|
|
697
|
+
const decisions = this.getDecisions();
|
|
698
|
+
const decision = decisionId ? decisions.find((d) => d.id === decisionId) : void 0;
|
|
722
699
|
const observationInput = {
|
|
700
|
+
decisionId,
|
|
723
701
|
event: inspEvent.event,
|
|
724
702
|
prevState,
|
|
725
703
|
state: inspEvent.snapshot,
|
|
726
|
-
machine: actorRef.src,
|
|
727
704
|
goal: decision?.goal
|
|
728
705
|
};
|
|
729
706
|
prevState = observationInput.state;
|
|
@@ -734,7 +711,7 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
734
711
|
} };
|
|
735
712
|
}
|
|
736
713
|
wrap(modelToWrap) {
|
|
737
|
-
return (0, import_ai4.
|
|
714
|
+
return (0, import_ai4.wrapLanguageModel)({
|
|
738
715
|
model: modelToWrap,
|
|
739
716
|
middleware: createAgentMiddleware(this)
|
|
740
717
|
});
|
|
@@ -747,14 +724,4286 @@ var Agent = class extends import_xstate4.Actor {
|
|
|
747
724
|
* - The `machine` (e.g. a state machine) that specifies what can happen next
|
|
748
725
|
* - Additional `context`
|
|
749
726
|
*/
|
|
750
|
-
async decide(
|
|
751
|
-
|
|
727
|
+
async decide(input) {
|
|
728
|
+
const resolvedOptions = input;
|
|
729
|
+
const {
|
|
730
|
+
policy = this.policy,
|
|
731
|
+
goal,
|
|
732
|
+
allowedEvents,
|
|
733
|
+
events = this.events,
|
|
734
|
+
state,
|
|
735
|
+
machine,
|
|
736
|
+
model = this.model,
|
|
737
|
+
messages,
|
|
738
|
+
episodeId = this.episodeId,
|
|
739
|
+
maxAttempts = 2,
|
|
740
|
+
...otherDecideInput
|
|
741
|
+
} = resolvedOptions;
|
|
742
|
+
const filteredEventSchemas = allowedEvents ? Object.fromEntries(
|
|
743
|
+
Object.entries(events).filter(([key]) => {
|
|
744
|
+
return allowedEvents.includes(key);
|
|
745
|
+
})
|
|
746
|
+
) : events;
|
|
747
|
+
let attempts = 0;
|
|
748
|
+
let decision;
|
|
749
|
+
const minimalState = {
|
|
750
|
+
value: state.value,
|
|
751
|
+
context: state.context
|
|
752
|
+
};
|
|
753
|
+
while (attempts++ < maxAttempts) {
|
|
754
|
+
decision = await policy(this, {
|
|
755
|
+
episodeId,
|
|
756
|
+
model,
|
|
757
|
+
goal,
|
|
758
|
+
events: filteredEventSchemas,
|
|
759
|
+
state: minimalState,
|
|
760
|
+
machine,
|
|
761
|
+
messages,
|
|
762
|
+
// TODO: fix UIMessage thing
|
|
763
|
+
...otherDecideInput
|
|
764
|
+
});
|
|
765
|
+
if (decision?.nextEvent) {
|
|
766
|
+
this.addDecision(decision);
|
|
767
|
+
break;
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
return decision;
|
|
771
|
+
}
|
|
772
|
+
};
|
|
773
|
+
|
|
774
|
+
// src/policies/chainOfThoughtPolicy.ts
|
|
775
|
+
var import_ai5 = require("ai");
|
|
776
|
+
var chainOfThoughtPromptTemplate = ({
|
|
777
|
+
stateValue,
|
|
778
|
+
context,
|
|
779
|
+
goal
|
|
780
|
+
}) => {
|
|
781
|
+
return `${convertToXml({ stateValue, context, goal })}
|
|
782
|
+
|
|
783
|
+
How would you achieve the goal? Think step-by-step.`;
|
|
784
|
+
};
|
|
785
|
+
async function chainOfThoughtPolicy(agent, input) {
|
|
786
|
+
const prompt = chainOfThoughtPromptTemplate({
|
|
787
|
+
stateValue: input.state.value,
|
|
788
|
+
context: input.context ?? input.state.context,
|
|
789
|
+
goal: input.goal
|
|
790
|
+
});
|
|
791
|
+
const messages = combinePromptAndMessages(prompt, input.messages);
|
|
792
|
+
const model = input.model ? agent.wrap(input.model) : agent.model;
|
|
793
|
+
const result = await (0, import_ai5.generateText)({
|
|
794
|
+
model,
|
|
795
|
+
system: input.system ?? agent.description,
|
|
796
|
+
messages
|
|
797
|
+
});
|
|
798
|
+
const decision = await toolPolicy(agent, {
|
|
799
|
+
...input,
|
|
800
|
+
messages: messages.concat(result.response.messages)
|
|
801
|
+
});
|
|
802
|
+
return decision;
|
|
803
|
+
}
|
|
804
|
+
|
|
805
|
+
// src/policies/shortestPathPolicy.ts
|
|
806
|
+
var import_ai6 = require("ai");
|
|
807
|
+
var import_graph = require("@xstate/graph");
|
|
808
|
+
|
|
809
|
+
// node_modules/.pnpm/zod@3.24.2/node_modules/zod/lib/index.mjs
|
|
810
|
+
var util;
|
|
811
|
+
(function(util2) {
|
|
812
|
+
util2.assertEqual = (val) => val;
|
|
813
|
+
function assertIs(_arg) {
|
|
814
|
+
}
|
|
815
|
+
util2.assertIs = assertIs;
|
|
816
|
+
function assertNever(_x) {
|
|
817
|
+
throw new Error();
|
|
818
|
+
}
|
|
819
|
+
util2.assertNever = assertNever;
|
|
820
|
+
util2.arrayToEnum = (items) => {
|
|
821
|
+
const obj = {};
|
|
822
|
+
for (const item of items) {
|
|
823
|
+
obj[item] = item;
|
|
824
|
+
}
|
|
825
|
+
return obj;
|
|
826
|
+
};
|
|
827
|
+
util2.getValidEnumValues = (obj) => {
|
|
828
|
+
const validKeys = util2.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
|
|
829
|
+
const filtered = {};
|
|
830
|
+
for (const k of validKeys) {
|
|
831
|
+
filtered[k] = obj[k];
|
|
832
|
+
}
|
|
833
|
+
return util2.objectValues(filtered);
|
|
834
|
+
};
|
|
835
|
+
util2.objectValues = (obj) => {
|
|
836
|
+
return util2.objectKeys(obj).map(function(e) {
|
|
837
|
+
return obj[e];
|
|
838
|
+
});
|
|
839
|
+
};
|
|
840
|
+
util2.objectKeys = typeof Object.keys === "function" ? (obj) => Object.keys(obj) : (object) => {
|
|
841
|
+
const keys = [];
|
|
842
|
+
for (const key in object) {
|
|
843
|
+
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
844
|
+
keys.push(key);
|
|
845
|
+
}
|
|
846
|
+
}
|
|
847
|
+
return keys;
|
|
848
|
+
};
|
|
849
|
+
util2.find = (arr, checker) => {
|
|
850
|
+
for (const item of arr) {
|
|
851
|
+
if (checker(item))
|
|
852
|
+
return item;
|
|
853
|
+
}
|
|
854
|
+
return void 0;
|
|
855
|
+
};
|
|
856
|
+
util2.isInteger = typeof Number.isInteger === "function" ? (val) => Number.isInteger(val) : (val) => typeof val === "number" && isFinite(val) && Math.floor(val) === val;
|
|
857
|
+
function joinValues(array, separator = " | ") {
|
|
858
|
+
return array.map((val) => typeof val === "string" ? `'${val}'` : val).join(separator);
|
|
859
|
+
}
|
|
860
|
+
util2.joinValues = joinValues;
|
|
861
|
+
util2.jsonStringifyReplacer = (_, value) => {
|
|
862
|
+
if (typeof value === "bigint") {
|
|
863
|
+
return value.toString();
|
|
864
|
+
}
|
|
865
|
+
return value;
|
|
866
|
+
};
|
|
867
|
+
})(util || (util = {}));
|
|
868
|
+
var objectUtil;
|
|
869
|
+
(function(objectUtil2) {
|
|
870
|
+
objectUtil2.mergeShapes = (first, second) => {
|
|
871
|
+
return {
|
|
872
|
+
...first,
|
|
873
|
+
...second
|
|
874
|
+
// second overwrites first
|
|
875
|
+
};
|
|
876
|
+
};
|
|
877
|
+
})(objectUtil || (objectUtil = {}));
|
|
878
|
+
var ZodParsedType = util.arrayToEnum([
|
|
879
|
+
"string",
|
|
880
|
+
"nan",
|
|
881
|
+
"number",
|
|
882
|
+
"integer",
|
|
883
|
+
"float",
|
|
884
|
+
"boolean",
|
|
885
|
+
"date",
|
|
886
|
+
"bigint",
|
|
887
|
+
"symbol",
|
|
888
|
+
"function",
|
|
889
|
+
"undefined",
|
|
890
|
+
"null",
|
|
891
|
+
"array",
|
|
892
|
+
"object",
|
|
893
|
+
"unknown",
|
|
894
|
+
"promise",
|
|
895
|
+
"void",
|
|
896
|
+
"never",
|
|
897
|
+
"map",
|
|
898
|
+
"set"
|
|
899
|
+
]);
|
|
900
|
+
var getParsedType = (data) => {
|
|
901
|
+
const t = typeof data;
|
|
902
|
+
switch (t) {
|
|
903
|
+
case "undefined":
|
|
904
|
+
return ZodParsedType.undefined;
|
|
905
|
+
case "string":
|
|
906
|
+
return ZodParsedType.string;
|
|
907
|
+
case "number":
|
|
908
|
+
return isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
|
|
909
|
+
case "boolean":
|
|
910
|
+
return ZodParsedType.boolean;
|
|
911
|
+
case "function":
|
|
912
|
+
return ZodParsedType.function;
|
|
913
|
+
case "bigint":
|
|
914
|
+
return ZodParsedType.bigint;
|
|
915
|
+
case "symbol":
|
|
916
|
+
return ZodParsedType.symbol;
|
|
917
|
+
case "object":
|
|
918
|
+
if (Array.isArray(data)) {
|
|
919
|
+
return ZodParsedType.array;
|
|
920
|
+
}
|
|
921
|
+
if (data === null) {
|
|
922
|
+
return ZodParsedType.null;
|
|
923
|
+
}
|
|
924
|
+
if (data.then && typeof data.then === "function" && data.catch && typeof data.catch === "function") {
|
|
925
|
+
return ZodParsedType.promise;
|
|
926
|
+
}
|
|
927
|
+
if (typeof Map !== "undefined" && data instanceof Map) {
|
|
928
|
+
return ZodParsedType.map;
|
|
929
|
+
}
|
|
930
|
+
if (typeof Set !== "undefined" && data instanceof Set) {
|
|
931
|
+
return ZodParsedType.set;
|
|
932
|
+
}
|
|
933
|
+
if (typeof Date !== "undefined" && data instanceof Date) {
|
|
934
|
+
return ZodParsedType.date;
|
|
935
|
+
}
|
|
936
|
+
return ZodParsedType.object;
|
|
937
|
+
default:
|
|
938
|
+
return ZodParsedType.unknown;
|
|
939
|
+
}
|
|
940
|
+
};
|
|
941
|
+
var ZodIssueCode = util.arrayToEnum([
|
|
942
|
+
"invalid_type",
|
|
943
|
+
"invalid_literal",
|
|
944
|
+
"custom",
|
|
945
|
+
"invalid_union",
|
|
946
|
+
"invalid_union_discriminator",
|
|
947
|
+
"invalid_enum_value",
|
|
948
|
+
"unrecognized_keys",
|
|
949
|
+
"invalid_arguments",
|
|
950
|
+
"invalid_return_type",
|
|
951
|
+
"invalid_date",
|
|
952
|
+
"invalid_string",
|
|
953
|
+
"too_small",
|
|
954
|
+
"too_big",
|
|
955
|
+
"invalid_intersection_types",
|
|
956
|
+
"not_multiple_of",
|
|
957
|
+
"not_finite"
|
|
958
|
+
]);
|
|
959
|
+
var quotelessJson = (obj) => {
|
|
960
|
+
const json = JSON.stringify(obj, null, 2);
|
|
961
|
+
return json.replace(/"([^"]+)":/g, "$1:");
|
|
962
|
+
};
|
|
963
|
+
var ZodError = class _ZodError extends Error {
|
|
964
|
+
get errors() {
|
|
965
|
+
return this.issues;
|
|
966
|
+
}
|
|
967
|
+
constructor(issues) {
|
|
968
|
+
super();
|
|
969
|
+
this.issues = [];
|
|
970
|
+
this.addIssue = (sub) => {
|
|
971
|
+
this.issues = [...this.issues, sub];
|
|
972
|
+
};
|
|
973
|
+
this.addIssues = (subs = []) => {
|
|
974
|
+
this.issues = [...this.issues, ...subs];
|
|
975
|
+
};
|
|
976
|
+
const actualProto = new.target.prototype;
|
|
977
|
+
if (Object.setPrototypeOf) {
|
|
978
|
+
Object.setPrototypeOf(this, actualProto);
|
|
979
|
+
} else {
|
|
980
|
+
this.__proto__ = actualProto;
|
|
981
|
+
}
|
|
982
|
+
this.name = "ZodError";
|
|
983
|
+
this.issues = issues;
|
|
984
|
+
}
|
|
985
|
+
format(_mapper) {
|
|
986
|
+
const mapper = _mapper || function(issue) {
|
|
987
|
+
return issue.message;
|
|
988
|
+
};
|
|
989
|
+
const fieldErrors = { _errors: [] };
|
|
990
|
+
const processError = (error) => {
|
|
991
|
+
for (const issue of error.issues) {
|
|
992
|
+
if (issue.code === "invalid_union") {
|
|
993
|
+
issue.unionErrors.map(processError);
|
|
994
|
+
} else if (issue.code === "invalid_return_type") {
|
|
995
|
+
processError(issue.returnTypeError);
|
|
996
|
+
} else if (issue.code === "invalid_arguments") {
|
|
997
|
+
processError(issue.argumentsError);
|
|
998
|
+
} else if (issue.path.length === 0) {
|
|
999
|
+
fieldErrors._errors.push(mapper(issue));
|
|
1000
|
+
} else {
|
|
1001
|
+
let curr = fieldErrors;
|
|
1002
|
+
let i = 0;
|
|
1003
|
+
while (i < issue.path.length) {
|
|
1004
|
+
const el = issue.path[i];
|
|
1005
|
+
const terminal = i === issue.path.length - 1;
|
|
1006
|
+
if (!terminal) {
|
|
1007
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
1008
|
+
} else {
|
|
1009
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
1010
|
+
curr[el]._errors.push(mapper(issue));
|
|
1011
|
+
}
|
|
1012
|
+
curr = curr[el];
|
|
1013
|
+
i++;
|
|
1014
|
+
}
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
};
|
|
1018
|
+
processError(this);
|
|
1019
|
+
return fieldErrors;
|
|
1020
|
+
}
|
|
1021
|
+
static assert(value) {
|
|
1022
|
+
if (!(value instanceof _ZodError)) {
|
|
1023
|
+
throw new Error(`Not a ZodError: ${value}`);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
toString() {
|
|
1027
|
+
return this.message;
|
|
1028
|
+
}
|
|
1029
|
+
get message() {
|
|
1030
|
+
return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
|
|
1031
|
+
}
|
|
1032
|
+
get isEmpty() {
|
|
1033
|
+
return this.issues.length === 0;
|
|
1034
|
+
}
|
|
1035
|
+
flatten(mapper = (issue) => issue.message) {
|
|
1036
|
+
const fieldErrors = {};
|
|
1037
|
+
const formErrors = [];
|
|
1038
|
+
for (const sub of this.issues) {
|
|
1039
|
+
if (sub.path.length > 0) {
|
|
1040
|
+
fieldErrors[sub.path[0]] = fieldErrors[sub.path[0]] || [];
|
|
1041
|
+
fieldErrors[sub.path[0]].push(mapper(sub));
|
|
1042
|
+
} else {
|
|
1043
|
+
formErrors.push(mapper(sub));
|
|
1044
|
+
}
|
|
1045
|
+
}
|
|
1046
|
+
return { formErrors, fieldErrors };
|
|
1047
|
+
}
|
|
1048
|
+
get formErrors() {
|
|
1049
|
+
return this.flatten();
|
|
1050
|
+
}
|
|
1051
|
+
};
|
|
1052
|
+
ZodError.create = (issues) => {
|
|
1053
|
+
const error = new ZodError(issues);
|
|
1054
|
+
return error;
|
|
1055
|
+
};
|
|
1056
|
+
var errorMap = (issue, _ctx) => {
|
|
1057
|
+
let message;
|
|
1058
|
+
switch (issue.code) {
|
|
1059
|
+
case ZodIssueCode.invalid_type:
|
|
1060
|
+
if (issue.received === ZodParsedType.undefined) {
|
|
1061
|
+
message = "Required";
|
|
1062
|
+
} else {
|
|
1063
|
+
message = `Expected ${issue.expected}, received ${issue.received}`;
|
|
1064
|
+
}
|
|
1065
|
+
break;
|
|
1066
|
+
case ZodIssueCode.invalid_literal:
|
|
1067
|
+
message = `Invalid literal value, expected ${JSON.stringify(issue.expected, util.jsonStringifyReplacer)}`;
|
|
1068
|
+
break;
|
|
1069
|
+
case ZodIssueCode.unrecognized_keys:
|
|
1070
|
+
message = `Unrecognized key(s) in object: ${util.joinValues(issue.keys, ", ")}`;
|
|
1071
|
+
break;
|
|
1072
|
+
case ZodIssueCode.invalid_union:
|
|
1073
|
+
message = `Invalid input`;
|
|
1074
|
+
break;
|
|
1075
|
+
case ZodIssueCode.invalid_union_discriminator:
|
|
1076
|
+
message = `Invalid discriminator value. Expected ${util.joinValues(issue.options)}`;
|
|
1077
|
+
break;
|
|
1078
|
+
case ZodIssueCode.invalid_enum_value:
|
|
1079
|
+
message = `Invalid enum value. Expected ${util.joinValues(issue.options)}, received '${issue.received}'`;
|
|
1080
|
+
break;
|
|
1081
|
+
case ZodIssueCode.invalid_arguments:
|
|
1082
|
+
message = `Invalid function arguments`;
|
|
1083
|
+
break;
|
|
1084
|
+
case ZodIssueCode.invalid_return_type:
|
|
1085
|
+
message = `Invalid function return type`;
|
|
1086
|
+
break;
|
|
1087
|
+
case ZodIssueCode.invalid_date:
|
|
1088
|
+
message = `Invalid date`;
|
|
1089
|
+
break;
|
|
1090
|
+
case ZodIssueCode.invalid_string:
|
|
1091
|
+
if (typeof issue.validation === "object") {
|
|
1092
|
+
if ("includes" in issue.validation) {
|
|
1093
|
+
message = `Invalid input: must include "${issue.validation.includes}"`;
|
|
1094
|
+
if (typeof issue.validation.position === "number") {
|
|
1095
|
+
message = `${message} at one or more positions greater than or equal to ${issue.validation.position}`;
|
|
1096
|
+
}
|
|
1097
|
+
} else if ("startsWith" in issue.validation) {
|
|
1098
|
+
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
1099
|
+
} else if ("endsWith" in issue.validation) {
|
|
1100
|
+
message = `Invalid input: must end with "${issue.validation.endsWith}"`;
|
|
1101
|
+
} else {
|
|
1102
|
+
util.assertNever(issue.validation);
|
|
1103
|
+
}
|
|
1104
|
+
} else if (issue.validation !== "regex") {
|
|
1105
|
+
message = `Invalid ${issue.validation}`;
|
|
1106
|
+
} else {
|
|
1107
|
+
message = "Invalid";
|
|
1108
|
+
}
|
|
1109
|
+
break;
|
|
1110
|
+
case ZodIssueCode.too_small:
|
|
1111
|
+
if (issue.type === "array")
|
|
1112
|
+
message = `Array must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `more than`} ${issue.minimum} element(s)`;
|
|
1113
|
+
else if (issue.type === "string")
|
|
1114
|
+
message = `String must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
|
|
1115
|
+
else if (issue.type === "number")
|
|
1116
|
+
message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
|
|
1117
|
+
else if (issue.type === "date")
|
|
1118
|
+
message = `Date must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${new Date(Number(issue.minimum))}`;
|
|
1119
|
+
else
|
|
1120
|
+
message = "Invalid input";
|
|
1121
|
+
break;
|
|
1122
|
+
case ZodIssueCode.too_big:
|
|
1123
|
+
if (issue.type === "array")
|
|
1124
|
+
message = `Array must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `less than`} ${issue.maximum} element(s)`;
|
|
1125
|
+
else if (issue.type === "string")
|
|
1126
|
+
message = `String must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
|
|
1127
|
+
else if (issue.type === "number")
|
|
1128
|
+
message = `Number must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
1129
|
+
else if (issue.type === "bigint")
|
|
1130
|
+
message = `BigInt must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
1131
|
+
else if (issue.type === "date")
|
|
1132
|
+
message = `Date must be ${issue.exact ? `exactly` : issue.inclusive ? `smaller than or equal to` : `smaller than`} ${new Date(Number(issue.maximum))}`;
|
|
1133
|
+
else
|
|
1134
|
+
message = "Invalid input";
|
|
1135
|
+
break;
|
|
1136
|
+
case ZodIssueCode.custom:
|
|
1137
|
+
message = `Invalid input`;
|
|
1138
|
+
break;
|
|
1139
|
+
case ZodIssueCode.invalid_intersection_types:
|
|
1140
|
+
message = `Intersection results could not be merged`;
|
|
1141
|
+
break;
|
|
1142
|
+
case ZodIssueCode.not_multiple_of:
|
|
1143
|
+
message = `Number must be a multiple of ${issue.multipleOf}`;
|
|
1144
|
+
break;
|
|
1145
|
+
case ZodIssueCode.not_finite:
|
|
1146
|
+
message = "Number must be finite";
|
|
1147
|
+
break;
|
|
1148
|
+
default:
|
|
1149
|
+
message = _ctx.defaultError;
|
|
1150
|
+
util.assertNever(issue);
|
|
1151
|
+
}
|
|
1152
|
+
return { message };
|
|
1153
|
+
};
|
|
1154
|
+
var overrideErrorMap = errorMap;
|
|
1155
|
+
function setErrorMap(map) {
|
|
1156
|
+
overrideErrorMap = map;
|
|
1157
|
+
}
|
|
1158
|
+
function getErrorMap() {
|
|
1159
|
+
return overrideErrorMap;
|
|
1160
|
+
}
|
|
1161
|
+
var makeIssue = (params) => {
|
|
1162
|
+
const { data, path, errorMaps, issueData } = params;
|
|
1163
|
+
const fullPath = [...path, ...issueData.path || []];
|
|
1164
|
+
const fullIssue = {
|
|
1165
|
+
...issueData,
|
|
1166
|
+
path: fullPath
|
|
1167
|
+
};
|
|
1168
|
+
if (issueData.message !== void 0) {
|
|
1169
|
+
return {
|
|
1170
|
+
...issueData,
|
|
1171
|
+
path: fullPath,
|
|
1172
|
+
message: issueData.message
|
|
1173
|
+
};
|
|
1174
|
+
}
|
|
1175
|
+
let errorMessage = "";
|
|
1176
|
+
const maps = errorMaps.filter((m) => !!m).slice().reverse();
|
|
1177
|
+
for (const map of maps) {
|
|
1178
|
+
errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;
|
|
1179
|
+
}
|
|
1180
|
+
return {
|
|
1181
|
+
...issueData,
|
|
1182
|
+
path: fullPath,
|
|
1183
|
+
message: errorMessage
|
|
1184
|
+
};
|
|
1185
|
+
};
|
|
1186
|
+
var EMPTY_PATH = [];
|
|
1187
|
+
function addIssueToContext(ctx, issueData) {
|
|
1188
|
+
const overrideMap = getErrorMap();
|
|
1189
|
+
const issue = makeIssue({
|
|
1190
|
+
issueData,
|
|
1191
|
+
data: ctx.data,
|
|
1192
|
+
path: ctx.path,
|
|
1193
|
+
errorMaps: [
|
|
1194
|
+
ctx.common.contextualErrorMap,
|
|
1195
|
+
// contextual error map is first priority
|
|
1196
|
+
ctx.schemaErrorMap,
|
|
1197
|
+
// then schema-bound map if available
|
|
1198
|
+
overrideMap,
|
|
1199
|
+
// then global override map
|
|
1200
|
+
overrideMap === errorMap ? void 0 : errorMap
|
|
1201
|
+
// then global default map
|
|
1202
|
+
].filter((x) => !!x)
|
|
1203
|
+
});
|
|
1204
|
+
ctx.common.issues.push(issue);
|
|
1205
|
+
}
|
|
1206
|
+
var ParseStatus = class _ParseStatus {
|
|
1207
|
+
constructor() {
|
|
1208
|
+
this.value = "valid";
|
|
1209
|
+
}
|
|
1210
|
+
dirty() {
|
|
1211
|
+
if (this.value === "valid")
|
|
1212
|
+
this.value = "dirty";
|
|
1213
|
+
}
|
|
1214
|
+
abort() {
|
|
1215
|
+
if (this.value !== "aborted")
|
|
1216
|
+
this.value = "aborted";
|
|
1217
|
+
}
|
|
1218
|
+
static mergeArray(status, results) {
|
|
1219
|
+
const arrayValue = [];
|
|
1220
|
+
for (const s of results) {
|
|
1221
|
+
if (s.status === "aborted")
|
|
1222
|
+
return INVALID;
|
|
1223
|
+
if (s.status === "dirty")
|
|
1224
|
+
status.dirty();
|
|
1225
|
+
arrayValue.push(s.value);
|
|
1226
|
+
}
|
|
1227
|
+
return { status: status.value, value: arrayValue };
|
|
1228
|
+
}
|
|
1229
|
+
static async mergeObjectAsync(status, pairs) {
|
|
1230
|
+
const syncPairs = [];
|
|
1231
|
+
for (const pair of pairs) {
|
|
1232
|
+
const key = await pair.key;
|
|
1233
|
+
const value = await pair.value;
|
|
1234
|
+
syncPairs.push({
|
|
1235
|
+
key,
|
|
1236
|
+
value
|
|
1237
|
+
});
|
|
1238
|
+
}
|
|
1239
|
+
return _ParseStatus.mergeObjectSync(status, syncPairs);
|
|
1240
|
+
}
|
|
1241
|
+
static mergeObjectSync(status, pairs) {
|
|
1242
|
+
const finalObject = {};
|
|
1243
|
+
for (const pair of pairs) {
|
|
1244
|
+
const { key, value } = pair;
|
|
1245
|
+
if (key.status === "aborted")
|
|
1246
|
+
return INVALID;
|
|
1247
|
+
if (value.status === "aborted")
|
|
1248
|
+
return INVALID;
|
|
1249
|
+
if (key.status === "dirty")
|
|
1250
|
+
status.dirty();
|
|
1251
|
+
if (value.status === "dirty")
|
|
1252
|
+
status.dirty();
|
|
1253
|
+
if (key.value !== "__proto__" && (typeof value.value !== "undefined" || pair.alwaysSet)) {
|
|
1254
|
+
finalObject[key.value] = value.value;
|
|
1255
|
+
}
|
|
1256
|
+
}
|
|
1257
|
+
return { status: status.value, value: finalObject };
|
|
1258
|
+
}
|
|
1259
|
+
};
|
|
1260
|
+
var INVALID = Object.freeze({
|
|
1261
|
+
status: "aborted"
|
|
1262
|
+
});
|
|
1263
|
+
var DIRTY = (value) => ({ status: "dirty", value });
|
|
1264
|
+
var OK = (value) => ({ status: "valid", value });
|
|
1265
|
+
var isAborted = (x) => x.status === "aborted";
|
|
1266
|
+
var isDirty = (x) => x.status === "dirty";
|
|
1267
|
+
var isValid = (x) => x.status === "valid";
|
|
1268
|
+
var isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
|
|
1269
|
+
function __classPrivateFieldGet(receiver, state, kind, f) {
|
|
1270
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
1271
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
1272
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
1273
|
+
}
|
|
1274
|
+
function __classPrivateFieldSet(receiver, state, value, kind, f) {
|
|
1275
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
1276
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
1277
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
1278
|
+
return kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value), value;
|
|
1279
|
+
}
|
|
1280
|
+
var errorUtil;
|
|
1281
|
+
(function(errorUtil2) {
|
|
1282
|
+
errorUtil2.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
1283
|
+
errorUtil2.toString = (message) => typeof message === "string" ? message : message === null || message === void 0 ? void 0 : message.message;
|
|
1284
|
+
})(errorUtil || (errorUtil = {}));
|
|
1285
|
+
var _ZodEnum_cache;
|
|
1286
|
+
var _ZodNativeEnum_cache;
|
|
1287
|
+
var ParseInputLazyPath = class {
|
|
1288
|
+
constructor(parent, value, path, key) {
|
|
1289
|
+
this._cachedPath = [];
|
|
1290
|
+
this.parent = parent;
|
|
1291
|
+
this.data = value;
|
|
1292
|
+
this._path = path;
|
|
1293
|
+
this._key = key;
|
|
1294
|
+
}
|
|
1295
|
+
get path() {
|
|
1296
|
+
if (!this._cachedPath.length) {
|
|
1297
|
+
if (this._key instanceof Array) {
|
|
1298
|
+
this._cachedPath.push(...this._path, ...this._key);
|
|
1299
|
+
} else {
|
|
1300
|
+
this._cachedPath.push(...this._path, this._key);
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
return this._cachedPath;
|
|
1304
|
+
}
|
|
1305
|
+
};
|
|
1306
|
+
var handleResult = (ctx, result) => {
|
|
1307
|
+
if (isValid(result)) {
|
|
1308
|
+
return { success: true, data: result.value };
|
|
1309
|
+
} else {
|
|
1310
|
+
if (!ctx.common.issues.length) {
|
|
1311
|
+
throw new Error("Validation failed but no issues detected.");
|
|
1312
|
+
}
|
|
1313
|
+
return {
|
|
1314
|
+
success: false,
|
|
1315
|
+
get error() {
|
|
1316
|
+
if (this._error)
|
|
1317
|
+
return this._error;
|
|
1318
|
+
const error = new ZodError(ctx.common.issues);
|
|
1319
|
+
this._error = error;
|
|
1320
|
+
return this._error;
|
|
1321
|
+
}
|
|
1322
|
+
};
|
|
752
1323
|
}
|
|
753
1324
|
};
|
|
1325
|
+
function processCreateParams(params) {
|
|
1326
|
+
if (!params)
|
|
1327
|
+
return {};
|
|
1328
|
+
const { errorMap: errorMap2, invalid_type_error, required_error, description } = params;
|
|
1329
|
+
if (errorMap2 && (invalid_type_error || required_error)) {
|
|
1330
|
+
throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`);
|
|
1331
|
+
}
|
|
1332
|
+
if (errorMap2)
|
|
1333
|
+
return { errorMap: errorMap2, description };
|
|
1334
|
+
const customMap = (iss, ctx) => {
|
|
1335
|
+
var _a, _b;
|
|
1336
|
+
const { message } = params;
|
|
1337
|
+
if (iss.code === "invalid_enum_value") {
|
|
1338
|
+
return { message: message !== null && message !== void 0 ? message : ctx.defaultError };
|
|
1339
|
+
}
|
|
1340
|
+
if (typeof ctx.data === "undefined") {
|
|
1341
|
+
return { message: (_a = message !== null && message !== void 0 ? message : required_error) !== null && _a !== void 0 ? _a : ctx.defaultError };
|
|
1342
|
+
}
|
|
1343
|
+
if (iss.code !== "invalid_type")
|
|
1344
|
+
return { message: ctx.defaultError };
|
|
1345
|
+
return { message: (_b = message !== null && message !== void 0 ? message : invalid_type_error) !== null && _b !== void 0 ? _b : ctx.defaultError };
|
|
1346
|
+
};
|
|
1347
|
+
return { errorMap: customMap, description };
|
|
1348
|
+
}
|
|
1349
|
+
var ZodType = class {
|
|
1350
|
+
get description() {
|
|
1351
|
+
return this._def.description;
|
|
1352
|
+
}
|
|
1353
|
+
_getType(input) {
|
|
1354
|
+
return getParsedType(input.data);
|
|
1355
|
+
}
|
|
1356
|
+
_getOrReturnCtx(input, ctx) {
|
|
1357
|
+
return ctx || {
|
|
1358
|
+
common: input.parent.common,
|
|
1359
|
+
data: input.data,
|
|
1360
|
+
parsedType: getParsedType(input.data),
|
|
1361
|
+
schemaErrorMap: this._def.errorMap,
|
|
1362
|
+
path: input.path,
|
|
1363
|
+
parent: input.parent
|
|
1364
|
+
};
|
|
1365
|
+
}
|
|
1366
|
+
_processInputParams(input) {
|
|
1367
|
+
return {
|
|
1368
|
+
status: new ParseStatus(),
|
|
1369
|
+
ctx: {
|
|
1370
|
+
common: input.parent.common,
|
|
1371
|
+
data: input.data,
|
|
1372
|
+
parsedType: getParsedType(input.data),
|
|
1373
|
+
schemaErrorMap: this._def.errorMap,
|
|
1374
|
+
path: input.path,
|
|
1375
|
+
parent: input.parent
|
|
1376
|
+
}
|
|
1377
|
+
};
|
|
1378
|
+
}
|
|
1379
|
+
_parseSync(input) {
|
|
1380
|
+
const result = this._parse(input);
|
|
1381
|
+
if (isAsync(result)) {
|
|
1382
|
+
throw new Error("Synchronous parse encountered promise.");
|
|
1383
|
+
}
|
|
1384
|
+
return result;
|
|
1385
|
+
}
|
|
1386
|
+
_parseAsync(input) {
|
|
1387
|
+
const result = this._parse(input);
|
|
1388
|
+
return Promise.resolve(result);
|
|
1389
|
+
}
|
|
1390
|
+
parse(data, params) {
|
|
1391
|
+
const result = this.safeParse(data, params);
|
|
1392
|
+
if (result.success)
|
|
1393
|
+
return result.data;
|
|
1394
|
+
throw result.error;
|
|
1395
|
+
}
|
|
1396
|
+
safeParse(data, params) {
|
|
1397
|
+
var _a;
|
|
1398
|
+
const ctx = {
|
|
1399
|
+
common: {
|
|
1400
|
+
issues: [],
|
|
1401
|
+
async: (_a = params === null || params === void 0 ? void 0 : params.async) !== null && _a !== void 0 ? _a : false,
|
|
1402
|
+
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap
|
|
1403
|
+
},
|
|
1404
|
+
path: (params === null || params === void 0 ? void 0 : params.path) || [],
|
|
1405
|
+
schemaErrorMap: this._def.errorMap,
|
|
1406
|
+
parent: null,
|
|
1407
|
+
data,
|
|
1408
|
+
parsedType: getParsedType(data)
|
|
1409
|
+
};
|
|
1410
|
+
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
|
|
1411
|
+
return handleResult(ctx, result);
|
|
1412
|
+
}
|
|
1413
|
+
"~validate"(data) {
|
|
1414
|
+
var _a, _b;
|
|
1415
|
+
const ctx = {
|
|
1416
|
+
common: {
|
|
1417
|
+
issues: [],
|
|
1418
|
+
async: !!this["~standard"].async
|
|
1419
|
+
},
|
|
1420
|
+
path: [],
|
|
1421
|
+
schemaErrorMap: this._def.errorMap,
|
|
1422
|
+
parent: null,
|
|
1423
|
+
data,
|
|
1424
|
+
parsedType: getParsedType(data)
|
|
1425
|
+
};
|
|
1426
|
+
if (!this["~standard"].async) {
|
|
1427
|
+
try {
|
|
1428
|
+
const result = this._parseSync({ data, path: [], parent: ctx });
|
|
1429
|
+
return isValid(result) ? {
|
|
1430
|
+
value: result.value
|
|
1431
|
+
} : {
|
|
1432
|
+
issues: ctx.common.issues
|
|
1433
|
+
};
|
|
1434
|
+
} catch (err) {
|
|
1435
|
+
if ((_b = (_a = err === null || err === void 0 ? void 0 : err.message) === null || _a === void 0 ? void 0 : _a.toLowerCase()) === null || _b === void 0 ? void 0 : _b.includes("encountered")) {
|
|
1436
|
+
this["~standard"].async = true;
|
|
1437
|
+
}
|
|
1438
|
+
ctx.common = {
|
|
1439
|
+
issues: [],
|
|
1440
|
+
async: true
|
|
1441
|
+
};
|
|
1442
|
+
}
|
|
1443
|
+
}
|
|
1444
|
+
return this._parseAsync({ data, path: [], parent: ctx }).then((result) => isValid(result) ? {
|
|
1445
|
+
value: result.value
|
|
1446
|
+
} : {
|
|
1447
|
+
issues: ctx.common.issues
|
|
1448
|
+
});
|
|
1449
|
+
}
|
|
1450
|
+
async parseAsync(data, params) {
|
|
1451
|
+
const result = await this.safeParseAsync(data, params);
|
|
1452
|
+
if (result.success)
|
|
1453
|
+
return result.data;
|
|
1454
|
+
throw result.error;
|
|
1455
|
+
}
|
|
1456
|
+
async safeParseAsync(data, params) {
|
|
1457
|
+
const ctx = {
|
|
1458
|
+
common: {
|
|
1459
|
+
issues: [],
|
|
1460
|
+
contextualErrorMap: params === null || params === void 0 ? void 0 : params.errorMap,
|
|
1461
|
+
async: true
|
|
1462
|
+
},
|
|
1463
|
+
path: (params === null || params === void 0 ? void 0 : params.path) || [],
|
|
1464
|
+
schemaErrorMap: this._def.errorMap,
|
|
1465
|
+
parent: null,
|
|
1466
|
+
data,
|
|
1467
|
+
parsedType: getParsedType(data)
|
|
1468
|
+
};
|
|
1469
|
+
const maybeAsyncResult = this._parse({ data, path: ctx.path, parent: ctx });
|
|
1470
|
+
const result = await (isAsync(maybeAsyncResult) ? maybeAsyncResult : Promise.resolve(maybeAsyncResult));
|
|
1471
|
+
return handleResult(ctx, result);
|
|
1472
|
+
}
|
|
1473
|
+
refine(check, message) {
|
|
1474
|
+
const getIssueProperties = (val) => {
|
|
1475
|
+
if (typeof message === "string" || typeof message === "undefined") {
|
|
1476
|
+
return { message };
|
|
1477
|
+
} else if (typeof message === "function") {
|
|
1478
|
+
return message(val);
|
|
1479
|
+
} else {
|
|
1480
|
+
return message;
|
|
1481
|
+
}
|
|
1482
|
+
};
|
|
1483
|
+
return this._refinement((val, ctx) => {
|
|
1484
|
+
const result = check(val);
|
|
1485
|
+
const setError = () => ctx.addIssue({
|
|
1486
|
+
code: ZodIssueCode.custom,
|
|
1487
|
+
...getIssueProperties(val)
|
|
1488
|
+
});
|
|
1489
|
+
if (typeof Promise !== "undefined" && result instanceof Promise) {
|
|
1490
|
+
return result.then((data) => {
|
|
1491
|
+
if (!data) {
|
|
1492
|
+
setError();
|
|
1493
|
+
return false;
|
|
1494
|
+
} else {
|
|
1495
|
+
return true;
|
|
1496
|
+
}
|
|
1497
|
+
});
|
|
1498
|
+
}
|
|
1499
|
+
if (!result) {
|
|
1500
|
+
setError();
|
|
1501
|
+
return false;
|
|
1502
|
+
} else {
|
|
1503
|
+
return true;
|
|
1504
|
+
}
|
|
1505
|
+
});
|
|
1506
|
+
}
|
|
1507
|
+
refinement(check, refinementData) {
|
|
1508
|
+
return this._refinement((val, ctx) => {
|
|
1509
|
+
if (!check(val)) {
|
|
1510
|
+
ctx.addIssue(typeof refinementData === "function" ? refinementData(val, ctx) : refinementData);
|
|
1511
|
+
return false;
|
|
1512
|
+
} else {
|
|
1513
|
+
return true;
|
|
1514
|
+
}
|
|
1515
|
+
});
|
|
1516
|
+
}
|
|
1517
|
+
_refinement(refinement) {
|
|
1518
|
+
return new ZodEffects({
|
|
1519
|
+
schema: this,
|
|
1520
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
1521
|
+
effect: { type: "refinement", refinement }
|
|
1522
|
+
});
|
|
1523
|
+
}
|
|
1524
|
+
superRefine(refinement) {
|
|
1525
|
+
return this._refinement(refinement);
|
|
1526
|
+
}
|
|
1527
|
+
constructor(def) {
|
|
1528
|
+
this.spa = this.safeParseAsync;
|
|
1529
|
+
this._def = def;
|
|
1530
|
+
this.parse = this.parse.bind(this);
|
|
1531
|
+
this.safeParse = this.safeParse.bind(this);
|
|
1532
|
+
this.parseAsync = this.parseAsync.bind(this);
|
|
1533
|
+
this.safeParseAsync = this.safeParseAsync.bind(this);
|
|
1534
|
+
this.spa = this.spa.bind(this);
|
|
1535
|
+
this.refine = this.refine.bind(this);
|
|
1536
|
+
this.refinement = this.refinement.bind(this);
|
|
1537
|
+
this.superRefine = this.superRefine.bind(this);
|
|
1538
|
+
this.optional = this.optional.bind(this);
|
|
1539
|
+
this.nullable = this.nullable.bind(this);
|
|
1540
|
+
this.nullish = this.nullish.bind(this);
|
|
1541
|
+
this.array = this.array.bind(this);
|
|
1542
|
+
this.promise = this.promise.bind(this);
|
|
1543
|
+
this.or = this.or.bind(this);
|
|
1544
|
+
this.and = this.and.bind(this);
|
|
1545
|
+
this.transform = this.transform.bind(this);
|
|
1546
|
+
this.brand = this.brand.bind(this);
|
|
1547
|
+
this.default = this.default.bind(this);
|
|
1548
|
+
this.catch = this.catch.bind(this);
|
|
1549
|
+
this.describe = this.describe.bind(this);
|
|
1550
|
+
this.pipe = this.pipe.bind(this);
|
|
1551
|
+
this.readonly = this.readonly.bind(this);
|
|
1552
|
+
this.isNullable = this.isNullable.bind(this);
|
|
1553
|
+
this.isOptional = this.isOptional.bind(this);
|
|
1554
|
+
this["~standard"] = {
|
|
1555
|
+
version: 1,
|
|
1556
|
+
vendor: "zod",
|
|
1557
|
+
validate: (data) => this["~validate"](data)
|
|
1558
|
+
};
|
|
1559
|
+
}
|
|
1560
|
+
optional() {
|
|
1561
|
+
return ZodOptional.create(this, this._def);
|
|
1562
|
+
}
|
|
1563
|
+
nullable() {
|
|
1564
|
+
return ZodNullable.create(this, this._def);
|
|
1565
|
+
}
|
|
1566
|
+
nullish() {
|
|
1567
|
+
return this.nullable().optional();
|
|
1568
|
+
}
|
|
1569
|
+
array() {
|
|
1570
|
+
return ZodArray.create(this);
|
|
1571
|
+
}
|
|
1572
|
+
promise() {
|
|
1573
|
+
return ZodPromise.create(this, this._def);
|
|
1574
|
+
}
|
|
1575
|
+
or(option) {
|
|
1576
|
+
return ZodUnion.create([this, option], this._def);
|
|
1577
|
+
}
|
|
1578
|
+
and(incoming) {
|
|
1579
|
+
return ZodIntersection.create(this, incoming, this._def);
|
|
1580
|
+
}
|
|
1581
|
+
transform(transform) {
|
|
1582
|
+
return new ZodEffects({
|
|
1583
|
+
...processCreateParams(this._def),
|
|
1584
|
+
schema: this,
|
|
1585
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
1586
|
+
effect: { type: "transform", transform }
|
|
1587
|
+
});
|
|
1588
|
+
}
|
|
1589
|
+
default(def) {
|
|
1590
|
+
const defaultValueFunc = typeof def === "function" ? def : () => def;
|
|
1591
|
+
return new ZodDefault({
|
|
1592
|
+
...processCreateParams(this._def),
|
|
1593
|
+
innerType: this,
|
|
1594
|
+
defaultValue: defaultValueFunc,
|
|
1595
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault
|
|
1596
|
+
});
|
|
1597
|
+
}
|
|
1598
|
+
brand() {
|
|
1599
|
+
return new ZodBranded({
|
|
1600
|
+
typeName: ZodFirstPartyTypeKind.ZodBranded,
|
|
1601
|
+
type: this,
|
|
1602
|
+
...processCreateParams(this._def)
|
|
1603
|
+
});
|
|
1604
|
+
}
|
|
1605
|
+
catch(def) {
|
|
1606
|
+
const catchValueFunc = typeof def === "function" ? def : () => def;
|
|
1607
|
+
return new ZodCatch({
|
|
1608
|
+
...processCreateParams(this._def),
|
|
1609
|
+
innerType: this,
|
|
1610
|
+
catchValue: catchValueFunc,
|
|
1611
|
+
typeName: ZodFirstPartyTypeKind.ZodCatch
|
|
1612
|
+
});
|
|
1613
|
+
}
|
|
1614
|
+
describe(description) {
|
|
1615
|
+
const This = this.constructor;
|
|
1616
|
+
return new This({
|
|
1617
|
+
...this._def,
|
|
1618
|
+
description
|
|
1619
|
+
});
|
|
1620
|
+
}
|
|
1621
|
+
pipe(target) {
|
|
1622
|
+
return ZodPipeline.create(this, target);
|
|
1623
|
+
}
|
|
1624
|
+
readonly() {
|
|
1625
|
+
return ZodReadonly.create(this);
|
|
1626
|
+
}
|
|
1627
|
+
isOptional() {
|
|
1628
|
+
return this.safeParse(void 0).success;
|
|
1629
|
+
}
|
|
1630
|
+
isNullable() {
|
|
1631
|
+
return this.safeParse(null).success;
|
|
1632
|
+
}
|
|
1633
|
+
};
|
|
1634
|
+
var cuidRegex = /^c[^\s-]{8,}$/i;
|
|
1635
|
+
var cuid2Regex = /^[0-9a-z]+$/;
|
|
1636
|
+
var ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
|
|
1637
|
+
var uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
|
|
1638
|
+
var nanoidRegex = /^[a-z0-9_-]{21}$/i;
|
|
1639
|
+
var jwtRegex = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/;
|
|
1640
|
+
var durationRegex = /^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
|
|
1641
|
+
var emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
|
|
1642
|
+
var _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
1643
|
+
var emojiRegex;
|
|
1644
|
+
var ipv4Regex = /^(?:(?: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])$/;
|
|
1645
|
+
var ipv4CidrRegex = /^(?:(?: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])\/(3[0-2]|[12]?[0-9])$/;
|
|
1646
|
+
var ipv6Regex = /^(([0-9a-fA-F]{1,4}:){7,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}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;
|
|
1647
|
+
var ipv6CidrRegex = /^(([0-9a-fA-F]{1,4}:){7,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}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/;
|
|
1648
|
+
var base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
1649
|
+
var base64urlRegex = /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/;
|
|
1650
|
+
var dateRegexSource = `((\\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])))`;
|
|
1651
|
+
var dateRegex = new RegExp(`^${dateRegexSource}$`);
|
|
1652
|
+
function timeRegexSource(args) {
|
|
1653
|
+
let regex = `([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d`;
|
|
1654
|
+
if (args.precision) {
|
|
1655
|
+
regex = `${regex}\\.\\d{${args.precision}}`;
|
|
1656
|
+
} else if (args.precision == null) {
|
|
1657
|
+
regex = `${regex}(\\.\\d+)?`;
|
|
1658
|
+
}
|
|
1659
|
+
return regex;
|
|
1660
|
+
}
|
|
1661
|
+
function timeRegex(args) {
|
|
1662
|
+
return new RegExp(`^${timeRegexSource(args)}$`);
|
|
1663
|
+
}
|
|
1664
|
+
function datetimeRegex(args) {
|
|
1665
|
+
let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
|
|
1666
|
+
const opts = [];
|
|
1667
|
+
opts.push(args.local ? `Z?` : `Z`);
|
|
1668
|
+
if (args.offset)
|
|
1669
|
+
opts.push(`([+-]\\d{2}:?\\d{2})`);
|
|
1670
|
+
regex = `${regex}(${opts.join("|")})`;
|
|
1671
|
+
return new RegExp(`^${regex}$`);
|
|
1672
|
+
}
|
|
1673
|
+
function isValidIP(ip, version) {
|
|
1674
|
+
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
|
|
1675
|
+
return true;
|
|
1676
|
+
}
|
|
1677
|
+
if ((version === "v6" || !version) && ipv6Regex.test(ip)) {
|
|
1678
|
+
return true;
|
|
1679
|
+
}
|
|
1680
|
+
return false;
|
|
1681
|
+
}
|
|
1682
|
+
function isValidJWT(jwt, alg) {
|
|
1683
|
+
if (!jwtRegex.test(jwt))
|
|
1684
|
+
return false;
|
|
1685
|
+
try {
|
|
1686
|
+
const [header] = jwt.split(".");
|
|
1687
|
+
const base64 = header.replace(/-/g, "+").replace(/_/g, "/").padEnd(header.length + (4 - header.length % 4) % 4, "=");
|
|
1688
|
+
const decoded = JSON.parse(atob(base64));
|
|
1689
|
+
if (typeof decoded !== "object" || decoded === null)
|
|
1690
|
+
return false;
|
|
1691
|
+
if (!decoded.typ || !decoded.alg)
|
|
1692
|
+
return false;
|
|
1693
|
+
if (alg && decoded.alg !== alg)
|
|
1694
|
+
return false;
|
|
1695
|
+
return true;
|
|
1696
|
+
} catch (_a) {
|
|
1697
|
+
return false;
|
|
1698
|
+
}
|
|
1699
|
+
}
|
|
1700
|
+
function isValidCidr(ip, version) {
|
|
1701
|
+
if ((version === "v4" || !version) && ipv4CidrRegex.test(ip)) {
|
|
1702
|
+
return true;
|
|
1703
|
+
}
|
|
1704
|
+
if ((version === "v6" || !version) && ipv6CidrRegex.test(ip)) {
|
|
1705
|
+
return true;
|
|
1706
|
+
}
|
|
1707
|
+
return false;
|
|
1708
|
+
}
|
|
1709
|
+
var ZodString = class _ZodString extends ZodType {
|
|
1710
|
+
_parse(input) {
|
|
1711
|
+
if (this._def.coerce) {
|
|
1712
|
+
input.data = String(input.data);
|
|
1713
|
+
}
|
|
1714
|
+
const parsedType = this._getType(input);
|
|
1715
|
+
if (parsedType !== ZodParsedType.string) {
|
|
1716
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
1717
|
+
addIssueToContext(ctx2, {
|
|
1718
|
+
code: ZodIssueCode.invalid_type,
|
|
1719
|
+
expected: ZodParsedType.string,
|
|
1720
|
+
received: ctx2.parsedType
|
|
1721
|
+
});
|
|
1722
|
+
return INVALID;
|
|
1723
|
+
}
|
|
1724
|
+
const status = new ParseStatus();
|
|
1725
|
+
let ctx = void 0;
|
|
1726
|
+
for (const check of this._def.checks) {
|
|
1727
|
+
if (check.kind === "min") {
|
|
1728
|
+
if (input.data.length < check.value) {
|
|
1729
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1730
|
+
addIssueToContext(ctx, {
|
|
1731
|
+
code: ZodIssueCode.too_small,
|
|
1732
|
+
minimum: check.value,
|
|
1733
|
+
type: "string",
|
|
1734
|
+
inclusive: true,
|
|
1735
|
+
exact: false,
|
|
1736
|
+
message: check.message
|
|
1737
|
+
});
|
|
1738
|
+
status.dirty();
|
|
1739
|
+
}
|
|
1740
|
+
} else if (check.kind === "max") {
|
|
1741
|
+
if (input.data.length > check.value) {
|
|
1742
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1743
|
+
addIssueToContext(ctx, {
|
|
1744
|
+
code: ZodIssueCode.too_big,
|
|
1745
|
+
maximum: check.value,
|
|
1746
|
+
type: "string",
|
|
1747
|
+
inclusive: true,
|
|
1748
|
+
exact: false,
|
|
1749
|
+
message: check.message
|
|
1750
|
+
});
|
|
1751
|
+
status.dirty();
|
|
1752
|
+
}
|
|
1753
|
+
} else if (check.kind === "length") {
|
|
1754
|
+
const tooBig = input.data.length > check.value;
|
|
1755
|
+
const tooSmall = input.data.length < check.value;
|
|
1756
|
+
if (tooBig || tooSmall) {
|
|
1757
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1758
|
+
if (tooBig) {
|
|
1759
|
+
addIssueToContext(ctx, {
|
|
1760
|
+
code: ZodIssueCode.too_big,
|
|
1761
|
+
maximum: check.value,
|
|
1762
|
+
type: "string",
|
|
1763
|
+
inclusive: true,
|
|
1764
|
+
exact: true,
|
|
1765
|
+
message: check.message
|
|
1766
|
+
});
|
|
1767
|
+
} else if (tooSmall) {
|
|
1768
|
+
addIssueToContext(ctx, {
|
|
1769
|
+
code: ZodIssueCode.too_small,
|
|
1770
|
+
minimum: check.value,
|
|
1771
|
+
type: "string",
|
|
1772
|
+
inclusive: true,
|
|
1773
|
+
exact: true,
|
|
1774
|
+
message: check.message
|
|
1775
|
+
});
|
|
1776
|
+
}
|
|
1777
|
+
status.dirty();
|
|
1778
|
+
}
|
|
1779
|
+
} else if (check.kind === "email") {
|
|
1780
|
+
if (!emailRegex.test(input.data)) {
|
|
1781
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1782
|
+
addIssueToContext(ctx, {
|
|
1783
|
+
validation: "email",
|
|
1784
|
+
code: ZodIssueCode.invalid_string,
|
|
1785
|
+
message: check.message
|
|
1786
|
+
});
|
|
1787
|
+
status.dirty();
|
|
1788
|
+
}
|
|
1789
|
+
} else if (check.kind === "emoji") {
|
|
1790
|
+
if (!emojiRegex) {
|
|
1791
|
+
emojiRegex = new RegExp(_emojiRegex, "u");
|
|
1792
|
+
}
|
|
1793
|
+
if (!emojiRegex.test(input.data)) {
|
|
1794
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1795
|
+
addIssueToContext(ctx, {
|
|
1796
|
+
validation: "emoji",
|
|
1797
|
+
code: ZodIssueCode.invalid_string,
|
|
1798
|
+
message: check.message
|
|
1799
|
+
});
|
|
1800
|
+
status.dirty();
|
|
1801
|
+
}
|
|
1802
|
+
} else if (check.kind === "uuid") {
|
|
1803
|
+
if (!uuidRegex.test(input.data)) {
|
|
1804
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1805
|
+
addIssueToContext(ctx, {
|
|
1806
|
+
validation: "uuid",
|
|
1807
|
+
code: ZodIssueCode.invalid_string,
|
|
1808
|
+
message: check.message
|
|
1809
|
+
});
|
|
1810
|
+
status.dirty();
|
|
1811
|
+
}
|
|
1812
|
+
} else if (check.kind === "nanoid") {
|
|
1813
|
+
if (!nanoidRegex.test(input.data)) {
|
|
1814
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1815
|
+
addIssueToContext(ctx, {
|
|
1816
|
+
validation: "nanoid",
|
|
1817
|
+
code: ZodIssueCode.invalid_string,
|
|
1818
|
+
message: check.message
|
|
1819
|
+
});
|
|
1820
|
+
status.dirty();
|
|
1821
|
+
}
|
|
1822
|
+
} else if (check.kind === "cuid") {
|
|
1823
|
+
if (!cuidRegex.test(input.data)) {
|
|
1824
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1825
|
+
addIssueToContext(ctx, {
|
|
1826
|
+
validation: "cuid",
|
|
1827
|
+
code: ZodIssueCode.invalid_string,
|
|
1828
|
+
message: check.message
|
|
1829
|
+
});
|
|
1830
|
+
status.dirty();
|
|
1831
|
+
}
|
|
1832
|
+
} else if (check.kind === "cuid2") {
|
|
1833
|
+
if (!cuid2Regex.test(input.data)) {
|
|
1834
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1835
|
+
addIssueToContext(ctx, {
|
|
1836
|
+
validation: "cuid2",
|
|
1837
|
+
code: ZodIssueCode.invalid_string,
|
|
1838
|
+
message: check.message
|
|
1839
|
+
});
|
|
1840
|
+
status.dirty();
|
|
1841
|
+
}
|
|
1842
|
+
} else if (check.kind === "ulid") {
|
|
1843
|
+
if (!ulidRegex.test(input.data)) {
|
|
1844
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1845
|
+
addIssueToContext(ctx, {
|
|
1846
|
+
validation: "ulid",
|
|
1847
|
+
code: ZodIssueCode.invalid_string,
|
|
1848
|
+
message: check.message
|
|
1849
|
+
});
|
|
1850
|
+
status.dirty();
|
|
1851
|
+
}
|
|
1852
|
+
} else if (check.kind === "url") {
|
|
1853
|
+
try {
|
|
1854
|
+
new URL(input.data);
|
|
1855
|
+
} catch (_a) {
|
|
1856
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1857
|
+
addIssueToContext(ctx, {
|
|
1858
|
+
validation: "url",
|
|
1859
|
+
code: ZodIssueCode.invalid_string,
|
|
1860
|
+
message: check.message
|
|
1861
|
+
});
|
|
1862
|
+
status.dirty();
|
|
1863
|
+
}
|
|
1864
|
+
} else if (check.kind === "regex") {
|
|
1865
|
+
check.regex.lastIndex = 0;
|
|
1866
|
+
const testResult = check.regex.test(input.data);
|
|
1867
|
+
if (!testResult) {
|
|
1868
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1869
|
+
addIssueToContext(ctx, {
|
|
1870
|
+
validation: "regex",
|
|
1871
|
+
code: ZodIssueCode.invalid_string,
|
|
1872
|
+
message: check.message
|
|
1873
|
+
});
|
|
1874
|
+
status.dirty();
|
|
1875
|
+
}
|
|
1876
|
+
} else if (check.kind === "trim") {
|
|
1877
|
+
input.data = input.data.trim();
|
|
1878
|
+
} else if (check.kind === "includes") {
|
|
1879
|
+
if (!input.data.includes(check.value, check.position)) {
|
|
1880
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1881
|
+
addIssueToContext(ctx, {
|
|
1882
|
+
code: ZodIssueCode.invalid_string,
|
|
1883
|
+
validation: { includes: check.value, position: check.position },
|
|
1884
|
+
message: check.message
|
|
1885
|
+
});
|
|
1886
|
+
status.dirty();
|
|
1887
|
+
}
|
|
1888
|
+
} else if (check.kind === "toLowerCase") {
|
|
1889
|
+
input.data = input.data.toLowerCase();
|
|
1890
|
+
} else if (check.kind === "toUpperCase") {
|
|
1891
|
+
input.data = input.data.toUpperCase();
|
|
1892
|
+
} else if (check.kind === "startsWith") {
|
|
1893
|
+
if (!input.data.startsWith(check.value)) {
|
|
1894
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1895
|
+
addIssueToContext(ctx, {
|
|
1896
|
+
code: ZodIssueCode.invalid_string,
|
|
1897
|
+
validation: { startsWith: check.value },
|
|
1898
|
+
message: check.message
|
|
1899
|
+
});
|
|
1900
|
+
status.dirty();
|
|
1901
|
+
}
|
|
1902
|
+
} else if (check.kind === "endsWith") {
|
|
1903
|
+
if (!input.data.endsWith(check.value)) {
|
|
1904
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1905
|
+
addIssueToContext(ctx, {
|
|
1906
|
+
code: ZodIssueCode.invalid_string,
|
|
1907
|
+
validation: { endsWith: check.value },
|
|
1908
|
+
message: check.message
|
|
1909
|
+
});
|
|
1910
|
+
status.dirty();
|
|
1911
|
+
}
|
|
1912
|
+
} else if (check.kind === "datetime") {
|
|
1913
|
+
const regex = datetimeRegex(check);
|
|
1914
|
+
if (!regex.test(input.data)) {
|
|
1915
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1916
|
+
addIssueToContext(ctx, {
|
|
1917
|
+
code: ZodIssueCode.invalid_string,
|
|
1918
|
+
validation: "datetime",
|
|
1919
|
+
message: check.message
|
|
1920
|
+
});
|
|
1921
|
+
status.dirty();
|
|
1922
|
+
}
|
|
1923
|
+
} else if (check.kind === "date") {
|
|
1924
|
+
const regex = dateRegex;
|
|
1925
|
+
if (!regex.test(input.data)) {
|
|
1926
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1927
|
+
addIssueToContext(ctx, {
|
|
1928
|
+
code: ZodIssueCode.invalid_string,
|
|
1929
|
+
validation: "date",
|
|
1930
|
+
message: check.message
|
|
1931
|
+
});
|
|
1932
|
+
status.dirty();
|
|
1933
|
+
}
|
|
1934
|
+
} else if (check.kind === "time") {
|
|
1935
|
+
const regex = timeRegex(check);
|
|
1936
|
+
if (!regex.test(input.data)) {
|
|
1937
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1938
|
+
addIssueToContext(ctx, {
|
|
1939
|
+
code: ZodIssueCode.invalid_string,
|
|
1940
|
+
validation: "time",
|
|
1941
|
+
message: check.message
|
|
1942
|
+
});
|
|
1943
|
+
status.dirty();
|
|
1944
|
+
}
|
|
1945
|
+
} else if (check.kind === "duration") {
|
|
1946
|
+
if (!durationRegex.test(input.data)) {
|
|
1947
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1948
|
+
addIssueToContext(ctx, {
|
|
1949
|
+
validation: "duration",
|
|
1950
|
+
code: ZodIssueCode.invalid_string,
|
|
1951
|
+
message: check.message
|
|
1952
|
+
});
|
|
1953
|
+
status.dirty();
|
|
1954
|
+
}
|
|
1955
|
+
} else if (check.kind === "ip") {
|
|
1956
|
+
if (!isValidIP(input.data, check.version)) {
|
|
1957
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1958
|
+
addIssueToContext(ctx, {
|
|
1959
|
+
validation: "ip",
|
|
1960
|
+
code: ZodIssueCode.invalid_string,
|
|
1961
|
+
message: check.message
|
|
1962
|
+
});
|
|
1963
|
+
status.dirty();
|
|
1964
|
+
}
|
|
1965
|
+
} else if (check.kind === "jwt") {
|
|
1966
|
+
if (!isValidJWT(input.data, check.alg)) {
|
|
1967
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1968
|
+
addIssueToContext(ctx, {
|
|
1969
|
+
validation: "jwt",
|
|
1970
|
+
code: ZodIssueCode.invalid_string,
|
|
1971
|
+
message: check.message
|
|
1972
|
+
});
|
|
1973
|
+
status.dirty();
|
|
1974
|
+
}
|
|
1975
|
+
} else if (check.kind === "cidr") {
|
|
1976
|
+
if (!isValidCidr(input.data, check.version)) {
|
|
1977
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1978
|
+
addIssueToContext(ctx, {
|
|
1979
|
+
validation: "cidr",
|
|
1980
|
+
code: ZodIssueCode.invalid_string,
|
|
1981
|
+
message: check.message
|
|
1982
|
+
});
|
|
1983
|
+
status.dirty();
|
|
1984
|
+
}
|
|
1985
|
+
} else if (check.kind === "base64") {
|
|
1986
|
+
if (!base64Regex.test(input.data)) {
|
|
1987
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1988
|
+
addIssueToContext(ctx, {
|
|
1989
|
+
validation: "base64",
|
|
1990
|
+
code: ZodIssueCode.invalid_string,
|
|
1991
|
+
message: check.message
|
|
1992
|
+
});
|
|
1993
|
+
status.dirty();
|
|
1994
|
+
}
|
|
1995
|
+
} else if (check.kind === "base64url") {
|
|
1996
|
+
if (!base64urlRegex.test(input.data)) {
|
|
1997
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1998
|
+
addIssueToContext(ctx, {
|
|
1999
|
+
validation: "base64url",
|
|
2000
|
+
code: ZodIssueCode.invalid_string,
|
|
2001
|
+
message: check.message
|
|
2002
|
+
});
|
|
2003
|
+
status.dirty();
|
|
2004
|
+
}
|
|
2005
|
+
} else {
|
|
2006
|
+
util.assertNever(check);
|
|
2007
|
+
}
|
|
2008
|
+
}
|
|
2009
|
+
return { status: status.value, value: input.data };
|
|
2010
|
+
}
|
|
2011
|
+
_regex(regex, validation, message) {
|
|
2012
|
+
return this.refinement((data) => regex.test(data), {
|
|
2013
|
+
validation,
|
|
2014
|
+
code: ZodIssueCode.invalid_string,
|
|
2015
|
+
...errorUtil.errToObj(message)
|
|
2016
|
+
});
|
|
2017
|
+
}
|
|
2018
|
+
_addCheck(check) {
|
|
2019
|
+
return new _ZodString({
|
|
2020
|
+
...this._def,
|
|
2021
|
+
checks: [...this._def.checks, check]
|
|
2022
|
+
});
|
|
2023
|
+
}
|
|
2024
|
+
email(message) {
|
|
2025
|
+
return this._addCheck({ kind: "email", ...errorUtil.errToObj(message) });
|
|
2026
|
+
}
|
|
2027
|
+
url(message) {
|
|
2028
|
+
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
|
|
2029
|
+
}
|
|
2030
|
+
emoji(message) {
|
|
2031
|
+
return this._addCheck({ kind: "emoji", ...errorUtil.errToObj(message) });
|
|
2032
|
+
}
|
|
2033
|
+
uuid(message) {
|
|
2034
|
+
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
|
|
2035
|
+
}
|
|
2036
|
+
nanoid(message) {
|
|
2037
|
+
return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) });
|
|
2038
|
+
}
|
|
2039
|
+
cuid(message) {
|
|
2040
|
+
return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
|
|
2041
|
+
}
|
|
2042
|
+
cuid2(message) {
|
|
2043
|
+
return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
|
|
2044
|
+
}
|
|
2045
|
+
ulid(message) {
|
|
2046
|
+
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
|
|
2047
|
+
}
|
|
2048
|
+
base64(message) {
|
|
2049
|
+
return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
|
|
2050
|
+
}
|
|
2051
|
+
base64url(message) {
|
|
2052
|
+
return this._addCheck({
|
|
2053
|
+
kind: "base64url",
|
|
2054
|
+
...errorUtil.errToObj(message)
|
|
2055
|
+
});
|
|
2056
|
+
}
|
|
2057
|
+
jwt(options) {
|
|
2058
|
+
return this._addCheck({ kind: "jwt", ...errorUtil.errToObj(options) });
|
|
2059
|
+
}
|
|
2060
|
+
ip(options) {
|
|
2061
|
+
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
|
|
2062
|
+
}
|
|
2063
|
+
cidr(options) {
|
|
2064
|
+
return this._addCheck({ kind: "cidr", ...errorUtil.errToObj(options) });
|
|
2065
|
+
}
|
|
2066
|
+
datetime(options) {
|
|
2067
|
+
var _a, _b;
|
|
2068
|
+
if (typeof options === "string") {
|
|
2069
|
+
return this._addCheck({
|
|
2070
|
+
kind: "datetime",
|
|
2071
|
+
precision: null,
|
|
2072
|
+
offset: false,
|
|
2073
|
+
local: false,
|
|
2074
|
+
message: options
|
|
2075
|
+
});
|
|
2076
|
+
}
|
|
2077
|
+
return this._addCheck({
|
|
2078
|
+
kind: "datetime",
|
|
2079
|
+
precision: typeof (options === null || options === void 0 ? void 0 : options.precision) === "undefined" ? null : options === null || options === void 0 ? void 0 : options.precision,
|
|
2080
|
+
offset: (_a = options === null || options === void 0 ? void 0 : options.offset) !== null && _a !== void 0 ? _a : false,
|
|
2081
|
+
local: (_b = options === null || options === void 0 ? void 0 : options.local) !== null && _b !== void 0 ? _b : false,
|
|
2082
|
+
...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message)
|
|
2083
|
+
});
|
|
2084
|
+
}
|
|
2085
|
+
date(message) {
|
|
2086
|
+
return this._addCheck({ kind: "date", message });
|
|
2087
|
+
}
|
|
2088
|
+
time(options) {
|
|
2089
|
+
if (typeof options === "string") {
|
|
2090
|
+
return this._addCheck({
|
|
2091
|
+
kind: "time",
|
|
2092
|
+
precision: null,
|
|
2093
|
+
message: options
|
|
2094
|
+
});
|
|
2095
|
+
}
|
|
2096
|
+
return this._addCheck({
|
|
2097
|
+
kind: "time",
|
|
2098
|
+
precision: typeof (options === null || options === void 0 ? void 0 : options.precision) === "undefined" ? null : options === null || options === void 0 ? void 0 : options.precision,
|
|
2099
|
+
...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message)
|
|
2100
|
+
});
|
|
2101
|
+
}
|
|
2102
|
+
duration(message) {
|
|
2103
|
+
return this._addCheck({ kind: "duration", ...errorUtil.errToObj(message) });
|
|
2104
|
+
}
|
|
2105
|
+
regex(regex, message) {
|
|
2106
|
+
return this._addCheck({
|
|
2107
|
+
kind: "regex",
|
|
2108
|
+
regex,
|
|
2109
|
+
...errorUtil.errToObj(message)
|
|
2110
|
+
});
|
|
2111
|
+
}
|
|
2112
|
+
includes(value, options) {
|
|
2113
|
+
return this._addCheck({
|
|
2114
|
+
kind: "includes",
|
|
2115
|
+
value,
|
|
2116
|
+
position: options === null || options === void 0 ? void 0 : options.position,
|
|
2117
|
+
...errorUtil.errToObj(options === null || options === void 0 ? void 0 : options.message)
|
|
2118
|
+
});
|
|
2119
|
+
}
|
|
2120
|
+
startsWith(value, message) {
|
|
2121
|
+
return this._addCheck({
|
|
2122
|
+
kind: "startsWith",
|
|
2123
|
+
value,
|
|
2124
|
+
...errorUtil.errToObj(message)
|
|
2125
|
+
});
|
|
2126
|
+
}
|
|
2127
|
+
endsWith(value, message) {
|
|
2128
|
+
return this._addCheck({
|
|
2129
|
+
kind: "endsWith",
|
|
2130
|
+
value,
|
|
2131
|
+
...errorUtil.errToObj(message)
|
|
2132
|
+
});
|
|
2133
|
+
}
|
|
2134
|
+
min(minLength, message) {
|
|
2135
|
+
return this._addCheck({
|
|
2136
|
+
kind: "min",
|
|
2137
|
+
value: minLength,
|
|
2138
|
+
...errorUtil.errToObj(message)
|
|
2139
|
+
});
|
|
2140
|
+
}
|
|
2141
|
+
max(maxLength, message) {
|
|
2142
|
+
return this._addCheck({
|
|
2143
|
+
kind: "max",
|
|
2144
|
+
value: maxLength,
|
|
2145
|
+
...errorUtil.errToObj(message)
|
|
2146
|
+
});
|
|
2147
|
+
}
|
|
2148
|
+
length(len, message) {
|
|
2149
|
+
return this._addCheck({
|
|
2150
|
+
kind: "length",
|
|
2151
|
+
value: len,
|
|
2152
|
+
...errorUtil.errToObj(message)
|
|
2153
|
+
});
|
|
2154
|
+
}
|
|
2155
|
+
/**
|
|
2156
|
+
* Equivalent to `.min(1)`
|
|
2157
|
+
*/
|
|
2158
|
+
nonempty(message) {
|
|
2159
|
+
return this.min(1, errorUtil.errToObj(message));
|
|
2160
|
+
}
|
|
2161
|
+
trim() {
|
|
2162
|
+
return new _ZodString({
|
|
2163
|
+
...this._def,
|
|
2164
|
+
checks: [...this._def.checks, { kind: "trim" }]
|
|
2165
|
+
});
|
|
2166
|
+
}
|
|
2167
|
+
toLowerCase() {
|
|
2168
|
+
return new _ZodString({
|
|
2169
|
+
...this._def,
|
|
2170
|
+
checks: [...this._def.checks, { kind: "toLowerCase" }]
|
|
2171
|
+
});
|
|
2172
|
+
}
|
|
2173
|
+
toUpperCase() {
|
|
2174
|
+
return new _ZodString({
|
|
2175
|
+
...this._def,
|
|
2176
|
+
checks: [...this._def.checks, { kind: "toUpperCase" }]
|
|
2177
|
+
});
|
|
2178
|
+
}
|
|
2179
|
+
get isDatetime() {
|
|
2180
|
+
return !!this._def.checks.find((ch) => ch.kind === "datetime");
|
|
2181
|
+
}
|
|
2182
|
+
get isDate() {
|
|
2183
|
+
return !!this._def.checks.find((ch) => ch.kind === "date");
|
|
2184
|
+
}
|
|
2185
|
+
get isTime() {
|
|
2186
|
+
return !!this._def.checks.find((ch) => ch.kind === "time");
|
|
2187
|
+
}
|
|
2188
|
+
get isDuration() {
|
|
2189
|
+
return !!this._def.checks.find((ch) => ch.kind === "duration");
|
|
2190
|
+
}
|
|
2191
|
+
get isEmail() {
|
|
2192
|
+
return !!this._def.checks.find((ch) => ch.kind === "email");
|
|
2193
|
+
}
|
|
2194
|
+
get isURL() {
|
|
2195
|
+
return !!this._def.checks.find((ch) => ch.kind === "url");
|
|
2196
|
+
}
|
|
2197
|
+
get isEmoji() {
|
|
2198
|
+
return !!this._def.checks.find((ch) => ch.kind === "emoji");
|
|
2199
|
+
}
|
|
2200
|
+
get isUUID() {
|
|
2201
|
+
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
2202
|
+
}
|
|
2203
|
+
get isNANOID() {
|
|
2204
|
+
return !!this._def.checks.find((ch) => ch.kind === "nanoid");
|
|
2205
|
+
}
|
|
2206
|
+
get isCUID() {
|
|
2207
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
2208
|
+
}
|
|
2209
|
+
get isCUID2() {
|
|
2210
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid2");
|
|
2211
|
+
}
|
|
2212
|
+
get isULID() {
|
|
2213
|
+
return !!this._def.checks.find((ch) => ch.kind === "ulid");
|
|
2214
|
+
}
|
|
2215
|
+
get isIP() {
|
|
2216
|
+
return !!this._def.checks.find((ch) => ch.kind === "ip");
|
|
2217
|
+
}
|
|
2218
|
+
get isCIDR() {
|
|
2219
|
+
return !!this._def.checks.find((ch) => ch.kind === "cidr");
|
|
2220
|
+
}
|
|
2221
|
+
get isBase64() {
|
|
2222
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64");
|
|
2223
|
+
}
|
|
2224
|
+
get isBase64url() {
|
|
2225
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64url");
|
|
2226
|
+
}
|
|
2227
|
+
get minLength() {
|
|
2228
|
+
let min = null;
|
|
2229
|
+
for (const ch of this._def.checks) {
|
|
2230
|
+
if (ch.kind === "min") {
|
|
2231
|
+
if (min === null || ch.value > min)
|
|
2232
|
+
min = ch.value;
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
2235
|
+
return min;
|
|
2236
|
+
}
|
|
2237
|
+
get maxLength() {
|
|
2238
|
+
let max = null;
|
|
2239
|
+
for (const ch of this._def.checks) {
|
|
2240
|
+
if (ch.kind === "max") {
|
|
2241
|
+
if (max === null || ch.value < max)
|
|
2242
|
+
max = ch.value;
|
|
2243
|
+
}
|
|
2244
|
+
}
|
|
2245
|
+
return max;
|
|
2246
|
+
}
|
|
2247
|
+
};
|
|
2248
|
+
ZodString.create = (params) => {
|
|
2249
|
+
var _a;
|
|
2250
|
+
return new ZodString({
|
|
2251
|
+
checks: [],
|
|
2252
|
+
typeName: ZodFirstPartyTypeKind.ZodString,
|
|
2253
|
+
coerce: (_a = params === null || params === void 0 ? void 0 : params.coerce) !== null && _a !== void 0 ? _a : false,
|
|
2254
|
+
...processCreateParams(params)
|
|
2255
|
+
});
|
|
2256
|
+
};
|
|
2257
|
+
function floatSafeRemainder(val, step) {
|
|
2258
|
+
const valDecCount = (val.toString().split(".")[1] || "").length;
|
|
2259
|
+
const stepDecCount = (step.toString().split(".")[1] || "").length;
|
|
2260
|
+
const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
|
|
2261
|
+
const valInt = parseInt(val.toFixed(decCount).replace(".", ""));
|
|
2262
|
+
const stepInt = parseInt(step.toFixed(decCount).replace(".", ""));
|
|
2263
|
+
return valInt % stepInt / Math.pow(10, decCount);
|
|
2264
|
+
}
|
|
2265
|
+
var ZodNumber = class _ZodNumber extends ZodType {
|
|
2266
|
+
constructor() {
|
|
2267
|
+
super(...arguments);
|
|
2268
|
+
this.min = this.gte;
|
|
2269
|
+
this.max = this.lte;
|
|
2270
|
+
this.step = this.multipleOf;
|
|
2271
|
+
}
|
|
2272
|
+
_parse(input) {
|
|
2273
|
+
if (this._def.coerce) {
|
|
2274
|
+
input.data = Number(input.data);
|
|
2275
|
+
}
|
|
2276
|
+
const parsedType = this._getType(input);
|
|
2277
|
+
if (parsedType !== ZodParsedType.number) {
|
|
2278
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
2279
|
+
addIssueToContext(ctx2, {
|
|
2280
|
+
code: ZodIssueCode.invalid_type,
|
|
2281
|
+
expected: ZodParsedType.number,
|
|
2282
|
+
received: ctx2.parsedType
|
|
2283
|
+
});
|
|
2284
|
+
return INVALID;
|
|
2285
|
+
}
|
|
2286
|
+
let ctx = void 0;
|
|
2287
|
+
const status = new ParseStatus();
|
|
2288
|
+
for (const check of this._def.checks) {
|
|
2289
|
+
if (check.kind === "int") {
|
|
2290
|
+
if (!util.isInteger(input.data)) {
|
|
2291
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2292
|
+
addIssueToContext(ctx, {
|
|
2293
|
+
code: ZodIssueCode.invalid_type,
|
|
2294
|
+
expected: "integer",
|
|
2295
|
+
received: "float",
|
|
2296
|
+
message: check.message
|
|
2297
|
+
});
|
|
2298
|
+
status.dirty();
|
|
2299
|
+
}
|
|
2300
|
+
} else if (check.kind === "min") {
|
|
2301
|
+
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
2302
|
+
if (tooSmall) {
|
|
2303
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2304
|
+
addIssueToContext(ctx, {
|
|
2305
|
+
code: ZodIssueCode.too_small,
|
|
2306
|
+
minimum: check.value,
|
|
2307
|
+
type: "number",
|
|
2308
|
+
inclusive: check.inclusive,
|
|
2309
|
+
exact: false,
|
|
2310
|
+
message: check.message
|
|
2311
|
+
});
|
|
2312
|
+
status.dirty();
|
|
2313
|
+
}
|
|
2314
|
+
} else if (check.kind === "max") {
|
|
2315
|
+
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
2316
|
+
if (tooBig) {
|
|
2317
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2318
|
+
addIssueToContext(ctx, {
|
|
2319
|
+
code: ZodIssueCode.too_big,
|
|
2320
|
+
maximum: check.value,
|
|
2321
|
+
type: "number",
|
|
2322
|
+
inclusive: check.inclusive,
|
|
2323
|
+
exact: false,
|
|
2324
|
+
message: check.message
|
|
2325
|
+
});
|
|
2326
|
+
status.dirty();
|
|
2327
|
+
}
|
|
2328
|
+
} else if (check.kind === "multipleOf") {
|
|
2329
|
+
if (floatSafeRemainder(input.data, check.value) !== 0) {
|
|
2330
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2331
|
+
addIssueToContext(ctx, {
|
|
2332
|
+
code: ZodIssueCode.not_multiple_of,
|
|
2333
|
+
multipleOf: check.value,
|
|
2334
|
+
message: check.message
|
|
2335
|
+
});
|
|
2336
|
+
status.dirty();
|
|
2337
|
+
}
|
|
2338
|
+
} else if (check.kind === "finite") {
|
|
2339
|
+
if (!Number.isFinite(input.data)) {
|
|
2340
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2341
|
+
addIssueToContext(ctx, {
|
|
2342
|
+
code: ZodIssueCode.not_finite,
|
|
2343
|
+
message: check.message
|
|
2344
|
+
});
|
|
2345
|
+
status.dirty();
|
|
2346
|
+
}
|
|
2347
|
+
} else {
|
|
2348
|
+
util.assertNever(check);
|
|
2349
|
+
}
|
|
2350
|
+
}
|
|
2351
|
+
return { status: status.value, value: input.data };
|
|
2352
|
+
}
|
|
2353
|
+
gte(value, message) {
|
|
2354
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
2355
|
+
}
|
|
2356
|
+
gt(value, message) {
|
|
2357
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
2358
|
+
}
|
|
2359
|
+
lte(value, message) {
|
|
2360
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
2361
|
+
}
|
|
2362
|
+
lt(value, message) {
|
|
2363
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
2364
|
+
}
|
|
2365
|
+
setLimit(kind, value, inclusive, message) {
|
|
2366
|
+
return new _ZodNumber({
|
|
2367
|
+
...this._def,
|
|
2368
|
+
checks: [
|
|
2369
|
+
...this._def.checks,
|
|
2370
|
+
{
|
|
2371
|
+
kind,
|
|
2372
|
+
value,
|
|
2373
|
+
inclusive,
|
|
2374
|
+
message: errorUtil.toString(message)
|
|
2375
|
+
}
|
|
2376
|
+
]
|
|
2377
|
+
});
|
|
2378
|
+
}
|
|
2379
|
+
_addCheck(check) {
|
|
2380
|
+
return new _ZodNumber({
|
|
2381
|
+
...this._def,
|
|
2382
|
+
checks: [...this._def.checks, check]
|
|
2383
|
+
});
|
|
2384
|
+
}
|
|
2385
|
+
int(message) {
|
|
2386
|
+
return this._addCheck({
|
|
2387
|
+
kind: "int",
|
|
2388
|
+
message: errorUtil.toString(message)
|
|
2389
|
+
});
|
|
2390
|
+
}
|
|
2391
|
+
positive(message) {
|
|
2392
|
+
return this._addCheck({
|
|
2393
|
+
kind: "min",
|
|
2394
|
+
value: 0,
|
|
2395
|
+
inclusive: false,
|
|
2396
|
+
message: errorUtil.toString(message)
|
|
2397
|
+
});
|
|
2398
|
+
}
|
|
2399
|
+
negative(message) {
|
|
2400
|
+
return this._addCheck({
|
|
2401
|
+
kind: "max",
|
|
2402
|
+
value: 0,
|
|
2403
|
+
inclusive: false,
|
|
2404
|
+
message: errorUtil.toString(message)
|
|
2405
|
+
});
|
|
2406
|
+
}
|
|
2407
|
+
nonpositive(message) {
|
|
2408
|
+
return this._addCheck({
|
|
2409
|
+
kind: "max",
|
|
2410
|
+
value: 0,
|
|
2411
|
+
inclusive: true,
|
|
2412
|
+
message: errorUtil.toString(message)
|
|
2413
|
+
});
|
|
2414
|
+
}
|
|
2415
|
+
nonnegative(message) {
|
|
2416
|
+
return this._addCheck({
|
|
2417
|
+
kind: "min",
|
|
2418
|
+
value: 0,
|
|
2419
|
+
inclusive: true,
|
|
2420
|
+
message: errorUtil.toString(message)
|
|
2421
|
+
});
|
|
2422
|
+
}
|
|
2423
|
+
multipleOf(value, message) {
|
|
2424
|
+
return this._addCheck({
|
|
2425
|
+
kind: "multipleOf",
|
|
2426
|
+
value,
|
|
2427
|
+
message: errorUtil.toString(message)
|
|
2428
|
+
});
|
|
2429
|
+
}
|
|
2430
|
+
finite(message) {
|
|
2431
|
+
return this._addCheck({
|
|
2432
|
+
kind: "finite",
|
|
2433
|
+
message: errorUtil.toString(message)
|
|
2434
|
+
});
|
|
2435
|
+
}
|
|
2436
|
+
safe(message) {
|
|
2437
|
+
return this._addCheck({
|
|
2438
|
+
kind: "min",
|
|
2439
|
+
inclusive: true,
|
|
2440
|
+
value: Number.MIN_SAFE_INTEGER,
|
|
2441
|
+
message: errorUtil.toString(message)
|
|
2442
|
+
})._addCheck({
|
|
2443
|
+
kind: "max",
|
|
2444
|
+
inclusive: true,
|
|
2445
|
+
value: Number.MAX_SAFE_INTEGER,
|
|
2446
|
+
message: errorUtil.toString(message)
|
|
2447
|
+
});
|
|
2448
|
+
}
|
|
2449
|
+
get minValue() {
|
|
2450
|
+
let min = null;
|
|
2451
|
+
for (const ch of this._def.checks) {
|
|
2452
|
+
if (ch.kind === "min") {
|
|
2453
|
+
if (min === null || ch.value > min)
|
|
2454
|
+
min = ch.value;
|
|
2455
|
+
}
|
|
2456
|
+
}
|
|
2457
|
+
return min;
|
|
2458
|
+
}
|
|
2459
|
+
get maxValue() {
|
|
2460
|
+
let max = null;
|
|
2461
|
+
for (const ch of this._def.checks) {
|
|
2462
|
+
if (ch.kind === "max") {
|
|
2463
|
+
if (max === null || ch.value < max)
|
|
2464
|
+
max = ch.value;
|
|
2465
|
+
}
|
|
2466
|
+
}
|
|
2467
|
+
return max;
|
|
2468
|
+
}
|
|
2469
|
+
get isInt() {
|
|
2470
|
+
return !!this._def.checks.find((ch) => ch.kind === "int" || ch.kind === "multipleOf" && util.isInteger(ch.value));
|
|
2471
|
+
}
|
|
2472
|
+
get isFinite() {
|
|
2473
|
+
let max = null, min = null;
|
|
2474
|
+
for (const ch of this._def.checks) {
|
|
2475
|
+
if (ch.kind === "finite" || ch.kind === "int" || ch.kind === "multipleOf") {
|
|
2476
|
+
return true;
|
|
2477
|
+
} else if (ch.kind === "min") {
|
|
2478
|
+
if (min === null || ch.value > min)
|
|
2479
|
+
min = ch.value;
|
|
2480
|
+
} else if (ch.kind === "max") {
|
|
2481
|
+
if (max === null || ch.value < max)
|
|
2482
|
+
max = ch.value;
|
|
2483
|
+
}
|
|
2484
|
+
}
|
|
2485
|
+
return Number.isFinite(min) && Number.isFinite(max);
|
|
2486
|
+
}
|
|
2487
|
+
};
|
|
2488
|
+
ZodNumber.create = (params) => {
|
|
2489
|
+
return new ZodNumber({
|
|
2490
|
+
checks: [],
|
|
2491
|
+
typeName: ZodFirstPartyTypeKind.ZodNumber,
|
|
2492
|
+
coerce: (params === null || params === void 0 ? void 0 : params.coerce) || false,
|
|
2493
|
+
...processCreateParams(params)
|
|
2494
|
+
});
|
|
2495
|
+
};
|
|
2496
|
+
var ZodBigInt = class _ZodBigInt extends ZodType {
|
|
2497
|
+
constructor() {
|
|
2498
|
+
super(...arguments);
|
|
2499
|
+
this.min = this.gte;
|
|
2500
|
+
this.max = this.lte;
|
|
2501
|
+
}
|
|
2502
|
+
_parse(input) {
|
|
2503
|
+
if (this._def.coerce) {
|
|
2504
|
+
try {
|
|
2505
|
+
input.data = BigInt(input.data);
|
|
2506
|
+
} catch (_a) {
|
|
2507
|
+
return this._getInvalidInput(input);
|
|
2508
|
+
}
|
|
2509
|
+
}
|
|
2510
|
+
const parsedType = this._getType(input);
|
|
2511
|
+
if (parsedType !== ZodParsedType.bigint) {
|
|
2512
|
+
return this._getInvalidInput(input);
|
|
2513
|
+
}
|
|
2514
|
+
let ctx = void 0;
|
|
2515
|
+
const status = new ParseStatus();
|
|
2516
|
+
for (const check of this._def.checks) {
|
|
2517
|
+
if (check.kind === "min") {
|
|
2518
|
+
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
2519
|
+
if (tooSmall) {
|
|
2520
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2521
|
+
addIssueToContext(ctx, {
|
|
2522
|
+
code: ZodIssueCode.too_small,
|
|
2523
|
+
type: "bigint",
|
|
2524
|
+
minimum: check.value,
|
|
2525
|
+
inclusive: check.inclusive,
|
|
2526
|
+
message: check.message
|
|
2527
|
+
});
|
|
2528
|
+
status.dirty();
|
|
2529
|
+
}
|
|
2530
|
+
} else if (check.kind === "max") {
|
|
2531
|
+
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
2532
|
+
if (tooBig) {
|
|
2533
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2534
|
+
addIssueToContext(ctx, {
|
|
2535
|
+
code: ZodIssueCode.too_big,
|
|
2536
|
+
type: "bigint",
|
|
2537
|
+
maximum: check.value,
|
|
2538
|
+
inclusive: check.inclusive,
|
|
2539
|
+
message: check.message
|
|
2540
|
+
});
|
|
2541
|
+
status.dirty();
|
|
2542
|
+
}
|
|
2543
|
+
} else if (check.kind === "multipleOf") {
|
|
2544
|
+
if (input.data % check.value !== BigInt(0)) {
|
|
2545
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2546
|
+
addIssueToContext(ctx, {
|
|
2547
|
+
code: ZodIssueCode.not_multiple_of,
|
|
2548
|
+
multipleOf: check.value,
|
|
2549
|
+
message: check.message
|
|
2550
|
+
});
|
|
2551
|
+
status.dirty();
|
|
2552
|
+
}
|
|
2553
|
+
} else {
|
|
2554
|
+
util.assertNever(check);
|
|
2555
|
+
}
|
|
2556
|
+
}
|
|
2557
|
+
return { status: status.value, value: input.data };
|
|
2558
|
+
}
|
|
2559
|
+
_getInvalidInput(input) {
|
|
2560
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2561
|
+
addIssueToContext(ctx, {
|
|
2562
|
+
code: ZodIssueCode.invalid_type,
|
|
2563
|
+
expected: ZodParsedType.bigint,
|
|
2564
|
+
received: ctx.parsedType
|
|
2565
|
+
});
|
|
2566
|
+
return INVALID;
|
|
2567
|
+
}
|
|
2568
|
+
gte(value, message) {
|
|
2569
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
2570
|
+
}
|
|
2571
|
+
gt(value, message) {
|
|
2572
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
2573
|
+
}
|
|
2574
|
+
lte(value, message) {
|
|
2575
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
2576
|
+
}
|
|
2577
|
+
lt(value, message) {
|
|
2578
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
2579
|
+
}
|
|
2580
|
+
setLimit(kind, value, inclusive, message) {
|
|
2581
|
+
return new _ZodBigInt({
|
|
2582
|
+
...this._def,
|
|
2583
|
+
checks: [
|
|
2584
|
+
...this._def.checks,
|
|
2585
|
+
{
|
|
2586
|
+
kind,
|
|
2587
|
+
value,
|
|
2588
|
+
inclusive,
|
|
2589
|
+
message: errorUtil.toString(message)
|
|
2590
|
+
}
|
|
2591
|
+
]
|
|
2592
|
+
});
|
|
2593
|
+
}
|
|
2594
|
+
_addCheck(check) {
|
|
2595
|
+
return new _ZodBigInt({
|
|
2596
|
+
...this._def,
|
|
2597
|
+
checks: [...this._def.checks, check]
|
|
2598
|
+
});
|
|
2599
|
+
}
|
|
2600
|
+
positive(message) {
|
|
2601
|
+
return this._addCheck({
|
|
2602
|
+
kind: "min",
|
|
2603
|
+
value: BigInt(0),
|
|
2604
|
+
inclusive: false,
|
|
2605
|
+
message: errorUtil.toString(message)
|
|
2606
|
+
});
|
|
2607
|
+
}
|
|
2608
|
+
negative(message) {
|
|
2609
|
+
return this._addCheck({
|
|
2610
|
+
kind: "max",
|
|
2611
|
+
value: BigInt(0),
|
|
2612
|
+
inclusive: false,
|
|
2613
|
+
message: errorUtil.toString(message)
|
|
2614
|
+
});
|
|
2615
|
+
}
|
|
2616
|
+
nonpositive(message) {
|
|
2617
|
+
return this._addCheck({
|
|
2618
|
+
kind: "max",
|
|
2619
|
+
value: BigInt(0),
|
|
2620
|
+
inclusive: true,
|
|
2621
|
+
message: errorUtil.toString(message)
|
|
2622
|
+
});
|
|
2623
|
+
}
|
|
2624
|
+
nonnegative(message) {
|
|
2625
|
+
return this._addCheck({
|
|
2626
|
+
kind: "min",
|
|
2627
|
+
value: BigInt(0),
|
|
2628
|
+
inclusive: true,
|
|
2629
|
+
message: errorUtil.toString(message)
|
|
2630
|
+
});
|
|
2631
|
+
}
|
|
2632
|
+
multipleOf(value, message) {
|
|
2633
|
+
return this._addCheck({
|
|
2634
|
+
kind: "multipleOf",
|
|
2635
|
+
value,
|
|
2636
|
+
message: errorUtil.toString(message)
|
|
2637
|
+
});
|
|
2638
|
+
}
|
|
2639
|
+
get minValue() {
|
|
2640
|
+
let min = null;
|
|
2641
|
+
for (const ch of this._def.checks) {
|
|
2642
|
+
if (ch.kind === "min") {
|
|
2643
|
+
if (min === null || ch.value > min)
|
|
2644
|
+
min = ch.value;
|
|
2645
|
+
}
|
|
2646
|
+
}
|
|
2647
|
+
return min;
|
|
2648
|
+
}
|
|
2649
|
+
get maxValue() {
|
|
2650
|
+
let max = null;
|
|
2651
|
+
for (const ch of this._def.checks) {
|
|
2652
|
+
if (ch.kind === "max") {
|
|
2653
|
+
if (max === null || ch.value < max)
|
|
2654
|
+
max = ch.value;
|
|
2655
|
+
}
|
|
2656
|
+
}
|
|
2657
|
+
return max;
|
|
2658
|
+
}
|
|
2659
|
+
};
|
|
2660
|
+
ZodBigInt.create = (params) => {
|
|
2661
|
+
var _a;
|
|
2662
|
+
return new ZodBigInt({
|
|
2663
|
+
checks: [],
|
|
2664
|
+
typeName: ZodFirstPartyTypeKind.ZodBigInt,
|
|
2665
|
+
coerce: (_a = params === null || params === void 0 ? void 0 : params.coerce) !== null && _a !== void 0 ? _a : false,
|
|
2666
|
+
...processCreateParams(params)
|
|
2667
|
+
});
|
|
2668
|
+
};
|
|
2669
|
+
var ZodBoolean = class extends ZodType {
|
|
2670
|
+
_parse(input) {
|
|
2671
|
+
if (this._def.coerce) {
|
|
2672
|
+
input.data = Boolean(input.data);
|
|
2673
|
+
}
|
|
2674
|
+
const parsedType = this._getType(input);
|
|
2675
|
+
if (parsedType !== ZodParsedType.boolean) {
|
|
2676
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2677
|
+
addIssueToContext(ctx, {
|
|
2678
|
+
code: ZodIssueCode.invalid_type,
|
|
2679
|
+
expected: ZodParsedType.boolean,
|
|
2680
|
+
received: ctx.parsedType
|
|
2681
|
+
});
|
|
2682
|
+
return INVALID;
|
|
2683
|
+
}
|
|
2684
|
+
return OK(input.data);
|
|
2685
|
+
}
|
|
2686
|
+
};
|
|
2687
|
+
ZodBoolean.create = (params) => {
|
|
2688
|
+
return new ZodBoolean({
|
|
2689
|
+
typeName: ZodFirstPartyTypeKind.ZodBoolean,
|
|
2690
|
+
coerce: (params === null || params === void 0 ? void 0 : params.coerce) || false,
|
|
2691
|
+
...processCreateParams(params)
|
|
2692
|
+
});
|
|
2693
|
+
};
|
|
2694
|
+
var ZodDate = class _ZodDate extends ZodType {
|
|
2695
|
+
_parse(input) {
|
|
2696
|
+
if (this._def.coerce) {
|
|
2697
|
+
input.data = new Date(input.data);
|
|
2698
|
+
}
|
|
2699
|
+
const parsedType = this._getType(input);
|
|
2700
|
+
if (parsedType !== ZodParsedType.date) {
|
|
2701
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
2702
|
+
addIssueToContext(ctx2, {
|
|
2703
|
+
code: ZodIssueCode.invalid_type,
|
|
2704
|
+
expected: ZodParsedType.date,
|
|
2705
|
+
received: ctx2.parsedType
|
|
2706
|
+
});
|
|
2707
|
+
return INVALID;
|
|
2708
|
+
}
|
|
2709
|
+
if (isNaN(input.data.getTime())) {
|
|
2710
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
2711
|
+
addIssueToContext(ctx2, {
|
|
2712
|
+
code: ZodIssueCode.invalid_date
|
|
2713
|
+
});
|
|
2714
|
+
return INVALID;
|
|
2715
|
+
}
|
|
2716
|
+
const status = new ParseStatus();
|
|
2717
|
+
let ctx = void 0;
|
|
2718
|
+
for (const check of this._def.checks) {
|
|
2719
|
+
if (check.kind === "min") {
|
|
2720
|
+
if (input.data.getTime() < check.value) {
|
|
2721
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2722
|
+
addIssueToContext(ctx, {
|
|
2723
|
+
code: ZodIssueCode.too_small,
|
|
2724
|
+
message: check.message,
|
|
2725
|
+
inclusive: true,
|
|
2726
|
+
exact: false,
|
|
2727
|
+
minimum: check.value,
|
|
2728
|
+
type: "date"
|
|
2729
|
+
});
|
|
2730
|
+
status.dirty();
|
|
2731
|
+
}
|
|
2732
|
+
} else if (check.kind === "max") {
|
|
2733
|
+
if (input.data.getTime() > check.value) {
|
|
2734
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2735
|
+
addIssueToContext(ctx, {
|
|
2736
|
+
code: ZodIssueCode.too_big,
|
|
2737
|
+
message: check.message,
|
|
2738
|
+
inclusive: true,
|
|
2739
|
+
exact: false,
|
|
2740
|
+
maximum: check.value,
|
|
2741
|
+
type: "date"
|
|
2742
|
+
});
|
|
2743
|
+
status.dirty();
|
|
2744
|
+
}
|
|
2745
|
+
} else {
|
|
2746
|
+
util.assertNever(check);
|
|
2747
|
+
}
|
|
2748
|
+
}
|
|
2749
|
+
return {
|
|
2750
|
+
status: status.value,
|
|
2751
|
+
value: new Date(input.data.getTime())
|
|
2752
|
+
};
|
|
2753
|
+
}
|
|
2754
|
+
_addCheck(check) {
|
|
2755
|
+
return new _ZodDate({
|
|
2756
|
+
...this._def,
|
|
2757
|
+
checks: [...this._def.checks, check]
|
|
2758
|
+
});
|
|
2759
|
+
}
|
|
2760
|
+
min(minDate, message) {
|
|
2761
|
+
return this._addCheck({
|
|
2762
|
+
kind: "min",
|
|
2763
|
+
value: minDate.getTime(),
|
|
2764
|
+
message: errorUtil.toString(message)
|
|
2765
|
+
});
|
|
2766
|
+
}
|
|
2767
|
+
max(maxDate, message) {
|
|
2768
|
+
return this._addCheck({
|
|
2769
|
+
kind: "max",
|
|
2770
|
+
value: maxDate.getTime(),
|
|
2771
|
+
message: errorUtil.toString(message)
|
|
2772
|
+
});
|
|
2773
|
+
}
|
|
2774
|
+
get minDate() {
|
|
2775
|
+
let min = null;
|
|
2776
|
+
for (const ch of this._def.checks) {
|
|
2777
|
+
if (ch.kind === "min") {
|
|
2778
|
+
if (min === null || ch.value > min)
|
|
2779
|
+
min = ch.value;
|
|
2780
|
+
}
|
|
2781
|
+
}
|
|
2782
|
+
return min != null ? new Date(min) : null;
|
|
2783
|
+
}
|
|
2784
|
+
get maxDate() {
|
|
2785
|
+
let max = null;
|
|
2786
|
+
for (const ch of this._def.checks) {
|
|
2787
|
+
if (ch.kind === "max") {
|
|
2788
|
+
if (max === null || ch.value < max)
|
|
2789
|
+
max = ch.value;
|
|
2790
|
+
}
|
|
2791
|
+
}
|
|
2792
|
+
return max != null ? new Date(max) : null;
|
|
2793
|
+
}
|
|
2794
|
+
};
|
|
2795
|
+
ZodDate.create = (params) => {
|
|
2796
|
+
return new ZodDate({
|
|
2797
|
+
checks: [],
|
|
2798
|
+
coerce: (params === null || params === void 0 ? void 0 : params.coerce) || false,
|
|
2799
|
+
typeName: ZodFirstPartyTypeKind.ZodDate,
|
|
2800
|
+
...processCreateParams(params)
|
|
2801
|
+
});
|
|
2802
|
+
};
|
|
2803
|
+
var ZodSymbol = class extends ZodType {
|
|
2804
|
+
_parse(input) {
|
|
2805
|
+
const parsedType = this._getType(input);
|
|
2806
|
+
if (parsedType !== ZodParsedType.symbol) {
|
|
2807
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2808
|
+
addIssueToContext(ctx, {
|
|
2809
|
+
code: ZodIssueCode.invalid_type,
|
|
2810
|
+
expected: ZodParsedType.symbol,
|
|
2811
|
+
received: ctx.parsedType
|
|
2812
|
+
});
|
|
2813
|
+
return INVALID;
|
|
2814
|
+
}
|
|
2815
|
+
return OK(input.data);
|
|
2816
|
+
}
|
|
2817
|
+
};
|
|
2818
|
+
ZodSymbol.create = (params) => {
|
|
2819
|
+
return new ZodSymbol({
|
|
2820
|
+
typeName: ZodFirstPartyTypeKind.ZodSymbol,
|
|
2821
|
+
...processCreateParams(params)
|
|
2822
|
+
});
|
|
2823
|
+
};
|
|
2824
|
+
var ZodUndefined = class extends ZodType {
|
|
2825
|
+
_parse(input) {
|
|
2826
|
+
const parsedType = this._getType(input);
|
|
2827
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
2828
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2829
|
+
addIssueToContext(ctx, {
|
|
2830
|
+
code: ZodIssueCode.invalid_type,
|
|
2831
|
+
expected: ZodParsedType.undefined,
|
|
2832
|
+
received: ctx.parsedType
|
|
2833
|
+
});
|
|
2834
|
+
return INVALID;
|
|
2835
|
+
}
|
|
2836
|
+
return OK(input.data);
|
|
2837
|
+
}
|
|
2838
|
+
};
|
|
2839
|
+
ZodUndefined.create = (params) => {
|
|
2840
|
+
return new ZodUndefined({
|
|
2841
|
+
typeName: ZodFirstPartyTypeKind.ZodUndefined,
|
|
2842
|
+
...processCreateParams(params)
|
|
2843
|
+
});
|
|
2844
|
+
};
|
|
2845
|
+
var ZodNull = class extends ZodType {
|
|
2846
|
+
_parse(input) {
|
|
2847
|
+
const parsedType = this._getType(input);
|
|
2848
|
+
if (parsedType !== ZodParsedType.null) {
|
|
2849
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2850
|
+
addIssueToContext(ctx, {
|
|
2851
|
+
code: ZodIssueCode.invalid_type,
|
|
2852
|
+
expected: ZodParsedType.null,
|
|
2853
|
+
received: ctx.parsedType
|
|
2854
|
+
});
|
|
2855
|
+
return INVALID;
|
|
2856
|
+
}
|
|
2857
|
+
return OK(input.data);
|
|
2858
|
+
}
|
|
2859
|
+
};
|
|
2860
|
+
ZodNull.create = (params) => {
|
|
2861
|
+
return new ZodNull({
|
|
2862
|
+
typeName: ZodFirstPartyTypeKind.ZodNull,
|
|
2863
|
+
...processCreateParams(params)
|
|
2864
|
+
});
|
|
2865
|
+
};
|
|
2866
|
+
var ZodAny = class extends ZodType {
|
|
2867
|
+
constructor() {
|
|
2868
|
+
super(...arguments);
|
|
2869
|
+
this._any = true;
|
|
2870
|
+
}
|
|
2871
|
+
_parse(input) {
|
|
2872
|
+
return OK(input.data);
|
|
2873
|
+
}
|
|
2874
|
+
};
|
|
2875
|
+
ZodAny.create = (params) => {
|
|
2876
|
+
return new ZodAny({
|
|
2877
|
+
typeName: ZodFirstPartyTypeKind.ZodAny,
|
|
2878
|
+
...processCreateParams(params)
|
|
2879
|
+
});
|
|
2880
|
+
};
|
|
2881
|
+
var ZodUnknown = class extends ZodType {
|
|
2882
|
+
constructor() {
|
|
2883
|
+
super(...arguments);
|
|
2884
|
+
this._unknown = true;
|
|
2885
|
+
}
|
|
2886
|
+
_parse(input) {
|
|
2887
|
+
return OK(input.data);
|
|
2888
|
+
}
|
|
2889
|
+
};
|
|
2890
|
+
ZodUnknown.create = (params) => {
|
|
2891
|
+
return new ZodUnknown({
|
|
2892
|
+
typeName: ZodFirstPartyTypeKind.ZodUnknown,
|
|
2893
|
+
...processCreateParams(params)
|
|
2894
|
+
});
|
|
2895
|
+
};
|
|
2896
|
+
var ZodNever = class extends ZodType {
|
|
2897
|
+
_parse(input) {
|
|
2898
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2899
|
+
addIssueToContext(ctx, {
|
|
2900
|
+
code: ZodIssueCode.invalid_type,
|
|
2901
|
+
expected: ZodParsedType.never,
|
|
2902
|
+
received: ctx.parsedType
|
|
2903
|
+
});
|
|
2904
|
+
return INVALID;
|
|
2905
|
+
}
|
|
2906
|
+
};
|
|
2907
|
+
ZodNever.create = (params) => {
|
|
2908
|
+
return new ZodNever({
|
|
2909
|
+
typeName: ZodFirstPartyTypeKind.ZodNever,
|
|
2910
|
+
...processCreateParams(params)
|
|
2911
|
+
});
|
|
2912
|
+
};
|
|
2913
|
+
var ZodVoid = class extends ZodType {
|
|
2914
|
+
_parse(input) {
|
|
2915
|
+
const parsedType = this._getType(input);
|
|
2916
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
2917
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2918
|
+
addIssueToContext(ctx, {
|
|
2919
|
+
code: ZodIssueCode.invalid_type,
|
|
2920
|
+
expected: ZodParsedType.void,
|
|
2921
|
+
received: ctx.parsedType
|
|
2922
|
+
});
|
|
2923
|
+
return INVALID;
|
|
2924
|
+
}
|
|
2925
|
+
return OK(input.data);
|
|
2926
|
+
}
|
|
2927
|
+
};
|
|
2928
|
+
ZodVoid.create = (params) => {
|
|
2929
|
+
return new ZodVoid({
|
|
2930
|
+
typeName: ZodFirstPartyTypeKind.ZodVoid,
|
|
2931
|
+
...processCreateParams(params)
|
|
2932
|
+
});
|
|
2933
|
+
};
|
|
2934
|
+
var ZodArray = class _ZodArray extends ZodType {
|
|
2935
|
+
_parse(input) {
|
|
2936
|
+
const { ctx, status } = this._processInputParams(input);
|
|
2937
|
+
const def = this._def;
|
|
2938
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
2939
|
+
addIssueToContext(ctx, {
|
|
2940
|
+
code: ZodIssueCode.invalid_type,
|
|
2941
|
+
expected: ZodParsedType.array,
|
|
2942
|
+
received: ctx.parsedType
|
|
2943
|
+
});
|
|
2944
|
+
return INVALID;
|
|
2945
|
+
}
|
|
2946
|
+
if (def.exactLength !== null) {
|
|
2947
|
+
const tooBig = ctx.data.length > def.exactLength.value;
|
|
2948
|
+
const tooSmall = ctx.data.length < def.exactLength.value;
|
|
2949
|
+
if (tooBig || tooSmall) {
|
|
2950
|
+
addIssueToContext(ctx, {
|
|
2951
|
+
code: tooBig ? ZodIssueCode.too_big : ZodIssueCode.too_small,
|
|
2952
|
+
minimum: tooSmall ? def.exactLength.value : void 0,
|
|
2953
|
+
maximum: tooBig ? def.exactLength.value : void 0,
|
|
2954
|
+
type: "array",
|
|
2955
|
+
inclusive: true,
|
|
2956
|
+
exact: true,
|
|
2957
|
+
message: def.exactLength.message
|
|
2958
|
+
});
|
|
2959
|
+
status.dirty();
|
|
2960
|
+
}
|
|
2961
|
+
}
|
|
2962
|
+
if (def.minLength !== null) {
|
|
2963
|
+
if (ctx.data.length < def.minLength.value) {
|
|
2964
|
+
addIssueToContext(ctx, {
|
|
2965
|
+
code: ZodIssueCode.too_small,
|
|
2966
|
+
minimum: def.minLength.value,
|
|
2967
|
+
type: "array",
|
|
2968
|
+
inclusive: true,
|
|
2969
|
+
exact: false,
|
|
2970
|
+
message: def.minLength.message
|
|
2971
|
+
});
|
|
2972
|
+
status.dirty();
|
|
2973
|
+
}
|
|
2974
|
+
}
|
|
2975
|
+
if (def.maxLength !== null) {
|
|
2976
|
+
if (ctx.data.length > def.maxLength.value) {
|
|
2977
|
+
addIssueToContext(ctx, {
|
|
2978
|
+
code: ZodIssueCode.too_big,
|
|
2979
|
+
maximum: def.maxLength.value,
|
|
2980
|
+
type: "array",
|
|
2981
|
+
inclusive: true,
|
|
2982
|
+
exact: false,
|
|
2983
|
+
message: def.maxLength.message
|
|
2984
|
+
});
|
|
2985
|
+
status.dirty();
|
|
2986
|
+
}
|
|
2987
|
+
}
|
|
2988
|
+
if (ctx.common.async) {
|
|
2989
|
+
return Promise.all([...ctx.data].map((item, i) => {
|
|
2990
|
+
return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
2991
|
+
})).then((result2) => {
|
|
2992
|
+
return ParseStatus.mergeArray(status, result2);
|
|
2993
|
+
});
|
|
2994
|
+
}
|
|
2995
|
+
const result = [...ctx.data].map((item, i) => {
|
|
2996
|
+
return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
2997
|
+
});
|
|
2998
|
+
return ParseStatus.mergeArray(status, result);
|
|
2999
|
+
}
|
|
3000
|
+
get element() {
|
|
3001
|
+
return this._def.type;
|
|
3002
|
+
}
|
|
3003
|
+
min(minLength, message) {
|
|
3004
|
+
return new _ZodArray({
|
|
3005
|
+
...this._def,
|
|
3006
|
+
minLength: { value: minLength, message: errorUtil.toString(message) }
|
|
3007
|
+
});
|
|
3008
|
+
}
|
|
3009
|
+
max(maxLength, message) {
|
|
3010
|
+
return new _ZodArray({
|
|
3011
|
+
...this._def,
|
|
3012
|
+
maxLength: { value: maxLength, message: errorUtil.toString(message) }
|
|
3013
|
+
});
|
|
3014
|
+
}
|
|
3015
|
+
length(len, message) {
|
|
3016
|
+
return new _ZodArray({
|
|
3017
|
+
...this._def,
|
|
3018
|
+
exactLength: { value: len, message: errorUtil.toString(message) }
|
|
3019
|
+
});
|
|
3020
|
+
}
|
|
3021
|
+
nonempty(message) {
|
|
3022
|
+
return this.min(1, message);
|
|
3023
|
+
}
|
|
3024
|
+
};
|
|
3025
|
+
ZodArray.create = (schema, params) => {
|
|
3026
|
+
return new ZodArray({
|
|
3027
|
+
type: schema,
|
|
3028
|
+
minLength: null,
|
|
3029
|
+
maxLength: null,
|
|
3030
|
+
exactLength: null,
|
|
3031
|
+
typeName: ZodFirstPartyTypeKind.ZodArray,
|
|
3032
|
+
...processCreateParams(params)
|
|
3033
|
+
});
|
|
3034
|
+
};
|
|
3035
|
+
function deepPartialify(schema) {
|
|
3036
|
+
if (schema instanceof ZodObject) {
|
|
3037
|
+
const newShape = {};
|
|
3038
|
+
for (const key in schema.shape) {
|
|
3039
|
+
const fieldSchema = schema.shape[key];
|
|
3040
|
+
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema));
|
|
3041
|
+
}
|
|
3042
|
+
return new ZodObject({
|
|
3043
|
+
...schema._def,
|
|
3044
|
+
shape: () => newShape
|
|
3045
|
+
});
|
|
3046
|
+
} else if (schema instanceof ZodArray) {
|
|
3047
|
+
return new ZodArray({
|
|
3048
|
+
...schema._def,
|
|
3049
|
+
type: deepPartialify(schema.element)
|
|
3050
|
+
});
|
|
3051
|
+
} else if (schema instanceof ZodOptional) {
|
|
3052
|
+
return ZodOptional.create(deepPartialify(schema.unwrap()));
|
|
3053
|
+
} else if (schema instanceof ZodNullable) {
|
|
3054
|
+
return ZodNullable.create(deepPartialify(schema.unwrap()));
|
|
3055
|
+
} else if (schema instanceof ZodTuple) {
|
|
3056
|
+
return ZodTuple.create(schema.items.map((item) => deepPartialify(item)));
|
|
3057
|
+
} else {
|
|
3058
|
+
return schema;
|
|
3059
|
+
}
|
|
3060
|
+
}
|
|
3061
|
+
var ZodObject = class _ZodObject extends ZodType {
|
|
3062
|
+
constructor() {
|
|
3063
|
+
super(...arguments);
|
|
3064
|
+
this._cached = null;
|
|
3065
|
+
this.nonstrict = this.passthrough;
|
|
3066
|
+
this.augment = this.extend;
|
|
3067
|
+
}
|
|
3068
|
+
_getCached() {
|
|
3069
|
+
if (this._cached !== null)
|
|
3070
|
+
return this._cached;
|
|
3071
|
+
const shape = this._def.shape();
|
|
3072
|
+
const keys = util.objectKeys(shape);
|
|
3073
|
+
return this._cached = { shape, keys };
|
|
3074
|
+
}
|
|
3075
|
+
_parse(input) {
|
|
3076
|
+
const parsedType = this._getType(input);
|
|
3077
|
+
if (parsedType !== ZodParsedType.object) {
|
|
3078
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
3079
|
+
addIssueToContext(ctx2, {
|
|
3080
|
+
code: ZodIssueCode.invalid_type,
|
|
3081
|
+
expected: ZodParsedType.object,
|
|
3082
|
+
received: ctx2.parsedType
|
|
3083
|
+
});
|
|
3084
|
+
return INVALID;
|
|
3085
|
+
}
|
|
3086
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3087
|
+
const { shape, keys: shapeKeys } = this._getCached();
|
|
3088
|
+
const extraKeys = [];
|
|
3089
|
+
if (!(this._def.catchall instanceof ZodNever && this._def.unknownKeys === "strip")) {
|
|
3090
|
+
for (const key in ctx.data) {
|
|
3091
|
+
if (!shapeKeys.includes(key)) {
|
|
3092
|
+
extraKeys.push(key);
|
|
3093
|
+
}
|
|
3094
|
+
}
|
|
3095
|
+
}
|
|
3096
|
+
const pairs = [];
|
|
3097
|
+
for (const key of shapeKeys) {
|
|
3098
|
+
const keyValidator = shape[key];
|
|
3099
|
+
const value = ctx.data[key];
|
|
3100
|
+
pairs.push({
|
|
3101
|
+
key: { status: "valid", value: key },
|
|
3102
|
+
value: keyValidator._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
|
|
3103
|
+
alwaysSet: key in ctx.data
|
|
3104
|
+
});
|
|
3105
|
+
}
|
|
3106
|
+
if (this._def.catchall instanceof ZodNever) {
|
|
3107
|
+
const unknownKeys = this._def.unknownKeys;
|
|
3108
|
+
if (unknownKeys === "passthrough") {
|
|
3109
|
+
for (const key of extraKeys) {
|
|
3110
|
+
pairs.push({
|
|
3111
|
+
key: { status: "valid", value: key },
|
|
3112
|
+
value: { status: "valid", value: ctx.data[key] }
|
|
3113
|
+
});
|
|
3114
|
+
}
|
|
3115
|
+
} else if (unknownKeys === "strict") {
|
|
3116
|
+
if (extraKeys.length > 0) {
|
|
3117
|
+
addIssueToContext(ctx, {
|
|
3118
|
+
code: ZodIssueCode.unrecognized_keys,
|
|
3119
|
+
keys: extraKeys
|
|
3120
|
+
});
|
|
3121
|
+
status.dirty();
|
|
3122
|
+
}
|
|
3123
|
+
} else if (unknownKeys === "strip") ;
|
|
3124
|
+
else {
|
|
3125
|
+
throw new Error(`Internal ZodObject error: invalid unknownKeys value.`);
|
|
3126
|
+
}
|
|
3127
|
+
} else {
|
|
3128
|
+
const catchall = this._def.catchall;
|
|
3129
|
+
for (const key of extraKeys) {
|
|
3130
|
+
const value = ctx.data[key];
|
|
3131
|
+
pairs.push({
|
|
3132
|
+
key: { status: "valid", value: key },
|
|
3133
|
+
value: catchall._parse(
|
|
3134
|
+
new ParseInputLazyPath(ctx, value, ctx.path, key)
|
|
3135
|
+
//, ctx.child(key), value, getParsedType(value)
|
|
3136
|
+
),
|
|
3137
|
+
alwaysSet: key in ctx.data
|
|
3138
|
+
});
|
|
3139
|
+
}
|
|
3140
|
+
}
|
|
3141
|
+
if (ctx.common.async) {
|
|
3142
|
+
return Promise.resolve().then(async () => {
|
|
3143
|
+
const syncPairs = [];
|
|
3144
|
+
for (const pair of pairs) {
|
|
3145
|
+
const key = await pair.key;
|
|
3146
|
+
const value = await pair.value;
|
|
3147
|
+
syncPairs.push({
|
|
3148
|
+
key,
|
|
3149
|
+
value,
|
|
3150
|
+
alwaysSet: pair.alwaysSet
|
|
3151
|
+
});
|
|
3152
|
+
}
|
|
3153
|
+
return syncPairs;
|
|
3154
|
+
}).then((syncPairs) => {
|
|
3155
|
+
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
3156
|
+
});
|
|
3157
|
+
} else {
|
|
3158
|
+
return ParseStatus.mergeObjectSync(status, pairs);
|
|
3159
|
+
}
|
|
3160
|
+
}
|
|
3161
|
+
get shape() {
|
|
3162
|
+
return this._def.shape();
|
|
3163
|
+
}
|
|
3164
|
+
strict(message) {
|
|
3165
|
+
errorUtil.errToObj;
|
|
3166
|
+
return new _ZodObject({
|
|
3167
|
+
...this._def,
|
|
3168
|
+
unknownKeys: "strict",
|
|
3169
|
+
...message !== void 0 ? {
|
|
3170
|
+
errorMap: (issue, ctx) => {
|
|
3171
|
+
var _a, _b, _c, _d;
|
|
3172
|
+
const defaultError = (_c = (_b = (_a = this._def).errorMap) === null || _b === void 0 ? void 0 : _b.call(_a, issue, ctx).message) !== null && _c !== void 0 ? _c : ctx.defaultError;
|
|
3173
|
+
if (issue.code === "unrecognized_keys")
|
|
3174
|
+
return {
|
|
3175
|
+
message: (_d = errorUtil.errToObj(message).message) !== null && _d !== void 0 ? _d : defaultError
|
|
3176
|
+
};
|
|
3177
|
+
return {
|
|
3178
|
+
message: defaultError
|
|
3179
|
+
};
|
|
3180
|
+
}
|
|
3181
|
+
} : {}
|
|
3182
|
+
});
|
|
3183
|
+
}
|
|
3184
|
+
strip() {
|
|
3185
|
+
return new _ZodObject({
|
|
3186
|
+
...this._def,
|
|
3187
|
+
unknownKeys: "strip"
|
|
3188
|
+
});
|
|
3189
|
+
}
|
|
3190
|
+
passthrough() {
|
|
3191
|
+
return new _ZodObject({
|
|
3192
|
+
...this._def,
|
|
3193
|
+
unknownKeys: "passthrough"
|
|
3194
|
+
});
|
|
3195
|
+
}
|
|
3196
|
+
// const AugmentFactory =
|
|
3197
|
+
// <Def extends ZodObjectDef>(def: Def) =>
|
|
3198
|
+
// <Augmentation extends ZodRawShape>(
|
|
3199
|
+
// augmentation: Augmentation
|
|
3200
|
+
// ): ZodObject<
|
|
3201
|
+
// extendShape<ReturnType<Def["shape"]>, Augmentation>,
|
|
3202
|
+
// Def["unknownKeys"],
|
|
3203
|
+
// Def["catchall"]
|
|
3204
|
+
// > => {
|
|
3205
|
+
// return new ZodObject({
|
|
3206
|
+
// ...def,
|
|
3207
|
+
// shape: () => ({
|
|
3208
|
+
// ...def.shape(),
|
|
3209
|
+
// ...augmentation,
|
|
3210
|
+
// }),
|
|
3211
|
+
// }) as any;
|
|
3212
|
+
// };
|
|
3213
|
+
extend(augmentation) {
|
|
3214
|
+
return new _ZodObject({
|
|
3215
|
+
...this._def,
|
|
3216
|
+
shape: () => ({
|
|
3217
|
+
...this._def.shape(),
|
|
3218
|
+
...augmentation
|
|
3219
|
+
})
|
|
3220
|
+
});
|
|
3221
|
+
}
|
|
3222
|
+
/**
|
|
3223
|
+
* Prior to zod@1.0.12 there was a bug in the
|
|
3224
|
+
* inferred type of merged objects. Please
|
|
3225
|
+
* upgrade if you are experiencing issues.
|
|
3226
|
+
*/
|
|
3227
|
+
merge(merging) {
|
|
3228
|
+
const merged = new _ZodObject({
|
|
3229
|
+
unknownKeys: merging._def.unknownKeys,
|
|
3230
|
+
catchall: merging._def.catchall,
|
|
3231
|
+
shape: () => ({
|
|
3232
|
+
...this._def.shape(),
|
|
3233
|
+
...merging._def.shape()
|
|
3234
|
+
}),
|
|
3235
|
+
typeName: ZodFirstPartyTypeKind.ZodObject
|
|
3236
|
+
});
|
|
3237
|
+
return merged;
|
|
3238
|
+
}
|
|
3239
|
+
// merge<
|
|
3240
|
+
// Incoming extends AnyZodObject,
|
|
3241
|
+
// Augmentation extends Incoming["shape"],
|
|
3242
|
+
// NewOutput extends {
|
|
3243
|
+
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
|
|
3244
|
+
// ? Augmentation[k]["_output"]
|
|
3245
|
+
// : k extends keyof Output
|
|
3246
|
+
// ? Output[k]
|
|
3247
|
+
// : never;
|
|
3248
|
+
// },
|
|
3249
|
+
// NewInput extends {
|
|
3250
|
+
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
|
|
3251
|
+
// ? Augmentation[k]["_input"]
|
|
3252
|
+
// : k extends keyof Input
|
|
3253
|
+
// ? Input[k]
|
|
3254
|
+
// : never;
|
|
3255
|
+
// }
|
|
3256
|
+
// >(
|
|
3257
|
+
// merging: Incoming
|
|
3258
|
+
// ): ZodObject<
|
|
3259
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
3260
|
+
// Incoming["_def"]["unknownKeys"],
|
|
3261
|
+
// Incoming["_def"]["catchall"],
|
|
3262
|
+
// NewOutput,
|
|
3263
|
+
// NewInput
|
|
3264
|
+
// > {
|
|
3265
|
+
// const merged: any = new ZodObject({
|
|
3266
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
3267
|
+
// catchall: merging._def.catchall,
|
|
3268
|
+
// shape: () =>
|
|
3269
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
3270
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3271
|
+
// }) as any;
|
|
3272
|
+
// return merged;
|
|
3273
|
+
// }
|
|
3274
|
+
setKey(key, schema) {
|
|
3275
|
+
return this.augment({ [key]: schema });
|
|
3276
|
+
}
|
|
3277
|
+
// merge<Incoming extends AnyZodObject>(
|
|
3278
|
+
// merging: Incoming
|
|
3279
|
+
// ): //ZodObject<T & Incoming["_shape"], UnknownKeys, Catchall> = (merging) => {
|
|
3280
|
+
// ZodObject<
|
|
3281
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
3282
|
+
// Incoming["_def"]["unknownKeys"],
|
|
3283
|
+
// Incoming["_def"]["catchall"]
|
|
3284
|
+
// > {
|
|
3285
|
+
// // const mergedShape = objectUtil.mergeShapes(
|
|
3286
|
+
// // this._def.shape(),
|
|
3287
|
+
// // merging._def.shape()
|
|
3288
|
+
// // );
|
|
3289
|
+
// const merged: any = new ZodObject({
|
|
3290
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
3291
|
+
// catchall: merging._def.catchall,
|
|
3292
|
+
// shape: () =>
|
|
3293
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
3294
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3295
|
+
// }) as any;
|
|
3296
|
+
// return merged;
|
|
3297
|
+
// }
|
|
3298
|
+
catchall(index) {
|
|
3299
|
+
return new _ZodObject({
|
|
3300
|
+
...this._def,
|
|
3301
|
+
catchall: index
|
|
3302
|
+
});
|
|
3303
|
+
}
|
|
3304
|
+
pick(mask) {
|
|
3305
|
+
const shape = {};
|
|
3306
|
+
util.objectKeys(mask).forEach((key) => {
|
|
3307
|
+
if (mask[key] && this.shape[key]) {
|
|
3308
|
+
shape[key] = this.shape[key];
|
|
3309
|
+
}
|
|
3310
|
+
});
|
|
3311
|
+
return new _ZodObject({
|
|
3312
|
+
...this._def,
|
|
3313
|
+
shape: () => shape
|
|
3314
|
+
});
|
|
3315
|
+
}
|
|
3316
|
+
omit(mask) {
|
|
3317
|
+
const shape = {};
|
|
3318
|
+
util.objectKeys(this.shape).forEach((key) => {
|
|
3319
|
+
if (!mask[key]) {
|
|
3320
|
+
shape[key] = this.shape[key];
|
|
3321
|
+
}
|
|
3322
|
+
});
|
|
3323
|
+
return new _ZodObject({
|
|
3324
|
+
...this._def,
|
|
3325
|
+
shape: () => shape
|
|
3326
|
+
});
|
|
3327
|
+
}
|
|
3328
|
+
/**
|
|
3329
|
+
* @deprecated
|
|
3330
|
+
*/
|
|
3331
|
+
deepPartial() {
|
|
3332
|
+
return deepPartialify(this);
|
|
3333
|
+
}
|
|
3334
|
+
partial(mask) {
|
|
3335
|
+
const newShape = {};
|
|
3336
|
+
util.objectKeys(this.shape).forEach((key) => {
|
|
3337
|
+
const fieldSchema = this.shape[key];
|
|
3338
|
+
if (mask && !mask[key]) {
|
|
3339
|
+
newShape[key] = fieldSchema;
|
|
3340
|
+
} else {
|
|
3341
|
+
newShape[key] = fieldSchema.optional();
|
|
3342
|
+
}
|
|
3343
|
+
});
|
|
3344
|
+
return new _ZodObject({
|
|
3345
|
+
...this._def,
|
|
3346
|
+
shape: () => newShape
|
|
3347
|
+
});
|
|
3348
|
+
}
|
|
3349
|
+
required(mask) {
|
|
3350
|
+
const newShape = {};
|
|
3351
|
+
util.objectKeys(this.shape).forEach((key) => {
|
|
3352
|
+
if (mask && !mask[key]) {
|
|
3353
|
+
newShape[key] = this.shape[key];
|
|
3354
|
+
} else {
|
|
3355
|
+
const fieldSchema = this.shape[key];
|
|
3356
|
+
let newField = fieldSchema;
|
|
3357
|
+
while (newField instanceof ZodOptional) {
|
|
3358
|
+
newField = newField._def.innerType;
|
|
3359
|
+
}
|
|
3360
|
+
newShape[key] = newField;
|
|
3361
|
+
}
|
|
3362
|
+
});
|
|
3363
|
+
return new _ZodObject({
|
|
3364
|
+
...this._def,
|
|
3365
|
+
shape: () => newShape
|
|
3366
|
+
});
|
|
3367
|
+
}
|
|
3368
|
+
keyof() {
|
|
3369
|
+
return createZodEnum(util.objectKeys(this.shape));
|
|
3370
|
+
}
|
|
3371
|
+
};
|
|
3372
|
+
ZodObject.create = (shape, params) => {
|
|
3373
|
+
return new ZodObject({
|
|
3374
|
+
shape: () => shape,
|
|
3375
|
+
unknownKeys: "strip",
|
|
3376
|
+
catchall: ZodNever.create(),
|
|
3377
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3378
|
+
...processCreateParams(params)
|
|
3379
|
+
});
|
|
3380
|
+
};
|
|
3381
|
+
ZodObject.strictCreate = (shape, params) => {
|
|
3382
|
+
return new ZodObject({
|
|
3383
|
+
shape: () => shape,
|
|
3384
|
+
unknownKeys: "strict",
|
|
3385
|
+
catchall: ZodNever.create(),
|
|
3386
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3387
|
+
...processCreateParams(params)
|
|
3388
|
+
});
|
|
3389
|
+
};
|
|
3390
|
+
ZodObject.lazycreate = (shape, params) => {
|
|
3391
|
+
return new ZodObject({
|
|
3392
|
+
shape,
|
|
3393
|
+
unknownKeys: "strip",
|
|
3394
|
+
catchall: ZodNever.create(),
|
|
3395
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3396
|
+
...processCreateParams(params)
|
|
3397
|
+
});
|
|
3398
|
+
};
|
|
3399
|
+
var ZodUnion = class extends ZodType {
|
|
3400
|
+
_parse(input) {
|
|
3401
|
+
const { ctx } = this._processInputParams(input);
|
|
3402
|
+
const options = this._def.options;
|
|
3403
|
+
function handleResults(results) {
|
|
3404
|
+
for (const result of results) {
|
|
3405
|
+
if (result.result.status === "valid") {
|
|
3406
|
+
return result.result;
|
|
3407
|
+
}
|
|
3408
|
+
}
|
|
3409
|
+
for (const result of results) {
|
|
3410
|
+
if (result.result.status === "dirty") {
|
|
3411
|
+
ctx.common.issues.push(...result.ctx.common.issues);
|
|
3412
|
+
return result.result;
|
|
3413
|
+
}
|
|
3414
|
+
}
|
|
3415
|
+
const unionErrors = results.map((result) => new ZodError(result.ctx.common.issues));
|
|
3416
|
+
addIssueToContext(ctx, {
|
|
3417
|
+
code: ZodIssueCode.invalid_union,
|
|
3418
|
+
unionErrors
|
|
3419
|
+
});
|
|
3420
|
+
return INVALID;
|
|
3421
|
+
}
|
|
3422
|
+
if (ctx.common.async) {
|
|
3423
|
+
return Promise.all(options.map(async (option) => {
|
|
3424
|
+
const childCtx = {
|
|
3425
|
+
...ctx,
|
|
3426
|
+
common: {
|
|
3427
|
+
...ctx.common,
|
|
3428
|
+
issues: []
|
|
3429
|
+
},
|
|
3430
|
+
parent: null
|
|
3431
|
+
};
|
|
3432
|
+
return {
|
|
3433
|
+
result: await option._parseAsync({
|
|
3434
|
+
data: ctx.data,
|
|
3435
|
+
path: ctx.path,
|
|
3436
|
+
parent: childCtx
|
|
3437
|
+
}),
|
|
3438
|
+
ctx: childCtx
|
|
3439
|
+
};
|
|
3440
|
+
})).then(handleResults);
|
|
3441
|
+
} else {
|
|
3442
|
+
let dirty = void 0;
|
|
3443
|
+
const issues = [];
|
|
3444
|
+
for (const option of options) {
|
|
3445
|
+
const childCtx = {
|
|
3446
|
+
...ctx,
|
|
3447
|
+
common: {
|
|
3448
|
+
...ctx.common,
|
|
3449
|
+
issues: []
|
|
3450
|
+
},
|
|
3451
|
+
parent: null
|
|
3452
|
+
};
|
|
3453
|
+
const result = option._parseSync({
|
|
3454
|
+
data: ctx.data,
|
|
3455
|
+
path: ctx.path,
|
|
3456
|
+
parent: childCtx
|
|
3457
|
+
});
|
|
3458
|
+
if (result.status === "valid") {
|
|
3459
|
+
return result;
|
|
3460
|
+
} else if (result.status === "dirty" && !dirty) {
|
|
3461
|
+
dirty = { result, ctx: childCtx };
|
|
3462
|
+
}
|
|
3463
|
+
if (childCtx.common.issues.length) {
|
|
3464
|
+
issues.push(childCtx.common.issues);
|
|
3465
|
+
}
|
|
3466
|
+
}
|
|
3467
|
+
if (dirty) {
|
|
3468
|
+
ctx.common.issues.push(...dirty.ctx.common.issues);
|
|
3469
|
+
return dirty.result;
|
|
3470
|
+
}
|
|
3471
|
+
const unionErrors = issues.map((issues2) => new ZodError(issues2));
|
|
3472
|
+
addIssueToContext(ctx, {
|
|
3473
|
+
code: ZodIssueCode.invalid_union,
|
|
3474
|
+
unionErrors
|
|
3475
|
+
});
|
|
3476
|
+
return INVALID;
|
|
3477
|
+
}
|
|
3478
|
+
}
|
|
3479
|
+
get options() {
|
|
3480
|
+
return this._def.options;
|
|
3481
|
+
}
|
|
3482
|
+
};
|
|
3483
|
+
ZodUnion.create = (types, params) => {
|
|
3484
|
+
return new ZodUnion({
|
|
3485
|
+
options: types,
|
|
3486
|
+
typeName: ZodFirstPartyTypeKind.ZodUnion,
|
|
3487
|
+
...processCreateParams(params)
|
|
3488
|
+
});
|
|
3489
|
+
};
|
|
3490
|
+
var getDiscriminator = (type) => {
|
|
3491
|
+
if (type instanceof ZodLazy) {
|
|
3492
|
+
return getDiscriminator(type.schema);
|
|
3493
|
+
} else if (type instanceof ZodEffects) {
|
|
3494
|
+
return getDiscriminator(type.innerType());
|
|
3495
|
+
} else if (type instanceof ZodLiteral) {
|
|
3496
|
+
return [type.value];
|
|
3497
|
+
} else if (type instanceof ZodEnum) {
|
|
3498
|
+
return type.options;
|
|
3499
|
+
} else if (type instanceof ZodNativeEnum) {
|
|
3500
|
+
return util.objectValues(type.enum);
|
|
3501
|
+
} else if (type instanceof ZodDefault) {
|
|
3502
|
+
return getDiscriminator(type._def.innerType);
|
|
3503
|
+
} else if (type instanceof ZodUndefined) {
|
|
3504
|
+
return [void 0];
|
|
3505
|
+
} else if (type instanceof ZodNull) {
|
|
3506
|
+
return [null];
|
|
3507
|
+
} else if (type instanceof ZodOptional) {
|
|
3508
|
+
return [void 0, ...getDiscriminator(type.unwrap())];
|
|
3509
|
+
} else if (type instanceof ZodNullable) {
|
|
3510
|
+
return [null, ...getDiscriminator(type.unwrap())];
|
|
3511
|
+
} else if (type instanceof ZodBranded) {
|
|
3512
|
+
return getDiscriminator(type.unwrap());
|
|
3513
|
+
} else if (type instanceof ZodReadonly) {
|
|
3514
|
+
return getDiscriminator(type.unwrap());
|
|
3515
|
+
} else if (type instanceof ZodCatch) {
|
|
3516
|
+
return getDiscriminator(type._def.innerType);
|
|
3517
|
+
} else {
|
|
3518
|
+
return [];
|
|
3519
|
+
}
|
|
3520
|
+
};
|
|
3521
|
+
var ZodDiscriminatedUnion = class _ZodDiscriminatedUnion extends ZodType {
|
|
3522
|
+
_parse(input) {
|
|
3523
|
+
const { ctx } = this._processInputParams(input);
|
|
3524
|
+
if (ctx.parsedType !== ZodParsedType.object) {
|
|
3525
|
+
addIssueToContext(ctx, {
|
|
3526
|
+
code: ZodIssueCode.invalid_type,
|
|
3527
|
+
expected: ZodParsedType.object,
|
|
3528
|
+
received: ctx.parsedType
|
|
3529
|
+
});
|
|
3530
|
+
return INVALID;
|
|
3531
|
+
}
|
|
3532
|
+
const discriminator = this.discriminator;
|
|
3533
|
+
const discriminatorValue = ctx.data[discriminator];
|
|
3534
|
+
const option = this.optionsMap.get(discriminatorValue);
|
|
3535
|
+
if (!option) {
|
|
3536
|
+
addIssueToContext(ctx, {
|
|
3537
|
+
code: ZodIssueCode.invalid_union_discriminator,
|
|
3538
|
+
options: Array.from(this.optionsMap.keys()),
|
|
3539
|
+
path: [discriminator]
|
|
3540
|
+
});
|
|
3541
|
+
return INVALID;
|
|
3542
|
+
}
|
|
3543
|
+
if (ctx.common.async) {
|
|
3544
|
+
return option._parseAsync({
|
|
3545
|
+
data: ctx.data,
|
|
3546
|
+
path: ctx.path,
|
|
3547
|
+
parent: ctx
|
|
3548
|
+
});
|
|
3549
|
+
} else {
|
|
3550
|
+
return option._parseSync({
|
|
3551
|
+
data: ctx.data,
|
|
3552
|
+
path: ctx.path,
|
|
3553
|
+
parent: ctx
|
|
3554
|
+
});
|
|
3555
|
+
}
|
|
3556
|
+
}
|
|
3557
|
+
get discriminator() {
|
|
3558
|
+
return this._def.discriminator;
|
|
3559
|
+
}
|
|
3560
|
+
get options() {
|
|
3561
|
+
return this._def.options;
|
|
3562
|
+
}
|
|
3563
|
+
get optionsMap() {
|
|
3564
|
+
return this._def.optionsMap;
|
|
3565
|
+
}
|
|
3566
|
+
/**
|
|
3567
|
+
* The constructor of the discriminated union schema. Its behaviour is very similar to that of the normal z.union() constructor.
|
|
3568
|
+
* However, it only allows a union of objects, all of which need to share a discriminator property. This property must
|
|
3569
|
+
* have a different value for each object in the union.
|
|
3570
|
+
* @param discriminator the name of the discriminator property
|
|
3571
|
+
* @param types an array of object schemas
|
|
3572
|
+
* @param params
|
|
3573
|
+
*/
|
|
3574
|
+
static create(discriminator, options, params) {
|
|
3575
|
+
const optionsMap = /* @__PURE__ */ new Map();
|
|
3576
|
+
for (const type of options) {
|
|
3577
|
+
const discriminatorValues = getDiscriminator(type.shape[discriminator]);
|
|
3578
|
+
if (!discriminatorValues.length) {
|
|
3579
|
+
throw new Error(`A discriminator value for key \`${discriminator}\` could not be extracted from all schema options`);
|
|
3580
|
+
}
|
|
3581
|
+
for (const value of discriminatorValues) {
|
|
3582
|
+
if (optionsMap.has(value)) {
|
|
3583
|
+
throw new Error(`Discriminator property ${String(discriminator)} has duplicate value ${String(value)}`);
|
|
3584
|
+
}
|
|
3585
|
+
optionsMap.set(value, type);
|
|
3586
|
+
}
|
|
3587
|
+
}
|
|
3588
|
+
return new _ZodDiscriminatedUnion({
|
|
3589
|
+
typeName: ZodFirstPartyTypeKind.ZodDiscriminatedUnion,
|
|
3590
|
+
discriminator,
|
|
3591
|
+
options,
|
|
3592
|
+
optionsMap,
|
|
3593
|
+
...processCreateParams(params)
|
|
3594
|
+
});
|
|
3595
|
+
}
|
|
3596
|
+
};
|
|
3597
|
+
function mergeValues(a, b) {
|
|
3598
|
+
const aType = getParsedType(a);
|
|
3599
|
+
const bType = getParsedType(b);
|
|
3600
|
+
if (a === b) {
|
|
3601
|
+
return { valid: true, data: a };
|
|
3602
|
+
} else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
|
|
3603
|
+
const bKeys = util.objectKeys(b);
|
|
3604
|
+
const sharedKeys = util.objectKeys(a).filter((key) => bKeys.indexOf(key) !== -1);
|
|
3605
|
+
const newObj = { ...a, ...b };
|
|
3606
|
+
for (const key of sharedKeys) {
|
|
3607
|
+
const sharedValue = mergeValues(a[key], b[key]);
|
|
3608
|
+
if (!sharedValue.valid) {
|
|
3609
|
+
return { valid: false };
|
|
3610
|
+
}
|
|
3611
|
+
newObj[key] = sharedValue.data;
|
|
3612
|
+
}
|
|
3613
|
+
return { valid: true, data: newObj };
|
|
3614
|
+
} else if (aType === ZodParsedType.array && bType === ZodParsedType.array) {
|
|
3615
|
+
if (a.length !== b.length) {
|
|
3616
|
+
return { valid: false };
|
|
3617
|
+
}
|
|
3618
|
+
const newArray = [];
|
|
3619
|
+
for (let index = 0; index < a.length; index++) {
|
|
3620
|
+
const itemA = a[index];
|
|
3621
|
+
const itemB = b[index];
|
|
3622
|
+
const sharedValue = mergeValues(itemA, itemB);
|
|
3623
|
+
if (!sharedValue.valid) {
|
|
3624
|
+
return { valid: false };
|
|
3625
|
+
}
|
|
3626
|
+
newArray.push(sharedValue.data);
|
|
3627
|
+
}
|
|
3628
|
+
return { valid: true, data: newArray };
|
|
3629
|
+
} else if (aType === ZodParsedType.date && bType === ZodParsedType.date && +a === +b) {
|
|
3630
|
+
return { valid: true, data: a };
|
|
3631
|
+
} else {
|
|
3632
|
+
return { valid: false };
|
|
3633
|
+
}
|
|
3634
|
+
}
|
|
3635
|
+
var ZodIntersection = class extends ZodType {
|
|
3636
|
+
_parse(input) {
|
|
3637
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3638
|
+
const handleParsed = (parsedLeft, parsedRight) => {
|
|
3639
|
+
if (isAborted(parsedLeft) || isAborted(parsedRight)) {
|
|
3640
|
+
return INVALID;
|
|
3641
|
+
}
|
|
3642
|
+
const merged = mergeValues(parsedLeft.value, parsedRight.value);
|
|
3643
|
+
if (!merged.valid) {
|
|
3644
|
+
addIssueToContext(ctx, {
|
|
3645
|
+
code: ZodIssueCode.invalid_intersection_types
|
|
3646
|
+
});
|
|
3647
|
+
return INVALID;
|
|
3648
|
+
}
|
|
3649
|
+
if (isDirty(parsedLeft) || isDirty(parsedRight)) {
|
|
3650
|
+
status.dirty();
|
|
3651
|
+
}
|
|
3652
|
+
return { status: status.value, value: merged.data };
|
|
3653
|
+
};
|
|
3654
|
+
if (ctx.common.async) {
|
|
3655
|
+
return Promise.all([
|
|
3656
|
+
this._def.left._parseAsync({
|
|
3657
|
+
data: ctx.data,
|
|
3658
|
+
path: ctx.path,
|
|
3659
|
+
parent: ctx
|
|
3660
|
+
}),
|
|
3661
|
+
this._def.right._parseAsync({
|
|
3662
|
+
data: ctx.data,
|
|
3663
|
+
path: ctx.path,
|
|
3664
|
+
parent: ctx
|
|
3665
|
+
})
|
|
3666
|
+
]).then(([left, right]) => handleParsed(left, right));
|
|
3667
|
+
} else {
|
|
3668
|
+
return handleParsed(this._def.left._parseSync({
|
|
3669
|
+
data: ctx.data,
|
|
3670
|
+
path: ctx.path,
|
|
3671
|
+
parent: ctx
|
|
3672
|
+
}), this._def.right._parseSync({
|
|
3673
|
+
data: ctx.data,
|
|
3674
|
+
path: ctx.path,
|
|
3675
|
+
parent: ctx
|
|
3676
|
+
}));
|
|
3677
|
+
}
|
|
3678
|
+
}
|
|
3679
|
+
};
|
|
3680
|
+
ZodIntersection.create = (left, right, params) => {
|
|
3681
|
+
return new ZodIntersection({
|
|
3682
|
+
left,
|
|
3683
|
+
right,
|
|
3684
|
+
typeName: ZodFirstPartyTypeKind.ZodIntersection,
|
|
3685
|
+
...processCreateParams(params)
|
|
3686
|
+
});
|
|
3687
|
+
};
|
|
3688
|
+
var ZodTuple = class _ZodTuple extends ZodType {
|
|
3689
|
+
_parse(input) {
|
|
3690
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3691
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
3692
|
+
addIssueToContext(ctx, {
|
|
3693
|
+
code: ZodIssueCode.invalid_type,
|
|
3694
|
+
expected: ZodParsedType.array,
|
|
3695
|
+
received: ctx.parsedType
|
|
3696
|
+
});
|
|
3697
|
+
return INVALID;
|
|
3698
|
+
}
|
|
3699
|
+
if (ctx.data.length < this._def.items.length) {
|
|
3700
|
+
addIssueToContext(ctx, {
|
|
3701
|
+
code: ZodIssueCode.too_small,
|
|
3702
|
+
minimum: this._def.items.length,
|
|
3703
|
+
inclusive: true,
|
|
3704
|
+
exact: false,
|
|
3705
|
+
type: "array"
|
|
3706
|
+
});
|
|
3707
|
+
return INVALID;
|
|
3708
|
+
}
|
|
3709
|
+
const rest = this._def.rest;
|
|
3710
|
+
if (!rest && ctx.data.length > this._def.items.length) {
|
|
3711
|
+
addIssueToContext(ctx, {
|
|
3712
|
+
code: ZodIssueCode.too_big,
|
|
3713
|
+
maximum: this._def.items.length,
|
|
3714
|
+
inclusive: true,
|
|
3715
|
+
exact: false,
|
|
3716
|
+
type: "array"
|
|
3717
|
+
});
|
|
3718
|
+
status.dirty();
|
|
3719
|
+
}
|
|
3720
|
+
const items = [...ctx.data].map((item, itemIndex) => {
|
|
3721
|
+
const schema = this._def.items[itemIndex] || this._def.rest;
|
|
3722
|
+
if (!schema)
|
|
3723
|
+
return null;
|
|
3724
|
+
return schema._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
|
|
3725
|
+
}).filter((x) => !!x);
|
|
3726
|
+
if (ctx.common.async) {
|
|
3727
|
+
return Promise.all(items).then((results) => {
|
|
3728
|
+
return ParseStatus.mergeArray(status, results);
|
|
3729
|
+
});
|
|
3730
|
+
} else {
|
|
3731
|
+
return ParseStatus.mergeArray(status, items);
|
|
3732
|
+
}
|
|
3733
|
+
}
|
|
3734
|
+
get items() {
|
|
3735
|
+
return this._def.items;
|
|
3736
|
+
}
|
|
3737
|
+
rest(rest) {
|
|
3738
|
+
return new _ZodTuple({
|
|
3739
|
+
...this._def,
|
|
3740
|
+
rest
|
|
3741
|
+
});
|
|
3742
|
+
}
|
|
3743
|
+
};
|
|
3744
|
+
ZodTuple.create = (schemas, params) => {
|
|
3745
|
+
if (!Array.isArray(schemas)) {
|
|
3746
|
+
throw new Error("You must pass an array of schemas to z.tuple([ ... ])");
|
|
3747
|
+
}
|
|
3748
|
+
return new ZodTuple({
|
|
3749
|
+
items: schemas,
|
|
3750
|
+
typeName: ZodFirstPartyTypeKind.ZodTuple,
|
|
3751
|
+
rest: null,
|
|
3752
|
+
...processCreateParams(params)
|
|
3753
|
+
});
|
|
3754
|
+
};
|
|
3755
|
+
var ZodRecord = class _ZodRecord extends ZodType {
|
|
3756
|
+
get keySchema() {
|
|
3757
|
+
return this._def.keyType;
|
|
3758
|
+
}
|
|
3759
|
+
get valueSchema() {
|
|
3760
|
+
return this._def.valueType;
|
|
3761
|
+
}
|
|
3762
|
+
_parse(input) {
|
|
3763
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3764
|
+
if (ctx.parsedType !== ZodParsedType.object) {
|
|
3765
|
+
addIssueToContext(ctx, {
|
|
3766
|
+
code: ZodIssueCode.invalid_type,
|
|
3767
|
+
expected: ZodParsedType.object,
|
|
3768
|
+
received: ctx.parsedType
|
|
3769
|
+
});
|
|
3770
|
+
return INVALID;
|
|
3771
|
+
}
|
|
3772
|
+
const pairs = [];
|
|
3773
|
+
const keyType = this._def.keyType;
|
|
3774
|
+
const valueType = this._def.valueType;
|
|
3775
|
+
for (const key in ctx.data) {
|
|
3776
|
+
pairs.push({
|
|
3777
|
+
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
|
|
3778
|
+
value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
|
|
3779
|
+
alwaysSet: key in ctx.data
|
|
3780
|
+
});
|
|
3781
|
+
}
|
|
3782
|
+
if (ctx.common.async) {
|
|
3783
|
+
return ParseStatus.mergeObjectAsync(status, pairs);
|
|
3784
|
+
} else {
|
|
3785
|
+
return ParseStatus.mergeObjectSync(status, pairs);
|
|
3786
|
+
}
|
|
3787
|
+
}
|
|
3788
|
+
get element() {
|
|
3789
|
+
return this._def.valueType;
|
|
3790
|
+
}
|
|
3791
|
+
static create(first, second, third) {
|
|
3792
|
+
if (second instanceof ZodType) {
|
|
3793
|
+
return new _ZodRecord({
|
|
3794
|
+
keyType: first,
|
|
3795
|
+
valueType: second,
|
|
3796
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
3797
|
+
...processCreateParams(third)
|
|
3798
|
+
});
|
|
3799
|
+
}
|
|
3800
|
+
return new _ZodRecord({
|
|
3801
|
+
keyType: ZodString.create(),
|
|
3802
|
+
valueType: first,
|
|
3803
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
3804
|
+
...processCreateParams(second)
|
|
3805
|
+
});
|
|
3806
|
+
}
|
|
3807
|
+
};
|
|
3808
|
+
var ZodMap = class extends ZodType {
|
|
3809
|
+
get keySchema() {
|
|
3810
|
+
return this._def.keyType;
|
|
3811
|
+
}
|
|
3812
|
+
get valueSchema() {
|
|
3813
|
+
return this._def.valueType;
|
|
3814
|
+
}
|
|
3815
|
+
_parse(input) {
|
|
3816
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3817
|
+
if (ctx.parsedType !== ZodParsedType.map) {
|
|
3818
|
+
addIssueToContext(ctx, {
|
|
3819
|
+
code: ZodIssueCode.invalid_type,
|
|
3820
|
+
expected: ZodParsedType.map,
|
|
3821
|
+
received: ctx.parsedType
|
|
3822
|
+
});
|
|
3823
|
+
return INVALID;
|
|
3824
|
+
}
|
|
3825
|
+
const keyType = this._def.keyType;
|
|
3826
|
+
const valueType = this._def.valueType;
|
|
3827
|
+
const pairs = [...ctx.data.entries()].map(([key, value], index) => {
|
|
3828
|
+
return {
|
|
3829
|
+
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, [index, "key"])),
|
|
3830
|
+
value: valueType._parse(new ParseInputLazyPath(ctx, value, ctx.path, [index, "value"]))
|
|
3831
|
+
};
|
|
3832
|
+
});
|
|
3833
|
+
if (ctx.common.async) {
|
|
3834
|
+
const finalMap = /* @__PURE__ */ new Map();
|
|
3835
|
+
return Promise.resolve().then(async () => {
|
|
3836
|
+
for (const pair of pairs) {
|
|
3837
|
+
const key = await pair.key;
|
|
3838
|
+
const value = await pair.value;
|
|
3839
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
3840
|
+
return INVALID;
|
|
3841
|
+
}
|
|
3842
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
3843
|
+
status.dirty();
|
|
3844
|
+
}
|
|
3845
|
+
finalMap.set(key.value, value.value);
|
|
3846
|
+
}
|
|
3847
|
+
return { status: status.value, value: finalMap };
|
|
3848
|
+
});
|
|
3849
|
+
} else {
|
|
3850
|
+
const finalMap = /* @__PURE__ */ new Map();
|
|
3851
|
+
for (const pair of pairs) {
|
|
3852
|
+
const key = pair.key;
|
|
3853
|
+
const value = pair.value;
|
|
3854
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
3855
|
+
return INVALID;
|
|
3856
|
+
}
|
|
3857
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
3858
|
+
status.dirty();
|
|
3859
|
+
}
|
|
3860
|
+
finalMap.set(key.value, value.value);
|
|
3861
|
+
}
|
|
3862
|
+
return { status: status.value, value: finalMap };
|
|
3863
|
+
}
|
|
3864
|
+
}
|
|
3865
|
+
};
|
|
3866
|
+
ZodMap.create = (keyType, valueType, params) => {
|
|
3867
|
+
return new ZodMap({
|
|
3868
|
+
valueType,
|
|
3869
|
+
keyType,
|
|
3870
|
+
typeName: ZodFirstPartyTypeKind.ZodMap,
|
|
3871
|
+
...processCreateParams(params)
|
|
3872
|
+
});
|
|
3873
|
+
};
|
|
3874
|
+
var ZodSet = class _ZodSet extends ZodType {
|
|
3875
|
+
_parse(input) {
|
|
3876
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3877
|
+
if (ctx.parsedType !== ZodParsedType.set) {
|
|
3878
|
+
addIssueToContext(ctx, {
|
|
3879
|
+
code: ZodIssueCode.invalid_type,
|
|
3880
|
+
expected: ZodParsedType.set,
|
|
3881
|
+
received: ctx.parsedType
|
|
3882
|
+
});
|
|
3883
|
+
return INVALID;
|
|
3884
|
+
}
|
|
3885
|
+
const def = this._def;
|
|
3886
|
+
if (def.minSize !== null) {
|
|
3887
|
+
if (ctx.data.size < def.minSize.value) {
|
|
3888
|
+
addIssueToContext(ctx, {
|
|
3889
|
+
code: ZodIssueCode.too_small,
|
|
3890
|
+
minimum: def.minSize.value,
|
|
3891
|
+
type: "set",
|
|
3892
|
+
inclusive: true,
|
|
3893
|
+
exact: false,
|
|
3894
|
+
message: def.minSize.message
|
|
3895
|
+
});
|
|
3896
|
+
status.dirty();
|
|
3897
|
+
}
|
|
3898
|
+
}
|
|
3899
|
+
if (def.maxSize !== null) {
|
|
3900
|
+
if (ctx.data.size > def.maxSize.value) {
|
|
3901
|
+
addIssueToContext(ctx, {
|
|
3902
|
+
code: ZodIssueCode.too_big,
|
|
3903
|
+
maximum: def.maxSize.value,
|
|
3904
|
+
type: "set",
|
|
3905
|
+
inclusive: true,
|
|
3906
|
+
exact: false,
|
|
3907
|
+
message: def.maxSize.message
|
|
3908
|
+
});
|
|
3909
|
+
status.dirty();
|
|
3910
|
+
}
|
|
3911
|
+
}
|
|
3912
|
+
const valueType = this._def.valueType;
|
|
3913
|
+
function finalizeSet(elements2) {
|
|
3914
|
+
const parsedSet = /* @__PURE__ */ new Set();
|
|
3915
|
+
for (const element of elements2) {
|
|
3916
|
+
if (element.status === "aborted")
|
|
3917
|
+
return INVALID;
|
|
3918
|
+
if (element.status === "dirty")
|
|
3919
|
+
status.dirty();
|
|
3920
|
+
parsedSet.add(element.value);
|
|
3921
|
+
}
|
|
3922
|
+
return { status: status.value, value: parsedSet };
|
|
3923
|
+
}
|
|
3924
|
+
const elements = [...ctx.data.values()].map((item, i) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i)));
|
|
3925
|
+
if (ctx.common.async) {
|
|
3926
|
+
return Promise.all(elements).then((elements2) => finalizeSet(elements2));
|
|
3927
|
+
} else {
|
|
3928
|
+
return finalizeSet(elements);
|
|
3929
|
+
}
|
|
3930
|
+
}
|
|
3931
|
+
min(minSize, message) {
|
|
3932
|
+
return new _ZodSet({
|
|
3933
|
+
...this._def,
|
|
3934
|
+
minSize: { value: minSize, message: errorUtil.toString(message) }
|
|
3935
|
+
});
|
|
3936
|
+
}
|
|
3937
|
+
max(maxSize, message) {
|
|
3938
|
+
return new _ZodSet({
|
|
3939
|
+
...this._def,
|
|
3940
|
+
maxSize: { value: maxSize, message: errorUtil.toString(message) }
|
|
3941
|
+
});
|
|
3942
|
+
}
|
|
3943
|
+
size(size, message) {
|
|
3944
|
+
return this.min(size, message).max(size, message);
|
|
3945
|
+
}
|
|
3946
|
+
nonempty(message) {
|
|
3947
|
+
return this.min(1, message);
|
|
3948
|
+
}
|
|
3949
|
+
};
|
|
3950
|
+
ZodSet.create = (valueType, params) => {
|
|
3951
|
+
return new ZodSet({
|
|
3952
|
+
valueType,
|
|
3953
|
+
minSize: null,
|
|
3954
|
+
maxSize: null,
|
|
3955
|
+
typeName: ZodFirstPartyTypeKind.ZodSet,
|
|
3956
|
+
...processCreateParams(params)
|
|
3957
|
+
});
|
|
3958
|
+
};
|
|
3959
|
+
var ZodFunction = class _ZodFunction extends ZodType {
|
|
3960
|
+
constructor() {
|
|
3961
|
+
super(...arguments);
|
|
3962
|
+
this.validate = this.implement;
|
|
3963
|
+
}
|
|
3964
|
+
_parse(input) {
|
|
3965
|
+
const { ctx } = this._processInputParams(input);
|
|
3966
|
+
if (ctx.parsedType !== ZodParsedType.function) {
|
|
3967
|
+
addIssueToContext(ctx, {
|
|
3968
|
+
code: ZodIssueCode.invalid_type,
|
|
3969
|
+
expected: ZodParsedType.function,
|
|
3970
|
+
received: ctx.parsedType
|
|
3971
|
+
});
|
|
3972
|
+
return INVALID;
|
|
3973
|
+
}
|
|
3974
|
+
function makeArgsIssue(args, error) {
|
|
3975
|
+
return makeIssue({
|
|
3976
|
+
data: args,
|
|
3977
|
+
path: ctx.path,
|
|
3978
|
+
errorMaps: [
|
|
3979
|
+
ctx.common.contextualErrorMap,
|
|
3980
|
+
ctx.schemaErrorMap,
|
|
3981
|
+
getErrorMap(),
|
|
3982
|
+
errorMap
|
|
3983
|
+
].filter((x) => !!x),
|
|
3984
|
+
issueData: {
|
|
3985
|
+
code: ZodIssueCode.invalid_arguments,
|
|
3986
|
+
argumentsError: error
|
|
3987
|
+
}
|
|
3988
|
+
});
|
|
3989
|
+
}
|
|
3990
|
+
function makeReturnsIssue(returns, error) {
|
|
3991
|
+
return makeIssue({
|
|
3992
|
+
data: returns,
|
|
3993
|
+
path: ctx.path,
|
|
3994
|
+
errorMaps: [
|
|
3995
|
+
ctx.common.contextualErrorMap,
|
|
3996
|
+
ctx.schemaErrorMap,
|
|
3997
|
+
getErrorMap(),
|
|
3998
|
+
errorMap
|
|
3999
|
+
].filter((x) => !!x),
|
|
4000
|
+
issueData: {
|
|
4001
|
+
code: ZodIssueCode.invalid_return_type,
|
|
4002
|
+
returnTypeError: error
|
|
4003
|
+
}
|
|
4004
|
+
});
|
|
4005
|
+
}
|
|
4006
|
+
const params = { errorMap: ctx.common.contextualErrorMap };
|
|
4007
|
+
const fn = ctx.data;
|
|
4008
|
+
if (this._def.returns instanceof ZodPromise) {
|
|
4009
|
+
const me = this;
|
|
4010
|
+
return OK(async function(...args) {
|
|
4011
|
+
const error = new ZodError([]);
|
|
4012
|
+
const parsedArgs = await me._def.args.parseAsync(args, params).catch((e) => {
|
|
4013
|
+
error.addIssue(makeArgsIssue(args, e));
|
|
4014
|
+
throw error;
|
|
4015
|
+
});
|
|
4016
|
+
const result = await Reflect.apply(fn, this, parsedArgs);
|
|
4017
|
+
const parsedReturns = await me._def.returns._def.type.parseAsync(result, params).catch((e) => {
|
|
4018
|
+
error.addIssue(makeReturnsIssue(result, e));
|
|
4019
|
+
throw error;
|
|
4020
|
+
});
|
|
4021
|
+
return parsedReturns;
|
|
4022
|
+
});
|
|
4023
|
+
} else {
|
|
4024
|
+
const me = this;
|
|
4025
|
+
return OK(function(...args) {
|
|
4026
|
+
const parsedArgs = me._def.args.safeParse(args, params);
|
|
4027
|
+
if (!parsedArgs.success) {
|
|
4028
|
+
throw new ZodError([makeArgsIssue(args, parsedArgs.error)]);
|
|
4029
|
+
}
|
|
4030
|
+
const result = Reflect.apply(fn, this, parsedArgs.data);
|
|
4031
|
+
const parsedReturns = me._def.returns.safeParse(result, params);
|
|
4032
|
+
if (!parsedReturns.success) {
|
|
4033
|
+
throw new ZodError([makeReturnsIssue(result, parsedReturns.error)]);
|
|
4034
|
+
}
|
|
4035
|
+
return parsedReturns.data;
|
|
4036
|
+
});
|
|
4037
|
+
}
|
|
4038
|
+
}
|
|
4039
|
+
parameters() {
|
|
4040
|
+
return this._def.args;
|
|
4041
|
+
}
|
|
4042
|
+
returnType() {
|
|
4043
|
+
return this._def.returns;
|
|
4044
|
+
}
|
|
4045
|
+
args(...items) {
|
|
4046
|
+
return new _ZodFunction({
|
|
4047
|
+
...this._def,
|
|
4048
|
+
args: ZodTuple.create(items).rest(ZodUnknown.create())
|
|
4049
|
+
});
|
|
4050
|
+
}
|
|
4051
|
+
returns(returnType) {
|
|
4052
|
+
return new _ZodFunction({
|
|
4053
|
+
...this._def,
|
|
4054
|
+
returns: returnType
|
|
4055
|
+
});
|
|
4056
|
+
}
|
|
4057
|
+
implement(func) {
|
|
4058
|
+
const validatedFunc = this.parse(func);
|
|
4059
|
+
return validatedFunc;
|
|
4060
|
+
}
|
|
4061
|
+
strictImplement(func) {
|
|
4062
|
+
const validatedFunc = this.parse(func);
|
|
4063
|
+
return validatedFunc;
|
|
4064
|
+
}
|
|
4065
|
+
static create(args, returns, params) {
|
|
4066
|
+
return new _ZodFunction({
|
|
4067
|
+
args: args ? args : ZodTuple.create([]).rest(ZodUnknown.create()),
|
|
4068
|
+
returns: returns || ZodUnknown.create(),
|
|
4069
|
+
typeName: ZodFirstPartyTypeKind.ZodFunction,
|
|
4070
|
+
...processCreateParams(params)
|
|
4071
|
+
});
|
|
4072
|
+
}
|
|
4073
|
+
};
|
|
4074
|
+
var ZodLazy = class extends ZodType {
|
|
4075
|
+
get schema() {
|
|
4076
|
+
return this._def.getter();
|
|
4077
|
+
}
|
|
4078
|
+
_parse(input) {
|
|
4079
|
+
const { ctx } = this._processInputParams(input);
|
|
4080
|
+
const lazySchema = this._def.getter();
|
|
4081
|
+
return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx });
|
|
4082
|
+
}
|
|
4083
|
+
};
|
|
4084
|
+
ZodLazy.create = (getter, params) => {
|
|
4085
|
+
return new ZodLazy({
|
|
4086
|
+
getter,
|
|
4087
|
+
typeName: ZodFirstPartyTypeKind.ZodLazy,
|
|
4088
|
+
...processCreateParams(params)
|
|
4089
|
+
});
|
|
4090
|
+
};
|
|
4091
|
+
var ZodLiteral = class extends ZodType {
|
|
4092
|
+
_parse(input) {
|
|
4093
|
+
if (input.data !== this._def.value) {
|
|
4094
|
+
const ctx = this._getOrReturnCtx(input);
|
|
4095
|
+
addIssueToContext(ctx, {
|
|
4096
|
+
received: ctx.data,
|
|
4097
|
+
code: ZodIssueCode.invalid_literal,
|
|
4098
|
+
expected: this._def.value
|
|
4099
|
+
});
|
|
4100
|
+
return INVALID;
|
|
4101
|
+
}
|
|
4102
|
+
return { status: "valid", value: input.data };
|
|
4103
|
+
}
|
|
4104
|
+
get value() {
|
|
4105
|
+
return this._def.value;
|
|
4106
|
+
}
|
|
4107
|
+
};
|
|
4108
|
+
ZodLiteral.create = (value, params) => {
|
|
4109
|
+
return new ZodLiteral({
|
|
4110
|
+
value,
|
|
4111
|
+
typeName: ZodFirstPartyTypeKind.ZodLiteral,
|
|
4112
|
+
...processCreateParams(params)
|
|
4113
|
+
});
|
|
4114
|
+
};
|
|
4115
|
+
function createZodEnum(values, params) {
|
|
4116
|
+
return new ZodEnum({
|
|
4117
|
+
values,
|
|
4118
|
+
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
4119
|
+
...processCreateParams(params)
|
|
4120
|
+
});
|
|
4121
|
+
}
|
|
4122
|
+
var ZodEnum = class _ZodEnum extends ZodType {
|
|
4123
|
+
constructor() {
|
|
4124
|
+
super(...arguments);
|
|
4125
|
+
_ZodEnum_cache.set(this, void 0);
|
|
4126
|
+
}
|
|
4127
|
+
_parse(input) {
|
|
4128
|
+
if (typeof input.data !== "string") {
|
|
4129
|
+
const ctx = this._getOrReturnCtx(input);
|
|
4130
|
+
const expectedValues = this._def.values;
|
|
4131
|
+
addIssueToContext(ctx, {
|
|
4132
|
+
expected: util.joinValues(expectedValues),
|
|
4133
|
+
received: ctx.parsedType,
|
|
4134
|
+
code: ZodIssueCode.invalid_type
|
|
4135
|
+
});
|
|
4136
|
+
return INVALID;
|
|
4137
|
+
}
|
|
4138
|
+
if (!__classPrivateFieldGet(this, _ZodEnum_cache, "f")) {
|
|
4139
|
+
__classPrivateFieldSet(this, _ZodEnum_cache, new Set(this._def.values), "f");
|
|
4140
|
+
}
|
|
4141
|
+
if (!__classPrivateFieldGet(this, _ZodEnum_cache, "f").has(input.data)) {
|
|
4142
|
+
const ctx = this._getOrReturnCtx(input);
|
|
4143
|
+
const expectedValues = this._def.values;
|
|
4144
|
+
addIssueToContext(ctx, {
|
|
4145
|
+
received: ctx.data,
|
|
4146
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
4147
|
+
options: expectedValues
|
|
4148
|
+
});
|
|
4149
|
+
return INVALID;
|
|
4150
|
+
}
|
|
4151
|
+
return OK(input.data);
|
|
4152
|
+
}
|
|
4153
|
+
get options() {
|
|
4154
|
+
return this._def.values;
|
|
4155
|
+
}
|
|
4156
|
+
get enum() {
|
|
4157
|
+
const enumValues = {};
|
|
4158
|
+
for (const val of this._def.values) {
|
|
4159
|
+
enumValues[val] = val;
|
|
4160
|
+
}
|
|
4161
|
+
return enumValues;
|
|
4162
|
+
}
|
|
4163
|
+
get Values() {
|
|
4164
|
+
const enumValues = {};
|
|
4165
|
+
for (const val of this._def.values) {
|
|
4166
|
+
enumValues[val] = val;
|
|
4167
|
+
}
|
|
4168
|
+
return enumValues;
|
|
4169
|
+
}
|
|
4170
|
+
get Enum() {
|
|
4171
|
+
const enumValues = {};
|
|
4172
|
+
for (const val of this._def.values) {
|
|
4173
|
+
enumValues[val] = val;
|
|
4174
|
+
}
|
|
4175
|
+
return enumValues;
|
|
4176
|
+
}
|
|
4177
|
+
extract(values, newDef = this._def) {
|
|
4178
|
+
return _ZodEnum.create(values, {
|
|
4179
|
+
...this._def,
|
|
4180
|
+
...newDef
|
|
4181
|
+
});
|
|
4182
|
+
}
|
|
4183
|
+
exclude(values, newDef = this._def) {
|
|
4184
|
+
return _ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
|
|
4185
|
+
...this._def,
|
|
4186
|
+
...newDef
|
|
4187
|
+
});
|
|
4188
|
+
}
|
|
4189
|
+
};
|
|
4190
|
+
_ZodEnum_cache = /* @__PURE__ */ new WeakMap();
|
|
4191
|
+
ZodEnum.create = createZodEnum;
|
|
4192
|
+
var ZodNativeEnum = class extends ZodType {
|
|
4193
|
+
constructor() {
|
|
4194
|
+
super(...arguments);
|
|
4195
|
+
_ZodNativeEnum_cache.set(this, void 0);
|
|
4196
|
+
}
|
|
4197
|
+
_parse(input) {
|
|
4198
|
+
const nativeEnumValues = util.getValidEnumValues(this._def.values);
|
|
4199
|
+
const ctx = this._getOrReturnCtx(input);
|
|
4200
|
+
if (ctx.parsedType !== ZodParsedType.string && ctx.parsedType !== ZodParsedType.number) {
|
|
4201
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
4202
|
+
addIssueToContext(ctx, {
|
|
4203
|
+
expected: util.joinValues(expectedValues),
|
|
4204
|
+
received: ctx.parsedType,
|
|
4205
|
+
code: ZodIssueCode.invalid_type
|
|
4206
|
+
});
|
|
4207
|
+
return INVALID;
|
|
4208
|
+
}
|
|
4209
|
+
if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache, "f")) {
|
|
4210
|
+
__classPrivateFieldSet(this, _ZodNativeEnum_cache, new Set(util.getValidEnumValues(this._def.values)), "f");
|
|
4211
|
+
}
|
|
4212
|
+
if (!__classPrivateFieldGet(this, _ZodNativeEnum_cache, "f").has(input.data)) {
|
|
4213
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
4214
|
+
addIssueToContext(ctx, {
|
|
4215
|
+
received: ctx.data,
|
|
4216
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
4217
|
+
options: expectedValues
|
|
4218
|
+
});
|
|
4219
|
+
return INVALID;
|
|
4220
|
+
}
|
|
4221
|
+
return OK(input.data);
|
|
4222
|
+
}
|
|
4223
|
+
get enum() {
|
|
4224
|
+
return this._def.values;
|
|
4225
|
+
}
|
|
4226
|
+
};
|
|
4227
|
+
_ZodNativeEnum_cache = /* @__PURE__ */ new WeakMap();
|
|
4228
|
+
ZodNativeEnum.create = (values, params) => {
|
|
4229
|
+
return new ZodNativeEnum({
|
|
4230
|
+
values,
|
|
4231
|
+
typeName: ZodFirstPartyTypeKind.ZodNativeEnum,
|
|
4232
|
+
...processCreateParams(params)
|
|
4233
|
+
});
|
|
4234
|
+
};
|
|
4235
|
+
var ZodPromise = class extends ZodType {
|
|
4236
|
+
unwrap() {
|
|
4237
|
+
return this._def.type;
|
|
4238
|
+
}
|
|
4239
|
+
_parse(input) {
|
|
4240
|
+
const { ctx } = this._processInputParams(input);
|
|
4241
|
+
if (ctx.parsedType !== ZodParsedType.promise && ctx.common.async === false) {
|
|
4242
|
+
addIssueToContext(ctx, {
|
|
4243
|
+
code: ZodIssueCode.invalid_type,
|
|
4244
|
+
expected: ZodParsedType.promise,
|
|
4245
|
+
received: ctx.parsedType
|
|
4246
|
+
});
|
|
4247
|
+
return INVALID;
|
|
4248
|
+
}
|
|
4249
|
+
const promisified = ctx.parsedType === ZodParsedType.promise ? ctx.data : Promise.resolve(ctx.data);
|
|
4250
|
+
return OK(promisified.then((data) => {
|
|
4251
|
+
return this._def.type.parseAsync(data, {
|
|
4252
|
+
path: ctx.path,
|
|
4253
|
+
errorMap: ctx.common.contextualErrorMap
|
|
4254
|
+
});
|
|
4255
|
+
}));
|
|
4256
|
+
}
|
|
4257
|
+
};
|
|
4258
|
+
ZodPromise.create = (schema, params) => {
|
|
4259
|
+
return new ZodPromise({
|
|
4260
|
+
type: schema,
|
|
4261
|
+
typeName: ZodFirstPartyTypeKind.ZodPromise,
|
|
4262
|
+
...processCreateParams(params)
|
|
4263
|
+
});
|
|
4264
|
+
};
|
|
4265
|
+
var ZodEffects = class extends ZodType {
|
|
4266
|
+
innerType() {
|
|
4267
|
+
return this._def.schema;
|
|
4268
|
+
}
|
|
4269
|
+
sourceType() {
|
|
4270
|
+
return this._def.schema._def.typeName === ZodFirstPartyTypeKind.ZodEffects ? this._def.schema.sourceType() : this._def.schema;
|
|
4271
|
+
}
|
|
4272
|
+
_parse(input) {
|
|
4273
|
+
const { status, ctx } = this._processInputParams(input);
|
|
4274
|
+
const effect = this._def.effect || null;
|
|
4275
|
+
const checkCtx = {
|
|
4276
|
+
addIssue: (arg) => {
|
|
4277
|
+
addIssueToContext(ctx, arg);
|
|
4278
|
+
if (arg.fatal) {
|
|
4279
|
+
status.abort();
|
|
4280
|
+
} else {
|
|
4281
|
+
status.dirty();
|
|
4282
|
+
}
|
|
4283
|
+
},
|
|
4284
|
+
get path() {
|
|
4285
|
+
return ctx.path;
|
|
4286
|
+
}
|
|
4287
|
+
};
|
|
4288
|
+
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
|
|
4289
|
+
if (effect.type === "preprocess") {
|
|
4290
|
+
const processed = effect.transform(ctx.data, checkCtx);
|
|
4291
|
+
if (ctx.common.async) {
|
|
4292
|
+
return Promise.resolve(processed).then(async (processed2) => {
|
|
4293
|
+
if (status.value === "aborted")
|
|
4294
|
+
return INVALID;
|
|
4295
|
+
const result = await this._def.schema._parseAsync({
|
|
4296
|
+
data: processed2,
|
|
4297
|
+
path: ctx.path,
|
|
4298
|
+
parent: ctx
|
|
4299
|
+
});
|
|
4300
|
+
if (result.status === "aborted")
|
|
4301
|
+
return INVALID;
|
|
4302
|
+
if (result.status === "dirty")
|
|
4303
|
+
return DIRTY(result.value);
|
|
4304
|
+
if (status.value === "dirty")
|
|
4305
|
+
return DIRTY(result.value);
|
|
4306
|
+
return result;
|
|
4307
|
+
});
|
|
4308
|
+
} else {
|
|
4309
|
+
if (status.value === "aborted")
|
|
4310
|
+
return INVALID;
|
|
4311
|
+
const result = this._def.schema._parseSync({
|
|
4312
|
+
data: processed,
|
|
4313
|
+
path: ctx.path,
|
|
4314
|
+
parent: ctx
|
|
4315
|
+
});
|
|
4316
|
+
if (result.status === "aborted")
|
|
4317
|
+
return INVALID;
|
|
4318
|
+
if (result.status === "dirty")
|
|
4319
|
+
return DIRTY(result.value);
|
|
4320
|
+
if (status.value === "dirty")
|
|
4321
|
+
return DIRTY(result.value);
|
|
4322
|
+
return result;
|
|
4323
|
+
}
|
|
4324
|
+
}
|
|
4325
|
+
if (effect.type === "refinement") {
|
|
4326
|
+
const executeRefinement = (acc) => {
|
|
4327
|
+
const result = effect.refinement(acc, checkCtx);
|
|
4328
|
+
if (ctx.common.async) {
|
|
4329
|
+
return Promise.resolve(result);
|
|
4330
|
+
}
|
|
4331
|
+
if (result instanceof Promise) {
|
|
4332
|
+
throw new Error("Async refinement encountered during synchronous parse operation. Use .parseAsync instead.");
|
|
4333
|
+
}
|
|
4334
|
+
return acc;
|
|
4335
|
+
};
|
|
4336
|
+
if (ctx.common.async === false) {
|
|
4337
|
+
const inner = this._def.schema._parseSync({
|
|
4338
|
+
data: ctx.data,
|
|
4339
|
+
path: ctx.path,
|
|
4340
|
+
parent: ctx
|
|
4341
|
+
});
|
|
4342
|
+
if (inner.status === "aborted")
|
|
4343
|
+
return INVALID;
|
|
4344
|
+
if (inner.status === "dirty")
|
|
4345
|
+
status.dirty();
|
|
4346
|
+
executeRefinement(inner.value);
|
|
4347
|
+
return { status: status.value, value: inner.value };
|
|
4348
|
+
} else {
|
|
4349
|
+
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((inner) => {
|
|
4350
|
+
if (inner.status === "aborted")
|
|
4351
|
+
return INVALID;
|
|
4352
|
+
if (inner.status === "dirty")
|
|
4353
|
+
status.dirty();
|
|
4354
|
+
return executeRefinement(inner.value).then(() => {
|
|
4355
|
+
return { status: status.value, value: inner.value };
|
|
4356
|
+
});
|
|
4357
|
+
});
|
|
4358
|
+
}
|
|
4359
|
+
}
|
|
4360
|
+
if (effect.type === "transform") {
|
|
4361
|
+
if (ctx.common.async === false) {
|
|
4362
|
+
const base = this._def.schema._parseSync({
|
|
4363
|
+
data: ctx.data,
|
|
4364
|
+
path: ctx.path,
|
|
4365
|
+
parent: ctx
|
|
4366
|
+
});
|
|
4367
|
+
if (!isValid(base))
|
|
4368
|
+
return base;
|
|
4369
|
+
const result = effect.transform(base.value, checkCtx);
|
|
4370
|
+
if (result instanceof Promise) {
|
|
4371
|
+
throw new Error(`Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.`);
|
|
4372
|
+
}
|
|
4373
|
+
return { status: status.value, value: result };
|
|
4374
|
+
} else {
|
|
4375
|
+
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((base) => {
|
|
4376
|
+
if (!isValid(base))
|
|
4377
|
+
return base;
|
|
4378
|
+
return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({ status: status.value, value: result }));
|
|
4379
|
+
});
|
|
4380
|
+
}
|
|
4381
|
+
}
|
|
4382
|
+
util.assertNever(effect);
|
|
4383
|
+
}
|
|
4384
|
+
};
|
|
4385
|
+
ZodEffects.create = (schema, effect, params) => {
|
|
4386
|
+
return new ZodEffects({
|
|
4387
|
+
schema,
|
|
4388
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
4389
|
+
effect,
|
|
4390
|
+
...processCreateParams(params)
|
|
4391
|
+
});
|
|
4392
|
+
};
|
|
4393
|
+
ZodEffects.createWithPreprocess = (preprocess, schema, params) => {
|
|
4394
|
+
return new ZodEffects({
|
|
4395
|
+
schema,
|
|
4396
|
+
effect: { type: "preprocess", transform: preprocess },
|
|
4397
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
4398
|
+
...processCreateParams(params)
|
|
4399
|
+
});
|
|
4400
|
+
};
|
|
4401
|
+
var ZodOptional = class extends ZodType {
|
|
4402
|
+
_parse(input) {
|
|
4403
|
+
const parsedType = this._getType(input);
|
|
4404
|
+
if (parsedType === ZodParsedType.undefined) {
|
|
4405
|
+
return OK(void 0);
|
|
4406
|
+
}
|
|
4407
|
+
return this._def.innerType._parse(input);
|
|
4408
|
+
}
|
|
4409
|
+
unwrap() {
|
|
4410
|
+
return this._def.innerType;
|
|
4411
|
+
}
|
|
4412
|
+
};
|
|
4413
|
+
ZodOptional.create = (type, params) => {
|
|
4414
|
+
return new ZodOptional({
|
|
4415
|
+
innerType: type,
|
|
4416
|
+
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
4417
|
+
...processCreateParams(params)
|
|
4418
|
+
});
|
|
4419
|
+
};
|
|
4420
|
+
var ZodNullable = class extends ZodType {
|
|
4421
|
+
_parse(input) {
|
|
4422
|
+
const parsedType = this._getType(input);
|
|
4423
|
+
if (parsedType === ZodParsedType.null) {
|
|
4424
|
+
return OK(null);
|
|
4425
|
+
}
|
|
4426
|
+
return this._def.innerType._parse(input);
|
|
4427
|
+
}
|
|
4428
|
+
unwrap() {
|
|
4429
|
+
return this._def.innerType;
|
|
4430
|
+
}
|
|
4431
|
+
};
|
|
4432
|
+
ZodNullable.create = (type, params) => {
|
|
4433
|
+
return new ZodNullable({
|
|
4434
|
+
innerType: type,
|
|
4435
|
+
typeName: ZodFirstPartyTypeKind.ZodNullable,
|
|
4436
|
+
...processCreateParams(params)
|
|
4437
|
+
});
|
|
4438
|
+
};
|
|
4439
|
+
var ZodDefault = class extends ZodType {
|
|
4440
|
+
_parse(input) {
|
|
4441
|
+
const { ctx } = this._processInputParams(input);
|
|
4442
|
+
let data = ctx.data;
|
|
4443
|
+
if (ctx.parsedType === ZodParsedType.undefined) {
|
|
4444
|
+
data = this._def.defaultValue();
|
|
4445
|
+
}
|
|
4446
|
+
return this._def.innerType._parse({
|
|
4447
|
+
data,
|
|
4448
|
+
path: ctx.path,
|
|
4449
|
+
parent: ctx
|
|
4450
|
+
});
|
|
4451
|
+
}
|
|
4452
|
+
removeDefault() {
|
|
4453
|
+
return this._def.innerType;
|
|
4454
|
+
}
|
|
4455
|
+
};
|
|
4456
|
+
ZodDefault.create = (type, params) => {
|
|
4457
|
+
return new ZodDefault({
|
|
4458
|
+
innerType: type,
|
|
4459
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
4460
|
+
defaultValue: typeof params.default === "function" ? params.default : () => params.default,
|
|
4461
|
+
...processCreateParams(params)
|
|
4462
|
+
});
|
|
4463
|
+
};
|
|
4464
|
+
var ZodCatch = class extends ZodType {
|
|
4465
|
+
_parse(input) {
|
|
4466
|
+
const { ctx } = this._processInputParams(input);
|
|
4467
|
+
const newCtx = {
|
|
4468
|
+
...ctx,
|
|
4469
|
+
common: {
|
|
4470
|
+
...ctx.common,
|
|
4471
|
+
issues: []
|
|
4472
|
+
}
|
|
4473
|
+
};
|
|
4474
|
+
const result = this._def.innerType._parse({
|
|
4475
|
+
data: newCtx.data,
|
|
4476
|
+
path: newCtx.path,
|
|
4477
|
+
parent: {
|
|
4478
|
+
...newCtx
|
|
4479
|
+
}
|
|
4480
|
+
});
|
|
4481
|
+
if (isAsync(result)) {
|
|
4482
|
+
return result.then((result2) => {
|
|
4483
|
+
return {
|
|
4484
|
+
status: "valid",
|
|
4485
|
+
value: result2.status === "valid" ? result2.value : this._def.catchValue({
|
|
4486
|
+
get error() {
|
|
4487
|
+
return new ZodError(newCtx.common.issues);
|
|
4488
|
+
},
|
|
4489
|
+
input: newCtx.data
|
|
4490
|
+
})
|
|
4491
|
+
};
|
|
4492
|
+
});
|
|
4493
|
+
} else {
|
|
4494
|
+
return {
|
|
4495
|
+
status: "valid",
|
|
4496
|
+
value: result.status === "valid" ? result.value : this._def.catchValue({
|
|
4497
|
+
get error() {
|
|
4498
|
+
return new ZodError(newCtx.common.issues);
|
|
4499
|
+
},
|
|
4500
|
+
input: newCtx.data
|
|
4501
|
+
})
|
|
4502
|
+
};
|
|
4503
|
+
}
|
|
4504
|
+
}
|
|
4505
|
+
removeCatch() {
|
|
4506
|
+
return this._def.innerType;
|
|
4507
|
+
}
|
|
4508
|
+
};
|
|
4509
|
+
ZodCatch.create = (type, params) => {
|
|
4510
|
+
return new ZodCatch({
|
|
4511
|
+
innerType: type,
|
|
4512
|
+
typeName: ZodFirstPartyTypeKind.ZodCatch,
|
|
4513
|
+
catchValue: typeof params.catch === "function" ? params.catch : () => params.catch,
|
|
4514
|
+
...processCreateParams(params)
|
|
4515
|
+
});
|
|
4516
|
+
};
|
|
4517
|
+
var ZodNaN = class extends ZodType {
|
|
4518
|
+
_parse(input) {
|
|
4519
|
+
const parsedType = this._getType(input);
|
|
4520
|
+
if (parsedType !== ZodParsedType.nan) {
|
|
4521
|
+
const ctx = this._getOrReturnCtx(input);
|
|
4522
|
+
addIssueToContext(ctx, {
|
|
4523
|
+
code: ZodIssueCode.invalid_type,
|
|
4524
|
+
expected: ZodParsedType.nan,
|
|
4525
|
+
received: ctx.parsedType
|
|
4526
|
+
});
|
|
4527
|
+
return INVALID;
|
|
4528
|
+
}
|
|
4529
|
+
return { status: "valid", value: input.data };
|
|
4530
|
+
}
|
|
4531
|
+
};
|
|
4532
|
+
ZodNaN.create = (params) => {
|
|
4533
|
+
return new ZodNaN({
|
|
4534
|
+
typeName: ZodFirstPartyTypeKind.ZodNaN,
|
|
4535
|
+
...processCreateParams(params)
|
|
4536
|
+
});
|
|
4537
|
+
};
|
|
4538
|
+
var BRAND = Symbol("zod_brand");
|
|
4539
|
+
var ZodBranded = class extends ZodType {
|
|
4540
|
+
_parse(input) {
|
|
4541
|
+
const { ctx } = this._processInputParams(input);
|
|
4542
|
+
const data = ctx.data;
|
|
4543
|
+
return this._def.type._parse({
|
|
4544
|
+
data,
|
|
4545
|
+
path: ctx.path,
|
|
4546
|
+
parent: ctx
|
|
4547
|
+
});
|
|
4548
|
+
}
|
|
4549
|
+
unwrap() {
|
|
4550
|
+
return this._def.type;
|
|
4551
|
+
}
|
|
4552
|
+
};
|
|
4553
|
+
var ZodPipeline = class _ZodPipeline extends ZodType {
|
|
4554
|
+
_parse(input) {
|
|
4555
|
+
const { status, ctx } = this._processInputParams(input);
|
|
4556
|
+
if (ctx.common.async) {
|
|
4557
|
+
const handleAsync = async () => {
|
|
4558
|
+
const inResult = await this._def.in._parseAsync({
|
|
4559
|
+
data: ctx.data,
|
|
4560
|
+
path: ctx.path,
|
|
4561
|
+
parent: ctx
|
|
4562
|
+
});
|
|
4563
|
+
if (inResult.status === "aborted")
|
|
4564
|
+
return INVALID;
|
|
4565
|
+
if (inResult.status === "dirty") {
|
|
4566
|
+
status.dirty();
|
|
4567
|
+
return DIRTY(inResult.value);
|
|
4568
|
+
} else {
|
|
4569
|
+
return this._def.out._parseAsync({
|
|
4570
|
+
data: inResult.value,
|
|
4571
|
+
path: ctx.path,
|
|
4572
|
+
parent: ctx
|
|
4573
|
+
});
|
|
4574
|
+
}
|
|
4575
|
+
};
|
|
4576
|
+
return handleAsync();
|
|
4577
|
+
} else {
|
|
4578
|
+
const inResult = this._def.in._parseSync({
|
|
4579
|
+
data: ctx.data,
|
|
4580
|
+
path: ctx.path,
|
|
4581
|
+
parent: ctx
|
|
4582
|
+
});
|
|
4583
|
+
if (inResult.status === "aborted")
|
|
4584
|
+
return INVALID;
|
|
4585
|
+
if (inResult.status === "dirty") {
|
|
4586
|
+
status.dirty();
|
|
4587
|
+
return {
|
|
4588
|
+
status: "dirty",
|
|
4589
|
+
value: inResult.value
|
|
4590
|
+
};
|
|
4591
|
+
} else {
|
|
4592
|
+
return this._def.out._parseSync({
|
|
4593
|
+
data: inResult.value,
|
|
4594
|
+
path: ctx.path,
|
|
4595
|
+
parent: ctx
|
|
4596
|
+
});
|
|
4597
|
+
}
|
|
4598
|
+
}
|
|
4599
|
+
}
|
|
4600
|
+
static create(a, b) {
|
|
4601
|
+
return new _ZodPipeline({
|
|
4602
|
+
in: a,
|
|
4603
|
+
out: b,
|
|
4604
|
+
typeName: ZodFirstPartyTypeKind.ZodPipeline
|
|
4605
|
+
});
|
|
4606
|
+
}
|
|
4607
|
+
};
|
|
4608
|
+
var ZodReadonly = class extends ZodType {
|
|
4609
|
+
_parse(input) {
|
|
4610
|
+
const result = this._def.innerType._parse(input);
|
|
4611
|
+
const freeze = (data) => {
|
|
4612
|
+
if (isValid(data)) {
|
|
4613
|
+
data.value = Object.freeze(data.value);
|
|
4614
|
+
}
|
|
4615
|
+
return data;
|
|
4616
|
+
};
|
|
4617
|
+
return isAsync(result) ? result.then((data) => freeze(data)) : freeze(result);
|
|
4618
|
+
}
|
|
4619
|
+
unwrap() {
|
|
4620
|
+
return this._def.innerType;
|
|
4621
|
+
}
|
|
4622
|
+
};
|
|
4623
|
+
ZodReadonly.create = (type, params) => {
|
|
4624
|
+
return new ZodReadonly({
|
|
4625
|
+
innerType: type,
|
|
4626
|
+
typeName: ZodFirstPartyTypeKind.ZodReadonly,
|
|
4627
|
+
...processCreateParams(params)
|
|
4628
|
+
});
|
|
4629
|
+
};
|
|
4630
|
+
function cleanParams(params, data) {
|
|
4631
|
+
const p = typeof params === "function" ? params(data) : typeof params === "string" ? { message: params } : params;
|
|
4632
|
+
const p2 = typeof p === "string" ? { message: p } : p;
|
|
4633
|
+
return p2;
|
|
4634
|
+
}
|
|
4635
|
+
function custom(check, _params = {}, fatal) {
|
|
4636
|
+
if (check)
|
|
4637
|
+
return ZodAny.create().superRefine((data, ctx) => {
|
|
4638
|
+
var _a, _b;
|
|
4639
|
+
const r = check(data);
|
|
4640
|
+
if (r instanceof Promise) {
|
|
4641
|
+
return r.then((r2) => {
|
|
4642
|
+
var _a2, _b2;
|
|
4643
|
+
if (!r2) {
|
|
4644
|
+
const params = cleanParams(_params, data);
|
|
4645
|
+
const _fatal = (_b2 = (_a2 = params.fatal) !== null && _a2 !== void 0 ? _a2 : fatal) !== null && _b2 !== void 0 ? _b2 : true;
|
|
4646
|
+
ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
|
|
4647
|
+
}
|
|
4648
|
+
});
|
|
4649
|
+
}
|
|
4650
|
+
if (!r) {
|
|
4651
|
+
const params = cleanParams(_params, data);
|
|
4652
|
+
const _fatal = (_b = (_a = params.fatal) !== null && _a !== void 0 ? _a : fatal) !== null && _b !== void 0 ? _b : true;
|
|
4653
|
+
ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
|
|
4654
|
+
}
|
|
4655
|
+
return;
|
|
4656
|
+
});
|
|
4657
|
+
return ZodAny.create();
|
|
4658
|
+
}
|
|
4659
|
+
var late = {
|
|
4660
|
+
object: ZodObject.lazycreate
|
|
4661
|
+
};
|
|
4662
|
+
var ZodFirstPartyTypeKind;
|
|
4663
|
+
(function(ZodFirstPartyTypeKind2) {
|
|
4664
|
+
ZodFirstPartyTypeKind2["ZodString"] = "ZodString";
|
|
4665
|
+
ZodFirstPartyTypeKind2["ZodNumber"] = "ZodNumber";
|
|
4666
|
+
ZodFirstPartyTypeKind2["ZodNaN"] = "ZodNaN";
|
|
4667
|
+
ZodFirstPartyTypeKind2["ZodBigInt"] = "ZodBigInt";
|
|
4668
|
+
ZodFirstPartyTypeKind2["ZodBoolean"] = "ZodBoolean";
|
|
4669
|
+
ZodFirstPartyTypeKind2["ZodDate"] = "ZodDate";
|
|
4670
|
+
ZodFirstPartyTypeKind2["ZodSymbol"] = "ZodSymbol";
|
|
4671
|
+
ZodFirstPartyTypeKind2["ZodUndefined"] = "ZodUndefined";
|
|
4672
|
+
ZodFirstPartyTypeKind2["ZodNull"] = "ZodNull";
|
|
4673
|
+
ZodFirstPartyTypeKind2["ZodAny"] = "ZodAny";
|
|
4674
|
+
ZodFirstPartyTypeKind2["ZodUnknown"] = "ZodUnknown";
|
|
4675
|
+
ZodFirstPartyTypeKind2["ZodNever"] = "ZodNever";
|
|
4676
|
+
ZodFirstPartyTypeKind2["ZodVoid"] = "ZodVoid";
|
|
4677
|
+
ZodFirstPartyTypeKind2["ZodArray"] = "ZodArray";
|
|
4678
|
+
ZodFirstPartyTypeKind2["ZodObject"] = "ZodObject";
|
|
4679
|
+
ZodFirstPartyTypeKind2["ZodUnion"] = "ZodUnion";
|
|
4680
|
+
ZodFirstPartyTypeKind2["ZodDiscriminatedUnion"] = "ZodDiscriminatedUnion";
|
|
4681
|
+
ZodFirstPartyTypeKind2["ZodIntersection"] = "ZodIntersection";
|
|
4682
|
+
ZodFirstPartyTypeKind2["ZodTuple"] = "ZodTuple";
|
|
4683
|
+
ZodFirstPartyTypeKind2["ZodRecord"] = "ZodRecord";
|
|
4684
|
+
ZodFirstPartyTypeKind2["ZodMap"] = "ZodMap";
|
|
4685
|
+
ZodFirstPartyTypeKind2["ZodSet"] = "ZodSet";
|
|
4686
|
+
ZodFirstPartyTypeKind2["ZodFunction"] = "ZodFunction";
|
|
4687
|
+
ZodFirstPartyTypeKind2["ZodLazy"] = "ZodLazy";
|
|
4688
|
+
ZodFirstPartyTypeKind2["ZodLiteral"] = "ZodLiteral";
|
|
4689
|
+
ZodFirstPartyTypeKind2["ZodEnum"] = "ZodEnum";
|
|
4690
|
+
ZodFirstPartyTypeKind2["ZodEffects"] = "ZodEffects";
|
|
4691
|
+
ZodFirstPartyTypeKind2["ZodNativeEnum"] = "ZodNativeEnum";
|
|
4692
|
+
ZodFirstPartyTypeKind2["ZodOptional"] = "ZodOptional";
|
|
4693
|
+
ZodFirstPartyTypeKind2["ZodNullable"] = "ZodNullable";
|
|
4694
|
+
ZodFirstPartyTypeKind2["ZodDefault"] = "ZodDefault";
|
|
4695
|
+
ZodFirstPartyTypeKind2["ZodCatch"] = "ZodCatch";
|
|
4696
|
+
ZodFirstPartyTypeKind2["ZodPromise"] = "ZodPromise";
|
|
4697
|
+
ZodFirstPartyTypeKind2["ZodBranded"] = "ZodBranded";
|
|
4698
|
+
ZodFirstPartyTypeKind2["ZodPipeline"] = "ZodPipeline";
|
|
4699
|
+
ZodFirstPartyTypeKind2["ZodReadonly"] = "ZodReadonly";
|
|
4700
|
+
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
|
|
4701
|
+
var instanceOfType = (cls, params = {
|
|
4702
|
+
message: `Input not instance of ${cls.name}`
|
|
4703
|
+
}) => custom((data) => data instanceof cls, params);
|
|
4704
|
+
var stringType = ZodString.create;
|
|
4705
|
+
var numberType = ZodNumber.create;
|
|
4706
|
+
var nanType = ZodNaN.create;
|
|
4707
|
+
var bigIntType = ZodBigInt.create;
|
|
4708
|
+
var booleanType = ZodBoolean.create;
|
|
4709
|
+
var dateType = ZodDate.create;
|
|
4710
|
+
var symbolType = ZodSymbol.create;
|
|
4711
|
+
var undefinedType = ZodUndefined.create;
|
|
4712
|
+
var nullType = ZodNull.create;
|
|
4713
|
+
var anyType = ZodAny.create;
|
|
4714
|
+
var unknownType = ZodUnknown.create;
|
|
4715
|
+
var neverType = ZodNever.create;
|
|
4716
|
+
var voidType = ZodVoid.create;
|
|
4717
|
+
var arrayType = ZodArray.create;
|
|
4718
|
+
var objectType = ZodObject.create;
|
|
4719
|
+
var strictObjectType = ZodObject.strictCreate;
|
|
4720
|
+
var unionType = ZodUnion.create;
|
|
4721
|
+
var discriminatedUnionType = ZodDiscriminatedUnion.create;
|
|
4722
|
+
var intersectionType = ZodIntersection.create;
|
|
4723
|
+
var tupleType = ZodTuple.create;
|
|
4724
|
+
var recordType = ZodRecord.create;
|
|
4725
|
+
var mapType = ZodMap.create;
|
|
4726
|
+
var setType = ZodSet.create;
|
|
4727
|
+
var functionType = ZodFunction.create;
|
|
4728
|
+
var lazyType = ZodLazy.create;
|
|
4729
|
+
var literalType = ZodLiteral.create;
|
|
4730
|
+
var enumType = ZodEnum.create;
|
|
4731
|
+
var nativeEnumType = ZodNativeEnum.create;
|
|
4732
|
+
var promiseType = ZodPromise.create;
|
|
4733
|
+
var effectsType = ZodEffects.create;
|
|
4734
|
+
var optionalType = ZodOptional.create;
|
|
4735
|
+
var nullableType = ZodNullable.create;
|
|
4736
|
+
var preprocessType = ZodEffects.createWithPreprocess;
|
|
4737
|
+
var pipelineType = ZodPipeline.create;
|
|
4738
|
+
var ostring = () => stringType().optional();
|
|
4739
|
+
var onumber = () => numberType().optional();
|
|
4740
|
+
var oboolean = () => booleanType().optional();
|
|
4741
|
+
var coerce = {
|
|
4742
|
+
string: (arg) => ZodString.create({ ...arg, coerce: true }),
|
|
4743
|
+
number: (arg) => ZodNumber.create({ ...arg, coerce: true }),
|
|
4744
|
+
boolean: (arg) => ZodBoolean.create({
|
|
4745
|
+
...arg,
|
|
4746
|
+
coerce: true
|
|
4747
|
+
}),
|
|
4748
|
+
bigint: (arg) => ZodBigInt.create({ ...arg, coerce: true }),
|
|
4749
|
+
date: (arg) => ZodDate.create({ ...arg, coerce: true })
|
|
4750
|
+
};
|
|
4751
|
+
var NEVER = INVALID;
|
|
4752
|
+
var z = /* @__PURE__ */ Object.freeze({
|
|
4753
|
+
__proto__: null,
|
|
4754
|
+
defaultErrorMap: errorMap,
|
|
4755
|
+
setErrorMap,
|
|
4756
|
+
getErrorMap,
|
|
4757
|
+
makeIssue,
|
|
4758
|
+
EMPTY_PATH,
|
|
4759
|
+
addIssueToContext,
|
|
4760
|
+
ParseStatus,
|
|
4761
|
+
INVALID,
|
|
4762
|
+
DIRTY,
|
|
4763
|
+
OK,
|
|
4764
|
+
isAborted,
|
|
4765
|
+
isDirty,
|
|
4766
|
+
isValid,
|
|
4767
|
+
isAsync,
|
|
4768
|
+
get util() {
|
|
4769
|
+
return util;
|
|
4770
|
+
},
|
|
4771
|
+
get objectUtil() {
|
|
4772
|
+
return objectUtil;
|
|
4773
|
+
},
|
|
4774
|
+
ZodParsedType,
|
|
4775
|
+
getParsedType,
|
|
4776
|
+
ZodType,
|
|
4777
|
+
datetimeRegex,
|
|
4778
|
+
ZodString,
|
|
4779
|
+
ZodNumber,
|
|
4780
|
+
ZodBigInt,
|
|
4781
|
+
ZodBoolean,
|
|
4782
|
+
ZodDate,
|
|
4783
|
+
ZodSymbol,
|
|
4784
|
+
ZodUndefined,
|
|
4785
|
+
ZodNull,
|
|
4786
|
+
ZodAny,
|
|
4787
|
+
ZodUnknown,
|
|
4788
|
+
ZodNever,
|
|
4789
|
+
ZodVoid,
|
|
4790
|
+
ZodArray,
|
|
4791
|
+
ZodObject,
|
|
4792
|
+
ZodUnion,
|
|
4793
|
+
ZodDiscriminatedUnion,
|
|
4794
|
+
ZodIntersection,
|
|
4795
|
+
ZodTuple,
|
|
4796
|
+
ZodRecord,
|
|
4797
|
+
ZodMap,
|
|
4798
|
+
ZodSet,
|
|
4799
|
+
ZodFunction,
|
|
4800
|
+
ZodLazy,
|
|
4801
|
+
ZodLiteral,
|
|
4802
|
+
ZodEnum,
|
|
4803
|
+
ZodNativeEnum,
|
|
4804
|
+
ZodPromise,
|
|
4805
|
+
ZodEffects,
|
|
4806
|
+
ZodTransformer: ZodEffects,
|
|
4807
|
+
ZodOptional,
|
|
4808
|
+
ZodNullable,
|
|
4809
|
+
ZodDefault,
|
|
4810
|
+
ZodCatch,
|
|
4811
|
+
ZodNaN,
|
|
4812
|
+
BRAND,
|
|
4813
|
+
ZodBranded,
|
|
4814
|
+
ZodPipeline,
|
|
4815
|
+
ZodReadonly,
|
|
4816
|
+
custom,
|
|
4817
|
+
Schema: ZodType,
|
|
4818
|
+
ZodSchema: ZodType,
|
|
4819
|
+
late,
|
|
4820
|
+
get ZodFirstPartyTypeKind() {
|
|
4821
|
+
return ZodFirstPartyTypeKind;
|
|
4822
|
+
},
|
|
4823
|
+
coerce,
|
|
4824
|
+
any: anyType,
|
|
4825
|
+
array: arrayType,
|
|
4826
|
+
bigint: bigIntType,
|
|
4827
|
+
boolean: booleanType,
|
|
4828
|
+
date: dateType,
|
|
4829
|
+
discriminatedUnion: discriminatedUnionType,
|
|
4830
|
+
effect: effectsType,
|
|
4831
|
+
"enum": enumType,
|
|
4832
|
+
"function": functionType,
|
|
4833
|
+
"instanceof": instanceOfType,
|
|
4834
|
+
intersection: intersectionType,
|
|
4835
|
+
lazy: lazyType,
|
|
4836
|
+
literal: literalType,
|
|
4837
|
+
map: mapType,
|
|
4838
|
+
nan: nanType,
|
|
4839
|
+
nativeEnum: nativeEnumType,
|
|
4840
|
+
never: neverType,
|
|
4841
|
+
"null": nullType,
|
|
4842
|
+
nullable: nullableType,
|
|
4843
|
+
number: numberType,
|
|
4844
|
+
object: objectType,
|
|
4845
|
+
oboolean,
|
|
4846
|
+
onumber,
|
|
4847
|
+
optional: optionalType,
|
|
4848
|
+
ostring,
|
|
4849
|
+
pipeline: pipelineType,
|
|
4850
|
+
preprocess: preprocessType,
|
|
4851
|
+
promise: promiseType,
|
|
4852
|
+
record: recordType,
|
|
4853
|
+
set: setType,
|
|
4854
|
+
strictObject: strictObjectType,
|
|
4855
|
+
string: stringType,
|
|
4856
|
+
symbol: symbolType,
|
|
4857
|
+
transformer: effectsType,
|
|
4858
|
+
tuple: tupleType,
|
|
4859
|
+
"undefined": undefinedType,
|
|
4860
|
+
union: unionType,
|
|
4861
|
+
unknown: unknownType,
|
|
4862
|
+
"void": voidType,
|
|
4863
|
+
NEVER,
|
|
4864
|
+
ZodIssueCode,
|
|
4865
|
+
quotelessJson,
|
|
4866
|
+
ZodError
|
|
4867
|
+
});
|
|
4868
|
+
|
|
4869
|
+
// src/policies/shortestPathPolicy.ts
|
|
4870
|
+
var import_zod_to_json_schema = require("zod-to-json-schema");
|
|
4871
|
+
var import_ajv = __toESM(require("ajv"));
|
|
4872
|
+
var ajv = new import_ajv.default();
|
|
4873
|
+
function observedStatesEqual(state1, state2) {
|
|
4874
|
+
return JSON.stringify(state1.value) === JSON.stringify(state2.value) && JSON.stringify(state1.context) === JSON.stringify(state2.context);
|
|
4875
|
+
}
|
|
4876
|
+
function trimSteps(steps, currentState) {
|
|
4877
|
+
const index = steps.findIndex(
|
|
4878
|
+
(step) => step.state && observedStatesEqual(step.state, currentState)
|
|
4879
|
+
);
|
|
4880
|
+
if (index === -1) {
|
|
4881
|
+
return void 0;
|
|
4882
|
+
}
|
|
4883
|
+
return steps.slice(index + 1, steps.length);
|
|
4884
|
+
}
|
|
4885
|
+
async function experimental_shortestPathPolicy(agent, input) {
|
|
4886
|
+
const costFunction = input.costFunction ?? ((path) => path.weight ?? Infinity);
|
|
4887
|
+
const existingDecision = input.decisions?.find(
|
|
4888
|
+
(p) => p.policy === "shortestPath" && p.goal === input.goal
|
|
4889
|
+
);
|
|
4890
|
+
let paths = existingDecision?.paths;
|
|
4891
|
+
if (existingDecision) {
|
|
4892
|
+
console.log("Existing decision found");
|
|
4893
|
+
}
|
|
4894
|
+
if (!input.machine && !existingDecision) {
|
|
4895
|
+
return;
|
|
4896
|
+
}
|
|
4897
|
+
if (input.machine && !existingDecision) {
|
|
4898
|
+
const contextSchema = (0, import_zod_to_json_schema.zodToJsonSchema)(z.object(agent.context));
|
|
4899
|
+
const result = await (0, import_ai6.generateObject)({
|
|
4900
|
+
model: agent.model,
|
|
4901
|
+
system: input.system ?? agent.description,
|
|
4902
|
+
prompt: `
|
|
4903
|
+
<goal>
|
|
4904
|
+
${input.goal}
|
|
4905
|
+
</goal>
|
|
4906
|
+
<contextSchema>
|
|
4907
|
+
${contextSchema}
|
|
4908
|
+
</contextSchema>
|
|
4909
|
+
|
|
4910
|
+
|
|
4911
|
+
Update the context JSON schema so that it validates the context to determine that it reaches the goal. Return the result as a diff.
|
|
4912
|
+
|
|
4913
|
+
The contextSchema properties must not change. Do not add or remove properties, or modify the name of the properties.
|
|
4914
|
+
Use "const" for exact required values and define ranges/types for flexible conditions.
|
|
4915
|
+
|
|
4916
|
+
Examples:
|
|
4917
|
+
1. For "user is logged in with admin role":
|
|
4918
|
+
{
|
|
4919
|
+
"contextSchema": "{"type": "object", "properties": {"role": {"const": "admin"}, "lastLogin": {"type": "string"}}, "required": ["role"]}"
|
|
4920
|
+
}
|
|
4921
|
+
|
|
4922
|
+
2. For "score is above 100":
|
|
4923
|
+
{
|
|
4924
|
+
"contextSchema": "{"type": "object", "properties": {"score": {"type": "number", "minimum": 100}}, "required": ["score"]}"
|
|
4925
|
+
}
|
|
4926
|
+
|
|
4927
|
+
3. For "fruits contain apple, orange, banana":
|
|
4928
|
+
{
|
|
4929
|
+
"type": "array",
|
|
4930
|
+
"allOf": [
|
|
4931
|
+
{ "contains": { "const": "apple" } },
|
|
4932
|
+
{ "contains": { "const": "orange" } },
|
|
4933
|
+
{ "contains": { "const": "banana" } }
|
|
4934
|
+
]
|
|
4935
|
+
}
|
|
4936
|
+
`.trim(),
|
|
4937
|
+
schema: z.object({
|
|
4938
|
+
// valueSchema: z
|
|
4939
|
+
// .string()
|
|
4940
|
+
// .describe('The JSON Schema representing the goal state value'),
|
|
4941
|
+
contextSchema: z.object({
|
|
4942
|
+
type: z.literal("object"),
|
|
4943
|
+
properties: z.object(
|
|
4944
|
+
Object.keys(contextSchema.properties).reduce(
|
|
4945
|
+
(acc, key) => {
|
|
4946
|
+
acc[key] = z.any();
|
|
4947
|
+
return acc;
|
|
4948
|
+
},
|
|
4949
|
+
{}
|
|
4950
|
+
)
|
|
4951
|
+
),
|
|
4952
|
+
required: z.array(z.string()).optional()
|
|
4953
|
+
}).describe("The JSON Schema representing the goal state context")
|
|
4954
|
+
})
|
|
4955
|
+
});
|
|
4956
|
+
console.log(result.object);
|
|
4957
|
+
const validateContext = ajv.compile(result.object.contextSchema);
|
|
4958
|
+
const stateFilter = (state) => {
|
|
4959
|
+
return validateContext(state.context);
|
|
4960
|
+
};
|
|
4961
|
+
const resolvedState = input.machine.resolveState({
|
|
4962
|
+
...input.state,
|
|
4963
|
+
context: input.state.context ?? {}
|
|
4964
|
+
});
|
|
4965
|
+
paths = (0, import_graph.getShortestPaths)(input.machine, {
|
|
4966
|
+
fromState: resolvedState,
|
|
4967
|
+
toState: stateFilter
|
|
4968
|
+
});
|
|
4969
|
+
}
|
|
4970
|
+
if (!paths) {
|
|
4971
|
+
return void 0;
|
|
4972
|
+
}
|
|
4973
|
+
const trimmedPaths = paths.map((path) => {
|
|
4974
|
+
const trimmedSteps = trimSteps(path.steps, input.state);
|
|
4975
|
+
if (!trimmedSteps) {
|
|
4976
|
+
return void 0;
|
|
4977
|
+
}
|
|
4978
|
+
return {
|
|
4979
|
+
...path,
|
|
4980
|
+
steps: trimmedSteps
|
|
4981
|
+
};
|
|
4982
|
+
}).filter((p) => p !== void 0);
|
|
4983
|
+
const sortedPaths = trimmedPaths.sort(
|
|
4984
|
+
(a, b) => costFunction(a) - costFunction(b)
|
|
4985
|
+
);
|
|
4986
|
+
const leastWeightPath = sortedPaths[0];
|
|
4987
|
+
const nextStep = leastWeightPath?.steps[0];
|
|
4988
|
+
return {
|
|
4989
|
+
id: randomId(),
|
|
4990
|
+
decisionId: input.decisionId ?? null,
|
|
4991
|
+
policy: "shortestPath",
|
|
4992
|
+
episodeId: agent.episodeId,
|
|
4993
|
+
goal: input.goal,
|
|
4994
|
+
goalState: paths[0]?.state ?? null,
|
|
4995
|
+
nextEvent: nextStep?.event ?? null,
|
|
4996
|
+
paths,
|
|
4997
|
+
timestamp: Date.now()
|
|
4998
|
+
};
|
|
4999
|
+
}
|
|
754
5000
|
// Annotate the CommonJS export names for ESM import in node:
|
|
755
5001
|
0 && (module.exports = {
|
|
5002
|
+
chainOfThoughtPolicy,
|
|
756
5003
|
createAgent,
|
|
5004
|
+
experimental_shortestPathPolicy,
|
|
757
5005
|
fromDecision,
|
|
758
5006
|
fromText,
|
|
759
|
-
fromTextStream
|
|
5007
|
+
fromTextStream,
|
|
5008
|
+
toolPolicy
|
|
760
5009
|
});
|