agent-memory-graph 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/plugin/entry.js +4432 -189
- package/dist/src/extract/dedup.js +21 -2
- package/dist/src/extract/dedup.js.map +1 -1
- package/dist/src/extract/extractor.d.ts.map +1 -1
- package/dist/src/extract/extractor.js +1 -0
- package/dist/src/extract/extractor.js.map +1 -1
- package/dist/src/graph/engine.d.ts +1 -0
- package/dist/src/graph/engine.d.ts.map +1 -1
- package/dist/src/graph/engine.js +1 -0
- package/dist/src/graph/engine.js.map +1 -1
- package/dist/src/search/natural-language.js +1 -1
- package/dist/src/search/natural-language.js.map +1 -1
- package/openclaw.plugin.json +5 -0
- package/package.json +2 -2
package/dist/plugin/entry.js
CHANGED
|
@@ -8,6 +8,44 @@ var __export = (target, all) => {
|
|
|
8
8
|
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
9
|
};
|
|
10
10
|
|
|
11
|
+
// node_modules/nanoid/url-alphabet/index.js
|
|
12
|
+
var urlAlphabet;
|
|
13
|
+
var init_url_alphabet = __esm({
|
|
14
|
+
"node_modules/nanoid/url-alphabet/index.js"() {
|
|
15
|
+
urlAlphabet = "useandom-26T198340PX75pxJACKVERYMINDBUSHWOLF_GQZbfghjklqvwyzrict";
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
// node_modules/nanoid/index.js
|
|
20
|
+
import { webcrypto as crypto } from "node:crypto";
|
|
21
|
+
function fillPool(bytes) {
|
|
22
|
+
if (bytes < 0 || bytes > 1024) throw new RangeError("Wrong ID size");
|
|
23
|
+
if (!pool || pool.length < bytes) {
|
|
24
|
+
pool = Buffer.allocUnsafe(bytes * POOL_SIZE_MULTIPLIER);
|
|
25
|
+
crypto.getRandomValues(pool);
|
|
26
|
+
poolOffset = 0;
|
|
27
|
+
} else if (poolOffset + bytes > pool.length) {
|
|
28
|
+
crypto.getRandomValues(pool);
|
|
29
|
+
poolOffset = 0;
|
|
30
|
+
}
|
|
31
|
+
poolOffset += bytes;
|
|
32
|
+
}
|
|
33
|
+
function nanoid(size = 21) {
|
|
34
|
+
fillPool(size |= 0);
|
|
35
|
+
let id = "";
|
|
36
|
+
for (let i = poolOffset - size; i < poolOffset; i++) {
|
|
37
|
+
id += urlAlphabet[pool[i] & 63];
|
|
38
|
+
}
|
|
39
|
+
return id;
|
|
40
|
+
}
|
|
41
|
+
var POOL_SIZE_MULTIPLIER, pool, poolOffset;
|
|
42
|
+
var init_nanoid = __esm({
|
|
43
|
+
"node_modules/nanoid/index.js"() {
|
|
44
|
+
init_url_alphabet();
|
|
45
|
+
POOL_SIZE_MULTIPLIER = 128;
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
|
|
11
49
|
// src/graph/schema.js
|
|
12
50
|
import Database from "better-sqlite3";
|
|
13
51
|
import { resolve } from "node:path";
|
|
@@ -140,11 +178,11 @@ END;
|
|
|
140
178
|
});
|
|
141
179
|
|
|
142
180
|
// src/graph/engine.js
|
|
143
|
-
import { nanoid } from "nanoid";
|
|
144
181
|
var GraphEngine;
|
|
145
182
|
var init_engine = __esm({
|
|
146
183
|
"src/graph/engine.js"() {
|
|
147
184
|
"use strict";
|
|
185
|
+
init_nanoid();
|
|
148
186
|
init_schema();
|
|
149
187
|
GraphEngine = class {
|
|
150
188
|
db;
|
|
@@ -286,223 +324,4330 @@ var init_engine = __esm({
|
|
|
286
324
|
`).all(entityId);
|
|
287
325
|
return rows.map((r) => ({ ...this.rowToRelationship(r), to_name: r.to_name, to_type: r.to_type }));
|
|
288
326
|
}
|
|
289
|
-
getRelationsTo(entityId) {
|
|
290
|
-
const rows = this.db.prepare(`
|
|
291
|
-
SELECT r.*, e.name as from_name, e.type as from_type
|
|
292
|
-
FROM relationships r
|
|
293
|
-
JOIN entities e ON r.from_id = e.id
|
|
294
|
-
WHERE r.to_id = ?
|
|
295
|
-
ORDER BY r.updated_at DESC
|
|
296
|
-
`).all(entityId);
|
|
297
|
-
return rows.map((r) => ({ ...this.rowToRelationship(r), from_name: r.from_name, from_type: r.from_type }));
|
|
327
|
+
getRelationsTo(entityId) {
|
|
328
|
+
const rows = this.db.prepare(`
|
|
329
|
+
SELECT r.*, e.name as from_name, e.type as from_type
|
|
330
|
+
FROM relationships r
|
|
331
|
+
JOIN entities e ON r.from_id = e.id
|
|
332
|
+
WHERE r.to_id = ?
|
|
333
|
+
ORDER BY r.updated_at DESC
|
|
334
|
+
`).all(entityId);
|
|
335
|
+
return rows.map((r) => ({ ...this.rowToRelationship(r), from_name: r.from_name, from_type: r.from_type }));
|
|
336
|
+
}
|
|
337
|
+
deleteRelation(id) {
|
|
338
|
+
const result = this.db.prepare(`DELETE FROM relationships WHERE id = ?`).run(id);
|
|
339
|
+
return result.changes > 0;
|
|
340
|
+
}
|
|
341
|
+
// ─── Search ────────────────────────────────────────────────────
|
|
342
|
+
searchEntities(query, limit = 10) {
|
|
343
|
+
const sanitized = query.replace(/[?!@#$%^&*(){}\[\]<>:;"'`~|/\\+=]/g, " ").trim();
|
|
344
|
+
if (sanitized.length > 0) {
|
|
345
|
+
try {
|
|
346
|
+
const ftsRows = this.db.prepare(`
|
|
347
|
+
SELECT e.* FROM entities_fts fts
|
|
348
|
+
JOIN entities e ON e.rowid = fts.rowid
|
|
349
|
+
WHERE entities_fts MATCH ?
|
|
350
|
+
LIMIT ?
|
|
351
|
+
`).all(sanitized, limit);
|
|
352
|
+
if (ftsRows.length > 0) {
|
|
353
|
+
return ftsRows.map((r) => this.rowToEntity(r));
|
|
354
|
+
}
|
|
355
|
+
} catch {
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
const likeQuery = sanitized.length > 0 ? sanitized : query;
|
|
359
|
+
const likeRows = this.db.prepare(`
|
|
360
|
+
SELECT * FROM entities
|
|
361
|
+
WHERE name LIKE ? COLLATE NOCASE OR type LIKE ? COLLATE NOCASE
|
|
362
|
+
LIMIT ?
|
|
363
|
+
`).all(`%${likeQuery}%`, `%${likeQuery}%`, limit);
|
|
364
|
+
return likeRows.map((r) => this.rowToEntity(r));
|
|
365
|
+
}
|
|
366
|
+
// ─── Graph Traversal ───────────────────────────────────────────
|
|
367
|
+
/**
|
|
368
|
+
* Find path between two entities (BFS, max depth)
|
|
369
|
+
*/
|
|
370
|
+
findPath(fromName, toName, maxHops = 3) {
|
|
371
|
+
const fromEntity = this.findEntityByName(fromName);
|
|
372
|
+
const toEntity = this.findEntityByName(toName);
|
|
373
|
+
if (!fromEntity || !toEntity) return null;
|
|
374
|
+
const queue = [
|
|
375
|
+
{ entityId: fromEntity.id, path: [fromEntity.name], relations: [] }
|
|
376
|
+
];
|
|
377
|
+
const visited = /* @__PURE__ */ new Set([fromEntity.id]);
|
|
378
|
+
while (queue.length > 0) {
|
|
379
|
+
const current = queue.shift();
|
|
380
|
+
if (current.path.length > maxHops + 1) break;
|
|
381
|
+
const outgoing = this.db.prepare(`
|
|
382
|
+
SELECT r.relation, r.to_id as neighbor_id, e.name as neighbor_name
|
|
383
|
+
FROM relationships r JOIN entities e ON r.to_id = e.id
|
|
384
|
+
WHERE r.from_id = ?
|
|
385
|
+
`).all(current.entityId);
|
|
386
|
+
const incoming = this.db.prepare(`
|
|
387
|
+
SELECT r.relation, r.from_id as neighbor_id, e.name as neighbor_name
|
|
388
|
+
FROM relationships r JOIN entities e ON r.from_id = e.id
|
|
389
|
+
WHERE r.to_id = ?
|
|
390
|
+
`).all(current.entityId);
|
|
391
|
+
const neighbors = [
|
|
392
|
+
...outgoing.map((n) => ({ ...n, direction: "->" })),
|
|
393
|
+
...incoming.map((n) => ({ ...n, direction: "<-" }))
|
|
394
|
+
];
|
|
395
|
+
for (const neighbor of neighbors) {
|
|
396
|
+
if (neighbor.neighbor_id === toEntity.id) {
|
|
397
|
+
return {
|
|
398
|
+
path: [...current.path, neighbor.neighbor_name],
|
|
399
|
+
relations: [...current.relations, `${neighbor.direction}[${neighbor.relation}]`]
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
if (!visited.has(neighbor.neighbor_id)) {
|
|
403
|
+
visited.add(neighbor.neighbor_id);
|
|
404
|
+
queue.push({
|
|
405
|
+
entityId: neighbor.neighbor_id,
|
|
406
|
+
path: [...current.path, neighbor.neighbor_name],
|
|
407
|
+
relations: [...current.relations, `${neighbor.direction}[${neighbor.relation}]`]
|
|
408
|
+
});
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return null;
|
|
413
|
+
}
|
|
414
|
+
/**
|
|
415
|
+
* Get neighborhood of an entity (all connected within N hops)
|
|
416
|
+
*/
|
|
417
|
+
getNeighborhood(entityName, hops = 1) {
|
|
418
|
+
const entity = this.findEntityByName(entityName);
|
|
419
|
+
if (!entity) return { entities: [], relationships: [] };
|
|
420
|
+
const entityIds = /* @__PURE__ */ new Set([entity.id]);
|
|
421
|
+
const relIds = /* @__PURE__ */ new Set();
|
|
422
|
+
let frontier = [entity.id];
|
|
423
|
+
for (let i = 0; i < hops; i++) {
|
|
424
|
+
const nextFrontier = [];
|
|
425
|
+
for (const nodeId of frontier) {
|
|
426
|
+
const rels = this.db.prepare(`
|
|
427
|
+
SELECT * FROM relationships WHERE from_id = ? OR to_id = ?
|
|
428
|
+
`).all(nodeId, nodeId);
|
|
429
|
+
for (const rel of rels) {
|
|
430
|
+
relIds.add(rel.id);
|
|
431
|
+
const neighborId = rel.from_id === nodeId ? rel.to_id : rel.from_id;
|
|
432
|
+
if (!entityIds.has(neighborId)) {
|
|
433
|
+
entityIds.add(neighborId);
|
|
434
|
+
nextFrontier.push(neighborId);
|
|
435
|
+
}
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
frontier = nextFrontier;
|
|
439
|
+
}
|
|
440
|
+
const entities = [...entityIds].map((id) => this.getEntity(id)).filter((e) => e !== null);
|
|
441
|
+
const relationships = [...relIds].map((id) => {
|
|
442
|
+
const row = this.db.prepare(`SELECT * FROM relationships WHERE id = ?`).get(id);
|
|
443
|
+
return row ? this.rowToRelationship(row) : null;
|
|
444
|
+
}).filter((r) => r !== null);
|
|
445
|
+
return { entities, relationships };
|
|
446
|
+
}
|
|
447
|
+
// ─── Stats ─────────────────────────────────────────────────────
|
|
448
|
+
stats() {
|
|
449
|
+
const entityCount = this.db.prepare(`SELECT COUNT(*) as c FROM entities`).get().c;
|
|
450
|
+
const relCount = this.db.prepare(`SELECT COUNT(*) as c FROM relationships`).get().c;
|
|
451
|
+
const entityTypes = this.db.prepare(`SELECT DISTINCT type FROM entities ORDER BY type`).all().map((r) => r.type);
|
|
452
|
+
const relationTypes = this.db.prepare(`SELECT DISTINCT relation FROM relationships ORDER BY relation`).all().map((r) => r.relation);
|
|
453
|
+
const oldest = this.db.prepare(`SELECT MIN(created_at) as t FROM entities`).get();
|
|
454
|
+
const newest = this.db.prepare(`SELECT MAX(updated_at) as t FROM entities`).get();
|
|
455
|
+
return {
|
|
456
|
+
entities: entityCount,
|
|
457
|
+
relationships: relCount,
|
|
458
|
+
entityTypes,
|
|
459
|
+
relationTypes,
|
|
460
|
+
oldestEntry: oldest?.t ?? null,
|
|
461
|
+
newestEntry: newest?.t ?? null
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
// ─── Memory Log ────────────────────────────────────────────────
|
|
465
|
+
logExtraction(rawText, entities, relations, sessionId) {
|
|
466
|
+
const id = `log-${nanoid(12)}`;
|
|
467
|
+
this.db.prepare(`
|
|
468
|
+
INSERT INTO memory_log (id, raw_text, extracted_entities, extracted_relations, session_id)
|
|
469
|
+
VALUES (?, ?, ?, ?, ?)
|
|
470
|
+
`).run(id, rawText, JSON.stringify(entities), JSON.stringify(relations), sessionId ?? null);
|
|
471
|
+
}
|
|
472
|
+
// ─── Helpers ───────────────────────────────────────────────────
|
|
473
|
+
rowToEntity(row) {
|
|
474
|
+
return {
|
|
475
|
+
id: row.id,
|
|
476
|
+
name: row.name,
|
|
477
|
+
type: row.type,
|
|
478
|
+
properties: JSON.parse(row.properties || "{}"),
|
|
479
|
+
created_at: row.created_at,
|
|
480
|
+
updated_at: row.updated_at,
|
|
481
|
+
source: row.source ?? void 0,
|
|
482
|
+
confidence: row.confidence,
|
|
483
|
+
mention_count: row.mention_count ?? 1
|
|
484
|
+
};
|
|
485
|
+
}
|
|
486
|
+
rowToRelationship(row) {
|
|
487
|
+
return {
|
|
488
|
+
id: row.id,
|
|
489
|
+
from_id: row.from_id,
|
|
490
|
+
to_id: row.to_id,
|
|
491
|
+
relation: row.relation,
|
|
492
|
+
properties: JSON.parse(row.properties || "{}"),
|
|
493
|
+
created_at: row.created_at,
|
|
494
|
+
updated_at: row.updated_at,
|
|
495
|
+
source: row.source ?? void 0,
|
|
496
|
+
confidence: row.confidence
|
|
497
|
+
};
|
|
498
|
+
}
|
|
499
|
+
/** Close database */
|
|
500
|
+
close() {
|
|
501
|
+
this.db.close();
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
// node_modules/zod/v3/helpers/util.js
|
|
508
|
+
var util, objectUtil, ZodParsedType, getParsedType;
|
|
509
|
+
var init_util = __esm({
|
|
510
|
+
"node_modules/zod/v3/helpers/util.js"() {
|
|
511
|
+
(function(util2) {
|
|
512
|
+
util2.assertEqual = (_) => {
|
|
513
|
+
};
|
|
514
|
+
function assertIs(_arg) {
|
|
515
|
+
}
|
|
516
|
+
util2.assertIs = assertIs;
|
|
517
|
+
function assertNever(_x) {
|
|
518
|
+
throw new Error();
|
|
519
|
+
}
|
|
520
|
+
util2.assertNever = assertNever;
|
|
521
|
+
util2.arrayToEnum = (items) => {
|
|
522
|
+
const obj = {};
|
|
523
|
+
for (const item of items) {
|
|
524
|
+
obj[item] = item;
|
|
525
|
+
}
|
|
526
|
+
return obj;
|
|
527
|
+
};
|
|
528
|
+
util2.getValidEnumValues = (obj) => {
|
|
529
|
+
const validKeys = util2.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
|
|
530
|
+
const filtered = {};
|
|
531
|
+
for (const k of validKeys) {
|
|
532
|
+
filtered[k] = obj[k];
|
|
533
|
+
}
|
|
534
|
+
return util2.objectValues(filtered);
|
|
535
|
+
};
|
|
536
|
+
util2.objectValues = (obj) => {
|
|
537
|
+
return util2.objectKeys(obj).map(function(e) {
|
|
538
|
+
return obj[e];
|
|
539
|
+
});
|
|
540
|
+
};
|
|
541
|
+
util2.objectKeys = typeof Object.keys === "function" ? (obj) => Object.keys(obj) : (object) => {
|
|
542
|
+
const keys = [];
|
|
543
|
+
for (const key in object) {
|
|
544
|
+
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
545
|
+
keys.push(key);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
return keys;
|
|
549
|
+
};
|
|
550
|
+
util2.find = (arr, checker) => {
|
|
551
|
+
for (const item of arr) {
|
|
552
|
+
if (checker(item))
|
|
553
|
+
return item;
|
|
554
|
+
}
|
|
555
|
+
return void 0;
|
|
556
|
+
};
|
|
557
|
+
util2.isInteger = typeof Number.isInteger === "function" ? (val) => Number.isInteger(val) : (val) => typeof val === "number" && Number.isFinite(val) && Math.floor(val) === val;
|
|
558
|
+
function joinValues(array, separator = " | ") {
|
|
559
|
+
return array.map((val) => typeof val === "string" ? `'${val}'` : val).join(separator);
|
|
560
|
+
}
|
|
561
|
+
util2.joinValues = joinValues;
|
|
562
|
+
util2.jsonStringifyReplacer = (_, value) => {
|
|
563
|
+
if (typeof value === "bigint") {
|
|
564
|
+
return value.toString();
|
|
565
|
+
}
|
|
566
|
+
return value;
|
|
567
|
+
};
|
|
568
|
+
})(util || (util = {}));
|
|
569
|
+
(function(objectUtil2) {
|
|
570
|
+
objectUtil2.mergeShapes = (first, second) => {
|
|
571
|
+
return {
|
|
572
|
+
...first,
|
|
573
|
+
...second
|
|
574
|
+
// second overwrites first
|
|
575
|
+
};
|
|
576
|
+
};
|
|
577
|
+
})(objectUtil || (objectUtil = {}));
|
|
578
|
+
ZodParsedType = util.arrayToEnum([
|
|
579
|
+
"string",
|
|
580
|
+
"nan",
|
|
581
|
+
"number",
|
|
582
|
+
"integer",
|
|
583
|
+
"float",
|
|
584
|
+
"boolean",
|
|
585
|
+
"date",
|
|
586
|
+
"bigint",
|
|
587
|
+
"symbol",
|
|
588
|
+
"function",
|
|
589
|
+
"undefined",
|
|
590
|
+
"null",
|
|
591
|
+
"array",
|
|
592
|
+
"object",
|
|
593
|
+
"unknown",
|
|
594
|
+
"promise",
|
|
595
|
+
"void",
|
|
596
|
+
"never",
|
|
597
|
+
"map",
|
|
598
|
+
"set"
|
|
599
|
+
]);
|
|
600
|
+
getParsedType = (data) => {
|
|
601
|
+
const t = typeof data;
|
|
602
|
+
switch (t) {
|
|
603
|
+
case "undefined":
|
|
604
|
+
return ZodParsedType.undefined;
|
|
605
|
+
case "string":
|
|
606
|
+
return ZodParsedType.string;
|
|
607
|
+
case "number":
|
|
608
|
+
return Number.isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
|
|
609
|
+
case "boolean":
|
|
610
|
+
return ZodParsedType.boolean;
|
|
611
|
+
case "function":
|
|
612
|
+
return ZodParsedType.function;
|
|
613
|
+
case "bigint":
|
|
614
|
+
return ZodParsedType.bigint;
|
|
615
|
+
case "symbol":
|
|
616
|
+
return ZodParsedType.symbol;
|
|
617
|
+
case "object":
|
|
618
|
+
if (Array.isArray(data)) {
|
|
619
|
+
return ZodParsedType.array;
|
|
620
|
+
}
|
|
621
|
+
if (data === null) {
|
|
622
|
+
return ZodParsedType.null;
|
|
623
|
+
}
|
|
624
|
+
if (data.then && typeof data.then === "function" && data.catch && typeof data.catch === "function") {
|
|
625
|
+
return ZodParsedType.promise;
|
|
626
|
+
}
|
|
627
|
+
if (typeof Map !== "undefined" && data instanceof Map) {
|
|
628
|
+
return ZodParsedType.map;
|
|
629
|
+
}
|
|
630
|
+
if (typeof Set !== "undefined" && data instanceof Set) {
|
|
631
|
+
return ZodParsedType.set;
|
|
632
|
+
}
|
|
633
|
+
if (typeof Date !== "undefined" && data instanceof Date) {
|
|
634
|
+
return ZodParsedType.date;
|
|
635
|
+
}
|
|
636
|
+
return ZodParsedType.object;
|
|
637
|
+
default:
|
|
638
|
+
return ZodParsedType.unknown;
|
|
639
|
+
}
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
// node_modules/zod/v3/ZodError.js
|
|
645
|
+
var ZodIssueCode, quotelessJson, ZodError;
|
|
646
|
+
var init_ZodError = __esm({
|
|
647
|
+
"node_modules/zod/v3/ZodError.js"() {
|
|
648
|
+
init_util();
|
|
649
|
+
ZodIssueCode = util.arrayToEnum([
|
|
650
|
+
"invalid_type",
|
|
651
|
+
"invalid_literal",
|
|
652
|
+
"custom",
|
|
653
|
+
"invalid_union",
|
|
654
|
+
"invalid_union_discriminator",
|
|
655
|
+
"invalid_enum_value",
|
|
656
|
+
"unrecognized_keys",
|
|
657
|
+
"invalid_arguments",
|
|
658
|
+
"invalid_return_type",
|
|
659
|
+
"invalid_date",
|
|
660
|
+
"invalid_string",
|
|
661
|
+
"too_small",
|
|
662
|
+
"too_big",
|
|
663
|
+
"invalid_intersection_types",
|
|
664
|
+
"not_multiple_of",
|
|
665
|
+
"not_finite"
|
|
666
|
+
]);
|
|
667
|
+
quotelessJson = (obj) => {
|
|
668
|
+
const json = JSON.stringify(obj, null, 2);
|
|
669
|
+
return json.replace(/"([^"]+)":/g, "$1:");
|
|
670
|
+
};
|
|
671
|
+
ZodError = class _ZodError extends Error {
|
|
672
|
+
get errors() {
|
|
673
|
+
return this.issues;
|
|
674
|
+
}
|
|
675
|
+
constructor(issues) {
|
|
676
|
+
super();
|
|
677
|
+
this.issues = [];
|
|
678
|
+
this.addIssue = (sub) => {
|
|
679
|
+
this.issues = [...this.issues, sub];
|
|
680
|
+
};
|
|
681
|
+
this.addIssues = (subs = []) => {
|
|
682
|
+
this.issues = [...this.issues, ...subs];
|
|
683
|
+
};
|
|
684
|
+
const actualProto = new.target.prototype;
|
|
685
|
+
if (Object.setPrototypeOf) {
|
|
686
|
+
Object.setPrototypeOf(this, actualProto);
|
|
687
|
+
} else {
|
|
688
|
+
this.__proto__ = actualProto;
|
|
689
|
+
}
|
|
690
|
+
this.name = "ZodError";
|
|
691
|
+
this.issues = issues;
|
|
692
|
+
}
|
|
693
|
+
format(_mapper) {
|
|
694
|
+
const mapper = _mapper || function(issue) {
|
|
695
|
+
return issue.message;
|
|
696
|
+
};
|
|
697
|
+
const fieldErrors = { _errors: [] };
|
|
698
|
+
const processError = (error) => {
|
|
699
|
+
for (const issue of error.issues) {
|
|
700
|
+
if (issue.code === "invalid_union") {
|
|
701
|
+
issue.unionErrors.map(processError);
|
|
702
|
+
} else if (issue.code === "invalid_return_type") {
|
|
703
|
+
processError(issue.returnTypeError);
|
|
704
|
+
} else if (issue.code === "invalid_arguments") {
|
|
705
|
+
processError(issue.argumentsError);
|
|
706
|
+
} else if (issue.path.length === 0) {
|
|
707
|
+
fieldErrors._errors.push(mapper(issue));
|
|
708
|
+
} else {
|
|
709
|
+
let curr = fieldErrors;
|
|
710
|
+
let i = 0;
|
|
711
|
+
while (i < issue.path.length) {
|
|
712
|
+
const el = issue.path[i];
|
|
713
|
+
const terminal = i === issue.path.length - 1;
|
|
714
|
+
if (!terminal) {
|
|
715
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
716
|
+
} else {
|
|
717
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
718
|
+
curr[el]._errors.push(mapper(issue));
|
|
719
|
+
}
|
|
720
|
+
curr = curr[el];
|
|
721
|
+
i++;
|
|
722
|
+
}
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
};
|
|
726
|
+
processError(this);
|
|
727
|
+
return fieldErrors;
|
|
728
|
+
}
|
|
729
|
+
static assert(value) {
|
|
730
|
+
if (!(value instanceof _ZodError)) {
|
|
731
|
+
throw new Error(`Not a ZodError: ${value}`);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
toString() {
|
|
735
|
+
return this.message;
|
|
736
|
+
}
|
|
737
|
+
get message() {
|
|
738
|
+
return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
|
|
739
|
+
}
|
|
740
|
+
get isEmpty() {
|
|
741
|
+
return this.issues.length === 0;
|
|
742
|
+
}
|
|
743
|
+
flatten(mapper = (issue) => issue.message) {
|
|
744
|
+
const fieldErrors = {};
|
|
745
|
+
const formErrors = [];
|
|
746
|
+
for (const sub of this.issues) {
|
|
747
|
+
if (sub.path.length > 0) {
|
|
748
|
+
const firstEl = sub.path[0];
|
|
749
|
+
fieldErrors[firstEl] = fieldErrors[firstEl] || [];
|
|
750
|
+
fieldErrors[firstEl].push(mapper(sub));
|
|
751
|
+
} else {
|
|
752
|
+
formErrors.push(mapper(sub));
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
return { formErrors, fieldErrors };
|
|
756
|
+
}
|
|
757
|
+
get formErrors() {
|
|
758
|
+
return this.flatten();
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
ZodError.create = (issues) => {
|
|
762
|
+
const error = new ZodError(issues);
|
|
763
|
+
return error;
|
|
764
|
+
};
|
|
765
|
+
}
|
|
766
|
+
});
|
|
767
|
+
|
|
768
|
+
// node_modules/zod/v3/locales/en.js
|
|
769
|
+
var errorMap, en_default;
|
|
770
|
+
var init_en = __esm({
|
|
771
|
+
"node_modules/zod/v3/locales/en.js"() {
|
|
772
|
+
init_ZodError();
|
|
773
|
+
init_util();
|
|
774
|
+
errorMap = (issue, _ctx) => {
|
|
775
|
+
let message;
|
|
776
|
+
switch (issue.code) {
|
|
777
|
+
case ZodIssueCode.invalid_type:
|
|
778
|
+
if (issue.received === ZodParsedType.undefined) {
|
|
779
|
+
message = "Required";
|
|
780
|
+
} else {
|
|
781
|
+
message = `Expected ${issue.expected}, received ${issue.received}`;
|
|
782
|
+
}
|
|
783
|
+
break;
|
|
784
|
+
case ZodIssueCode.invalid_literal:
|
|
785
|
+
message = `Invalid literal value, expected ${JSON.stringify(issue.expected, util.jsonStringifyReplacer)}`;
|
|
786
|
+
break;
|
|
787
|
+
case ZodIssueCode.unrecognized_keys:
|
|
788
|
+
message = `Unrecognized key(s) in object: ${util.joinValues(issue.keys, ", ")}`;
|
|
789
|
+
break;
|
|
790
|
+
case ZodIssueCode.invalid_union:
|
|
791
|
+
message = `Invalid input`;
|
|
792
|
+
break;
|
|
793
|
+
case ZodIssueCode.invalid_union_discriminator:
|
|
794
|
+
message = `Invalid discriminator value. Expected ${util.joinValues(issue.options)}`;
|
|
795
|
+
break;
|
|
796
|
+
case ZodIssueCode.invalid_enum_value:
|
|
797
|
+
message = `Invalid enum value. Expected ${util.joinValues(issue.options)}, received '${issue.received}'`;
|
|
798
|
+
break;
|
|
799
|
+
case ZodIssueCode.invalid_arguments:
|
|
800
|
+
message = `Invalid function arguments`;
|
|
801
|
+
break;
|
|
802
|
+
case ZodIssueCode.invalid_return_type:
|
|
803
|
+
message = `Invalid function return type`;
|
|
804
|
+
break;
|
|
805
|
+
case ZodIssueCode.invalid_date:
|
|
806
|
+
message = `Invalid date`;
|
|
807
|
+
break;
|
|
808
|
+
case ZodIssueCode.invalid_string:
|
|
809
|
+
if (typeof issue.validation === "object") {
|
|
810
|
+
if ("includes" in issue.validation) {
|
|
811
|
+
message = `Invalid input: must include "${issue.validation.includes}"`;
|
|
812
|
+
if (typeof issue.validation.position === "number") {
|
|
813
|
+
message = `${message} at one or more positions greater than or equal to ${issue.validation.position}`;
|
|
814
|
+
}
|
|
815
|
+
} else if ("startsWith" in issue.validation) {
|
|
816
|
+
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
817
|
+
} else if ("endsWith" in issue.validation) {
|
|
818
|
+
message = `Invalid input: must end with "${issue.validation.endsWith}"`;
|
|
819
|
+
} else {
|
|
820
|
+
util.assertNever(issue.validation);
|
|
821
|
+
}
|
|
822
|
+
} else if (issue.validation !== "regex") {
|
|
823
|
+
message = `Invalid ${issue.validation}`;
|
|
824
|
+
} else {
|
|
825
|
+
message = "Invalid";
|
|
826
|
+
}
|
|
827
|
+
break;
|
|
828
|
+
case ZodIssueCode.too_small:
|
|
829
|
+
if (issue.type === "array")
|
|
830
|
+
message = `Array must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `more than`} ${issue.minimum} element(s)`;
|
|
831
|
+
else if (issue.type === "string")
|
|
832
|
+
message = `String must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
|
|
833
|
+
else if (issue.type === "number")
|
|
834
|
+
message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
|
|
835
|
+
else if (issue.type === "bigint")
|
|
836
|
+
message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
|
|
837
|
+
else if (issue.type === "date")
|
|
838
|
+
message = `Date must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${new Date(Number(issue.minimum))}`;
|
|
839
|
+
else
|
|
840
|
+
message = "Invalid input";
|
|
841
|
+
break;
|
|
842
|
+
case ZodIssueCode.too_big:
|
|
843
|
+
if (issue.type === "array")
|
|
844
|
+
message = `Array must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `less than`} ${issue.maximum} element(s)`;
|
|
845
|
+
else if (issue.type === "string")
|
|
846
|
+
message = `String must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
|
|
847
|
+
else if (issue.type === "number")
|
|
848
|
+
message = `Number must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
849
|
+
else if (issue.type === "bigint")
|
|
850
|
+
message = `BigInt must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
851
|
+
else if (issue.type === "date")
|
|
852
|
+
message = `Date must be ${issue.exact ? `exactly` : issue.inclusive ? `smaller than or equal to` : `smaller than`} ${new Date(Number(issue.maximum))}`;
|
|
853
|
+
else
|
|
854
|
+
message = "Invalid input";
|
|
855
|
+
break;
|
|
856
|
+
case ZodIssueCode.custom:
|
|
857
|
+
message = `Invalid input`;
|
|
858
|
+
break;
|
|
859
|
+
case ZodIssueCode.invalid_intersection_types:
|
|
860
|
+
message = `Intersection results could not be merged`;
|
|
861
|
+
break;
|
|
862
|
+
case ZodIssueCode.not_multiple_of:
|
|
863
|
+
message = `Number must be a multiple of ${issue.multipleOf}`;
|
|
864
|
+
break;
|
|
865
|
+
case ZodIssueCode.not_finite:
|
|
866
|
+
message = "Number must be finite";
|
|
867
|
+
break;
|
|
868
|
+
default:
|
|
869
|
+
message = _ctx.defaultError;
|
|
870
|
+
util.assertNever(issue);
|
|
871
|
+
}
|
|
872
|
+
return { message };
|
|
873
|
+
};
|
|
874
|
+
en_default = errorMap;
|
|
875
|
+
}
|
|
876
|
+
});
|
|
877
|
+
|
|
878
|
+
// node_modules/zod/v3/errors.js
|
|
879
|
+
function setErrorMap(map) {
|
|
880
|
+
overrideErrorMap = map;
|
|
881
|
+
}
|
|
882
|
+
function getErrorMap() {
|
|
883
|
+
return overrideErrorMap;
|
|
884
|
+
}
|
|
885
|
+
var overrideErrorMap;
|
|
886
|
+
var init_errors = __esm({
|
|
887
|
+
"node_modules/zod/v3/errors.js"() {
|
|
888
|
+
init_en();
|
|
889
|
+
overrideErrorMap = en_default;
|
|
890
|
+
}
|
|
891
|
+
});
|
|
892
|
+
|
|
893
|
+
// node_modules/zod/v3/helpers/parseUtil.js
|
|
894
|
+
function addIssueToContext(ctx, issueData) {
|
|
895
|
+
const overrideMap = getErrorMap();
|
|
896
|
+
const issue = makeIssue({
|
|
897
|
+
issueData,
|
|
898
|
+
data: ctx.data,
|
|
899
|
+
path: ctx.path,
|
|
900
|
+
errorMaps: [
|
|
901
|
+
ctx.common.contextualErrorMap,
|
|
902
|
+
// contextual error map is first priority
|
|
903
|
+
ctx.schemaErrorMap,
|
|
904
|
+
// then schema-bound map if available
|
|
905
|
+
overrideMap,
|
|
906
|
+
// then global override map
|
|
907
|
+
overrideMap === en_default ? void 0 : en_default
|
|
908
|
+
// then global default map
|
|
909
|
+
].filter((x) => !!x)
|
|
910
|
+
});
|
|
911
|
+
ctx.common.issues.push(issue);
|
|
912
|
+
}
|
|
913
|
+
var makeIssue, EMPTY_PATH, ParseStatus, INVALID, DIRTY, OK, isAborted, isDirty, isValid, isAsync;
|
|
914
|
+
var init_parseUtil = __esm({
|
|
915
|
+
"node_modules/zod/v3/helpers/parseUtil.js"() {
|
|
916
|
+
init_errors();
|
|
917
|
+
init_en();
|
|
918
|
+
makeIssue = (params) => {
|
|
919
|
+
const { data, path, errorMaps, issueData } = params;
|
|
920
|
+
const fullPath = [...path, ...issueData.path || []];
|
|
921
|
+
const fullIssue = {
|
|
922
|
+
...issueData,
|
|
923
|
+
path: fullPath
|
|
924
|
+
};
|
|
925
|
+
if (issueData.message !== void 0) {
|
|
926
|
+
return {
|
|
927
|
+
...issueData,
|
|
928
|
+
path: fullPath,
|
|
929
|
+
message: issueData.message
|
|
930
|
+
};
|
|
931
|
+
}
|
|
932
|
+
let errorMessage = "";
|
|
933
|
+
const maps = errorMaps.filter((m) => !!m).slice().reverse();
|
|
934
|
+
for (const map of maps) {
|
|
935
|
+
errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;
|
|
936
|
+
}
|
|
937
|
+
return {
|
|
938
|
+
...issueData,
|
|
939
|
+
path: fullPath,
|
|
940
|
+
message: errorMessage
|
|
941
|
+
};
|
|
942
|
+
};
|
|
943
|
+
EMPTY_PATH = [];
|
|
944
|
+
ParseStatus = class _ParseStatus {
|
|
945
|
+
constructor() {
|
|
946
|
+
this.value = "valid";
|
|
947
|
+
}
|
|
948
|
+
dirty() {
|
|
949
|
+
if (this.value === "valid")
|
|
950
|
+
this.value = "dirty";
|
|
951
|
+
}
|
|
952
|
+
abort() {
|
|
953
|
+
if (this.value !== "aborted")
|
|
954
|
+
this.value = "aborted";
|
|
955
|
+
}
|
|
956
|
+
static mergeArray(status, results) {
|
|
957
|
+
const arrayValue = [];
|
|
958
|
+
for (const s of results) {
|
|
959
|
+
if (s.status === "aborted")
|
|
960
|
+
return INVALID;
|
|
961
|
+
if (s.status === "dirty")
|
|
962
|
+
status.dirty();
|
|
963
|
+
arrayValue.push(s.value);
|
|
964
|
+
}
|
|
965
|
+
return { status: status.value, value: arrayValue };
|
|
966
|
+
}
|
|
967
|
+
static async mergeObjectAsync(status, pairs) {
|
|
968
|
+
const syncPairs = [];
|
|
969
|
+
for (const pair of pairs) {
|
|
970
|
+
const key = await pair.key;
|
|
971
|
+
const value = await pair.value;
|
|
972
|
+
syncPairs.push({
|
|
973
|
+
key,
|
|
974
|
+
value
|
|
975
|
+
});
|
|
976
|
+
}
|
|
977
|
+
return _ParseStatus.mergeObjectSync(status, syncPairs);
|
|
978
|
+
}
|
|
979
|
+
static mergeObjectSync(status, pairs) {
|
|
980
|
+
const finalObject = {};
|
|
981
|
+
for (const pair of pairs) {
|
|
982
|
+
const { key, value } = pair;
|
|
983
|
+
if (key.status === "aborted")
|
|
984
|
+
return INVALID;
|
|
985
|
+
if (value.status === "aborted")
|
|
986
|
+
return INVALID;
|
|
987
|
+
if (key.status === "dirty")
|
|
988
|
+
status.dirty();
|
|
989
|
+
if (value.status === "dirty")
|
|
990
|
+
status.dirty();
|
|
991
|
+
if (key.value !== "__proto__" && (typeof value.value !== "undefined" || pair.alwaysSet)) {
|
|
992
|
+
finalObject[key.value] = value.value;
|
|
993
|
+
}
|
|
994
|
+
}
|
|
995
|
+
return { status: status.value, value: finalObject };
|
|
996
|
+
}
|
|
997
|
+
};
|
|
998
|
+
INVALID = Object.freeze({
|
|
999
|
+
status: "aborted"
|
|
1000
|
+
});
|
|
1001
|
+
DIRTY = (value) => ({ status: "dirty", value });
|
|
1002
|
+
OK = (value) => ({ status: "valid", value });
|
|
1003
|
+
isAborted = (x) => x.status === "aborted";
|
|
1004
|
+
isDirty = (x) => x.status === "dirty";
|
|
1005
|
+
isValid = (x) => x.status === "valid";
|
|
1006
|
+
isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
|
|
1007
|
+
}
|
|
1008
|
+
});
|
|
1009
|
+
|
|
1010
|
+
// node_modules/zod/v3/helpers/typeAliases.js
|
|
1011
|
+
var init_typeAliases = __esm({
|
|
1012
|
+
"node_modules/zod/v3/helpers/typeAliases.js"() {
|
|
1013
|
+
}
|
|
1014
|
+
});
|
|
1015
|
+
|
|
1016
|
+
// node_modules/zod/v3/helpers/errorUtil.js
|
|
1017
|
+
var errorUtil;
|
|
1018
|
+
var init_errorUtil = __esm({
|
|
1019
|
+
"node_modules/zod/v3/helpers/errorUtil.js"() {
|
|
1020
|
+
(function(errorUtil2) {
|
|
1021
|
+
errorUtil2.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
1022
|
+
errorUtil2.toString = (message) => typeof message === "string" ? message : message?.message;
|
|
1023
|
+
})(errorUtil || (errorUtil = {}));
|
|
1024
|
+
}
|
|
1025
|
+
});
|
|
1026
|
+
|
|
1027
|
+
// node_modules/zod/v3/types.js
|
|
1028
|
+
function processCreateParams(params) {
|
|
1029
|
+
if (!params)
|
|
1030
|
+
return {};
|
|
1031
|
+
const { errorMap: errorMap2, invalid_type_error, required_error, description } = params;
|
|
1032
|
+
if (errorMap2 && (invalid_type_error || required_error)) {
|
|
1033
|
+
throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`);
|
|
1034
|
+
}
|
|
1035
|
+
if (errorMap2)
|
|
1036
|
+
return { errorMap: errorMap2, description };
|
|
1037
|
+
const customMap = (iss, ctx) => {
|
|
1038
|
+
const { message } = params;
|
|
1039
|
+
if (iss.code === "invalid_enum_value") {
|
|
1040
|
+
return { message: message ?? ctx.defaultError };
|
|
1041
|
+
}
|
|
1042
|
+
if (typeof ctx.data === "undefined") {
|
|
1043
|
+
return { message: message ?? required_error ?? ctx.defaultError };
|
|
1044
|
+
}
|
|
1045
|
+
if (iss.code !== "invalid_type")
|
|
1046
|
+
return { message: ctx.defaultError };
|
|
1047
|
+
return { message: message ?? invalid_type_error ?? ctx.defaultError };
|
|
1048
|
+
};
|
|
1049
|
+
return { errorMap: customMap, description };
|
|
1050
|
+
}
|
|
1051
|
+
function timeRegexSource(args) {
|
|
1052
|
+
let secondsRegexSource = `[0-5]\\d`;
|
|
1053
|
+
if (args.precision) {
|
|
1054
|
+
secondsRegexSource = `${secondsRegexSource}\\.\\d{${args.precision}}`;
|
|
1055
|
+
} else if (args.precision == null) {
|
|
1056
|
+
secondsRegexSource = `${secondsRegexSource}(\\.\\d+)?`;
|
|
1057
|
+
}
|
|
1058
|
+
const secondsQuantifier = args.precision ? "+" : "?";
|
|
1059
|
+
return `([01]\\d|2[0-3]):[0-5]\\d(:${secondsRegexSource})${secondsQuantifier}`;
|
|
1060
|
+
}
|
|
1061
|
+
function timeRegex(args) {
|
|
1062
|
+
return new RegExp(`^${timeRegexSource(args)}$`);
|
|
1063
|
+
}
|
|
1064
|
+
function datetimeRegex(args) {
|
|
1065
|
+
let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
|
|
1066
|
+
const opts = [];
|
|
1067
|
+
opts.push(args.local ? `Z?` : `Z`);
|
|
1068
|
+
if (args.offset)
|
|
1069
|
+
opts.push(`([+-]\\d{2}:?\\d{2})`);
|
|
1070
|
+
regex = `${regex}(${opts.join("|")})`;
|
|
1071
|
+
return new RegExp(`^${regex}$`);
|
|
1072
|
+
}
|
|
1073
|
+
function isValidIP(ip, version) {
|
|
1074
|
+
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
|
|
1075
|
+
return true;
|
|
1076
|
+
}
|
|
1077
|
+
if ((version === "v6" || !version) && ipv6Regex.test(ip)) {
|
|
1078
|
+
return true;
|
|
1079
|
+
}
|
|
1080
|
+
return false;
|
|
1081
|
+
}
|
|
1082
|
+
function isValidJWT(jwt, alg) {
|
|
1083
|
+
if (!jwtRegex.test(jwt))
|
|
1084
|
+
return false;
|
|
1085
|
+
try {
|
|
1086
|
+
const [header] = jwt.split(".");
|
|
1087
|
+
if (!header)
|
|
1088
|
+
return false;
|
|
1089
|
+
const base64 = header.replace(/-/g, "+").replace(/_/g, "/").padEnd(header.length + (4 - header.length % 4) % 4, "=");
|
|
1090
|
+
const decoded = JSON.parse(atob(base64));
|
|
1091
|
+
if (typeof decoded !== "object" || decoded === null)
|
|
1092
|
+
return false;
|
|
1093
|
+
if ("typ" in decoded && decoded?.typ !== "JWT")
|
|
1094
|
+
return false;
|
|
1095
|
+
if (!decoded.alg)
|
|
1096
|
+
return false;
|
|
1097
|
+
if (alg && decoded.alg !== alg)
|
|
1098
|
+
return false;
|
|
1099
|
+
return true;
|
|
1100
|
+
} catch {
|
|
1101
|
+
return false;
|
|
1102
|
+
}
|
|
1103
|
+
}
|
|
1104
|
+
function isValidCidr(ip, version) {
|
|
1105
|
+
if ((version === "v4" || !version) && ipv4CidrRegex.test(ip)) {
|
|
1106
|
+
return true;
|
|
1107
|
+
}
|
|
1108
|
+
if ((version === "v6" || !version) && ipv6CidrRegex.test(ip)) {
|
|
1109
|
+
return true;
|
|
1110
|
+
}
|
|
1111
|
+
return false;
|
|
1112
|
+
}
|
|
1113
|
+
function floatSafeRemainder(val, step) {
|
|
1114
|
+
const valDecCount = (val.toString().split(".")[1] || "").length;
|
|
1115
|
+
const stepDecCount = (step.toString().split(".")[1] || "").length;
|
|
1116
|
+
const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
|
|
1117
|
+
const valInt = Number.parseInt(val.toFixed(decCount).replace(".", ""));
|
|
1118
|
+
const stepInt = Number.parseInt(step.toFixed(decCount).replace(".", ""));
|
|
1119
|
+
return valInt % stepInt / 10 ** decCount;
|
|
1120
|
+
}
|
|
1121
|
+
function deepPartialify(schema) {
|
|
1122
|
+
if (schema instanceof ZodObject) {
|
|
1123
|
+
const newShape = {};
|
|
1124
|
+
for (const key in schema.shape) {
|
|
1125
|
+
const fieldSchema = schema.shape[key];
|
|
1126
|
+
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema));
|
|
1127
|
+
}
|
|
1128
|
+
return new ZodObject({
|
|
1129
|
+
...schema._def,
|
|
1130
|
+
shape: () => newShape
|
|
1131
|
+
});
|
|
1132
|
+
} else if (schema instanceof ZodArray) {
|
|
1133
|
+
return new ZodArray({
|
|
1134
|
+
...schema._def,
|
|
1135
|
+
type: deepPartialify(schema.element)
|
|
1136
|
+
});
|
|
1137
|
+
} else if (schema instanceof ZodOptional) {
|
|
1138
|
+
return ZodOptional.create(deepPartialify(schema.unwrap()));
|
|
1139
|
+
} else if (schema instanceof ZodNullable) {
|
|
1140
|
+
return ZodNullable.create(deepPartialify(schema.unwrap()));
|
|
1141
|
+
} else if (schema instanceof ZodTuple) {
|
|
1142
|
+
return ZodTuple.create(schema.items.map((item) => deepPartialify(item)));
|
|
1143
|
+
} else {
|
|
1144
|
+
return schema;
|
|
1145
|
+
}
|
|
1146
|
+
}
|
|
1147
|
+
function mergeValues(a, b) {
|
|
1148
|
+
const aType = getParsedType(a);
|
|
1149
|
+
const bType = getParsedType(b);
|
|
1150
|
+
if (a === b) {
|
|
1151
|
+
return { valid: true, data: a };
|
|
1152
|
+
} else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
|
|
1153
|
+
const bKeys = util.objectKeys(b);
|
|
1154
|
+
const sharedKeys = util.objectKeys(a).filter((key) => bKeys.indexOf(key) !== -1);
|
|
1155
|
+
const newObj = { ...a, ...b };
|
|
1156
|
+
for (const key of sharedKeys) {
|
|
1157
|
+
const sharedValue = mergeValues(a[key], b[key]);
|
|
1158
|
+
if (!sharedValue.valid) {
|
|
1159
|
+
return { valid: false };
|
|
1160
|
+
}
|
|
1161
|
+
newObj[key] = sharedValue.data;
|
|
1162
|
+
}
|
|
1163
|
+
return { valid: true, data: newObj };
|
|
1164
|
+
} else if (aType === ZodParsedType.array && bType === ZodParsedType.array) {
|
|
1165
|
+
if (a.length !== b.length) {
|
|
1166
|
+
return { valid: false };
|
|
1167
|
+
}
|
|
1168
|
+
const newArray = [];
|
|
1169
|
+
for (let index = 0; index < a.length; index++) {
|
|
1170
|
+
const itemA = a[index];
|
|
1171
|
+
const itemB = b[index];
|
|
1172
|
+
const sharedValue = mergeValues(itemA, itemB);
|
|
1173
|
+
if (!sharedValue.valid) {
|
|
1174
|
+
return { valid: false };
|
|
1175
|
+
}
|
|
1176
|
+
newArray.push(sharedValue.data);
|
|
1177
|
+
}
|
|
1178
|
+
return { valid: true, data: newArray };
|
|
1179
|
+
} else if (aType === ZodParsedType.date && bType === ZodParsedType.date && +a === +b) {
|
|
1180
|
+
return { valid: true, data: a };
|
|
1181
|
+
} else {
|
|
1182
|
+
return { valid: false };
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
function createZodEnum(values, params) {
|
|
1186
|
+
return new ZodEnum({
|
|
1187
|
+
values,
|
|
1188
|
+
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
1189
|
+
...processCreateParams(params)
|
|
1190
|
+
});
|
|
1191
|
+
}
|
|
1192
|
+
function cleanParams(params, data) {
|
|
1193
|
+
const p = typeof params === "function" ? params(data) : typeof params === "string" ? { message: params } : params;
|
|
1194
|
+
const p2 = typeof p === "string" ? { message: p } : p;
|
|
1195
|
+
return p2;
|
|
1196
|
+
}
|
|
1197
|
+
function custom(check, _params = {}, fatal) {
|
|
1198
|
+
if (check)
|
|
1199
|
+
return ZodAny.create().superRefine((data, ctx) => {
|
|
1200
|
+
const r = check(data);
|
|
1201
|
+
if (r instanceof Promise) {
|
|
1202
|
+
return r.then((r2) => {
|
|
1203
|
+
if (!r2) {
|
|
1204
|
+
const params = cleanParams(_params, data);
|
|
1205
|
+
const _fatal = params.fatal ?? fatal ?? true;
|
|
1206
|
+
ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
|
|
1207
|
+
}
|
|
1208
|
+
});
|
|
1209
|
+
}
|
|
1210
|
+
if (!r) {
|
|
1211
|
+
const params = cleanParams(_params, data);
|
|
1212
|
+
const _fatal = params.fatal ?? fatal ?? true;
|
|
1213
|
+
ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
|
|
1214
|
+
}
|
|
1215
|
+
return;
|
|
1216
|
+
});
|
|
1217
|
+
return ZodAny.create();
|
|
1218
|
+
}
|
|
1219
|
+
var ParseInputLazyPath, handleResult, ZodType, cuidRegex, cuid2Regex, ulidRegex, uuidRegex, nanoidRegex, jwtRegex, durationRegex, emailRegex, _emojiRegex, emojiRegex, ipv4Regex, ipv4CidrRegex, ipv6Regex, ipv6CidrRegex, base64Regex, base64urlRegex, dateRegexSource, dateRegex, ZodString, ZodNumber, ZodBigInt, ZodBoolean, ZodDate, ZodSymbol, ZodUndefined, ZodNull, ZodAny, ZodUnknown, ZodNever, ZodVoid, ZodArray, ZodObject, ZodUnion, getDiscriminator, ZodDiscriminatedUnion, ZodIntersection, ZodTuple, ZodRecord, ZodMap, ZodSet, ZodFunction, ZodLazy, ZodLiteral, ZodEnum, ZodNativeEnum, ZodPromise, ZodEffects, ZodOptional, ZodNullable, ZodDefault, ZodCatch, ZodNaN, BRAND, ZodBranded, ZodPipeline, ZodReadonly, late, ZodFirstPartyTypeKind, instanceOfType, stringType, numberType, nanType, bigIntType, booleanType, dateType, symbolType, undefinedType, nullType, anyType, unknownType, neverType, voidType, arrayType, objectType, strictObjectType, unionType, discriminatedUnionType, intersectionType, tupleType, recordType, mapType, setType, functionType, lazyType, literalType, enumType, nativeEnumType, promiseType, effectsType, optionalType, nullableType, preprocessType, pipelineType, ostring, onumber, oboolean, coerce, NEVER;
|
|
1220
|
+
var init_types = __esm({
|
|
1221
|
+
"node_modules/zod/v3/types.js"() {
|
|
1222
|
+
init_ZodError();
|
|
1223
|
+
init_errors();
|
|
1224
|
+
init_errorUtil();
|
|
1225
|
+
init_parseUtil();
|
|
1226
|
+
init_util();
|
|
1227
|
+
ParseInputLazyPath = class {
|
|
1228
|
+
constructor(parent, value, path, key) {
|
|
1229
|
+
this._cachedPath = [];
|
|
1230
|
+
this.parent = parent;
|
|
1231
|
+
this.data = value;
|
|
1232
|
+
this._path = path;
|
|
1233
|
+
this._key = key;
|
|
1234
|
+
}
|
|
1235
|
+
get path() {
|
|
1236
|
+
if (!this._cachedPath.length) {
|
|
1237
|
+
if (Array.isArray(this._key)) {
|
|
1238
|
+
this._cachedPath.push(...this._path, ...this._key);
|
|
1239
|
+
} else {
|
|
1240
|
+
this._cachedPath.push(...this._path, this._key);
|
|
1241
|
+
}
|
|
1242
|
+
}
|
|
1243
|
+
return this._cachedPath;
|
|
1244
|
+
}
|
|
1245
|
+
};
|
|
1246
|
+
handleResult = (ctx, result) => {
|
|
1247
|
+
if (isValid(result)) {
|
|
1248
|
+
return { success: true, data: result.value };
|
|
1249
|
+
} else {
|
|
1250
|
+
if (!ctx.common.issues.length) {
|
|
1251
|
+
throw new Error("Validation failed but no issues detected.");
|
|
1252
|
+
}
|
|
1253
|
+
return {
|
|
1254
|
+
success: false,
|
|
1255
|
+
get error() {
|
|
1256
|
+
if (this._error)
|
|
1257
|
+
return this._error;
|
|
1258
|
+
const error = new ZodError(ctx.common.issues);
|
|
1259
|
+
this._error = error;
|
|
1260
|
+
return this._error;
|
|
1261
|
+
}
|
|
1262
|
+
};
|
|
1263
|
+
}
|
|
1264
|
+
};
|
|
1265
|
+
ZodType = class {
|
|
1266
|
+
get description() {
|
|
1267
|
+
return this._def.description;
|
|
1268
|
+
}
|
|
1269
|
+
_getType(input) {
|
|
1270
|
+
return getParsedType(input.data);
|
|
1271
|
+
}
|
|
1272
|
+
_getOrReturnCtx(input, ctx) {
|
|
1273
|
+
return ctx || {
|
|
1274
|
+
common: input.parent.common,
|
|
1275
|
+
data: input.data,
|
|
1276
|
+
parsedType: getParsedType(input.data),
|
|
1277
|
+
schemaErrorMap: this._def.errorMap,
|
|
1278
|
+
path: input.path,
|
|
1279
|
+
parent: input.parent
|
|
1280
|
+
};
|
|
1281
|
+
}
|
|
1282
|
+
_processInputParams(input) {
|
|
1283
|
+
return {
|
|
1284
|
+
status: new ParseStatus(),
|
|
1285
|
+
ctx: {
|
|
1286
|
+
common: input.parent.common,
|
|
1287
|
+
data: input.data,
|
|
1288
|
+
parsedType: getParsedType(input.data),
|
|
1289
|
+
schemaErrorMap: this._def.errorMap,
|
|
1290
|
+
path: input.path,
|
|
1291
|
+
parent: input.parent
|
|
1292
|
+
}
|
|
1293
|
+
};
|
|
1294
|
+
}
|
|
1295
|
+
_parseSync(input) {
|
|
1296
|
+
const result = this._parse(input);
|
|
1297
|
+
if (isAsync(result)) {
|
|
1298
|
+
throw new Error("Synchronous parse encountered promise.");
|
|
1299
|
+
}
|
|
1300
|
+
return result;
|
|
1301
|
+
}
|
|
1302
|
+
_parseAsync(input) {
|
|
1303
|
+
const result = this._parse(input);
|
|
1304
|
+
return Promise.resolve(result);
|
|
1305
|
+
}
|
|
1306
|
+
parse(data, params) {
|
|
1307
|
+
const result = this.safeParse(data, params);
|
|
1308
|
+
if (result.success)
|
|
1309
|
+
return result.data;
|
|
1310
|
+
throw result.error;
|
|
1311
|
+
}
|
|
1312
|
+
safeParse(data, params) {
|
|
1313
|
+
const ctx = {
|
|
1314
|
+
common: {
|
|
1315
|
+
issues: [],
|
|
1316
|
+
async: params?.async ?? false,
|
|
1317
|
+
contextualErrorMap: params?.errorMap
|
|
1318
|
+
},
|
|
1319
|
+
path: params?.path || [],
|
|
1320
|
+
schemaErrorMap: this._def.errorMap,
|
|
1321
|
+
parent: null,
|
|
1322
|
+
data,
|
|
1323
|
+
parsedType: getParsedType(data)
|
|
1324
|
+
};
|
|
1325
|
+
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
|
|
1326
|
+
return handleResult(ctx, result);
|
|
1327
|
+
}
|
|
1328
|
+
"~validate"(data) {
|
|
1329
|
+
const ctx = {
|
|
1330
|
+
common: {
|
|
1331
|
+
issues: [],
|
|
1332
|
+
async: !!this["~standard"].async
|
|
1333
|
+
},
|
|
1334
|
+
path: [],
|
|
1335
|
+
schemaErrorMap: this._def.errorMap,
|
|
1336
|
+
parent: null,
|
|
1337
|
+
data,
|
|
1338
|
+
parsedType: getParsedType(data)
|
|
1339
|
+
};
|
|
1340
|
+
if (!this["~standard"].async) {
|
|
1341
|
+
try {
|
|
1342
|
+
const result = this._parseSync({ data, path: [], parent: ctx });
|
|
1343
|
+
return isValid(result) ? {
|
|
1344
|
+
value: result.value
|
|
1345
|
+
} : {
|
|
1346
|
+
issues: ctx.common.issues
|
|
1347
|
+
};
|
|
1348
|
+
} catch (err) {
|
|
1349
|
+
if (err?.message?.toLowerCase()?.includes("encountered")) {
|
|
1350
|
+
this["~standard"].async = true;
|
|
1351
|
+
}
|
|
1352
|
+
ctx.common = {
|
|
1353
|
+
issues: [],
|
|
1354
|
+
async: true
|
|
1355
|
+
};
|
|
1356
|
+
}
|
|
1357
|
+
}
|
|
1358
|
+
return this._parseAsync({ data, path: [], parent: ctx }).then((result) => isValid(result) ? {
|
|
1359
|
+
value: result.value
|
|
1360
|
+
} : {
|
|
1361
|
+
issues: ctx.common.issues
|
|
1362
|
+
});
|
|
1363
|
+
}
|
|
1364
|
+
async parseAsync(data, params) {
|
|
1365
|
+
const result = await this.safeParseAsync(data, params);
|
|
1366
|
+
if (result.success)
|
|
1367
|
+
return result.data;
|
|
1368
|
+
throw result.error;
|
|
1369
|
+
}
|
|
1370
|
+
async safeParseAsync(data, params) {
|
|
1371
|
+
const ctx = {
|
|
1372
|
+
common: {
|
|
1373
|
+
issues: [],
|
|
1374
|
+
contextualErrorMap: params?.errorMap,
|
|
1375
|
+
async: true
|
|
1376
|
+
},
|
|
1377
|
+
path: params?.path || [],
|
|
1378
|
+
schemaErrorMap: this._def.errorMap,
|
|
1379
|
+
parent: null,
|
|
1380
|
+
data,
|
|
1381
|
+
parsedType: getParsedType(data)
|
|
1382
|
+
};
|
|
1383
|
+
const maybeAsyncResult = this._parse({ data, path: ctx.path, parent: ctx });
|
|
1384
|
+
const result = await (isAsync(maybeAsyncResult) ? maybeAsyncResult : Promise.resolve(maybeAsyncResult));
|
|
1385
|
+
return handleResult(ctx, result);
|
|
1386
|
+
}
|
|
1387
|
+
refine(check, message) {
|
|
1388
|
+
const getIssueProperties = (val) => {
|
|
1389
|
+
if (typeof message === "string" || typeof message === "undefined") {
|
|
1390
|
+
return { message };
|
|
1391
|
+
} else if (typeof message === "function") {
|
|
1392
|
+
return message(val);
|
|
1393
|
+
} else {
|
|
1394
|
+
return message;
|
|
1395
|
+
}
|
|
1396
|
+
};
|
|
1397
|
+
return this._refinement((val, ctx) => {
|
|
1398
|
+
const result = check(val);
|
|
1399
|
+
const setError = () => ctx.addIssue({
|
|
1400
|
+
code: ZodIssueCode.custom,
|
|
1401
|
+
...getIssueProperties(val)
|
|
1402
|
+
});
|
|
1403
|
+
if (typeof Promise !== "undefined" && result instanceof Promise) {
|
|
1404
|
+
return result.then((data) => {
|
|
1405
|
+
if (!data) {
|
|
1406
|
+
setError();
|
|
1407
|
+
return false;
|
|
1408
|
+
} else {
|
|
1409
|
+
return true;
|
|
1410
|
+
}
|
|
1411
|
+
});
|
|
1412
|
+
}
|
|
1413
|
+
if (!result) {
|
|
1414
|
+
setError();
|
|
1415
|
+
return false;
|
|
1416
|
+
} else {
|
|
1417
|
+
return true;
|
|
1418
|
+
}
|
|
1419
|
+
});
|
|
1420
|
+
}
|
|
1421
|
+
refinement(check, refinementData) {
|
|
1422
|
+
return this._refinement((val, ctx) => {
|
|
1423
|
+
if (!check(val)) {
|
|
1424
|
+
ctx.addIssue(typeof refinementData === "function" ? refinementData(val, ctx) : refinementData);
|
|
1425
|
+
return false;
|
|
1426
|
+
} else {
|
|
1427
|
+
return true;
|
|
1428
|
+
}
|
|
1429
|
+
});
|
|
1430
|
+
}
|
|
1431
|
+
_refinement(refinement) {
|
|
1432
|
+
return new ZodEffects({
|
|
1433
|
+
schema: this,
|
|
1434
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
1435
|
+
effect: { type: "refinement", refinement }
|
|
1436
|
+
});
|
|
1437
|
+
}
|
|
1438
|
+
superRefine(refinement) {
|
|
1439
|
+
return this._refinement(refinement);
|
|
1440
|
+
}
|
|
1441
|
+
constructor(def) {
|
|
1442
|
+
this.spa = this.safeParseAsync;
|
|
1443
|
+
this._def = def;
|
|
1444
|
+
this.parse = this.parse.bind(this);
|
|
1445
|
+
this.safeParse = this.safeParse.bind(this);
|
|
1446
|
+
this.parseAsync = this.parseAsync.bind(this);
|
|
1447
|
+
this.safeParseAsync = this.safeParseAsync.bind(this);
|
|
1448
|
+
this.spa = this.spa.bind(this);
|
|
1449
|
+
this.refine = this.refine.bind(this);
|
|
1450
|
+
this.refinement = this.refinement.bind(this);
|
|
1451
|
+
this.superRefine = this.superRefine.bind(this);
|
|
1452
|
+
this.optional = this.optional.bind(this);
|
|
1453
|
+
this.nullable = this.nullable.bind(this);
|
|
1454
|
+
this.nullish = this.nullish.bind(this);
|
|
1455
|
+
this.array = this.array.bind(this);
|
|
1456
|
+
this.promise = this.promise.bind(this);
|
|
1457
|
+
this.or = this.or.bind(this);
|
|
1458
|
+
this.and = this.and.bind(this);
|
|
1459
|
+
this.transform = this.transform.bind(this);
|
|
1460
|
+
this.brand = this.brand.bind(this);
|
|
1461
|
+
this.default = this.default.bind(this);
|
|
1462
|
+
this.catch = this.catch.bind(this);
|
|
1463
|
+
this.describe = this.describe.bind(this);
|
|
1464
|
+
this.pipe = this.pipe.bind(this);
|
|
1465
|
+
this.readonly = this.readonly.bind(this);
|
|
1466
|
+
this.isNullable = this.isNullable.bind(this);
|
|
1467
|
+
this.isOptional = this.isOptional.bind(this);
|
|
1468
|
+
this["~standard"] = {
|
|
1469
|
+
version: 1,
|
|
1470
|
+
vendor: "zod",
|
|
1471
|
+
validate: (data) => this["~validate"](data)
|
|
1472
|
+
};
|
|
1473
|
+
}
|
|
1474
|
+
optional() {
|
|
1475
|
+
return ZodOptional.create(this, this._def);
|
|
1476
|
+
}
|
|
1477
|
+
nullable() {
|
|
1478
|
+
return ZodNullable.create(this, this._def);
|
|
1479
|
+
}
|
|
1480
|
+
nullish() {
|
|
1481
|
+
return this.nullable().optional();
|
|
1482
|
+
}
|
|
1483
|
+
array() {
|
|
1484
|
+
return ZodArray.create(this);
|
|
1485
|
+
}
|
|
1486
|
+
promise() {
|
|
1487
|
+
return ZodPromise.create(this, this._def);
|
|
1488
|
+
}
|
|
1489
|
+
or(option) {
|
|
1490
|
+
return ZodUnion.create([this, option], this._def);
|
|
1491
|
+
}
|
|
1492
|
+
and(incoming) {
|
|
1493
|
+
return ZodIntersection.create(this, incoming, this._def);
|
|
1494
|
+
}
|
|
1495
|
+
transform(transform) {
|
|
1496
|
+
return new ZodEffects({
|
|
1497
|
+
...processCreateParams(this._def),
|
|
1498
|
+
schema: this,
|
|
1499
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
1500
|
+
effect: { type: "transform", transform }
|
|
1501
|
+
});
|
|
1502
|
+
}
|
|
1503
|
+
default(def) {
|
|
1504
|
+
const defaultValueFunc = typeof def === "function" ? def : () => def;
|
|
1505
|
+
return new ZodDefault({
|
|
1506
|
+
...processCreateParams(this._def),
|
|
1507
|
+
innerType: this,
|
|
1508
|
+
defaultValue: defaultValueFunc,
|
|
1509
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault
|
|
1510
|
+
});
|
|
1511
|
+
}
|
|
1512
|
+
brand() {
|
|
1513
|
+
return new ZodBranded({
|
|
1514
|
+
typeName: ZodFirstPartyTypeKind.ZodBranded,
|
|
1515
|
+
type: this,
|
|
1516
|
+
...processCreateParams(this._def)
|
|
1517
|
+
});
|
|
1518
|
+
}
|
|
1519
|
+
catch(def) {
|
|
1520
|
+
const catchValueFunc = typeof def === "function" ? def : () => def;
|
|
1521
|
+
return new ZodCatch({
|
|
1522
|
+
...processCreateParams(this._def),
|
|
1523
|
+
innerType: this,
|
|
1524
|
+
catchValue: catchValueFunc,
|
|
1525
|
+
typeName: ZodFirstPartyTypeKind.ZodCatch
|
|
1526
|
+
});
|
|
1527
|
+
}
|
|
1528
|
+
describe(description) {
|
|
1529
|
+
const This = this.constructor;
|
|
1530
|
+
return new This({
|
|
1531
|
+
...this._def,
|
|
1532
|
+
description
|
|
1533
|
+
});
|
|
1534
|
+
}
|
|
1535
|
+
pipe(target) {
|
|
1536
|
+
return ZodPipeline.create(this, target);
|
|
1537
|
+
}
|
|
1538
|
+
readonly() {
|
|
1539
|
+
return ZodReadonly.create(this);
|
|
1540
|
+
}
|
|
1541
|
+
isOptional() {
|
|
1542
|
+
return this.safeParse(void 0).success;
|
|
1543
|
+
}
|
|
1544
|
+
isNullable() {
|
|
1545
|
+
return this.safeParse(null).success;
|
|
1546
|
+
}
|
|
1547
|
+
};
|
|
1548
|
+
cuidRegex = /^c[^\s-]{8,}$/i;
|
|
1549
|
+
cuid2Regex = /^[0-9a-z]+$/;
|
|
1550
|
+
ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
|
|
1551
|
+
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;
|
|
1552
|
+
nanoidRegex = /^[a-z0-9_-]{21}$/i;
|
|
1553
|
+
jwtRegex = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/;
|
|
1554
|
+
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)?)??$/;
|
|
1555
|
+
emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
|
|
1556
|
+
_emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
1557
|
+
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])$/;
|
|
1558
|
+
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])$/;
|
|
1559
|
+
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]))$/;
|
|
1560
|
+
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])$/;
|
|
1561
|
+
base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
1562
|
+
base64urlRegex = /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/;
|
|
1563
|
+
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])))`;
|
|
1564
|
+
dateRegex = new RegExp(`^${dateRegexSource}$`);
|
|
1565
|
+
ZodString = class _ZodString extends ZodType {
|
|
1566
|
+
_parse(input) {
|
|
1567
|
+
if (this._def.coerce) {
|
|
1568
|
+
input.data = String(input.data);
|
|
1569
|
+
}
|
|
1570
|
+
const parsedType = this._getType(input);
|
|
1571
|
+
if (parsedType !== ZodParsedType.string) {
|
|
1572
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
1573
|
+
addIssueToContext(ctx2, {
|
|
1574
|
+
code: ZodIssueCode.invalid_type,
|
|
1575
|
+
expected: ZodParsedType.string,
|
|
1576
|
+
received: ctx2.parsedType
|
|
1577
|
+
});
|
|
1578
|
+
return INVALID;
|
|
1579
|
+
}
|
|
1580
|
+
const status = new ParseStatus();
|
|
1581
|
+
let ctx = void 0;
|
|
1582
|
+
for (const check of this._def.checks) {
|
|
1583
|
+
if (check.kind === "min") {
|
|
1584
|
+
if (input.data.length < check.value) {
|
|
1585
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1586
|
+
addIssueToContext(ctx, {
|
|
1587
|
+
code: ZodIssueCode.too_small,
|
|
1588
|
+
minimum: check.value,
|
|
1589
|
+
type: "string",
|
|
1590
|
+
inclusive: true,
|
|
1591
|
+
exact: false,
|
|
1592
|
+
message: check.message
|
|
1593
|
+
});
|
|
1594
|
+
status.dirty();
|
|
1595
|
+
}
|
|
1596
|
+
} else if (check.kind === "max") {
|
|
1597
|
+
if (input.data.length > check.value) {
|
|
1598
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1599
|
+
addIssueToContext(ctx, {
|
|
1600
|
+
code: ZodIssueCode.too_big,
|
|
1601
|
+
maximum: check.value,
|
|
1602
|
+
type: "string",
|
|
1603
|
+
inclusive: true,
|
|
1604
|
+
exact: false,
|
|
1605
|
+
message: check.message
|
|
1606
|
+
});
|
|
1607
|
+
status.dirty();
|
|
1608
|
+
}
|
|
1609
|
+
} else if (check.kind === "length") {
|
|
1610
|
+
const tooBig = input.data.length > check.value;
|
|
1611
|
+
const tooSmall = input.data.length < check.value;
|
|
1612
|
+
if (tooBig || tooSmall) {
|
|
1613
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1614
|
+
if (tooBig) {
|
|
1615
|
+
addIssueToContext(ctx, {
|
|
1616
|
+
code: ZodIssueCode.too_big,
|
|
1617
|
+
maximum: check.value,
|
|
1618
|
+
type: "string",
|
|
1619
|
+
inclusive: true,
|
|
1620
|
+
exact: true,
|
|
1621
|
+
message: check.message
|
|
1622
|
+
});
|
|
1623
|
+
} else if (tooSmall) {
|
|
1624
|
+
addIssueToContext(ctx, {
|
|
1625
|
+
code: ZodIssueCode.too_small,
|
|
1626
|
+
minimum: check.value,
|
|
1627
|
+
type: "string",
|
|
1628
|
+
inclusive: true,
|
|
1629
|
+
exact: true,
|
|
1630
|
+
message: check.message
|
|
1631
|
+
});
|
|
1632
|
+
}
|
|
1633
|
+
status.dirty();
|
|
1634
|
+
}
|
|
1635
|
+
} else if (check.kind === "email") {
|
|
1636
|
+
if (!emailRegex.test(input.data)) {
|
|
1637
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1638
|
+
addIssueToContext(ctx, {
|
|
1639
|
+
validation: "email",
|
|
1640
|
+
code: ZodIssueCode.invalid_string,
|
|
1641
|
+
message: check.message
|
|
1642
|
+
});
|
|
1643
|
+
status.dirty();
|
|
1644
|
+
}
|
|
1645
|
+
} else if (check.kind === "emoji") {
|
|
1646
|
+
if (!emojiRegex) {
|
|
1647
|
+
emojiRegex = new RegExp(_emojiRegex, "u");
|
|
1648
|
+
}
|
|
1649
|
+
if (!emojiRegex.test(input.data)) {
|
|
1650
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1651
|
+
addIssueToContext(ctx, {
|
|
1652
|
+
validation: "emoji",
|
|
1653
|
+
code: ZodIssueCode.invalid_string,
|
|
1654
|
+
message: check.message
|
|
1655
|
+
});
|
|
1656
|
+
status.dirty();
|
|
1657
|
+
}
|
|
1658
|
+
} else if (check.kind === "uuid") {
|
|
1659
|
+
if (!uuidRegex.test(input.data)) {
|
|
1660
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1661
|
+
addIssueToContext(ctx, {
|
|
1662
|
+
validation: "uuid",
|
|
1663
|
+
code: ZodIssueCode.invalid_string,
|
|
1664
|
+
message: check.message
|
|
1665
|
+
});
|
|
1666
|
+
status.dirty();
|
|
1667
|
+
}
|
|
1668
|
+
} else if (check.kind === "nanoid") {
|
|
1669
|
+
if (!nanoidRegex.test(input.data)) {
|
|
1670
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1671
|
+
addIssueToContext(ctx, {
|
|
1672
|
+
validation: "nanoid",
|
|
1673
|
+
code: ZodIssueCode.invalid_string,
|
|
1674
|
+
message: check.message
|
|
1675
|
+
});
|
|
1676
|
+
status.dirty();
|
|
1677
|
+
}
|
|
1678
|
+
} else if (check.kind === "cuid") {
|
|
1679
|
+
if (!cuidRegex.test(input.data)) {
|
|
1680
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1681
|
+
addIssueToContext(ctx, {
|
|
1682
|
+
validation: "cuid",
|
|
1683
|
+
code: ZodIssueCode.invalid_string,
|
|
1684
|
+
message: check.message
|
|
1685
|
+
});
|
|
1686
|
+
status.dirty();
|
|
1687
|
+
}
|
|
1688
|
+
} else if (check.kind === "cuid2") {
|
|
1689
|
+
if (!cuid2Regex.test(input.data)) {
|
|
1690
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1691
|
+
addIssueToContext(ctx, {
|
|
1692
|
+
validation: "cuid2",
|
|
1693
|
+
code: ZodIssueCode.invalid_string,
|
|
1694
|
+
message: check.message
|
|
1695
|
+
});
|
|
1696
|
+
status.dirty();
|
|
1697
|
+
}
|
|
1698
|
+
} else if (check.kind === "ulid") {
|
|
1699
|
+
if (!ulidRegex.test(input.data)) {
|
|
1700
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1701
|
+
addIssueToContext(ctx, {
|
|
1702
|
+
validation: "ulid",
|
|
1703
|
+
code: ZodIssueCode.invalid_string,
|
|
1704
|
+
message: check.message
|
|
1705
|
+
});
|
|
1706
|
+
status.dirty();
|
|
1707
|
+
}
|
|
1708
|
+
} else if (check.kind === "url") {
|
|
1709
|
+
try {
|
|
1710
|
+
new URL(input.data);
|
|
1711
|
+
} catch {
|
|
1712
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1713
|
+
addIssueToContext(ctx, {
|
|
1714
|
+
validation: "url",
|
|
1715
|
+
code: ZodIssueCode.invalid_string,
|
|
1716
|
+
message: check.message
|
|
1717
|
+
});
|
|
1718
|
+
status.dirty();
|
|
1719
|
+
}
|
|
1720
|
+
} else if (check.kind === "regex") {
|
|
1721
|
+
check.regex.lastIndex = 0;
|
|
1722
|
+
const testResult = check.regex.test(input.data);
|
|
1723
|
+
if (!testResult) {
|
|
1724
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1725
|
+
addIssueToContext(ctx, {
|
|
1726
|
+
validation: "regex",
|
|
1727
|
+
code: ZodIssueCode.invalid_string,
|
|
1728
|
+
message: check.message
|
|
1729
|
+
});
|
|
1730
|
+
status.dirty();
|
|
1731
|
+
}
|
|
1732
|
+
} else if (check.kind === "trim") {
|
|
1733
|
+
input.data = input.data.trim();
|
|
1734
|
+
} else if (check.kind === "includes") {
|
|
1735
|
+
if (!input.data.includes(check.value, check.position)) {
|
|
1736
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1737
|
+
addIssueToContext(ctx, {
|
|
1738
|
+
code: ZodIssueCode.invalid_string,
|
|
1739
|
+
validation: { includes: check.value, position: check.position },
|
|
1740
|
+
message: check.message
|
|
1741
|
+
});
|
|
1742
|
+
status.dirty();
|
|
1743
|
+
}
|
|
1744
|
+
} else if (check.kind === "toLowerCase") {
|
|
1745
|
+
input.data = input.data.toLowerCase();
|
|
1746
|
+
} else if (check.kind === "toUpperCase") {
|
|
1747
|
+
input.data = input.data.toUpperCase();
|
|
1748
|
+
} else if (check.kind === "startsWith") {
|
|
1749
|
+
if (!input.data.startsWith(check.value)) {
|
|
1750
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1751
|
+
addIssueToContext(ctx, {
|
|
1752
|
+
code: ZodIssueCode.invalid_string,
|
|
1753
|
+
validation: { startsWith: check.value },
|
|
1754
|
+
message: check.message
|
|
1755
|
+
});
|
|
1756
|
+
status.dirty();
|
|
1757
|
+
}
|
|
1758
|
+
} else if (check.kind === "endsWith") {
|
|
1759
|
+
if (!input.data.endsWith(check.value)) {
|
|
1760
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1761
|
+
addIssueToContext(ctx, {
|
|
1762
|
+
code: ZodIssueCode.invalid_string,
|
|
1763
|
+
validation: { endsWith: check.value },
|
|
1764
|
+
message: check.message
|
|
1765
|
+
});
|
|
1766
|
+
status.dirty();
|
|
1767
|
+
}
|
|
1768
|
+
} else if (check.kind === "datetime") {
|
|
1769
|
+
const regex = datetimeRegex(check);
|
|
1770
|
+
if (!regex.test(input.data)) {
|
|
1771
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1772
|
+
addIssueToContext(ctx, {
|
|
1773
|
+
code: ZodIssueCode.invalid_string,
|
|
1774
|
+
validation: "datetime",
|
|
1775
|
+
message: check.message
|
|
1776
|
+
});
|
|
1777
|
+
status.dirty();
|
|
1778
|
+
}
|
|
1779
|
+
} else if (check.kind === "date") {
|
|
1780
|
+
const regex = dateRegex;
|
|
1781
|
+
if (!regex.test(input.data)) {
|
|
1782
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1783
|
+
addIssueToContext(ctx, {
|
|
1784
|
+
code: ZodIssueCode.invalid_string,
|
|
1785
|
+
validation: "date",
|
|
1786
|
+
message: check.message
|
|
1787
|
+
});
|
|
1788
|
+
status.dirty();
|
|
1789
|
+
}
|
|
1790
|
+
} else if (check.kind === "time") {
|
|
1791
|
+
const regex = timeRegex(check);
|
|
1792
|
+
if (!regex.test(input.data)) {
|
|
1793
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1794
|
+
addIssueToContext(ctx, {
|
|
1795
|
+
code: ZodIssueCode.invalid_string,
|
|
1796
|
+
validation: "time",
|
|
1797
|
+
message: check.message
|
|
1798
|
+
});
|
|
1799
|
+
status.dirty();
|
|
1800
|
+
}
|
|
1801
|
+
} else if (check.kind === "duration") {
|
|
1802
|
+
if (!durationRegex.test(input.data)) {
|
|
1803
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1804
|
+
addIssueToContext(ctx, {
|
|
1805
|
+
validation: "duration",
|
|
1806
|
+
code: ZodIssueCode.invalid_string,
|
|
1807
|
+
message: check.message
|
|
1808
|
+
});
|
|
1809
|
+
status.dirty();
|
|
1810
|
+
}
|
|
1811
|
+
} else if (check.kind === "ip") {
|
|
1812
|
+
if (!isValidIP(input.data, check.version)) {
|
|
1813
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1814
|
+
addIssueToContext(ctx, {
|
|
1815
|
+
validation: "ip",
|
|
1816
|
+
code: ZodIssueCode.invalid_string,
|
|
1817
|
+
message: check.message
|
|
1818
|
+
});
|
|
1819
|
+
status.dirty();
|
|
1820
|
+
}
|
|
1821
|
+
} else if (check.kind === "jwt") {
|
|
1822
|
+
if (!isValidJWT(input.data, check.alg)) {
|
|
1823
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1824
|
+
addIssueToContext(ctx, {
|
|
1825
|
+
validation: "jwt",
|
|
1826
|
+
code: ZodIssueCode.invalid_string,
|
|
1827
|
+
message: check.message
|
|
1828
|
+
});
|
|
1829
|
+
status.dirty();
|
|
1830
|
+
}
|
|
1831
|
+
} else if (check.kind === "cidr") {
|
|
1832
|
+
if (!isValidCidr(input.data, check.version)) {
|
|
1833
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1834
|
+
addIssueToContext(ctx, {
|
|
1835
|
+
validation: "cidr",
|
|
1836
|
+
code: ZodIssueCode.invalid_string,
|
|
1837
|
+
message: check.message
|
|
1838
|
+
});
|
|
1839
|
+
status.dirty();
|
|
1840
|
+
}
|
|
1841
|
+
} else if (check.kind === "base64") {
|
|
1842
|
+
if (!base64Regex.test(input.data)) {
|
|
1843
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1844
|
+
addIssueToContext(ctx, {
|
|
1845
|
+
validation: "base64",
|
|
1846
|
+
code: ZodIssueCode.invalid_string,
|
|
1847
|
+
message: check.message
|
|
1848
|
+
});
|
|
1849
|
+
status.dirty();
|
|
1850
|
+
}
|
|
1851
|
+
} else if (check.kind === "base64url") {
|
|
1852
|
+
if (!base64urlRegex.test(input.data)) {
|
|
1853
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1854
|
+
addIssueToContext(ctx, {
|
|
1855
|
+
validation: "base64url",
|
|
1856
|
+
code: ZodIssueCode.invalid_string,
|
|
1857
|
+
message: check.message
|
|
1858
|
+
});
|
|
1859
|
+
status.dirty();
|
|
1860
|
+
}
|
|
1861
|
+
} else {
|
|
1862
|
+
util.assertNever(check);
|
|
1863
|
+
}
|
|
1864
|
+
}
|
|
1865
|
+
return { status: status.value, value: input.data };
|
|
1866
|
+
}
|
|
1867
|
+
_regex(regex, validation, message) {
|
|
1868
|
+
return this.refinement((data) => regex.test(data), {
|
|
1869
|
+
validation,
|
|
1870
|
+
code: ZodIssueCode.invalid_string,
|
|
1871
|
+
...errorUtil.errToObj(message)
|
|
1872
|
+
});
|
|
1873
|
+
}
|
|
1874
|
+
_addCheck(check) {
|
|
1875
|
+
return new _ZodString({
|
|
1876
|
+
...this._def,
|
|
1877
|
+
checks: [...this._def.checks, check]
|
|
1878
|
+
});
|
|
1879
|
+
}
|
|
1880
|
+
email(message) {
|
|
1881
|
+
return this._addCheck({ kind: "email", ...errorUtil.errToObj(message) });
|
|
1882
|
+
}
|
|
1883
|
+
url(message) {
|
|
1884
|
+
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
|
|
1885
|
+
}
|
|
1886
|
+
emoji(message) {
|
|
1887
|
+
return this._addCheck({ kind: "emoji", ...errorUtil.errToObj(message) });
|
|
1888
|
+
}
|
|
1889
|
+
uuid(message) {
|
|
1890
|
+
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
|
|
1891
|
+
}
|
|
1892
|
+
nanoid(message) {
|
|
1893
|
+
return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) });
|
|
1894
|
+
}
|
|
1895
|
+
cuid(message) {
|
|
1896
|
+
return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
|
|
1897
|
+
}
|
|
1898
|
+
cuid2(message) {
|
|
1899
|
+
return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
|
|
1900
|
+
}
|
|
1901
|
+
ulid(message) {
|
|
1902
|
+
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
|
|
1903
|
+
}
|
|
1904
|
+
base64(message) {
|
|
1905
|
+
return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
|
|
1906
|
+
}
|
|
1907
|
+
base64url(message) {
|
|
1908
|
+
return this._addCheck({
|
|
1909
|
+
kind: "base64url",
|
|
1910
|
+
...errorUtil.errToObj(message)
|
|
1911
|
+
});
|
|
1912
|
+
}
|
|
1913
|
+
jwt(options) {
|
|
1914
|
+
return this._addCheck({ kind: "jwt", ...errorUtil.errToObj(options) });
|
|
1915
|
+
}
|
|
1916
|
+
ip(options) {
|
|
1917
|
+
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
|
|
1918
|
+
}
|
|
1919
|
+
cidr(options) {
|
|
1920
|
+
return this._addCheck({ kind: "cidr", ...errorUtil.errToObj(options) });
|
|
1921
|
+
}
|
|
1922
|
+
datetime(options) {
|
|
1923
|
+
if (typeof options === "string") {
|
|
1924
|
+
return this._addCheck({
|
|
1925
|
+
kind: "datetime",
|
|
1926
|
+
precision: null,
|
|
1927
|
+
offset: false,
|
|
1928
|
+
local: false,
|
|
1929
|
+
message: options
|
|
1930
|
+
});
|
|
1931
|
+
}
|
|
1932
|
+
return this._addCheck({
|
|
1933
|
+
kind: "datetime",
|
|
1934
|
+
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
1935
|
+
offset: options?.offset ?? false,
|
|
1936
|
+
local: options?.local ?? false,
|
|
1937
|
+
...errorUtil.errToObj(options?.message)
|
|
1938
|
+
});
|
|
1939
|
+
}
|
|
1940
|
+
date(message) {
|
|
1941
|
+
return this._addCheck({ kind: "date", message });
|
|
1942
|
+
}
|
|
1943
|
+
time(options) {
|
|
1944
|
+
if (typeof options === "string") {
|
|
1945
|
+
return this._addCheck({
|
|
1946
|
+
kind: "time",
|
|
1947
|
+
precision: null,
|
|
1948
|
+
message: options
|
|
1949
|
+
});
|
|
1950
|
+
}
|
|
1951
|
+
return this._addCheck({
|
|
1952
|
+
kind: "time",
|
|
1953
|
+
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
1954
|
+
...errorUtil.errToObj(options?.message)
|
|
1955
|
+
});
|
|
1956
|
+
}
|
|
1957
|
+
duration(message) {
|
|
1958
|
+
return this._addCheck({ kind: "duration", ...errorUtil.errToObj(message) });
|
|
1959
|
+
}
|
|
1960
|
+
regex(regex, message) {
|
|
1961
|
+
return this._addCheck({
|
|
1962
|
+
kind: "regex",
|
|
1963
|
+
regex,
|
|
1964
|
+
...errorUtil.errToObj(message)
|
|
1965
|
+
});
|
|
1966
|
+
}
|
|
1967
|
+
includes(value, options) {
|
|
1968
|
+
return this._addCheck({
|
|
1969
|
+
kind: "includes",
|
|
1970
|
+
value,
|
|
1971
|
+
position: options?.position,
|
|
1972
|
+
...errorUtil.errToObj(options?.message)
|
|
1973
|
+
});
|
|
1974
|
+
}
|
|
1975
|
+
startsWith(value, message) {
|
|
1976
|
+
return this._addCheck({
|
|
1977
|
+
kind: "startsWith",
|
|
1978
|
+
value,
|
|
1979
|
+
...errorUtil.errToObj(message)
|
|
1980
|
+
});
|
|
1981
|
+
}
|
|
1982
|
+
endsWith(value, message) {
|
|
1983
|
+
return this._addCheck({
|
|
1984
|
+
kind: "endsWith",
|
|
1985
|
+
value,
|
|
1986
|
+
...errorUtil.errToObj(message)
|
|
1987
|
+
});
|
|
1988
|
+
}
|
|
1989
|
+
min(minLength, message) {
|
|
1990
|
+
return this._addCheck({
|
|
1991
|
+
kind: "min",
|
|
1992
|
+
value: minLength,
|
|
1993
|
+
...errorUtil.errToObj(message)
|
|
1994
|
+
});
|
|
1995
|
+
}
|
|
1996
|
+
max(maxLength, message) {
|
|
1997
|
+
return this._addCheck({
|
|
1998
|
+
kind: "max",
|
|
1999
|
+
value: maxLength,
|
|
2000
|
+
...errorUtil.errToObj(message)
|
|
2001
|
+
});
|
|
2002
|
+
}
|
|
2003
|
+
length(len, message) {
|
|
2004
|
+
return this._addCheck({
|
|
2005
|
+
kind: "length",
|
|
2006
|
+
value: len,
|
|
2007
|
+
...errorUtil.errToObj(message)
|
|
2008
|
+
});
|
|
2009
|
+
}
|
|
2010
|
+
/**
|
|
2011
|
+
* Equivalent to `.min(1)`
|
|
2012
|
+
*/
|
|
2013
|
+
nonempty(message) {
|
|
2014
|
+
return this.min(1, errorUtil.errToObj(message));
|
|
2015
|
+
}
|
|
2016
|
+
trim() {
|
|
2017
|
+
return new _ZodString({
|
|
2018
|
+
...this._def,
|
|
2019
|
+
checks: [...this._def.checks, { kind: "trim" }]
|
|
2020
|
+
});
|
|
2021
|
+
}
|
|
2022
|
+
toLowerCase() {
|
|
2023
|
+
return new _ZodString({
|
|
2024
|
+
...this._def,
|
|
2025
|
+
checks: [...this._def.checks, { kind: "toLowerCase" }]
|
|
2026
|
+
});
|
|
2027
|
+
}
|
|
2028
|
+
toUpperCase() {
|
|
2029
|
+
return new _ZodString({
|
|
2030
|
+
...this._def,
|
|
2031
|
+
checks: [...this._def.checks, { kind: "toUpperCase" }]
|
|
2032
|
+
});
|
|
2033
|
+
}
|
|
2034
|
+
get isDatetime() {
|
|
2035
|
+
return !!this._def.checks.find((ch) => ch.kind === "datetime");
|
|
2036
|
+
}
|
|
2037
|
+
get isDate() {
|
|
2038
|
+
return !!this._def.checks.find((ch) => ch.kind === "date");
|
|
2039
|
+
}
|
|
2040
|
+
get isTime() {
|
|
2041
|
+
return !!this._def.checks.find((ch) => ch.kind === "time");
|
|
2042
|
+
}
|
|
2043
|
+
get isDuration() {
|
|
2044
|
+
return !!this._def.checks.find((ch) => ch.kind === "duration");
|
|
2045
|
+
}
|
|
2046
|
+
get isEmail() {
|
|
2047
|
+
return !!this._def.checks.find((ch) => ch.kind === "email");
|
|
2048
|
+
}
|
|
2049
|
+
get isURL() {
|
|
2050
|
+
return !!this._def.checks.find((ch) => ch.kind === "url");
|
|
2051
|
+
}
|
|
2052
|
+
get isEmoji() {
|
|
2053
|
+
return !!this._def.checks.find((ch) => ch.kind === "emoji");
|
|
2054
|
+
}
|
|
2055
|
+
get isUUID() {
|
|
2056
|
+
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
2057
|
+
}
|
|
2058
|
+
get isNANOID() {
|
|
2059
|
+
return !!this._def.checks.find((ch) => ch.kind === "nanoid");
|
|
2060
|
+
}
|
|
2061
|
+
get isCUID() {
|
|
2062
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
2063
|
+
}
|
|
2064
|
+
get isCUID2() {
|
|
2065
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid2");
|
|
2066
|
+
}
|
|
2067
|
+
get isULID() {
|
|
2068
|
+
return !!this._def.checks.find((ch) => ch.kind === "ulid");
|
|
2069
|
+
}
|
|
2070
|
+
get isIP() {
|
|
2071
|
+
return !!this._def.checks.find((ch) => ch.kind === "ip");
|
|
2072
|
+
}
|
|
2073
|
+
get isCIDR() {
|
|
2074
|
+
return !!this._def.checks.find((ch) => ch.kind === "cidr");
|
|
2075
|
+
}
|
|
2076
|
+
get isBase64() {
|
|
2077
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64");
|
|
2078
|
+
}
|
|
2079
|
+
get isBase64url() {
|
|
2080
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64url");
|
|
2081
|
+
}
|
|
2082
|
+
get minLength() {
|
|
2083
|
+
let min = null;
|
|
2084
|
+
for (const ch of this._def.checks) {
|
|
2085
|
+
if (ch.kind === "min") {
|
|
2086
|
+
if (min === null || ch.value > min)
|
|
2087
|
+
min = ch.value;
|
|
2088
|
+
}
|
|
2089
|
+
}
|
|
2090
|
+
return min;
|
|
2091
|
+
}
|
|
2092
|
+
get maxLength() {
|
|
2093
|
+
let max = null;
|
|
2094
|
+
for (const ch of this._def.checks) {
|
|
2095
|
+
if (ch.kind === "max") {
|
|
2096
|
+
if (max === null || ch.value < max)
|
|
2097
|
+
max = ch.value;
|
|
2098
|
+
}
|
|
2099
|
+
}
|
|
2100
|
+
return max;
|
|
2101
|
+
}
|
|
2102
|
+
};
|
|
2103
|
+
ZodString.create = (params) => {
|
|
2104
|
+
return new ZodString({
|
|
2105
|
+
checks: [],
|
|
2106
|
+
typeName: ZodFirstPartyTypeKind.ZodString,
|
|
2107
|
+
coerce: params?.coerce ?? false,
|
|
2108
|
+
...processCreateParams(params)
|
|
2109
|
+
});
|
|
2110
|
+
};
|
|
2111
|
+
ZodNumber = class _ZodNumber extends ZodType {
|
|
2112
|
+
constructor() {
|
|
2113
|
+
super(...arguments);
|
|
2114
|
+
this.min = this.gte;
|
|
2115
|
+
this.max = this.lte;
|
|
2116
|
+
this.step = this.multipleOf;
|
|
2117
|
+
}
|
|
2118
|
+
_parse(input) {
|
|
2119
|
+
if (this._def.coerce) {
|
|
2120
|
+
input.data = Number(input.data);
|
|
2121
|
+
}
|
|
2122
|
+
const parsedType = this._getType(input);
|
|
2123
|
+
if (parsedType !== ZodParsedType.number) {
|
|
2124
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
2125
|
+
addIssueToContext(ctx2, {
|
|
2126
|
+
code: ZodIssueCode.invalid_type,
|
|
2127
|
+
expected: ZodParsedType.number,
|
|
2128
|
+
received: ctx2.parsedType
|
|
2129
|
+
});
|
|
2130
|
+
return INVALID;
|
|
2131
|
+
}
|
|
2132
|
+
let ctx = void 0;
|
|
2133
|
+
const status = new ParseStatus();
|
|
2134
|
+
for (const check of this._def.checks) {
|
|
2135
|
+
if (check.kind === "int") {
|
|
2136
|
+
if (!util.isInteger(input.data)) {
|
|
2137
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2138
|
+
addIssueToContext(ctx, {
|
|
2139
|
+
code: ZodIssueCode.invalid_type,
|
|
2140
|
+
expected: "integer",
|
|
2141
|
+
received: "float",
|
|
2142
|
+
message: check.message
|
|
2143
|
+
});
|
|
2144
|
+
status.dirty();
|
|
2145
|
+
}
|
|
2146
|
+
} else if (check.kind === "min") {
|
|
2147
|
+
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
2148
|
+
if (tooSmall) {
|
|
2149
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2150
|
+
addIssueToContext(ctx, {
|
|
2151
|
+
code: ZodIssueCode.too_small,
|
|
2152
|
+
minimum: check.value,
|
|
2153
|
+
type: "number",
|
|
2154
|
+
inclusive: check.inclusive,
|
|
2155
|
+
exact: false,
|
|
2156
|
+
message: check.message
|
|
2157
|
+
});
|
|
2158
|
+
status.dirty();
|
|
2159
|
+
}
|
|
2160
|
+
} else if (check.kind === "max") {
|
|
2161
|
+
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
2162
|
+
if (tooBig) {
|
|
2163
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2164
|
+
addIssueToContext(ctx, {
|
|
2165
|
+
code: ZodIssueCode.too_big,
|
|
2166
|
+
maximum: check.value,
|
|
2167
|
+
type: "number",
|
|
2168
|
+
inclusive: check.inclusive,
|
|
2169
|
+
exact: false,
|
|
2170
|
+
message: check.message
|
|
2171
|
+
});
|
|
2172
|
+
status.dirty();
|
|
2173
|
+
}
|
|
2174
|
+
} else if (check.kind === "multipleOf") {
|
|
2175
|
+
if (floatSafeRemainder(input.data, check.value) !== 0) {
|
|
2176
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2177
|
+
addIssueToContext(ctx, {
|
|
2178
|
+
code: ZodIssueCode.not_multiple_of,
|
|
2179
|
+
multipleOf: check.value,
|
|
2180
|
+
message: check.message
|
|
2181
|
+
});
|
|
2182
|
+
status.dirty();
|
|
2183
|
+
}
|
|
2184
|
+
} else if (check.kind === "finite") {
|
|
2185
|
+
if (!Number.isFinite(input.data)) {
|
|
2186
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2187
|
+
addIssueToContext(ctx, {
|
|
2188
|
+
code: ZodIssueCode.not_finite,
|
|
2189
|
+
message: check.message
|
|
2190
|
+
});
|
|
2191
|
+
status.dirty();
|
|
2192
|
+
}
|
|
2193
|
+
} else {
|
|
2194
|
+
util.assertNever(check);
|
|
2195
|
+
}
|
|
2196
|
+
}
|
|
2197
|
+
return { status: status.value, value: input.data };
|
|
2198
|
+
}
|
|
2199
|
+
gte(value, message) {
|
|
2200
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
2201
|
+
}
|
|
2202
|
+
gt(value, message) {
|
|
2203
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
2204
|
+
}
|
|
2205
|
+
lte(value, message) {
|
|
2206
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
2207
|
+
}
|
|
2208
|
+
lt(value, message) {
|
|
2209
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
2210
|
+
}
|
|
2211
|
+
setLimit(kind, value, inclusive, message) {
|
|
2212
|
+
return new _ZodNumber({
|
|
2213
|
+
...this._def,
|
|
2214
|
+
checks: [
|
|
2215
|
+
...this._def.checks,
|
|
2216
|
+
{
|
|
2217
|
+
kind,
|
|
2218
|
+
value,
|
|
2219
|
+
inclusive,
|
|
2220
|
+
message: errorUtil.toString(message)
|
|
2221
|
+
}
|
|
2222
|
+
]
|
|
2223
|
+
});
|
|
2224
|
+
}
|
|
2225
|
+
_addCheck(check) {
|
|
2226
|
+
return new _ZodNumber({
|
|
2227
|
+
...this._def,
|
|
2228
|
+
checks: [...this._def.checks, check]
|
|
2229
|
+
});
|
|
2230
|
+
}
|
|
2231
|
+
int(message) {
|
|
2232
|
+
return this._addCheck({
|
|
2233
|
+
kind: "int",
|
|
2234
|
+
message: errorUtil.toString(message)
|
|
2235
|
+
});
|
|
2236
|
+
}
|
|
2237
|
+
positive(message) {
|
|
2238
|
+
return this._addCheck({
|
|
2239
|
+
kind: "min",
|
|
2240
|
+
value: 0,
|
|
2241
|
+
inclusive: false,
|
|
2242
|
+
message: errorUtil.toString(message)
|
|
2243
|
+
});
|
|
2244
|
+
}
|
|
2245
|
+
negative(message) {
|
|
2246
|
+
return this._addCheck({
|
|
2247
|
+
kind: "max",
|
|
2248
|
+
value: 0,
|
|
2249
|
+
inclusive: false,
|
|
2250
|
+
message: errorUtil.toString(message)
|
|
2251
|
+
});
|
|
2252
|
+
}
|
|
2253
|
+
nonpositive(message) {
|
|
2254
|
+
return this._addCheck({
|
|
2255
|
+
kind: "max",
|
|
2256
|
+
value: 0,
|
|
2257
|
+
inclusive: true,
|
|
2258
|
+
message: errorUtil.toString(message)
|
|
2259
|
+
});
|
|
2260
|
+
}
|
|
2261
|
+
nonnegative(message) {
|
|
2262
|
+
return this._addCheck({
|
|
2263
|
+
kind: "min",
|
|
2264
|
+
value: 0,
|
|
2265
|
+
inclusive: true,
|
|
2266
|
+
message: errorUtil.toString(message)
|
|
2267
|
+
});
|
|
2268
|
+
}
|
|
2269
|
+
multipleOf(value, message) {
|
|
2270
|
+
return this._addCheck({
|
|
2271
|
+
kind: "multipleOf",
|
|
2272
|
+
value,
|
|
2273
|
+
message: errorUtil.toString(message)
|
|
2274
|
+
});
|
|
2275
|
+
}
|
|
2276
|
+
finite(message) {
|
|
2277
|
+
return this._addCheck({
|
|
2278
|
+
kind: "finite",
|
|
2279
|
+
message: errorUtil.toString(message)
|
|
2280
|
+
});
|
|
2281
|
+
}
|
|
2282
|
+
safe(message) {
|
|
2283
|
+
return this._addCheck({
|
|
2284
|
+
kind: "min",
|
|
2285
|
+
inclusive: true,
|
|
2286
|
+
value: Number.MIN_SAFE_INTEGER,
|
|
2287
|
+
message: errorUtil.toString(message)
|
|
2288
|
+
})._addCheck({
|
|
2289
|
+
kind: "max",
|
|
2290
|
+
inclusive: true,
|
|
2291
|
+
value: Number.MAX_SAFE_INTEGER,
|
|
2292
|
+
message: errorUtil.toString(message)
|
|
2293
|
+
});
|
|
2294
|
+
}
|
|
2295
|
+
get minValue() {
|
|
2296
|
+
let min = null;
|
|
2297
|
+
for (const ch of this._def.checks) {
|
|
2298
|
+
if (ch.kind === "min") {
|
|
2299
|
+
if (min === null || ch.value > min)
|
|
2300
|
+
min = ch.value;
|
|
2301
|
+
}
|
|
2302
|
+
}
|
|
2303
|
+
return min;
|
|
2304
|
+
}
|
|
2305
|
+
get maxValue() {
|
|
2306
|
+
let max = null;
|
|
2307
|
+
for (const ch of this._def.checks) {
|
|
2308
|
+
if (ch.kind === "max") {
|
|
2309
|
+
if (max === null || ch.value < max)
|
|
2310
|
+
max = ch.value;
|
|
2311
|
+
}
|
|
2312
|
+
}
|
|
2313
|
+
return max;
|
|
2314
|
+
}
|
|
2315
|
+
get isInt() {
|
|
2316
|
+
return !!this._def.checks.find((ch) => ch.kind === "int" || ch.kind === "multipleOf" && util.isInteger(ch.value));
|
|
2317
|
+
}
|
|
2318
|
+
get isFinite() {
|
|
2319
|
+
let max = null;
|
|
2320
|
+
let min = null;
|
|
2321
|
+
for (const ch of this._def.checks) {
|
|
2322
|
+
if (ch.kind === "finite" || ch.kind === "int" || ch.kind === "multipleOf") {
|
|
2323
|
+
return true;
|
|
2324
|
+
} else if (ch.kind === "min") {
|
|
2325
|
+
if (min === null || ch.value > min)
|
|
2326
|
+
min = ch.value;
|
|
2327
|
+
} else if (ch.kind === "max") {
|
|
2328
|
+
if (max === null || ch.value < max)
|
|
2329
|
+
max = ch.value;
|
|
2330
|
+
}
|
|
2331
|
+
}
|
|
2332
|
+
return Number.isFinite(min) && Number.isFinite(max);
|
|
2333
|
+
}
|
|
2334
|
+
};
|
|
2335
|
+
ZodNumber.create = (params) => {
|
|
2336
|
+
return new ZodNumber({
|
|
2337
|
+
checks: [],
|
|
2338
|
+
typeName: ZodFirstPartyTypeKind.ZodNumber,
|
|
2339
|
+
coerce: params?.coerce || false,
|
|
2340
|
+
...processCreateParams(params)
|
|
2341
|
+
});
|
|
2342
|
+
};
|
|
2343
|
+
ZodBigInt = class _ZodBigInt extends ZodType {
|
|
2344
|
+
constructor() {
|
|
2345
|
+
super(...arguments);
|
|
2346
|
+
this.min = this.gte;
|
|
2347
|
+
this.max = this.lte;
|
|
2348
|
+
}
|
|
2349
|
+
_parse(input) {
|
|
2350
|
+
if (this._def.coerce) {
|
|
2351
|
+
try {
|
|
2352
|
+
input.data = BigInt(input.data);
|
|
2353
|
+
} catch {
|
|
2354
|
+
return this._getInvalidInput(input);
|
|
2355
|
+
}
|
|
2356
|
+
}
|
|
2357
|
+
const parsedType = this._getType(input);
|
|
2358
|
+
if (parsedType !== ZodParsedType.bigint) {
|
|
2359
|
+
return this._getInvalidInput(input);
|
|
2360
|
+
}
|
|
2361
|
+
let ctx = void 0;
|
|
2362
|
+
const status = new ParseStatus();
|
|
2363
|
+
for (const check of this._def.checks) {
|
|
2364
|
+
if (check.kind === "min") {
|
|
2365
|
+
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
2366
|
+
if (tooSmall) {
|
|
2367
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2368
|
+
addIssueToContext(ctx, {
|
|
2369
|
+
code: ZodIssueCode.too_small,
|
|
2370
|
+
type: "bigint",
|
|
2371
|
+
minimum: check.value,
|
|
2372
|
+
inclusive: check.inclusive,
|
|
2373
|
+
message: check.message
|
|
2374
|
+
});
|
|
2375
|
+
status.dirty();
|
|
2376
|
+
}
|
|
2377
|
+
} else if (check.kind === "max") {
|
|
2378
|
+
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
2379
|
+
if (tooBig) {
|
|
2380
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2381
|
+
addIssueToContext(ctx, {
|
|
2382
|
+
code: ZodIssueCode.too_big,
|
|
2383
|
+
type: "bigint",
|
|
2384
|
+
maximum: check.value,
|
|
2385
|
+
inclusive: check.inclusive,
|
|
2386
|
+
message: check.message
|
|
2387
|
+
});
|
|
2388
|
+
status.dirty();
|
|
2389
|
+
}
|
|
2390
|
+
} else if (check.kind === "multipleOf") {
|
|
2391
|
+
if (input.data % check.value !== BigInt(0)) {
|
|
2392
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2393
|
+
addIssueToContext(ctx, {
|
|
2394
|
+
code: ZodIssueCode.not_multiple_of,
|
|
2395
|
+
multipleOf: check.value,
|
|
2396
|
+
message: check.message
|
|
2397
|
+
});
|
|
2398
|
+
status.dirty();
|
|
2399
|
+
}
|
|
2400
|
+
} else {
|
|
2401
|
+
util.assertNever(check);
|
|
2402
|
+
}
|
|
2403
|
+
}
|
|
2404
|
+
return { status: status.value, value: input.data };
|
|
2405
|
+
}
|
|
2406
|
+
_getInvalidInput(input) {
|
|
2407
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2408
|
+
addIssueToContext(ctx, {
|
|
2409
|
+
code: ZodIssueCode.invalid_type,
|
|
2410
|
+
expected: ZodParsedType.bigint,
|
|
2411
|
+
received: ctx.parsedType
|
|
2412
|
+
});
|
|
2413
|
+
return INVALID;
|
|
2414
|
+
}
|
|
2415
|
+
gte(value, message) {
|
|
2416
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
2417
|
+
}
|
|
2418
|
+
gt(value, message) {
|
|
2419
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
2420
|
+
}
|
|
2421
|
+
lte(value, message) {
|
|
2422
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
2423
|
+
}
|
|
2424
|
+
lt(value, message) {
|
|
2425
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
2426
|
+
}
|
|
2427
|
+
setLimit(kind, value, inclusive, message) {
|
|
2428
|
+
return new _ZodBigInt({
|
|
2429
|
+
...this._def,
|
|
2430
|
+
checks: [
|
|
2431
|
+
...this._def.checks,
|
|
2432
|
+
{
|
|
2433
|
+
kind,
|
|
2434
|
+
value,
|
|
2435
|
+
inclusive,
|
|
2436
|
+
message: errorUtil.toString(message)
|
|
2437
|
+
}
|
|
2438
|
+
]
|
|
2439
|
+
});
|
|
2440
|
+
}
|
|
2441
|
+
_addCheck(check) {
|
|
2442
|
+
return new _ZodBigInt({
|
|
2443
|
+
...this._def,
|
|
2444
|
+
checks: [...this._def.checks, check]
|
|
2445
|
+
});
|
|
2446
|
+
}
|
|
2447
|
+
positive(message) {
|
|
2448
|
+
return this._addCheck({
|
|
2449
|
+
kind: "min",
|
|
2450
|
+
value: BigInt(0),
|
|
2451
|
+
inclusive: false,
|
|
2452
|
+
message: errorUtil.toString(message)
|
|
2453
|
+
});
|
|
2454
|
+
}
|
|
2455
|
+
negative(message) {
|
|
2456
|
+
return this._addCheck({
|
|
2457
|
+
kind: "max",
|
|
2458
|
+
value: BigInt(0),
|
|
2459
|
+
inclusive: false,
|
|
2460
|
+
message: errorUtil.toString(message)
|
|
2461
|
+
});
|
|
2462
|
+
}
|
|
2463
|
+
nonpositive(message) {
|
|
2464
|
+
return this._addCheck({
|
|
2465
|
+
kind: "max",
|
|
2466
|
+
value: BigInt(0),
|
|
2467
|
+
inclusive: true,
|
|
2468
|
+
message: errorUtil.toString(message)
|
|
2469
|
+
});
|
|
2470
|
+
}
|
|
2471
|
+
nonnegative(message) {
|
|
2472
|
+
return this._addCheck({
|
|
2473
|
+
kind: "min",
|
|
2474
|
+
value: BigInt(0),
|
|
2475
|
+
inclusive: true,
|
|
2476
|
+
message: errorUtil.toString(message)
|
|
2477
|
+
});
|
|
2478
|
+
}
|
|
2479
|
+
multipleOf(value, message) {
|
|
2480
|
+
return this._addCheck({
|
|
2481
|
+
kind: "multipleOf",
|
|
2482
|
+
value,
|
|
2483
|
+
message: errorUtil.toString(message)
|
|
2484
|
+
});
|
|
2485
|
+
}
|
|
2486
|
+
get minValue() {
|
|
2487
|
+
let min = null;
|
|
2488
|
+
for (const ch of this._def.checks) {
|
|
2489
|
+
if (ch.kind === "min") {
|
|
2490
|
+
if (min === null || ch.value > min)
|
|
2491
|
+
min = ch.value;
|
|
2492
|
+
}
|
|
2493
|
+
}
|
|
2494
|
+
return min;
|
|
2495
|
+
}
|
|
2496
|
+
get maxValue() {
|
|
2497
|
+
let max = null;
|
|
2498
|
+
for (const ch of this._def.checks) {
|
|
2499
|
+
if (ch.kind === "max") {
|
|
2500
|
+
if (max === null || ch.value < max)
|
|
2501
|
+
max = ch.value;
|
|
2502
|
+
}
|
|
2503
|
+
}
|
|
2504
|
+
return max;
|
|
2505
|
+
}
|
|
2506
|
+
};
|
|
2507
|
+
ZodBigInt.create = (params) => {
|
|
2508
|
+
return new ZodBigInt({
|
|
2509
|
+
checks: [],
|
|
2510
|
+
typeName: ZodFirstPartyTypeKind.ZodBigInt,
|
|
2511
|
+
coerce: params?.coerce ?? false,
|
|
2512
|
+
...processCreateParams(params)
|
|
2513
|
+
});
|
|
2514
|
+
};
|
|
2515
|
+
ZodBoolean = class extends ZodType {
|
|
2516
|
+
_parse(input) {
|
|
2517
|
+
if (this._def.coerce) {
|
|
2518
|
+
input.data = Boolean(input.data);
|
|
2519
|
+
}
|
|
2520
|
+
const parsedType = this._getType(input);
|
|
2521
|
+
if (parsedType !== ZodParsedType.boolean) {
|
|
2522
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2523
|
+
addIssueToContext(ctx, {
|
|
2524
|
+
code: ZodIssueCode.invalid_type,
|
|
2525
|
+
expected: ZodParsedType.boolean,
|
|
2526
|
+
received: ctx.parsedType
|
|
2527
|
+
});
|
|
2528
|
+
return INVALID;
|
|
2529
|
+
}
|
|
2530
|
+
return OK(input.data);
|
|
2531
|
+
}
|
|
2532
|
+
};
|
|
2533
|
+
ZodBoolean.create = (params) => {
|
|
2534
|
+
return new ZodBoolean({
|
|
2535
|
+
typeName: ZodFirstPartyTypeKind.ZodBoolean,
|
|
2536
|
+
coerce: params?.coerce || false,
|
|
2537
|
+
...processCreateParams(params)
|
|
2538
|
+
});
|
|
2539
|
+
};
|
|
2540
|
+
ZodDate = class _ZodDate extends ZodType {
|
|
2541
|
+
_parse(input) {
|
|
2542
|
+
if (this._def.coerce) {
|
|
2543
|
+
input.data = new Date(input.data);
|
|
2544
|
+
}
|
|
2545
|
+
const parsedType = this._getType(input);
|
|
2546
|
+
if (parsedType !== ZodParsedType.date) {
|
|
2547
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
2548
|
+
addIssueToContext(ctx2, {
|
|
2549
|
+
code: ZodIssueCode.invalid_type,
|
|
2550
|
+
expected: ZodParsedType.date,
|
|
2551
|
+
received: ctx2.parsedType
|
|
2552
|
+
});
|
|
2553
|
+
return INVALID;
|
|
2554
|
+
}
|
|
2555
|
+
if (Number.isNaN(input.data.getTime())) {
|
|
2556
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
2557
|
+
addIssueToContext(ctx2, {
|
|
2558
|
+
code: ZodIssueCode.invalid_date
|
|
2559
|
+
});
|
|
2560
|
+
return INVALID;
|
|
2561
|
+
}
|
|
2562
|
+
const status = new ParseStatus();
|
|
2563
|
+
let ctx = void 0;
|
|
2564
|
+
for (const check of this._def.checks) {
|
|
2565
|
+
if (check.kind === "min") {
|
|
2566
|
+
if (input.data.getTime() < check.value) {
|
|
2567
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2568
|
+
addIssueToContext(ctx, {
|
|
2569
|
+
code: ZodIssueCode.too_small,
|
|
2570
|
+
message: check.message,
|
|
2571
|
+
inclusive: true,
|
|
2572
|
+
exact: false,
|
|
2573
|
+
minimum: check.value,
|
|
2574
|
+
type: "date"
|
|
2575
|
+
});
|
|
2576
|
+
status.dirty();
|
|
2577
|
+
}
|
|
2578
|
+
} else if (check.kind === "max") {
|
|
2579
|
+
if (input.data.getTime() > check.value) {
|
|
2580
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
2581
|
+
addIssueToContext(ctx, {
|
|
2582
|
+
code: ZodIssueCode.too_big,
|
|
2583
|
+
message: check.message,
|
|
2584
|
+
inclusive: true,
|
|
2585
|
+
exact: false,
|
|
2586
|
+
maximum: check.value,
|
|
2587
|
+
type: "date"
|
|
2588
|
+
});
|
|
2589
|
+
status.dirty();
|
|
2590
|
+
}
|
|
2591
|
+
} else {
|
|
2592
|
+
util.assertNever(check);
|
|
2593
|
+
}
|
|
2594
|
+
}
|
|
2595
|
+
return {
|
|
2596
|
+
status: status.value,
|
|
2597
|
+
value: new Date(input.data.getTime())
|
|
2598
|
+
};
|
|
2599
|
+
}
|
|
2600
|
+
_addCheck(check) {
|
|
2601
|
+
return new _ZodDate({
|
|
2602
|
+
...this._def,
|
|
2603
|
+
checks: [...this._def.checks, check]
|
|
2604
|
+
});
|
|
2605
|
+
}
|
|
2606
|
+
min(minDate, message) {
|
|
2607
|
+
return this._addCheck({
|
|
2608
|
+
kind: "min",
|
|
2609
|
+
value: minDate.getTime(),
|
|
2610
|
+
message: errorUtil.toString(message)
|
|
2611
|
+
});
|
|
2612
|
+
}
|
|
2613
|
+
max(maxDate, message) {
|
|
2614
|
+
return this._addCheck({
|
|
2615
|
+
kind: "max",
|
|
2616
|
+
value: maxDate.getTime(),
|
|
2617
|
+
message: errorUtil.toString(message)
|
|
2618
|
+
});
|
|
2619
|
+
}
|
|
2620
|
+
get minDate() {
|
|
2621
|
+
let min = null;
|
|
2622
|
+
for (const ch of this._def.checks) {
|
|
2623
|
+
if (ch.kind === "min") {
|
|
2624
|
+
if (min === null || ch.value > min)
|
|
2625
|
+
min = ch.value;
|
|
2626
|
+
}
|
|
2627
|
+
}
|
|
2628
|
+
return min != null ? new Date(min) : null;
|
|
2629
|
+
}
|
|
2630
|
+
get maxDate() {
|
|
2631
|
+
let max = null;
|
|
2632
|
+
for (const ch of this._def.checks) {
|
|
2633
|
+
if (ch.kind === "max") {
|
|
2634
|
+
if (max === null || ch.value < max)
|
|
2635
|
+
max = ch.value;
|
|
2636
|
+
}
|
|
2637
|
+
}
|
|
2638
|
+
return max != null ? new Date(max) : null;
|
|
2639
|
+
}
|
|
2640
|
+
};
|
|
2641
|
+
ZodDate.create = (params) => {
|
|
2642
|
+
return new ZodDate({
|
|
2643
|
+
checks: [],
|
|
2644
|
+
coerce: params?.coerce || false,
|
|
2645
|
+
typeName: ZodFirstPartyTypeKind.ZodDate,
|
|
2646
|
+
...processCreateParams(params)
|
|
2647
|
+
});
|
|
2648
|
+
};
|
|
2649
|
+
ZodSymbol = class extends ZodType {
|
|
2650
|
+
_parse(input) {
|
|
2651
|
+
const parsedType = this._getType(input);
|
|
2652
|
+
if (parsedType !== ZodParsedType.symbol) {
|
|
2653
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2654
|
+
addIssueToContext(ctx, {
|
|
2655
|
+
code: ZodIssueCode.invalid_type,
|
|
2656
|
+
expected: ZodParsedType.symbol,
|
|
2657
|
+
received: ctx.parsedType
|
|
2658
|
+
});
|
|
2659
|
+
return INVALID;
|
|
2660
|
+
}
|
|
2661
|
+
return OK(input.data);
|
|
2662
|
+
}
|
|
2663
|
+
};
|
|
2664
|
+
ZodSymbol.create = (params) => {
|
|
2665
|
+
return new ZodSymbol({
|
|
2666
|
+
typeName: ZodFirstPartyTypeKind.ZodSymbol,
|
|
2667
|
+
...processCreateParams(params)
|
|
2668
|
+
});
|
|
2669
|
+
};
|
|
2670
|
+
ZodUndefined = class extends ZodType {
|
|
2671
|
+
_parse(input) {
|
|
2672
|
+
const parsedType = this._getType(input);
|
|
2673
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
2674
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2675
|
+
addIssueToContext(ctx, {
|
|
2676
|
+
code: ZodIssueCode.invalid_type,
|
|
2677
|
+
expected: ZodParsedType.undefined,
|
|
2678
|
+
received: ctx.parsedType
|
|
2679
|
+
});
|
|
2680
|
+
return INVALID;
|
|
2681
|
+
}
|
|
2682
|
+
return OK(input.data);
|
|
2683
|
+
}
|
|
2684
|
+
};
|
|
2685
|
+
ZodUndefined.create = (params) => {
|
|
2686
|
+
return new ZodUndefined({
|
|
2687
|
+
typeName: ZodFirstPartyTypeKind.ZodUndefined,
|
|
2688
|
+
...processCreateParams(params)
|
|
2689
|
+
});
|
|
2690
|
+
};
|
|
2691
|
+
ZodNull = class extends ZodType {
|
|
2692
|
+
_parse(input) {
|
|
2693
|
+
const parsedType = this._getType(input);
|
|
2694
|
+
if (parsedType !== ZodParsedType.null) {
|
|
2695
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2696
|
+
addIssueToContext(ctx, {
|
|
2697
|
+
code: ZodIssueCode.invalid_type,
|
|
2698
|
+
expected: ZodParsedType.null,
|
|
2699
|
+
received: ctx.parsedType
|
|
2700
|
+
});
|
|
2701
|
+
return INVALID;
|
|
2702
|
+
}
|
|
2703
|
+
return OK(input.data);
|
|
2704
|
+
}
|
|
2705
|
+
};
|
|
2706
|
+
ZodNull.create = (params) => {
|
|
2707
|
+
return new ZodNull({
|
|
2708
|
+
typeName: ZodFirstPartyTypeKind.ZodNull,
|
|
2709
|
+
...processCreateParams(params)
|
|
2710
|
+
});
|
|
2711
|
+
};
|
|
2712
|
+
ZodAny = class extends ZodType {
|
|
2713
|
+
constructor() {
|
|
2714
|
+
super(...arguments);
|
|
2715
|
+
this._any = true;
|
|
2716
|
+
}
|
|
2717
|
+
_parse(input) {
|
|
2718
|
+
return OK(input.data);
|
|
2719
|
+
}
|
|
2720
|
+
};
|
|
2721
|
+
ZodAny.create = (params) => {
|
|
2722
|
+
return new ZodAny({
|
|
2723
|
+
typeName: ZodFirstPartyTypeKind.ZodAny,
|
|
2724
|
+
...processCreateParams(params)
|
|
2725
|
+
});
|
|
2726
|
+
};
|
|
2727
|
+
ZodUnknown = class extends ZodType {
|
|
2728
|
+
constructor() {
|
|
2729
|
+
super(...arguments);
|
|
2730
|
+
this._unknown = true;
|
|
2731
|
+
}
|
|
2732
|
+
_parse(input) {
|
|
2733
|
+
return OK(input.data);
|
|
2734
|
+
}
|
|
2735
|
+
};
|
|
2736
|
+
ZodUnknown.create = (params) => {
|
|
2737
|
+
return new ZodUnknown({
|
|
2738
|
+
typeName: ZodFirstPartyTypeKind.ZodUnknown,
|
|
2739
|
+
...processCreateParams(params)
|
|
2740
|
+
});
|
|
2741
|
+
};
|
|
2742
|
+
ZodNever = class extends ZodType {
|
|
2743
|
+
_parse(input) {
|
|
2744
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2745
|
+
addIssueToContext(ctx, {
|
|
2746
|
+
code: ZodIssueCode.invalid_type,
|
|
2747
|
+
expected: ZodParsedType.never,
|
|
2748
|
+
received: ctx.parsedType
|
|
2749
|
+
});
|
|
2750
|
+
return INVALID;
|
|
2751
|
+
}
|
|
2752
|
+
};
|
|
2753
|
+
ZodNever.create = (params) => {
|
|
2754
|
+
return new ZodNever({
|
|
2755
|
+
typeName: ZodFirstPartyTypeKind.ZodNever,
|
|
2756
|
+
...processCreateParams(params)
|
|
2757
|
+
});
|
|
2758
|
+
};
|
|
2759
|
+
ZodVoid = class extends ZodType {
|
|
2760
|
+
_parse(input) {
|
|
2761
|
+
const parsedType = this._getType(input);
|
|
2762
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
2763
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2764
|
+
addIssueToContext(ctx, {
|
|
2765
|
+
code: ZodIssueCode.invalid_type,
|
|
2766
|
+
expected: ZodParsedType.void,
|
|
2767
|
+
received: ctx.parsedType
|
|
2768
|
+
});
|
|
2769
|
+
return INVALID;
|
|
2770
|
+
}
|
|
2771
|
+
return OK(input.data);
|
|
2772
|
+
}
|
|
2773
|
+
};
|
|
2774
|
+
ZodVoid.create = (params) => {
|
|
2775
|
+
return new ZodVoid({
|
|
2776
|
+
typeName: ZodFirstPartyTypeKind.ZodVoid,
|
|
2777
|
+
...processCreateParams(params)
|
|
2778
|
+
});
|
|
2779
|
+
};
|
|
2780
|
+
ZodArray = class _ZodArray extends ZodType {
|
|
2781
|
+
_parse(input) {
|
|
2782
|
+
const { ctx, status } = this._processInputParams(input);
|
|
2783
|
+
const def = this._def;
|
|
2784
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
2785
|
+
addIssueToContext(ctx, {
|
|
2786
|
+
code: ZodIssueCode.invalid_type,
|
|
2787
|
+
expected: ZodParsedType.array,
|
|
2788
|
+
received: ctx.parsedType
|
|
2789
|
+
});
|
|
2790
|
+
return INVALID;
|
|
2791
|
+
}
|
|
2792
|
+
if (def.exactLength !== null) {
|
|
2793
|
+
const tooBig = ctx.data.length > def.exactLength.value;
|
|
2794
|
+
const tooSmall = ctx.data.length < def.exactLength.value;
|
|
2795
|
+
if (tooBig || tooSmall) {
|
|
2796
|
+
addIssueToContext(ctx, {
|
|
2797
|
+
code: tooBig ? ZodIssueCode.too_big : ZodIssueCode.too_small,
|
|
2798
|
+
minimum: tooSmall ? def.exactLength.value : void 0,
|
|
2799
|
+
maximum: tooBig ? def.exactLength.value : void 0,
|
|
2800
|
+
type: "array",
|
|
2801
|
+
inclusive: true,
|
|
2802
|
+
exact: true,
|
|
2803
|
+
message: def.exactLength.message
|
|
2804
|
+
});
|
|
2805
|
+
status.dirty();
|
|
2806
|
+
}
|
|
2807
|
+
}
|
|
2808
|
+
if (def.minLength !== null) {
|
|
2809
|
+
if (ctx.data.length < def.minLength.value) {
|
|
2810
|
+
addIssueToContext(ctx, {
|
|
2811
|
+
code: ZodIssueCode.too_small,
|
|
2812
|
+
minimum: def.minLength.value,
|
|
2813
|
+
type: "array",
|
|
2814
|
+
inclusive: true,
|
|
2815
|
+
exact: false,
|
|
2816
|
+
message: def.minLength.message
|
|
2817
|
+
});
|
|
2818
|
+
status.dirty();
|
|
2819
|
+
}
|
|
2820
|
+
}
|
|
2821
|
+
if (def.maxLength !== null) {
|
|
2822
|
+
if (ctx.data.length > def.maxLength.value) {
|
|
2823
|
+
addIssueToContext(ctx, {
|
|
2824
|
+
code: ZodIssueCode.too_big,
|
|
2825
|
+
maximum: def.maxLength.value,
|
|
2826
|
+
type: "array",
|
|
2827
|
+
inclusive: true,
|
|
2828
|
+
exact: false,
|
|
2829
|
+
message: def.maxLength.message
|
|
2830
|
+
});
|
|
2831
|
+
status.dirty();
|
|
2832
|
+
}
|
|
2833
|
+
}
|
|
2834
|
+
if (ctx.common.async) {
|
|
2835
|
+
return Promise.all([...ctx.data].map((item, i) => {
|
|
2836
|
+
return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
2837
|
+
})).then((result2) => {
|
|
2838
|
+
return ParseStatus.mergeArray(status, result2);
|
|
2839
|
+
});
|
|
2840
|
+
}
|
|
2841
|
+
const result = [...ctx.data].map((item, i) => {
|
|
2842
|
+
return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
2843
|
+
});
|
|
2844
|
+
return ParseStatus.mergeArray(status, result);
|
|
2845
|
+
}
|
|
2846
|
+
get element() {
|
|
2847
|
+
return this._def.type;
|
|
2848
|
+
}
|
|
2849
|
+
min(minLength, message) {
|
|
2850
|
+
return new _ZodArray({
|
|
2851
|
+
...this._def,
|
|
2852
|
+
minLength: { value: minLength, message: errorUtil.toString(message) }
|
|
2853
|
+
});
|
|
2854
|
+
}
|
|
2855
|
+
max(maxLength, message) {
|
|
2856
|
+
return new _ZodArray({
|
|
2857
|
+
...this._def,
|
|
2858
|
+
maxLength: { value: maxLength, message: errorUtil.toString(message) }
|
|
2859
|
+
});
|
|
2860
|
+
}
|
|
2861
|
+
length(len, message) {
|
|
2862
|
+
return new _ZodArray({
|
|
2863
|
+
...this._def,
|
|
2864
|
+
exactLength: { value: len, message: errorUtil.toString(message) }
|
|
2865
|
+
});
|
|
2866
|
+
}
|
|
2867
|
+
nonempty(message) {
|
|
2868
|
+
return this.min(1, message);
|
|
2869
|
+
}
|
|
2870
|
+
};
|
|
2871
|
+
ZodArray.create = (schema, params) => {
|
|
2872
|
+
return new ZodArray({
|
|
2873
|
+
type: schema,
|
|
2874
|
+
minLength: null,
|
|
2875
|
+
maxLength: null,
|
|
2876
|
+
exactLength: null,
|
|
2877
|
+
typeName: ZodFirstPartyTypeKind.ZodArray,
|
|
2878
|
+
...processCreateParams(params)
|
|
2879
|
+
});
|
|
2880
|
+
};
|
|
2881
|
+
ZodObject = class _ZodObject extends ZodType {
|
|
2882
|
+
constructor() {
|
|
2883
|
+
super(...arguments);
|
|
2884
|
+
this._cached = null;
|
|
2885
|
+
this.nonstrict = this.passthrough;
|
|
2886
|
+
this.augment = this.extend;
|
|
2887
|
+
}
|
|
2888
|
+
_getCached() {
|
|
2889
|
+
if (this._cached !== null)
|
|
2890
|
+
return this._cached;
|
|
2891
|
+
const shape = this._def.shape();
|
|
2892
|
+
const keys = util.objectKeys(shape);
|
|
2893
|
+
this._cached = { shape, keys };
|
|
2894
|
+
return this._cached;
|
|
2895
|
+
}
|
|
2896
|
+
_parse(input) {
|
|
2897
|
+
const parsedType = this._getType(input);
|
|
2898
|
+
if (parsedType !== ZodParsedType.object) {
|
|
2899
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
2900
|
+
addIssueToContext(ctx2, {
|
|
2901
|
+
code: ZodIssueCode.invalid_type,
|
|
2902
|
+
expected: ZodParsedType.object,
|
|
2903
|
+
received: ctx2.parsedType
|
|
2904
|
+
});
|
|
2905
|
+
return INVALID;
|
|
2906
|
+
}
|
|
2907
|
+
const { status, ctx } = this._processInputParams(input);
|
|
2908
|
+
const { shape, keys: shapeKeys } = this._getCached();
|
|
2909
|
+
const extraKeys = [];
|
|
2910
|
+
if (!(this._def.catchall instanceof ZodNever && this._def.unknownKeys === "strip")) {
|
|
2911
|
+
for (const key in ctx.data) {
|
|
2912
|
+
if (!shapeKeys.includes(key)) {
|
|
2913
|
+
extraKeys.push(key);
|
|
2914
|
+
}
|
|
2915
|
+
}
|
|
2916
|
+
}
|
|
2917
|
+
const pairs = [];
|
|
2918
|
+
for (const key of shapeKeys) {
|
|
2919
|
+
const keyValidator = shape[key];
|
|
2920
|
+
const value = ctx.data[key];
|
|
2921
|
+
pairs.push({
|
|
2922
|
+
key: { status: "valid", value: key },
|
|
2923
|
+
value: keyValidator._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
|
|
2924
|
+
alwaysSet: key in ctx.data
|
|
2925
|
+
});
|
|
2926
|
+
}
|
|
2927
|
+
if (this._def.catchall instanceof ZodNever) {
|
|
2928
|
+
const unknownKeys = this._def.unknownKeys;
|
|
2929
|
+
if (unknownKeys === "passthrough") {
|
|
2930
|
+
for (const key of extraKeys) {
|
|
2931
|
+
pairs.push({
|
|
2932
|
+
key: { status: "valid", value: key },
|
|
2933
|
+
value: { status: "valid", value: ctx.data[key] }
|
|
2934
|
+
});
|
|
2935
|
+
}
|
|
2936
|
+
} else if (unknownKeys === "strict") {
|
|
2937
|
+
if (extraKeys.length > 0) {
|
|
2938
|
+
addIssueToContext(ctx, {
|
|
2939
|
+
code: ZodIssueCode.unrecognized_keys,
|
|
2940
|
+
keys: extraKeys
|
|
2941
|
+
});
|
|
2942
|
+
status.dirty();
|
|
2943
|
+
}
|
|
2944
|
+
} else if (unknownKeys === "strip") {
|
|
2945
|
+
} else {
|
|
2946
|
+
throw new Error(`Internal ZodObject error: invalid unknownKeys value.`);
|
|
2947
|
+
}
|
|
2948
|
+
} else {
|
|
2949
|
+
const catchall = this._def.catchall;
|
|
2950
|
+
for (const key of extraKeys) {
|
|
2951
|
+
const value = ctx.data[key];
|
|
2952
|
+
pairs.push({
|
|
2953
|
+
key: { status: "valid", value: key },
|
|
2954
|
+
value: catchall._parse(
|
|
2955
|
+
new ParseInputLazyPath(ctx, value, ctx.path, key)
|
|
2956
|
+
//, ctx.child(key), value, getParsedType(value)
|
|
2957
|
+
),
|
|
2958
|
+
alwaysSet: key in ctx.data
|
|
2959
|
+
});
|
|
2960
|
+
}
|
|
2961
|
+
}
|
|
2962
|
+
if (ctx.common.async) {
|
|
2963
|
+
return Promise.resolve().then(async () => {
|
|
2964
|
+
const syncPairs = [];
|
|
2965
|
+
for (const pair of pairs) {
|
|
2966
|
+
const key = await pair.key;
|
|
2967
|
+
const value = await pair.value;
|
|
2968
|
+
syncPairs.push({
|
|
2969
|
+
key,
|
|
2970
|
+
value,
|
|
2971
|
+
alwaysSet: pair.alwaysSet
|
|
2972
|
+
});
|
|
2973
|
+
}
|
|
2974
|
+
return syncPairs;
|
|
2975
|
+
}).then((syncPairs) => {
|
|
2976
|
+
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
2977
|
+
});
|
|
2978
|
+
} else {
|
|
2979
|
+
return ParseStatus.mergeObjectSync(status, pairs);
|
|
2980
|
+
}
|
|
2981
|
+
}
|
|
2982
|
+
get shape() {
|
|
2983
|
+
return this._def.shape();
|
|
2984
|
+
}
|
|
2985
|
+
strict(message) {
|
|
2986
|
+
errorUtil.errToObj;
|
|
2987
|
+
return new _ZodObject({
|
|
2988
|
+
...this._def,
|
|
2989
|
+
unknownKeys: "strict",
|
|
2990
|
+
...message !== void 0 ? {
|
|
2991
|
+
errorMap: (issue, ctx) => {
|
|
2992
|
+
const defaultError = this._def.errorMap?.(issue, ctx).message ?? ctx.defaultError;
|
|
2993
|
+
if (issue.code === "unrecognized_keys")
|
|
2994
|
+
return {
|
|
2995
|
+
message: errorUtil.errToObj(message).message ?? defaultError
|
|
2996
|
+
};
|
|
2997
|
+
return {
|
|
2998
|
+
message: defaultError
|
|
2999
|
+
};
|
|
3000
|
+
}
|
|
3001
|
+
} : {}
|
|
3002
|
+
});
|
|
3003
|
+
}
|
|
3004
|
+
strip() {
|
|
3005
|
+
return new _ZodObject({
|
|
3006
|
+
...this._def,
|
|
3007
|
+
unknownKeys: "strip"
|
|
3008
|
+
});
|
|
3009
|
+
}
|
|
3010
|
+
passthrough() {
|
|
3011
|
+
return new _ZodObject({
|
|
3012
|
+
...this._def,
|
|
3013
|
+
unknownKeys: "passthrough"
|
|
3014
|
+
});
|
|
3015
|
+
}
|
|
3016
|
+
// const AugmentFactory =
|
|
3017
|
+
// <Def extends ZodObjectDef>(def: Def) =>
|
|
3018
|
+
// <Augmentation extends ZodRawShape>(
|
|
3019
|
+
// augmentation: Augmentation
|
|
3020
|
+
// ): ZodObject<
|
|
3021
|
+
// extendShape<ReturnType<Def["shape"]>, Augmentation>,
|
|
3022
|
+
// Def["unknownKeys"],
|
|
3023
|
+
// Def["catchall"]
|
|
3024
|
+
// > => {
|
|
3025
|
+
// return new ZodObject({
|
|
3026
|
+
// ...def,
|
|
3027
|
+
// shape: () => ({
|
|
3028
|
+
// ...def.shape(),
|
|
3029
|
+
// ...augmentation,
|
|
3030
|
+
// }),
|
|
3031
|
+
// }) as any;
|
|
3032
|
+
// };
|
|
3033
|
+
extend(augmentation) {
|
|
3034
|
+
return new _ZodObject({
|
|
3035
|
+
...this._def,
|
|
3036
|
+
shape: () => ({
|
|
3037
|
+
...this._def.shape(),
|
|
3038
|
+
...augmentation
|
|
3039
|
+
})
|
|
3040
|
+
});
|
|
3041
|
+
}
|
|
3042
|
+
/**
|
|
3043
|
+
* Prior to zod@1.0.12 there was a bug in the
|
|
3044
|
+
* inferred type of merged objects. Please
|
|
3045
|
+
* upgrade if you are experiencing issues.
|
|
3046
|
+
*/
|
|
3047
|
+
merge(merging) {
|
|
3048
|
+
const merged = new _ZodObject({
|
|
3049
|
+
unknownKeys: merging._def.unknownKeys,
|
|
3050
|
+
catchall: merging._def.catchall,
|
|
3051
|
+
shape: () => ({
|
|
3052
|
+
...this._def.shape(),
|
|
3053
|
+
...merging._def.shape()
|
|
3054
|
+
}),
|
|
3055
|
+
typeName: ZodFirstPartyTypeKind.ZodObject
|
|
3056
|
+
});
|
|
3057
|
+
return merged;
|
|
3058
|
+
}
|
|
3059
|
+
// merge<
|
|
3060
|
+
// Incoming extends AnyZodObject,
|
|
3061
|
+
// Augmentation extends Incoming["shape"],
|
|
3062
|
+
// NewOutput extends {
|
|
3063
|
+
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
|
|
3064
|
+
// ? Augmentation[k]["_output"]
|
|
3065
|
+
// : k extends keyof Output
|
|
3066
|
+
// ? Output[k]
|
|
3067
|
+
// : never;
|
|
3068
|
+
// },
|
|
3069
|
+
// NewInput extends {
|
|
3070
|
+
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
|
|
3071
|
+
// ? Augmentation[k]["_input"]
|
|
3072
|
+
// : k extends keyof Input
|
|
3073
|
+
// ? Input[k]
|
|
3074
|
+
// : never;
|
|
3075
|
+
// }
|
|
3076
|
+
// >(
|
|
3077
|
+
// merging: Incoming
|
|
3078
|
+
// ): ZodObject<
|
|
3079
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
3080
|
+
// Incoming["_def"]["unknownKeys"],
|
|
3081
|
+
// Incoming["_def"]["catchall"],
|
|
3082
|
+
// NewOutput,
|
|
3083
|
+
// NewInput
|
|
3084
|
+
// > {
|
|
3085
|
+
// const merged: any = new ZodObject({
|
|
3086
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
3087
|
+
// catchall: merging._def.catchall,
|
|
3088
|
+
// shape: () =>
|
|
3089
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
3090
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3091
|
+
// }) as any;
|
|
3092
|
+
// return merged;
|
|
3093
|
+
// }
|
|
3094
|
+
setKey(key, schema) {
|
|
3095
|
+
return this.augment({ [key]: schema });
|
|
3096
|
+
}
|
|
3097
|
+
// merge<Incoming extends AnyZodObject>(
|
|
3098
|
+
// merging: Incoming
|
|
3099
|
+
// ): //ZodObject<T & Incoming["_shape"], UnknownKeys, Catchall> = (merging) => {
|
|
3100
|
+
// ZodObject<
|
|
3101
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
3102
|
+
// Incoming["_def"]["unknownKeys"],
|
|
3103
|
+
// Incoming["_def"]["catchall"]
|
|
3104
|
+
// > {
|
|
3105
|
+
// // const mergedShape = objectUtil.mergeShapes(
|
|
3106
|
+
// // this._def.shape(),
|
|
3107
|
+
// // merging._def.shape()
|
|
3108
|
+
// // );
|
|
3109
|
+
// const merged: any = new ZodObject({
|
|
3110
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
3111
|
+
// catchall: merging._def.catchall,
|
|
3112
|
+
// shape: () =>
|
|
3113
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
3114
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3115
|
+
// }) as any;
|
|
3116
|
+
// return merged;
|
|
3117
|
+
// }
|
|
3118
|
+
catchall(index) {
|
|
3119
|
+
return new _ZodObject({
|
|
3120
|
+
...this._def,
|
|
3121
|
+
catchall: index
|
|
3122
|
+
});
|
|
3123
|
+
}
|
|
3124
|
+
pick(mask) {
|
|
3125
|
+
const shape = {};
|
|
3126
|
+
for (const key of util.objectKeys(mask)) {
|
|
3127
|
+
if (mask[key] && this.shape[key]) {
|
|
3128
|
+
shape[key] = this.shape[key];
|
|
3129
|
+
}
|
|
3130
|
+
}
|
|
3131
|
+
return new _ZodObject({
|
|
3132
|
+
...this._def,
|
|
3133
|
+
shape: () => shape
|
|
3134
|
+
});
|
|
3135
|
+
}
|
|
3136
|
+
omit(mask) {
|
|
3137
|
+
const shape = {};
|
|
3138
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
3139
|
+
if (!mask[key]) {
|
|
3140
|
+
shape[key] = this.shape[key];
|
|
3141
|
+
}
|
|
3142
|
+
}
|
|
3143
|
+
return new _ZodObject({
|
|
3144
|
+
...this._def,
|
|
3145
|
+
shape: () => shape
|
|
3146
|
+
});
|
|
3147
|
+
}
|
|
3148
|
+
/**
|
|
3149
|
+
* @deprecated
|
|
3150
|
+
*/
|
|
3151
|
+
deepPartial() {
|
|
3152
|
+
return deepPartialify(this);
|
|
3153
|
+
}
|
|
3154
|
+
partial(mask) {
|
|
3155
|
+
const newShape = {};
|
|
3156
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
3157
|
+
const fieldSchema = this.shape[key];
|
|
3158
|
+
if (mask && !mask[key]) {
|
|
3159
|
+
newShape[key] = fieldSchema;
|
|
3160
|
+
} else {
|
|
3161
|
+
newShape[key] = fieldSchema.optional();
|
|
3162
|
+
}
|
|
3163
|
+
}
|
|
3164
|
+
return new _ZodObject({
|
|
3165
|
+
...this._def,
|
|
3166
|
+
shape: () => newShape
|
|
3167
|
+
});
|
|
3168
|
+
}
|
|
3169
|
+
required(mask) {
|
|
3170
|
+
const newShape = {};
|
|
3171
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
3172
|
+
if (mask && !mask[key]) {
|
|
3173
|
+
newShape[key] = this.shape[key];
|
|
3174
|
+
} else {
|
|
3175
|
+
const fieldSchema = this.shape[key];
|
|
3176
|
+
let newField = fieldSchema;
|
|
3177
|
+
while (newField instanceof ZodOptional) {
|
|
3178
|
+
newField = newField._def.innerType;
|
|
3179
|
+
}
|
|
3180
|
+
newShape[key] = newField;
|
|
3181
|
+
}
|
|
3182
|
+
}
|
|
3183
|
+
return new _ZodObject({
|
|
3184
|
+
...this._def,
|
|
3185
|
+
shape: () => newShape
|
|
3186
|
+
});
|
|
3187
|
+
}
|
|
3188
|
+
keyof() {
|
|
3189
|
+
return createZodEnum(util.objectKeys(this.shape));
|
|
3190
|
+
}
|
|
3191
|
+
};
|
|
3192
|
+
ZodObject.create = (shape, params) => {
|
|
3193
|
+
return new ZodObject({
|
|
3194
|
+
shape: () => shape,
|
|
3195
|
+
unknownKeys: "strip",
|
|
3196
|
+
catchall: ZodNever.create(),
|
|
3197
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3198
|
+
...processCreateParams(params)
|
|
3199
|
+
});
|
|
3200
|
+
};
|
|
3201
|
+
ZodObject.strictCreate = (shape, params) => {
|
|
3202
|
+
return new ZodObject({
|
|
3203
|
+
shape: () => shape,
|
|
3204
|
+
unknownKeys: "strict",
|
|
3205
|
+
catchall: ZodNever.create(),
|
|
3206
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3207
|
+
...processCreateParams(params)
|
|
3208
|
+
});
|
|
3209
|
+
};
|
|
3210
|
+
ZodObject.lazycreate = (shape, params) => {
|
|
3211
|
+
return new ZodObject({
|
|
3212
|
+
shape,
|
|
3213
|
+
unknownKeys: "strip",
|
|
3214
|
+
catchall: ZodNever.create(),
|
|
3215
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
3216
|
+
...processCreateParams(params)
|
|
3217
|
+
});
|
|
3218
|
+
};
|
|
3219
|
+
ZodUnion = class extends ZodType {
|
|
3220
|
+
_parse(input) {
|
|
3221
|
+
const { ctx } = this._processInputParams(input);
|
|
3222
|
+
const options = this._def.options;
|
|
3223
|
+
function handleResults(results) {
|
|
3224
|
+
for (const result of results) {
|
|
3225
|
+
if (result.result.status === "valid") {
|
|
3226
|
+
return result.result;
|
|
3227
|
+
}
|
|
3228
|
+
}
|
|
3229
|
+
for (const result of results) {
|
|
3230
|
+
if (result.result.status === "dirty") {
|
|
3231
|
+
ctx.common.issues.push(...result.ctx.common.issues);
|
|
3232
|
+
return result.result;
|
|
3233
|
+
}
|
|
3234
|
+
}
|
|
3235
|
+
const unionErrors = results.map((result) => new ZodError(result.ctx.common.issues));
|
|
3236
|
+
addIssueToContext(ctx, {
|
|
3237
|
+
code: ZodIssueCode.invalid_union,
|
|
3238
|
+
unionErrors
|
|
3239
|
+
});
|
|
3240
|
+
return INVALID;
|
|
3241
|
+
}
|
|
3242
|
+
if (ctx.common.async) {
|
|
3243
|
+
return Promise.all(options.map(async (option) => {
|
|
3244
|
+
const childCtx = {
|
|
3245
|
+
...ctx,
|
|
3246
|
+
common: {
|
|
3247
|
+
...ctx.common,
|
|
3248
|
+
issues: []
|
|
3249
|
+
},
|
|
3250
|
+
parent: null
|
|
3251
|
+
};
|
|
3252
|
+
return {
|
|
3253
|
+
result: await option._parseAsync({
|
|
3254
|
+
data: ctx.data,
|
|
3255
|
+
path: ctx.path,
|
|
3256
|
+
parent: childCtx
|
|
3257
|
+
}),
|
|
3258
|
+
ctx: childCtx
|
|
3259
|
+
};
|
|
3260
|
+
})).then(handleResults);
|
|
3261
|
+
} else {
|
|
3262
|
+
let dirty = void 0;
|
|
3263
|
+
const issues = [];
|
|
3264
|
+
for (const option of options) {
|
|
3265
|
+
const childCtx = {
|
|
3266
|
+
...ctx,
|
|
3267
|
+
common: {
|
|
3268
|
+
...ctx.common,
|
|
3269
|
+
issues: []
|
|
3270
|
+
},
|
|
3271
|
+
parent: null
|
|
3272
|
+
};
|
|
3273
|
+
const result = option._parseSync({
|
|
3274
|
+
data: ctx.data,
|
|
3275
|
+
path: ctx.path,
|
|
3276
|
+
parent: childCtx
|
|
3277
|
+
});
|
|
3278
|
+
if (result.status === "valid") {
|
|
3279
|
+
return result;
|
|
3280
|
+
} else if (result.status === "dirty" && !dirty) {
|
|
3281
|
+
dirty = { result, ctx: childCtx };
|
|
3282
|
+
}
|
|
3283
|
+
if (childCtx.common.issues.length) {
|
|
3284
|
+
issues.push(childCtx.common.issues);
|
|
3285
|
+
}
|
|
3286
|
+
}
|
|
3287
|
+
if (dirty) {
|
|
3288
|
+
ctx.common.issues.push(...dirty.ctx.common.issues);
|
|
3289
|
+
return dirty.result;
|
|
3290
|
+
}
|
|
3291
|
+
const unionErrors = issues.map((issues2) => new ZodError(issues2));
|
|
3292
|
+
addIssueToContext(ctx, {
|
|
3293
|
+
code: ZodIssueCode.invalid_union,
|
|
3294
|
+
unionErrors
|
|
3295
|
+
});
|
|
3296
|
+
return INVALID;
|
|
3297
|
+
}
|
|
3298
|
+
}
|
|
3299
|
+
get options() {
|
|
3300
|
+
return this._def.options;
|
|
3301
|
+
}
|
|
3302
|
+
};
|
|
3303
|
+
ZodUnion.create = (types, params) => {
|
|
3304
|
+
return new ZodUnion({
|
|
3305
|
+
options: types,
|
|
3306
|
+
typeName: ZodFirstPartyTypeKind.ZodUnion,
|
|
3307
|
+
...processCreateParams(params)
|
|
3308
|
+
});
|
|
3309
|
+
};
|
|
3310
|
+
getDiscriminator = (type) => {
|
|
3311
|
+
if (type instanceof ZodLazy) {
|
|
3312
|
+
return getDiscriminator(type.schema);
|
|
3313
|
+
} else if (type instanceof ZodEffects) {
|
|
3314
|
+
return getDiscriminator(type.innerType());
|
|
3315
|
+
} else if (type instanceof ZodLiteral) {
|
|
3316
|
+
return [type.value];
|
|
3317
|
+
} else if (type instanceof ZodEnum) {
|
|
3318
|
+
return type.options;
|
|
3319
|
+
} else if (type instanceof ZodNativeEnum) {
|
|
3320
|
+
return util.objectValues(type.enum);
|
|
3321
|
+
} else if (type instanceof ZodDefault) {
|
|
3322
|
+
return getDiscriminator(type._def.innerType);
|
|
3323
|
+
} else if (type instanceof ZodUndefined) {
|
|
3324
|
+
return [void 0];
|
|
3325
|
+
} else if (type instanceof ZodNull) {
|
|
3326
|
+
return [null];
|
|
3327
|
+
} else if (type instanceof ZodOptional) {
|
|
3328
|
+
return [void 0, ...getDiscriminator(type.unwrap())];
|
|
3329
|
+
} else if (type instanceof ZodNullable) {
|
|
3330
|
+
return [null, ...getDiscriminator(type.unwrap())];
|
|
3331
|
+
} else if (type instanceof ZodBranded) {
|
|
3332
|
+
return getDiscriminator(type.unwrap());
|
|
3333
|
+
} else if (type instanceof ZodReadonly) {
|
|
3334
|
+
return getDiscriminator(type.unwrap());
|
|
3335
|
+
} else if (type instanceof ZodCatch) {
|
|
3336
|
+
return getDiscriminator(type._def.innerType);
|
|
3337
|
+
} else {
|
|
3338
|
+
return [];
|
|
3339
|
+
}
|
|
3340
|
+
};
|
|
3341
|
+
ZodDiscriminatedUnion = class _ZodDiscriminatedUnion extends ZodType {
|
|
3342
|
+
_parse(input) {
|
|
3343
|
+
const { ctx } = this._processInputParams(input);
|
|
3344
|
+
if (ctx.parsedType !== ZodParsedType.object) {
|
|
3345
|
+
addIssueToContext(ctx, {
|
|
3346
|
+
code: ZodIssueCode.invalid_type,
|
|
3347
|
+
expected: ZodParsedType.object,
|
|
3348
|
+
received: ctx.parsedType
|
|
3349
|
+
});
|
|
3350
|
+
return INVALID;
|
|
3351
|
+
}
|
|
3352
|
+
const discriminator = this.discriminator;
|
|
3353
|
+
const discriminatorValue = ctx.data[discriminator];
|
|
3354
|
+
const option = this.optionsMap.get(discriminatorValue);
|
|
3355
|
+
if (!option) {
|
|
3356
|
+
addIssueToContext(ctx, {
|
|
3357
|
+
code: ZodIssueCode.invalid_union_discriminator,
|
|
3358
|
+
options: Array.from(this.optionsMap.keys()),
|
|
3359
|
+
path: [discriminator]
|
|
3360
|
+
});
|
|
3361
|
+
return INVALID;
|
|
3362
|
+
}
|
|
3363
|
+
if (ctx.common.async) {
|
|
3364
|
+
return option._parseAsync({
|
|
3365
|
+
data: ctx.data,
|
|
3366
|
+
path: ctx.path,
|
|
3367
|
+
parent: ctx
|
|
3368
|
+
});
|
|
3369
|
+
} else {
|
|
3370
|
+
return option._parseSync({
|
|
3371
|
+
data: ctx.data,
|
|
3372
|
+
path: ctx.path,
|
|
3373
|
+
parent: ctx
|
|
3374
|
+
});
|
|
3375
|
+
}
|
|
3376
|
+
}
|
|
3377
|
+
get discriminator() {
|
|
3378
|
+
return this._def.discriminator;
|
|
3379
|
+
}
|
|
3380
|
+
get options() {
|
|
3381
|
+
return this._def.options;
|
|
3382
|
+
}
|
|
3383
|
+
get optionsMap() {
|
|
3384
|
+
return this._def.optionsMap;
|
|
3385
|
+
}
|
|
3386
|
+
/**
|
|
3387
|
+
* The constructor of the discriminated union schema. Its behaviour is very similar to that of the normal z.union() constructor.
|
|
3388
|
+
* However, it only allows a union of objects, all of which need to share a discriminator property. This property must
|
|
3389
|
+
* have a different value for each object in the union.
|
|
3390
|
+
* @param discriminator the name of the discriminator property
|
|
3391
|
+
* @param types an array of object schemas
|
|
3392
|
+
* @param params
|
|
3393
|
+
*/
|
|
3394
|
+
static create(discriminator, options, params) {
|
|
3395
|
+
const optionsMap = /* @__PURE__ */ new Map();
|
|
3396
|
+
for (const type of options) {
|
|
3397
|
+
const discriminatorValues = getDiscriminator(type.shape[discriminator]);
|
|
3398
|
+
if (!discriminatorValues.length) {
|
|
3399
|
+
throw new Error(`A discriminator value for key \`${discriminator}\` could not be extracted from all schema options`);
|
|
3400
|
+
}
|
|
3401
|
+
for (const value of discriminatorValues) {
|
|
3402
|
+
if (optionsMap.has(value)) {
|
|
3403
|
+
throw new Error(`Discriminator property ${String(discriminator)} has duplicate value ${String(value)}`);
|
|
3404
|
+
}
|
|
3405
|
+
optionsMap.set(value, type);
|
|
3406
|
+
}
|
|
3407
|
+
}
|
|
3408
|
+
return new _ZodDiscriminatedUnion({
|
|
3409
|
+
typeName: ZodFirstPartyTypeKind.ZodDiscriminatedUnion,
|
|
3410
|
+
discriminator,
|
|
3411
|
+
options,
|
|
3412
|
+
optionsMap,
|
|
3413
|
+
...processCreateParams(params)
|
|
3414
|
+
});
|
|
3415
|
+
}
|
|
3416
|
+
};
|
|
3417
|
+
ZodIntersection = class extends ZodType {
|
|
3418
|
+
_parse(input) {
|
|
3419
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3420
|
+
const handleParsed = (parsedLeft, parsedRight) => {
|
|
3421
|
+
if (isAborted(parsedLeft) || isAborted(parsedRight)) {
|
|
3422
|
+
return INVALID;
|
|
3423
|
+
}
|
|
3424
|
+
const merged = mergeValues(parsedLeft.value, parsedRight.value);
|
|
3425
|
+
if (!merged.valid) {
|
|
3426
|
+
addIssueToContext(ctx, {
|
|
3427
|
+
code: ZodIssueCode.invalid_intersection_types
|
|
3428
|
+
});
|
|
3429
|
+
return INVALID;
|
|
3430
|
+
}
|
|
3431
|
+
if (isDirty(parsedLeft) || isDirty(parsedRight)) {
|
|
3432
|
+
status.dirty();
|
|
3433
|
+
}
|
|
3434
|
+
return { status: status.value, value: merged.data };
|
|
3435
|
+
};
|
|
3436
|
+
if (ctx.common.async) {
|
|
3437
|
+
return Promise.all([
|
|
3438
|
+
this._def.left._parseAsync({
|
|
3439
|
+
data: ctx.data,
|
|
3440
|
+
path: ctx.path,
|
|
3441
|
+
parent: ctx
|
|
3442
|
+
}),
|
|
3443
|
+
this._def.right._parseAsync({
|
|
3444
|
+
data: ctx.data,
|
|
3445
|
+
path: ctx.path,
|
|
3446
|
+
parent: ctx
|
|
3447
|
+
})
|
|
3448
|
+
]).then(([left, right]) => handleParsed(left, right));
|
|
3449
|
+
} else {
|
|
3450
|
+
return handleParsed(this._def.left._parseSync({
|
|
3451
|
+
data: ctx.data,
|
|
3452
|
+
path: ctx.path,
|
|
3453
|
+
parent: ctx
|
|
3454
|
+
}), this._def.right._parseSync({
|
|
3455
|
+
data: ctx.data,
|
|
3456
|
+
path: ctx.path,
|
|
3457
|
+
parent: ctx
|
|
3458
|
+
}));
|
|
3459
|
+
}
|
|
3460
|
+
}
|
|
3461
|
+
};
|
|
3462
|
+
ZodIntersection.create = (left, right, params) => {
|
|
3463
|
+
return new ZodIntersection({
|
|
3464
|
+
left,
|
|
3465
|
+
right,
|
|
3466
|
+
typeName: ZodFirstPartyTypeKind.ZodIntersection,
|
|
3467
|
+
...processCreateParams(params)
|
|
3468
|
+
});
|
|
3469
|
+
};
|
|
3470
|
+
ZodTuple = class _ZodTuple extends ZodType {
|
|
3471
|
+
_parse(input) {
|
|
3472
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3473
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
3474
|
+
addIssueToContext(ctx, {
|
|
3475
|
+
code: ZodIssueCode.invalid_type,
|
|
3476
|
+
expected: ZodParsedType.array,
|
|
3477
|
+
received: ctx.parsedType
|
|
3478
|
+
});
|
|
3479
|
+
return INVALID;
|
|
3480
|
+
}
|
|
3481
|
+
if (ctx.data.length < this._def.items.length) {
|
|
3482
|
+
addIssueToContext(ctx, {
|
|
3483
|
+
code: ZodIssueCode.too_small,
|
|
3484
|
+
minimum: this._def.items.length,
|
|
3485
|
+
inclusive: true,
|
|
3486
|
+
exact: false,
|
|
3487
|
+
type: "array"
|
|
3488
|
+
});
|
|
3489
|
+
return INVALID;
|
|
3490
|
+
}
|
|
3491
|
+
const rest = this._def.rest;
|
|
3492
|
+
if (!rest && ctx.data.length > this._def.items.length) {
|
|
3493
|
+
addIssueToContext(ctx, {
|
|
3494
|
+
code: ZodIssueCode.too_big,
|
|
3495
|
+
maximum: this._def.items.length,
|
|
3496
|
+
inclusive: true,
|
|
3497
|
+
exact: false,
|
|
3498
|
+
type: "array"
|
|
3499
|
+
});
|
|
3500
|
+
status.dirty();
|
|
3501
|
+
}
|
|
3502
|
+
const items = [...ctx.data].map((item, itemIndex) => {
|
|
3503
|
+
const schema = this._def.items[itemIndex] || this._def.rest;
|
|
3504
|
+
if (!schema)
|
|
3505
|
+
return null;
|
|
3506
|
+
return schema._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
|
|
3507
|
+
}).filter((x) => !!x);
|
|
3508
|
+
if (ctx.common.async) {
|
|
3509
|
+
return Promise.all(items).then((results) => {
|
|
3510
|
+
return ParseStatus.mergeArray(status, results);
|
|
3511
|
+
});
|
|
3512
|
+
} else {
|
|
3513
|
+
return ParseStatus.mergeArray(status, items);
|
|
3514
|
+
}
|
|
3515
|
+
}
|
|
3516
|
+
get items() {
|
|
3517
|
+
return this._def.items;
|
|
3518
|
+
}
|
|
3519
|
+
rest(rest) {
|
|
3520
|
+
return new _ZodTuple({
|
|
3521
|
+
...this._def,
|
|
3522
|
+
rest
|
|
3523
|
+
});
|
|
3524
|
+
}
|
|
3525
|
+
};
|
|
3526
|
+
ZodTuple.create = (schemas, params) => {
|
|
3527
|
+
if (!Array.isArray(schemas)) {
|
|
3528
|
+
throw new Error("You must pass an array of schemas to z.tuple([ ... ])");
|
|
3529
|
+
}
|
|
3530
|
+
return new ZodTuple({
|
|
3531
|
+
items: schemas,
|
|
3532
|
+
typeName: ZodFirstPartyTypeKind.ZodTuple,
|
|
3533
|
+
rest: null,
|
|
3534
|
+
...processCreateParams(params)
|
|
3535
|
+
});
|
|
3536
|
+
};
|
|
3537
|
+
ZodRecord = class _ZodRecord extends ZodType {
|
|
3538
|
+
get keySchema() {
|
|
3539
|
+
return this._def.keyType;
|
|
3540
|
+
}
|
|
3541
|
+
get valueSchema() {
|
|
3542
|
+
return this._def.valueType;
|
|
3543
|
+
}
|
|
3544
|
+
_parse(input) {
|
|
3545
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3546
|
+
if (ctx.parsedType !== ZodParsedType.object) {
|
|
3547
|
+
addIssueToContext(ctx, {
|
|
3548
|
+
code: ZodIssueCode.invalid_type,
|
|
3549
|
+
expected: ZodParsedType.object,
|
|
3550
|
+
received: ctx.parsedType
|
|
3551
|
+
});
|
|
3552
|
+
return INVALID;
|
|
3553
|
+
}
|
|
3554
|
+
const pairs = [];
|
|
3555
|
+
const keyType = this._def.keyType;
|
|
3556
|
+
const valueType = this._def.valueType;
|
|
3557
|
+
for (const key in ctx.data) {
|
|
3558
|
+
pairs.push({
|
|
3559
|
+
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
|
|
3560
|
+
value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
|
|
3561
|
+
alwaysSet: key in ctx.data
|
|
3562
|
+
});
|
|
3563
|
+
}
|
|
3564
|
+
if (ctx.common.async) {
|
|
3565
|
+
return ParseStatus.mergeObjectAsync(status, pairs);
|
|
3566
|
+
} else {
|
|
3567
|
+
return ParseStatus.mergeObjectSync(status, pairs);
|
|
3568
|
+
}
|
|
3569
|
+
}
|
|
3570
|
+
get element() {
|
|
3571
|
+
return this._def.valueType;
|
|
3572
|
+
}
|
|
3573
|
+
static create(first, second, third) {
|
|
3574
|
+
if (second instanceof ZodType) {
|
|
3575
|
+
return new _ZodRecord({
|
|
3576
|
+
keyType: first,
|
|
3577
|
+
valueType: second,
|
|
3578
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
3579
|
+
...processCreateParams(third)
|
|
3580
|
+
});
|
|
3581
|
+
}
|
|
3582
|
+
return new _ZodRecord({
|
|
3583
|
+
keyType: ZodString.create(),
|
|
3584
|
+
valueType: first,
|
|
3585
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
3586
|
+
...processCreateParams(second)
|
|
3587
|
+
});
|
|
3588
|
+
}
|
|
3589
|
+
};
|
|
3590
|
+
ZodMap = class extends ZodType {
|
|
3591
|
+
get keySchema() {
|
|
3592
|
+
return this._def.keyType;
|
|
3593
|
+
}
|
|
3594
|
+
get valueSchema() {
|
|
3595
|
+
return this._def.valueType;
|
|
3596
|
+
}
|
|
3597
|
+
_parse(input) {
|
|
3598
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3599
|
+
if (ctx.parsedType !== ZodParsedType.map) {
|
|
3600
|
+
addIssueToContext(ctx, {
|
|
3601
|
+
code: ZodIssueCode.invalid_type,
|
|
3602
|
+
expected: ZodParsedType.map,
|
|
3603
|
+
received: ctx.parsedType
|
|
3604
|
+
});
|
|
3605
|
+
return INVALID;
|
|
3606
|
+
}
|
|
3607
|
+
const keyType = this._def.keyType;
|
|
3608
|
+
const valueType = this._def.valueType;
|
|
3609
|
+
const pairs = [...ctx.data.entries()].map(([key, value], index) => {
|
|
3610
|
+
return {
|
|
3611
|
+
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, [index, "key"])),
|
|
3612
|
+
value: valueType._parse(new ParseInputLazyPath(ctx, value, ctx.path, [index, "value"]))
|
|
3613
|
+
};
|
|
3614
|
+
});
|
|
3615
|
+
if (ctx.common.async) {
|
|
3616
|
+
const finalMap = /* @__PURE__ */ new Map();
|
|
3617
|
+
return Promise.resolve().then(async () => {
|
|
3618
|
+
for (const pair of pairs) {
|
|
3619
|
+
const key = await pair.key;
|
|
3620
|
+
const value = await pair.value;
|
|
3621
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
3622
|
+
return INVALID;
|
|
3623
|
+
}
|
|
3624
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
3625
|
+
status.dirty();
|
|
3626
|
+
}
|
|
3627
|
+
finalMap.set(key.value, value.value);
|
|
3628
|
+
}
|
|
3629
|
+
return { status: status.value, value: finalMap };
|
|
3630
|
+
});
|
|
3631
|
+
} else {
|
|
3632
|
+
const finalMap = /* @__PURE__ */ new Map();
|
|
3633
|
+
for (const pair of pairs) {
|
|
3634
|
+
const key = pair.key;
|
|
3635
|
+
const value = pair.value;
|
|
3636
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
3637
|
+
return INVALID;
|
|
3638
|
+
}
|
|
3639
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
3640
|
+
status.dirty();
|
|
3641
|
+
}
|
|
3642
|
+
finalMap.set(key.value, value.value);
|
|
3643
|
+
}
|
|
3644
|
+
return { status: status.value, value: finalMap };
|
|
3645
|
+
}
|
|
3646
|
+
}
|
|
3647
|
+
};
|
|
3648
|
+
ZodMap.create = (keyType, valueType, params) => {
|
|
3649
|
+
return new ZodMap({
|
|
3650
|
+
valueType,
|
|
3651
|
+
keyType,
|
|
3652
|
+
typeName: ZodFirstPartyTypeKind.ZodMap,
|
|
3653
|
+
...processCreateParams(params)
|
|
3654
|
+
});
|
|
3655
|
+
};
|
|
3656
|
+
ZodSet = class _ZodSet extends ZodType {
|
|
3657
|
+
_parse(input) {
|
|
3658
|
+
const { status, ctx } = this._processInputParams(input);
|
|
3659
|
+
if (ctx.parsedType !== ZodParsedType.set) {
|
|
3660
|
+
addIssueToContext(ctx, {
|
|
3661
|
+
code: ZodIssueCode.invalid_type,
|
|
3662
|
+
expected: ZodParsedType.set,
|
|
3663
|
+
received: ctx.parsedType
|
|
3664
|
+
});
|
|
3665
|
+
return INVALID;
|
|
3666
|
+
}
|
|
3667
|
+
const def = this._def;
|
|
3668
|
+
if (def.minSize !== null) {
|
|
3669
|
+
if (ctx.data.size < def.minSize.value) {
|
|
3670
|
+
addIssueToContext(ctx, {
|
|
3671
|
+
code: ZodIssueCode.too_small,
|
|
3672
|
+
minimum: def.minSize.value,
|
|
3673
|
+
type: "set",
|
|
3674
|
+
inclusive: true,
|
|
3675
|
+
exact: false,
|
|
3676
|
+
message: def.minSize.message
|
|
3677
|
+
});
|
|
3678
|
+
status.dirty();
|
|
3679
|
+
}
|
|
3680
|
+
}
|
|
3681
|
+
if (def.maxSize !== null) {
|
|
3682
|
+
if (ctx.data.size > def.maxSize.value) {
|
|
3683
|
+
addIssueToContext(ctx, {
|
|
3684
|
+
code: ZodIssueCode.too_big,
|
|
3685
|
+
maximum: def.maxSize.value,
|
|
3686
|
+
type: "set",
|
|
3687
|
+
inclusive: true,
|
|
3688
|
+
exact: false,
|
|
3689
|
+
message: def.maxSize.message
|
|
3690
|
+
});
|
|
3691
|
+
status.dirty();
|
|
3692
|
+
}
|
|
3693
|
+
}
|
|
3694
|
+
const valueType = this._def.valueType;
|
|
3695
|
+
function finalizeSet(elements2) {
|
|
3696
|
+
const parsedSet = /* @__PURE__ */ new Set();
|
|
3697
|
+
for (const element of elements2) {
|
|
3698
|
+
if (element.status === "aborted")
|
|
3699
|
+
return INVALID;
|
|
3700
|
+
if (element.status === "dirty")
|
|
3701
|
+
status.dirty();
|
|
3702
|
+
parsedSet.add(element.value);
|
|
3703
|
+
}
|
|
3704
|
+
return { status: status.value, value: parsedSet };
|
|
3705
|
+
}
|
|
3706
|
+
const elements = [...ctx.data.values()].map((item, i) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i)));
|
|
3707
|
+
if (ctx.common.async) {
|
|
3708
|
+
return Promise.all(elements).then((elements2) => finalizeSet(elements2));
|
|
3709
|
+
} else {
|
|
3710
|
+
return finalizeSet(elements);
|
|
3711
|
+
}
|
|
3712
|
+
}
|
|
3713
|
+
min(minSize, message) {
|
|
3714
|
+
return new _ZodSet({
|
|
3715
|
+
...this._def,
|
|
3716
|
+
minSize: { value: minSize, message: errorUtil.toString(message) }
|
|
3717
|
+
});
|
|
3718
|
+
}
|
|
3719
|
+
max(maxSize, message) {
|
|
3720
|
+
return new _ZodSet({
|
|
3721
|
+
...this._def,
|
|
3722
|
+
maxSize: { value: maxSize, message: errorUtil.toString(message) }
|
|
3723
|
+
});
|
|
3724
|
+
}
|
|
3725
|
+
size(size, message) {
|
|
3726
|
+
return this.min(size, message).max(size, message);
|
|
3727
|
+
}
|
|
3728
|
+
nonempty(message) {
|
|
3729
|
+
return this.min(1, message);
|
|
3730
|
+
}
|
|
3731
|
+
};
|
|
3732
|
+
ZodSet.create = (valueType, params) => {
|
|
3733
|
+
return new ZodSet({
|
|
3734
|
+
valueType,
|
|
3735
|
+
minSize: null,
|
|
3736
|
+
maxSize: null,
|
|
3737
|
+
typeName: ZodFirstPartyTypeKind.ZodSet,
|
|
3738
|
+
...processCreateParams(params)
|
|
3739
|
+
});
|
|
3740
|
+
};
|
|
3741
|
+
ZodFunction = class _ZodFunction extends ZodType {
|
|
3742
|
+
constructor() {
|
|
3743
|
+
super(...arguments);
|
|
3744
|
+
this.validate = this.implement;
|
|
3745
|
+
}
|
|
3746
|
+
_parse(input) {
|
|
3747
|
+
const { ctx } = this._processInputParams(input);
|
|
3748
|
+
if (ctx.parsedType !== ZodParsedType.function) {
|
|
3749
|
+
addIssueToContext(ctx, {
|
|
3750
|
+
code: ZodIssueCode.invalid_type,
|
|
3751
|
+
expected: ZodParsedType.function,
|
|
3752
|
+
received: ctx.parsedType
|
|
3753
|
+
});
|
|
3754
|
+
return INVALID;
|
|
3755
|
+
}
|
|
3756
|
+
function makeArgsIssue(args, error) {
|
|
3757
|
+
return makeIssue({
|
|
3758
|
+
data: args,
|
|
3759
|
+
path: ctx.path,
|
|
3760
|
+
errorMaps: [ctx.common.contextualErrorMap, ctx.schemaErrorMap, getErrorMap(), en_default].filter((x) => !!x),
|
|
3761
|
+
issueData: {
|
|
3762
|
+
code: ZodIssueCode.invalid_arguments,
|
|
3763
|
+
argumentsError: error
|
|
3764
|
+
}
|
|
3765
|
+
});
|
|
3766
|
+
}
|
|
3767
|
+
function makeReturnsIssue(returns, error) {
|
|
3768
|
+
return makeIssue({
|
|
3769
|
+
data: returns,
|
|
3770
|
+
path: ctx.path,
|
|
3771
|
+
errorMaps: [ctx.common.contextualErrorMap, ctx.schemaErrorMap, getErrorMap(), en_default].filter((x) => !!x),
|
|
3772
|
+
issueData: {
|
|
3773
|
+
code: ZodIssueCode.invalid_return_type,
|
|
3774
|
+
returnTypeError: error
|
|
3775
|
+
}
|
|
3776
|
+
});
|
|
3777
|
+
}
|
|
3778
|
+
const params = { errorMap: ctx.common.contextualErrorMap };
|
|
3779
|
+
const fn = ctx.data;
|
|
3780
|
+
if (this._def.returns instanceof ZodPromise) {
|
|
3781
|
+
const me = this;
|
|
3782
|
+
return OK(async function(...args) {
|
|
3783
|
+
const error = new ZodError([]);
|
|
3784
|
+
const parsedArgs = await me._def.args.parseAsync(args, params).catch((e) => {
|
|
3785
|
+
error.addIssue(makeArgsIssue(args, e));
|
|
3786
|
+
throw error;
|
|
3787
|
+
});
|
|
3788
|
+
const result = await Reflect.apply(fn, this, parsedArgs);
|
|
3789
|
+
const parsedReturns = await me._def.returns._def.type.parseAsync(result, params).catch((e) => {
|
|
3790
|
+
error.addIssue(makeReturnsIssue(result, e));
|
|
3791
|
+
throw error;
|
|
3792
|
+
});
|
|
3793
|
+
return parsedReturns;
|
|
3794
|
+
});
|
|
3795
|
+
} else {
|
|
3796
|
+
const me = this;
|
|
3797
|
+
return OK(function(...args) {
|
|
3798
|
+
const parsedArgs = me._def.args.safeParse(args, params);
|
|
3799
|
+
if (!parsedArgs.success) {
|
|
3800
|
+
throw new ZodError([makeArgsIssue(args, parsedArgs.error)]);
|
|
3801
|
+
}
|
|
3802
|
+
const result = Reflect.apply(fn, this, parsedArgs.data);
|
|
3803
|
+
const parsedReturns = me._def.returns.safeParse(result, params);
|
|
3804
|
+
if (!parsedReturns.success) {
|
|
3805
|
+
throw new ZodError([makeReturnsIssue(result, parsedReturns.error)]);
|
|
3806
|
+
}
|
|
3807
|
+
return parsedReturns.data;
|
|
3808
|
+
});
|
|
3809
|
+
}
|
|
3810
|
+
}
|
|
3811
|
+
parameters() {
|
|
3812
|
+
return this._def.args;
|
|
3813
|
+
}
|
|
3814
|
+
returnType() {
|
|
3815
|
+
return this._def.returns;
|
|
3816
|
+
}
|
|
3817
|
+
args(...items) {
|
|
3818
|
+
return new _ZodFunction({
|
|
3819
|
+
...this._def,
|
|
3820
|
+
args: ZodTuple.create(items).rest(ZodUnknown.create())
|
|
3821
|
+
});
|
|
3822
|
+
}
|
|
3823
|
+
returns(returnType) {
|
|
3824
|
+
return new _ZodFunction({
|
|
3825
|
+
...this._def,
|
|
3826
|
+
returns: returnType
|
|
3827
|
+
});
|
|
3828
|
+
}
|
|
3829
|
+
implement(func) {
|
|
3830
|
+
const validatedFunc = this.parse(func);
|
|
3831
|
+
return validatedFunc;
|
|
3832
|
+
}
|
|
3833
|
+
strictImplement(func) {
|
|
3834
|
+
const validatedFunc = this.parse(func);
|
|
3835
|
+
return validatedFunc;
|
|
3836
|
+
}
|
|
3837
|
+
static create(args, returns, params) {
|
|
3838
|
+
return new _ZodFunction({
|
|
3839
|
+
args: args ? args : ZodTuple.create([]).rest(ZodUnknown.create()),
|
|
3840
|
+
returns: returns || ZodUnknown.create(),
|
|
3841
|
+
typeName: ZodFirstPartyTypeKind.ZodFunction,
|
|
3842
|
+
...processCreateParams(params)
|
|
3843
|
+
});
|
|
3844
|
+
}
|
|
3845
|
+
};
|
|
3846
|
+
ZodLazy = class extends ZodType {
|
|
3847
|
+
get schema() {
|
|
3848
|
+
return this._def.getter();
|
|
3849
|
+
}
|
|
3850
|
+
_parse(input) {
|
|
3851
|
+
const { ctx } = this._processInputParams(input);
|
|
3852
|
+
const lazySchema = this._def.getter();
|
|
3853
|
+
return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx });
|
|
3854
|
+
}
|
|
3855
|
+
};
|
|
3856
|
+
ZodLazy.create = (getter, params) => {
|
|
3857
|
+
return new ZodLazy({
|
|
3858
|
+
getter,
|
|
3859
|
+
typeName: ZodFirstPartyTypeKind.ZodLazy,
|
|
3860
|
+
...processCreateParams(params)
|
|
3861
|
+
});
|
|
3862
|
+
};
|
|
3863
|
+
ZodLiteral = class extends ZodType {
|
|
3864
|
+
_parse(input) {
|
|
3865
|
+
if (input.data !== this._def.value) {
|
|
3866
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3867
|
+
addIssueToContext(ctx, {
|
|
3868
|
+
received: ctx.data,
|
|
3869
|
+
code: ZodIssueCode.invalid_literal,
|
|
3870
|
+
expected: this._def.value
|
|
3871
|
+
});
|
|
3872
|
+
return INVALID;
|
|
3873
|
+
}
|
|
3874
|
+
return { status: "valid", value: input.data };
|
|
3875
|
+
}
|
|
3876
|
+
get value() {
|
|
3877
|
+
return this._def.value;
|
|
3878
|
+
}
|
|
3879
|
+
};
|
|
3880
|
+
ZodLiteral.create = (value, params) => {
|
|
3881
|
+
return new ZodLiteral({
|
|
3882
|
+
value,
|
|
3883
|
+
typeName: ZodFirstPartyTypeKind.ZodLiteral,
|
|
3884
|
+
...processCreateParams(params)
|
|
3885
|
+
});
|
|
3886
|
+
};
|
|
3887
|
+
ZodEnum = class _ZodEnum extends ZodType {
|
|
3888
|
+
_parse(input) {
|
|
3889
|
+
if (typeof input.data !== "string") {
|
|
3890
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3891
|
+
const expectedValues = this._def.values;
|
|
3892
|
+
addIssueToContext(ctx, {
|
|
3893
|
+
expected: util.joinValues(expectedValues),
|
|
3894
|
+
received: ctx.parsedType,
|
|
3895
|
+
code: ZodIssueCode.invalid_type
|
|
3896
|
+
});
|
|
3897
|
+
return INVALID;
|
|
3898
|
+
}
|
|
3899
|
+
if (!this._cache) {
|
|
3900
|
+
this._cache = new Set(this._def.values);
|
|
3901
|
+
}
|
|
3902
|
+
if (!this._cache.has(input.data)) {
|
|
3903
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3904
|
+
const expectedValues = this._def.values;
|
|
3905
|
+
addIssueToContext(ctx, {
|
|
3906
|
+
received: ctx.data,
|
|
3907
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
3908
|
+
options: expectedValues
|
|
3909
|
+
});
|
|
3910
|
+
return INVALID;
|
|
3911
|
+
}
|
|
3912
|
+
return OK(input.data);
|
|
3913
|
+
}
|
|
3914
|
+
get options() {
|
|
3915
|
+
return this._def.values;
|
|
3916
|
+
}
|
|
3917
|
+
get enum() {
|
|
3918
|
+
const enumValues = {};
|
|
3919
|
+
for (const val of this._def.values) {
|
|
3920
|
+
enumValues[val] = val;
|
|
3921
|
+
}
|
|
3922
|
+
return enumValues;
|
|
3923
|
+
}
|
|
3924
|
+
get Values() {
|
|
3925
|
+
const enumValues = {};
|
|
3926
|
+
for (const val of this._def.values) {
|
|
3927
|
+
enumValues[val] = val;
|
|
3928
|
+
}
|
|
3929
|
+
return enumValues;
|
|
3930
|
+
}
|
|
3931
|
+
get Enum() {
|
|
3932
|
+
const enumValues = {};
|
|
3933
|
+
for (const val of this._def.values) {
|
|
3934
|
+
enumValues[val] = val;
|
|
3935
|
+
}
|
|
3936
|
+
return enumValues;
|
|
3937
|
+
}
|
|
3938
|
+
extract(values, newDef = this._def) {
|
|
3939
|
+
return _ZodEnum.create(values, {
|
|
3940
|
+
...this._def,
|
|
3941
|
+
...newDef
|
|
3942
|
+
});
|
|
3943
|
+
}
|
|
3944
|
+
exclude(values, newDef = this._def) {
|
|
3945
|
+
return _ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
|
|
3946
|
+
...this._def,
|
|
3947
|
+
...newDef
|
|
3948
|
+
});
|
|
3949
|
+
}
|
|
3950
|
+
};
|
|
3951
|
+
ZodEnum.create = createZodEnum;
|
|
3952
|
+
ZodNativeEnum = class extends ZodType {
|
|
3953
|
+
_parse(input) {
|
|
3954
|
+
const nativeEnumValues = util.getValidEnumValues(this._def.values);
|
|
3955
|
+
const ctx = this._getOrReturnCtx(input);
|
|
3956
|
+
if (ctx.parsedType !== ZodParsedType.string && ctx.parsedType !== ZodParsedType.number) {
|
|
3957
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
3958
|
+
addIssueToContext(ctx, {
|
|
3959
|
+
expected: util.joinValues(expectedValues),
|
|
3960
|
+
received: ctx.parsedType,
|
|
3961
|
+
code: ZodIssueCode.invalid_type
|
|
3962
|
+
});
|
|
3963
|
+
return INVALID;
|
|
3964
|
+
}
|
|
3965
|
+
if (!this._cache) {
|
|
3966
|
+
this._cache = new Set(util.getValidEnumValues(this._def.values));
|
|
3967
|
+
}
|
|
3968
|
+
if (!this._cache.has(input.data)) {
|
|
3969
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
3970
|
+
addIssueToContext(ctx, {
|
|
3971
|
+
received: ctx.data,
|
|
3972
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
3973
|
+
options: expectedValues
|
|
3974
|
+
});
|
|
3975
|
+
return INVALID;
|
|
3976
|
+
}
|
|
3977
|
+
return OK(input.data);
|
|
3978
|
+
}
|
|
3979
|
+
get enum() {
|
|
3980
|
+
return this._def.values;
|
|
3981
|
+
}
|
|
3982
|
+
};
|
|
3983
|
+
ZodNativeEnum.create = (values, params) => {
|
|
3984
|
+
return new ZodNativeEnum({
|
|
3985
|
+
values,
|
|
3986
|
+
typeName: ZodFirstPartyTypeKind.ZodNativeEnum,
|
|
3987
|
+
...processCreateParams(params)
|
|
3988
|
+
});
|
|
3989
|
+
};
|
|
3990
|
+
ZodPromise = class extends ZodType {
|
|
3991
|
+
unwrap() {
|
|
3992
|
+
return this._def.type;
|
|
3993
|
+
}
|
|
3994
|
+
_parse(input) {
|
|
3995
|
+
const { ctx } = this._processInputParams(input);
|
|
3996
|
+
if (ctx.parsedType !== ZodParsedType.promise && ctx.common.async === false) {
|
|
3997
|
+
addIssueToContext(ctx, {
|
|
3998
|
+
code: ZodIssueCode.invalid_type,
|
|
3999
|
+
expected: ZodParsedType.promise,
|
|
4000
|
+
received: ctx.parsedType
|
|
4001
|
+
});
|
|
4002
|
+
return INVALID;
|
|
4003
|
+
}
|
|
4004
|
+
const promisified = ctx.parsedType === ZodParsedType.promise ? ctx.data : Promise.resolve(ctx.data);
|
|
4005
|
+
return OK(promisified.then((data) => {
|
|
4006
|
+
return this._def.type.parseAsync(data, {
|
|
4007
|
+
path: ctx.path,
|
|
4008
|
+
errorMap: ctx.common.contextualErrorMap
|
|
4009
|
+
});
|
|
4010
|
+
}));
|
|
4011
|
+
}
|
|
4012
|
+
};
|
|
4013
|
+
ZodPromise.create = (schema, params) => {
|
|
4014
|
+
return new ZodPromise({
|
|
4015
|
+
type: schema,
|
|
4016
|
+
typeName: ZodFirstPartyTypeKind.ZodPromise,
|
|
4017
|
+
...processCreateParams(params)
|
|
4018
|
+
});
|
|
4019
|
+
};
|
|
4020
|
+
ZodEffects = class extends ZodType {
|
|
4021
|
+
innerType() {
|
|
4022
|
+
return this._def.schema;
|
|
298
4023
|
}
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
return result.changes > 0;
|
|
4024
|
+
sourceType() {
|
|
4025
|
+
return this._def.schema._def.typeName === ZodFirstPartyTypeKind.ZodEffects ? this._def.schema.sourceType() : this._def.schema;
|
|
302
4026
|
}
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
const
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
`).all(sanitized, limit);
|
|
314
|
-
if (ftsRows.length > 0) {
|
|
315
|
-
return ftsRows.map((r) => this.rowToEntity(r));
|
|
4027
|
+
_parse(input) {
|
|
4028
|
+
const { status, ctx } = this._processInputParams(input);
|
|
4029
|
+
const effect = this._def.effect || null;
|
|
4030
|
+
const checkCtx = {
|
|
4031
|
+
addIssue: (arg) => {
|
|
4032
|
+
addIssueToContext(ctx, arg);
|
|
4033
|
+
if (arg.fatal) {
|
|
4034
|
+
status.abort();
|
|
4035
|
+
} else {
|
|
4036
|
+
status.dirty();
|
|
316
4037
|
}
|
|
317
|
-
}
|
|
4038
|
+
},
|
|
4039
|
+
get path() {
|
|
4040
|
+
return ctx.path;
|
|
4041
|
+
}
|
|
4042
|
+
};
|
|
4043
|
+
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
|
|
4044
|
+
if (effect.type === "preprocess") {
|
|
4045
|
+
const processed = effect.transform(ctx.data, checkCtx);
|
|
4046
|
+
if (ctx.common.async) {
|
|
4047
|
+
return Promise.resolve(processed).then(async (processed2) => {
|
|
4048
|
+
if (status.value === "aborted")
|
|
4049
|
+
return INVALID;
|
|
4050
|
+
const result = await this._def.schema._parseAsync({
|
|
4051
|
+
data: processed2,
|
|
4052
|
+
path: ctx.path,
|
|
4053
|
+
parent: ctx
|
|
4054
|
+
});
|
|
4055
|
+
if (result.status === "aborted")
|
|
4056
|
+
return INVALID;
|
|
4057
|
+
if (result.status === "dirty")
|
|
4058
|
+
return DIRTY(result.value);
|
|
4059
|
+
if (status.value === "dirty")
|
|
4060
|
+
return DIRTY(result.value);
|
|
4061
|
+
return result;
|
|
4062
|
+
});
|
|
4063
|
+
} else {
|
|
4064
|
+
if (status.value === "aborted")
|
|
4065
|
+
return INVALID;
|
|
4066
|
+
const result = this._def.schema._parseSync({
|
|
4067
|
+
data: processed,
|
|
4068
|
+
path: ctx.path,
|
|
4069
|
+
parent: ctx
|
|
4070
|
+
});
|
|
4071
|
+
if (result.status === "aborted")
|
|
4072
|
+
return INVALID;
|
|
4073
|
+
if (result.status === "dirty")
|
|
4074
|
+
return DIRTY(result.value);
|
|
4075
|
+
if (status.value === "dirty")
|
|
4076
|
+
return DIRTY(result.value);
|
|
4077
|
+
return result;
|
|
318
4078
|
}
|
|
319
4079
|
}
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
`).all(`%${likeQuery}%`, `%${likeQuery}%`, limit);
|
|
326
|
-
return likeRows.map((r) => this.rowToEntity(r));
|
|
327
|
-
}
|
|
328
|
-
// ─── Graph Traversal ───────────────────────────────────────────
|
|
329
|
-
/**
|
|
330
|
-
* Find path between two entities (BFS, max depth)
|
|
331
|
-
*/
|
|
332
|
-
findPath(fromName, toName, maxHops = 3) {
|
|
333
|
-
const fromEntity = this.findEntityByName(fromName);
|
|
334
|
-
const toEntity = this.findEntityByName(toName);
|
|
335
|
-
if (!fromEntity || !toEntity) return null;
|
|
336
|
-
const queue = [
|
|
337
|
-
{ entityId: fromEntity.id, path: [fromEntity.name], relations: [] }
|
|
338
|
-
];
|
|
339
|
-
const visited = /* @__PURE__ */ new Set([fromEntity.id]);
|
|
340
|
-
while (queue.length > 0) {
|
|
341
|
-
const current = queue.shift();
|
|
342
|
-
if (current.path.length > maxHops + 1) break;
|
|
343
|
-
const outgoing = this.db.prepare(`
|
|
344
|
-
SELECT r.relation, r.to_id as neighbor_id, e.name as neighbor_name
|
|
345
|
-
FROM relationships r JOIN entities e ON r.to_id = e.id
|
|
346
|
-
WHERE r.from_id = ?
|
|
347
|
-
`).all(current.entityId);
|
|
348
|
-
const incoming = this.db.prepare(`
|
|
349
|
-
SELECT r.relation, r.from_id as neighbor_id, e.name as neighbor_name
|
|
350
|
-
FROM relationships r JOIN entities e ON r.from_id = e.id
|
|
351
|
-
WHERE r.to_id = ?
|
|
352
|
-
`).all(current.entityId);
|
|
353
|
-
const neighbors = [
|
|
354
|
-
...outgoing.map((n) => ({ ...n, direction: "->" })),
|
|
355
|
-
...incoming.map((n) => ({ ...n, direction: "<-" }))
|
|
356
|
-
];
|
|
357
|
-
for (const neighbor of neighbors) {
|
|
358
|
-
if (neighbor.neighbor_id === toEntity.id) {
|
|
359
|
-
return {
|
|
360
|
-
path: [...current.path, neighbor.neighbor_name],
|
|
361
|
-
relations: [...current.relations, `${neighbor.direction}[${neighbor.relation}]`]
|
|
362
|
-
};
|
|
4080
|
+
if (effect.type === "refinement") {
|
|
4081
|
+
const executeRefinement = (acc) => {
|
|
4082
|
+
const result = effect.refinement(acc, checkCtx);
|
|
4083
|
+
if (ctx.common.async) {
|
|
4084
|
+
return Promise.resolve(result);
|
|
363
4085
|
}
|
|
364
|
-
if (
|
|
365
|
-
|
|
366
|
-
queue.push({
|
|
367
|
-
entityId: neighbor.neighbor_id,
|
|
368
|
-
path: [...current.path, neighbor.neighbor_name],
|
|
369
|
-
relations: [...current.relations, `${neighbor.direction}[${neighbor.relation}]`]
|
|
370
|
-
});
|
|
4086
|
+
if (result instanceof Promise) {
|
|
4087
|
+
throw new Error("Async refinement encountered during synchronous parse operation. Use .parseAsync instead.");
|
|
371
4088
|
}
|
|
4089
|
+
return acc;
|
|
4090
|
+
};
|
|
4091
|
+
if (ctx.common.async === false) {
|
|
4092
|
+
const inner = this._def.schema._parseSync({
|
|
4093
|
+
data: ctx.data,
|
|
4094
|
+
path: ctx.path,
|
|
4095
|
+
parent: ctx
|
|
4096
|
+
});
|
|
4097
|
+
if (inner.status === "aborted")
|
|
4098
|
+
return INVALID;
|
|
4099
|
+
if (inner.status === "dirty")
|
|
4100
|
+
status.dirty();
|
|
4101
|
+
executeRefinement(inner.value);
|
|
4102
|
+
return { status: status.value, value: inner.value };
|
|
4103
|
+
} else {
|
|
4104
|
+
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((inner) => {
|
|
4105
|
+
if (inner.status === "aborted")
|
|
4106
|
+
return INVALID;
|
|
4107
|
+
if (inner.status === "dirty")
|
|
4108
|
+
status.dirty();
|
|
4109
|
+
return executeRefinement(inner.value).then(() => {
|
|
4110
|
+
return { status: status.value, value: inner.value };
|
|
4111
|
+
});
|
|
4112
|
+
});
|
|
372
4113
|
}
|
|
373
4114
|
}
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
const nextFrontier = [];
|
|
387
|
-
for (const nodeId of frontier) {
|
|
388
|
-
const rels = this.db.prepare(`
|
|
389
|
-
SELECT * FROM relationships WHERE from_id = ? OR to_id = ?
|
|
390
|
-
`).all(nodeId, nodeId);
|
|
391
|
-
for (const rel of rels) {
|
|
392
|
-
relIds.add(rel.id);
|
|
393
|
-
const neighborId = rel.from_id === nodeId ? rel.to_id : rel.from_id;
|
|
394
|
-
if (!entityIds.has(neighborId)) {
|
|
395
|
-
entityIds.add(neighborId);
|
|
396
|
-
nextFrontier.push(neighborId);
|
|
397
|
-
}
|
|
4115
|
+
if (effect.type === "transform") {
|
|
4116
|
+
if (ctx.common.async === false) {
|
|
4117
|
+
const base = this._def.schema._parseSync({
|
|
4118
|
+
data: ctx.data,
|
|
4119
|
+
path: ctx.path,
|
|
4120
|
+
parent: ctx
|
|
4121
|
+
});
|
|
4122
|
+
if (!isValid(base))
|
|
4123
|
+
return INVALID;
|
|
4124
|
+
const result = effect.transform(base.value, checkCtx);
|
|
4125
|
+
if (result instanceof Promise) {
|
|
4126
|
+
throw new Error(`Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.`);
|
|
398
4127
|
}
|
|
4128
|
+
return { status: status.value, value: result };
|
|
4129
|
+
} else {
|
|
4130
|
+
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((base) => {
|
|
4131
|
+
if (!isValid(base))
|
|
4132
|
+
return INVALID;
|
|
4133
|
+
return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({
|
|
4134
|
+
status: status.value,
|
|
4135
|
+
value: result
|
|
4136
|
+
}));
|
|
4137
|
+
});
|
|
399
4138
|
}
|
|
400
|
-
frontier = nextFrontier;
|
|
401
4139
|
}
|
|
402
|
-
|
|
403
|
-
const relationships = [...relIds].map((id) => {
|
|
404
|
-
const row = this.db.prepare(`SELECT * FROM relationships WHERE id = ?`).get(id);
|
|
405
|
-
return row ? this.rowToRelationship(row) : null;
|
|
406
|
-
}).filter((r) => r !== null);
|
|
407
|
-
return { entities, relationships };
|
|
4140
|
+
util.assertNever(effect);
|
|
408
4141
|
}
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
4142
|
+
};
|
|
4143
|
+
ZodEffects.create = (schema, effect, params) => {
|
|
4144
|
+
return new ZodEffects({
|
|
4145
|
+
schema,
|
|
4146
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
4147
|
+
effect,
|
|
4148
|
+
...processCreateParams(params)
|
|
4149
|
+
});
|
|
4150
|
+
};
|
|
4151
|
+
ZodEffects.createWithPreprocess = (preprocess, schema, params) => {
|
|
4152
|
+
return new ZodEffects({
|
|
4153
|
+
schema,
|
|
4154
|
+
effect: { type: "preprocess", transform: preprocess },
|
|
4155
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
4156
|
+
...processCreateParams(params)
|
|
4157
|
+
});
|
|
4158
|
+
};
|
|
4159
|
+
ZodOptional = class extends ZodType {
|
|
4160
|
+
_parse(input) {
|
|
4161
|
+
const parsedType = this._getType(input);
|
|
4162
|
+
if (parsedType === ZodParsedType.undefined) {
|
|
4163
|
+
return OK(void 0);
|
|
4164
|
+
}
|
|
4165
|
+
return this._def.innerType._parse(input);
|
|
425
4166
|
}
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
const id = `log-${nanoid(12)}`;
|
|
429
|
-
this.db.prepare(`
|
|
430
|
-
INSERT INTO memory_log (id, raw_text, extracted_entities, extracted_relations, session_id)
|
|
431
|
-
VALUES (?, ?, ?, ?, ?)
|
|
432
|
-
`).run(id, rawText, JSON.stringify(entities), JSON.stringify(relations), sessionId ?? null);
|
|
4167
|
+
unwrap() {
|
|
4168
|
+
return this._def.innerType;
|
|
433
4169
|
}
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
4170
|
+
};
|
|
4171
|
+
ZodOptional.create = (type, params) => {
|
|
4172
|
+
return new ZodOptional({
|
|
4173
|
+
innerType: type,
|
|
4174
|
+
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
4175
|
+
...processCreateParams(params)
|
|
4176
|
+
});
|
|
4177
|
+
};
|
|
4178
|
+
ZodNullable = class extends ZodType {
|
|
4179
|
+
_parse(input) {
|
|
4180
|
+
const parsedType = this._getType(input);
|
|
4181
|
+
if (parsedType === ZodParsedType.null) {
|
|
4182
|
+
return OK(null);
|
|
4183
|
+
}
|
|
4184
|
+
return this._def.innerType._parse(input);
|
|
4185
|
+
}
|
|
4186
|
+
unwrap() {
|
|
4187
|
+
return this._def.innerType;
|
|
4188
|
+
}
|
|
4189
|
+
};
|
|
4190
|
+
ZodNullable.create = (type, params) => {
|
|
4191
|
+
return new ZodNullable({
|
|
4192
|
+
innerType: type,
|
|
4193
|
+
typeName: ZodFirstPartyTypeKind.ZodNullable,
|
|
4194
|
+
...processCreateParams(params)
|
|
4195
|
+
});
|
|
4196
|
+
};
|
|
4197
|
+
ZodDefault = class extends ZodType {
|
|
4198
|
+
_parse(input) {
|
|
4199
|
+
const { ctx } = this._processInputParams(input);
|
|
4200
|
+
let data = ctx.data;
|
|
4201
|
+
if (ctx.parsedType === ZodParsedType.undefined) {
|
|
4202
|
+
data = this._def.defaultValue();
|
|
4203
|
+
}
|
|
4204
|
+
return this._def.innerType._parse({
|
|
4205
|
+
data,
|
|
4206
|
+
path: ctx.path,
|
|
4207
|
+
parent: ctx
|
|
4208
|
+
});
|
|
4209
|
+
}
|
|
4210
|
+
removeDefault() {
|
|
4211
|
+
return this._def.innerType;
|
|
4212
|
+
}
|
|
4213
|
+
};
|
|
4214
|
+
ZodDefault.create = (type, params) => {
|
|
4215
|
+
return new ZodDefault({
|
|
4216
|
+
innerType: type,
|
|
4217
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
4218
|
+
defaultValue: typeof params.default === "function" ? params.default : () => params.default,
|
|
4219
|
+
...processCreateParams(params)
|
|
4220
|
+
});
|
|
4221
|
+
};
|
|
4222
|
+
ZodCatch = class extends ZodType {
|
|
4223
|
+
_parse(input) {
|
|
4224
|
+
const { ctx } = this._processInputParams(input);
|
|
4225
|
+
const newCtx = {
|
|
4226
|
+
...ctx,
|
|
4227
|
+
common: {
|
|
4228
|
+
...ctx.common,
|
|
4229
|
+
issues: []
|
|
4230
|
+
}
|
|
445
4231
|
};
|
|
4232
|
+
const result = this._def.innerType._parse({
|
|
4233
|
+
data: newCtx.data,
|
|
4234
|
+
path: newCtx.path,
|
|
4235
|
+
parent: {
|
|
4236
|
+
...newCtx
|
|
4237
|
+
}
|
|
4238
|
+
});
|
|
4239
|
+
if (isAsync(result)) {
|
|
4240
|
+
return result.then((result2) => {
|
|
4241
|
+
return {
|
|
4242
|
+
status: "valid",
|
|
4243
|
+
value: result2.status === "valid" ? result2.value : this._def.catchValue({
|
|
4244
|
+
get error() {
|
|
4245
|
+
return new ZodError(newCtx.common.issues);
|
|
4246
|
+
},
|
|
4247
|
+
input: newCtx.data
|
|
4248
|
+
})
|
|
4249
|
+
};
|
|
4250
|
+
});
|
|
4251
|
+
} else {
|
|
4252
|
+
return {
|
|
4253
|
+
status: "valid",
|
|
4254
|
+
value: result.status === "valid" ? result.value : this._def.catchValue({
|
|
4255
|
+
get error() {
|
|
4256
|
+
return new ZodError(newCtx.common.issues);
|
|
4257
|
+
},
|
|
4258
|
+
input: newCtx.data
|
|
4259
|
+
})
|
|
4260
|
+
};
|
|
4261
|
+
}
|
|
446
4262
|
}
|
|
447
|
-
|
|
448
|
-
return
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
4263
|
+
removeCatch() {
|
|
4264
|
+
return this._def.innerType;
|
|
4265
|
+
}
|
|
4266
|
+
};
|
|
4267
|
+
ZodCatch.create = (type, params) => {
|
|
4268
|
+
return new ZodCatch({
|
|
4269
|
+
innerType: type,
|
|
4270
|
+
typeName: ZodFirstPartyTypeKind.ZodCatch,
|
|
4271
|
+
catchValue: typeof params.catch === "function" ? params.catch : () => params.catch,
|
|
4272
|
+
...processCreateParams(params)
|
|
4273
|
+
});
|
|
4274
|
+
};
|
|
4275
|
+
ZodNaN = class extends ZodType {
|
|
4276
|
+
_parse(input) {
|
|
4277
|
+
const parsedType = this._getType(input);
|
|
4278
|
+
if (parsedType !== ZodParsedType.nan) {
|
|
4279
|
+
const ctx = this._getOrReturnCtx(input);
|
|
4280
|
+
addIssueToContext(ctx, {
|
|
4281
|
+
code: ZodIssueCode.invalid_type,
|
|
4282
|
+
expected: ZodParsedType.nan,
|
|
4283
|
+
received: ctx.parsedType
|
|
4284
|
+
});
|
|
4285
|
+
return INVALID;
|
|
4286
|
+
}
|
|
4287
|
+
return { status: "valid", value: input.data };
|
|
4288
|
+
}
|
|
4289
|
+
};
|
|
4290
|
+
ZodNaN.create = (params) => {
|
|
4291
|
+
return new ZodNaN({
|
|
4292
|
+
typeName: ZodFirstPartyTypeKind.ZodNaN,
|
|
4293
|
+
...processCreateParams(params)
|
|
4294
|
+
});
|
|
4295
|
+
};
|
|
4296
|
+
BRAND = /* @__PURE__ */ Symbol("zod_brand");
|
|
4297
|
+
ZodBranded = class extends ZodType {
|
|
4298
|
+
_parse(input) {
|
|
4299
|
+
const { ctx } = this._processInputParams(input);
|
|
4300
|
+
const data = ctx.data;
|
|
4301
|
+
return this._def.type._parse({
|
|
4302
|
+
data,
|
|
4303
|
+
path: ctx.path,
|
|
4304
|
+
parent: ctx
|
|
4305
|
+
});
|
|
4306
|
+
}
|
|
4307
|
+
unwrap() {
|
|
4308
|
+
return this._def.type;
|
|
4309
|
+
}
|
|
4310
|
+
};
|
|
4311
|
+
ZodPipeline = class _ZodPipeline extends ZodType {
|
|
4312
|
+
_parse(input) {
|
|
4313
|
+
const { status, ctx } = this._processInputParams(input);
|
|
4314
|
+
if (ctx.common.async) {
|
|
4315
|
+
const handleAsync = async () => {
|
|
4316
|
+
const inResult = await this._def.in._parseAsync({
|
|
4317
|
+
data: ctx.data,
|
|
4318
|
+
path: ctx.path,
|
|
4319
|
+
parent: ctx
|
|
4320
|
+
});
|
|
4321
|
+
if (inResult.status === "aborted")
|
|
4322
|
+
return INVALID;
|
|
4323
|
+
if (inResult.status === "dirty") {
|
|
4324
|
+
status.dirty();
|
|
4325
|
+
return DIRTY(inResult.value);
|
|
4326
|
+
} else {
|
|
4327
|
+
return this._def.out._parseAsync({
|
|
4328
|
+
data: inResult.value,
|
|
4329
|
+
path: ctx.path,
|
|
4330
|
+
parent: ctx
|
|
4331
|
+
});
|
|
4332
|
+
}
|
|
4333
|
+
};
|
|
4334
|
+
return handleAsync();
|
|
4335
|
+
} else {
|
|
4336
|
+
const inResult = this._def.in._parseSync({
|
|
4337
|
+
data: ctx.data,
|
|
4338
|
+
path: ctx.path,
|
|
4339
|
+
parent: ctx
|
|
4340
|
+
});
|
|
4341
|
+
if (inResult.status === "aborted")
|
|
4342
|
+
return INVALID;
|
|
4343
|
+
if (inResult.status === "dirty") {
|
|
4344
|
+
status.dirty();
|
|
4345
|
+
return {
|
|
4346
|
+
status: "dirty",
|
|
4347
|
+
value: inResult.value
|
|
4348
|
+
};
|
|
4349
|
+
} else {
|
|
4350
|
+
return this._def.out._parseSync({
|
|
4351
|
+
data: inResult.value,
|
|
4352
|
+
path: ctx.path,
|
|
4353
|
+
parent: ctx
|
|
4354
|
+
});
|
|
4355
|
+
}
|
|
4356
|
+
}
|
|
4357
|
+
}
|
|
4358
|
+
static create(a, b) {
|
|
4359
|
+
return new _ZodPipeline({
|
|
4360
|
+
in: a,
|
|
4361
|
+
out: b,
|
|
4362
|
+
typeName: ZodFirstPartyTypeKind.ZodPipeline
|
|
4363
|
+
});
|
|
4364
|
+
}
|
|
4365
|
+
};
|
|
4366
|
+
ZodReadonly = class extends ZodType {
|
|
4367
|
+
_parse(input) {
|
|
4368
|
+
const result = this._def.innerType._parse(input);
|
|
4369
|
+
const freeze = (data) => {
|
|
4370
|
+
if (isValid(data)) {
|
|
4371
|
+
data.value = Object.freeze(data.value);
|
|
4372
|
+
}
|
|
4373
|
+
return data;
|
|
458
4374
|
};
|
|
4375
|
+
return isAsync(result) ? result.then((data) => freeze(data)) : freeze(result);
|
|
459
4376
|
}
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
this.db.close();
|
|
4377
|
+
unwrap() {
|
|
4378
|
+
return this._def.innerType;
|
|
463
4379
|
}
|
|
464
4380
|
};
|
|
4381
|
+
ZodReadonly.create = (type, params) => {
|
|
4382
|
+
return new ZodReadonly({
|
|
4383
|
+
innerType: type,
|
|
4384
|
+
typeName: ZodFirstPartyTypeKind.ZodReadonly,
|
|
4385
|
+
...processCreateParams(params)
|
|
4386
|
+
});
|
|
4387
|
+
};
|
|
4388
|
+
late = {
|
|
4389
|
+
object: ZodObject.lazycreate
|
|
4390
|
+
};
|
|
4391
|
+
(function(ZodFirstPartyTypeKind2) {
|
|
4392
|
+
ZodFirstPartyTypeKind2["ZodString"] = "ZodString";
|
|
4393
|
+
ZodFirstPartyTypeKind2["ZodNumber"] = "ZodNumber";
|
|
4394
|
+
ZodFirstPartyTypeKind2["ZodNaN"] = "ZodNaN";
|
|
4395
|
+
ZodFirstPartyTypeKind2["ZodBigInt"] = "ZodBigInt";
|
|
4396
|
+
ZodFirstPartyTypeKind2["ZodBoolean"] = "ZodBoolean";
|
|
4397
|
+
ZodFirstPartyTypeKind2["ZodDate"] = "ZodDate";
|
|
4398
|
+
ZodFirstPartyTypeKind2["ZodSymbol"] = "ZodSymbol";
|
|
4399
|
+
ZodFirstPartyTypeKind2["ZodUndefined"] = "ZodUndefined";
|
|
4400
|
+
ZodFirstPartyTypeKind2["ZodNull"] = "ZodNull";
|
|
4401
|
+
ZodFirstPartyTypeKind2["ZodAny"] = "ZodAny";
|
|
4402
|
+
ZodFirstPartyTypeKind2["ZodUnknown"] = "ZodUnknown";
|
|
4403
|
+
ZodFirstPartyTypeKind2["ZodNever"] = "ZodNever";
|
|
4404
|
+
ZodFirstPartyTypeKind2["ZodVoid"] = "ZodVoid";
|
|
4405
|
+
ZodFirstPartyTypeKind2["ZodArray"] = "ZodArray";
|
|
4406
|
+
ZodFirstPartyTypeKind2["ZodObject"] = "ZodObject";
|
|
4407
|
+
ZodFirstPartyTypeKind2["ZodUnion"] = "ZodUnion";
|
|
4408
|
+
ZodFirstPartyTypeKind2["ZodDiscriminatedUnion"] = "ZodDiscriminatedUnion";
|
|
4409
|
+
ZodFirstPartyTypeKind2["ZodIntersection"] = "ZodIntersection";
|
|
4410
|
+
ZodFirstPartyTypeKind2["ZodTuple"] = "ZodTuple";
|
|
4411
|
+
ZodFirstPartyTypeKind2["ZodRecord"] = "ZodRecord";
|
|
4412
|
+
ZodFirstPartyTypeKind2["ZodMap"] = "ZodMap";
|
|
4413
|
+
ZodFirstPartyTypeKind2["ZodSet"] = "ZodSet";
|
|
4414
|
+
ZodFirstPartyTypeKind2["ZodFunction"] = "ZodFunction";
|
|
4415
|
+
ZodFirstPartyTypeKind2["ZodLazy"] = "ZodLazy";
|
|
4416
|
+
ZodFirstPartyTypeKind2["ZodLiteral"] = "ZodLiteral";
|
|
4417
|
+
ZodFirstPartyTypeKind2["ZodEnum"] = "ZodEnum";
|
|
4418
|
+
ZodFirstPartyTypeKind2["ZodEffects"] = "ZodEffects";
|
|
4419
|
+
ZodFirstPartyTypeKind2["ZodNativeEnum"] = "ZodNativeEnum";
|
|
4420
|
+
ZodFirstPartyTypeKind2["ZodOptional"] = "ZodOptional";
|
|
4421
|
+
ZodFirstPartyTypeKind2["ZodNullable"] = "ZodNullable";
|
|
4422
|
+
ZodFirstPartyTypeKind2["ZodDefault"] = "ZodDefault";
|
|
4423
|
+
ZodFirstPartyTypeKind2["ZodCatch"] = "ZodCatch";
|
|
4424
|
+
ZodFirstPartyTypeKind2["ZodPromise"] = "ZodPromise";
|
|
4425
|
+
ZodFirstPartyTypeKind2["ZodBranded"] = "ZodBranded";
|
|
4426
|
+
ZodFirstPartyTypeKind2["ZodPipeline"] = "ZodPipeline";
|
|
4427
|
+
ZodFirstPartyTypeKind2["ZodReadonly"] = "ZodReadonly";
|
|
4428
|
+
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
|
|
4429
|
+
instanceOfType = (cls, params = {
|
|
4430
|
+
message: `Input not instance of ${cls.name}`
|
|
4431
|
+
}) => custom((data) => data instanceof cls, params);
|
|
4432
|
+
stringType = ZodString.create;
|
|
4433
|
+
numberType = ZodNumber.create;
|
|
4434
|
+
nanType = ZodNaN.create;
|
|
4435
|
+
bigIntType = ZodBigInt.create;
|
|
4436
|
+
booleanType = ZodBoolean.create;
|
|
4437
|
+
dateType = ZodDate.create;
|
|
4438
|
+
symbolType = ZodSymbol.create;
|
|
4439
|
+
undefinedType = ZodUndefined.create;
|
|
4440
|
+
nullType = ZodNull.create;
|
|
4441
|
+
anyType = ZodAny.create;
|
|
4442
|
+
unknownType = ZodUnknown.create;
|
|
4443
|
+
neverType = ZodNever.create;
|
|
4444
|
+
voidType = ZodVoid.create;
|
|
4445
|
+
arrayType = ZodArray.create;
|
|
4446
|
+
objectType = ZodObject.create;
|
|
4447
|
+
strictObjectType = ZodObject.strictCreate;
|
|
4448
|
+
unionType = ZodUnion.create;
|
|
4449
|
+
discriminatedUnionType = ZodDiscriminatedUnion.create;
|
|
4450
|
+
intersectionType = ZodIntersection.create;
|
|
4451
|
+
tupleType = ZodTuple.create;
|
|
4452
|
+
recordType = ZodRecord.create;
|
|
4453
|
+
mapType = ZodMap.create;
|
|
4454
|
+
setType = ZodSet.create;
|
|
4455
|
+
functionType = ZodFunction.create;
|
|
4456
|
+
lazyType = ZodLazy.create;
|
|
4457
|
+
literalType = ZodLiteral.create;
|
|
4458
|
+
enumType = ZodEnum.create;
|
|
4459
|
+
nativeEnumType = ZodNativeEnum.create;
|
|
4460
|
+
promiseType = ZodPromise.create;
|
|
4461
|
+
effectsType = ZodEffects.create;
|
|
4462
|
+
optionalType = ZodOptional.create;
|
|
4463
|
+
nullableType = ZodNullable.create;
|
|
4464
|
+
preprocessType = ZodEffects.createWithPreprocess;
|
|
4465
|
+
pipelineType = ZodPipeline.create;
|
|
4466
|
+
ostring = () => stringType().optional();
|
|
4467
|
+
onumber = () => numberType().optional();
|
|
4468
|
+
oboolean = () => booleanType().optional();
|
|
4469
|
+
coerce = {
|
|
4470
|
+
string: ((arg) => ZodString.create({ ...arg, coerce: true })),
|
|
4471
|
+
number: ((arg) => ZodNumber.create({ ...arg, coerce: true })),
|
|
4472
|
+
boolean: ((arg) => ZodBoolean.create({
|
|
4473
|
+
...arg,
|
|
4474
|
+
coerce: true
|
|
4475
|
+
})),
|
|
4476
|
+
bigint: ((arg) => ZodBigInt.create({ ...arg, coerce: true })),
|
|
4477
|
+
date: ((arg) => ZodDate.create({ ...arg, coerce: true }))
|
|
4478
|
+
};
|
|
4479
|
+
NEVER = INVALID;
|
|
4480
|
+
}
|
|
4481
|
+
});
|
|
4482
|
+
|
|
4483
|
+
// node_modules/zod/v3/external.js
|
|
4484
|
+
var external_exports = {};
|
|
4485
|
+
__export(external_exports, {
|
|
4486
|
+
BRAND: () => BRAND,
|
|
4487
|
+
DIRTY: () => DIRTY,
|
|
4488
|
+
EMPTY_PATH: () => EMPTY_PATH,
|
|
4489
|
+
INVALID: () => INVALID,
|
|
4490
|
+
NEVER: () => NEVER,
|
|
4491
|
+
OK: () => OK,
|
|
4492
|
+
ParseStatus: () => ParseStatus,
|
|
4493
|
+
Schema: () => ZodType,
|
|
4494
|
+
ZodAny: () => ZodAny,
|
|
4495
|
+
ZodArray: () => ZodArray,
|
|
4496
|
+
ZodBigInt: () => ZodBigInt,
|
|
4497
|
+
ZodBoolean: () => ZodBoolean,
|
|
4498
|
+
ZodBranded: () => ZodBranded,
|
|
4499
|
+
ZodCatch: () => ZodCatch,
|
|
4500
|
+
ZodDate: () => ZodDate,
|
|
4501
|
+
ZodDefault: () => ZodDefault,
|
|
4502
|
+
ZodDiscriminatedUnion: () => ZodDiscriminatedUnion,
|
|
4503
|
+
ZodEffects: () => ZodEffects,
|
|
4504
|
+
ZodEnum: () => ZodEnum,
|
|
4505
|
+
ZodError: () => ZodError,
|
|
4506
|
+
ZodFirstPartyTypeKind: () => ZodFirstPartyTypeKind,
|
|
4507
|
+
ZodFunction: () => ZodFunction,
|
|
4508
|
+
ZodIntersection: () => ZodIntersection,
|
|
4509
|
+
ZodIssueCode: () => ZodIssueCode,
|
|
4510
|
+
ZodLazy: () => ZodLazy,
|
|
4511
|
+
ZodLiteral: () => ZodLiteral,
|
|
4512
|
+
ZodMap: () => ZodMap,
|
|
4513
|
+
ZodNaN: () => ZodNaN,
|
|
4514
|
+
ZodNativeEnum: () => ZodNativeEnum,
|
|
4515
|
+
ZodNever: () => ZodNever,
|
|
4516
|
+
ZodNull: () => ZodNull,
|
|
4517
|
+
ZodNullable: () => ZodNullable,
|
|
4518
|
+
ZodNumber: () => ZodNumber,
|
|
4519
|
+
ZodObject: () => ZodObject,
|
|
4520
|
+
ZodOptional: () => ZodOptional,
|
|
4521
|
+
ZodParsedType: () => ZodParsedType,
|
|
4522
|
+
ZodPipeline: () => ZodPipeline,
|
|
4523
|
+
ZodPromise: () => ZodPromise,
|
|
4524
|
+
ZodReadonly: () => ZodReadonly,
|
|
4525
|
+
ZodRecord: () => ZodRecord,
|
|
4526
|
+
ZodSchema: () => ZodType,
|
|
4527
|
+
ZodSet: () => ZodSet,
|
|
4528
|
+
ZodString: () => ZodString,
|
|
4529
|
+
ZodSymbol: () => ZodSymbol,
|
|
4530
|
+
ZodTransformer: () => ZodEffects,
|
|
4531
|
+
ZodTuple: () => ZodTuple,
|
|
4532
|
+
ZodType: () => ZodType,
|
|
4533
|
+
ZodUndefined: () => ZodUndefined,
|
|
4534
|
+
ZodUnion: () => ZodUnion,
|
|
4535
|
+
ZodUnknown: () => ZodUnknown,
|
|
4536
|
+
ZodVoid: () => ZodVoid,
|
|
4537
|
+
addIssueToContext: () => addIssueToContext,
|
|
4538
|
+
any: () => anyType,
|
|
4539
|
+
array: () => arrayType,
|
|
4540
|
+
bigint: () => bigIntType,
|
|
4541
|
+
boolean: () => booleanType,
|
|
4542
|
+
coerce: () => coerce,
|
|
4543
|
+
custom: () => custom,
|
|
4544
|
+
date: () => dateType,
|
|
4545
|
+
datetimeRegex: () => datetimeRegex,
|
|
4546
|
+
defaultErrorMap: () => en_default,
|
|
4547
|
+
discriminatedUnion: () => discriminatedUnionType,
|
|
4548
|
+
effect: () => effectsType,
|
|
4549
|
+
enum: () => enumType,
|
|
4550
|
+
function: () => functionType,
|
|
4551
|
+
getErrorMap: () => getErrorMap,
|
|
4552
|
+
getParsedType: () => getParsedType,
|
|
4553
|
+
instanceof: () => instanceOfType,
|
|
4554
|
+
intersection: () => intersectionType,
|
|
4555
|
+
isAborted: () => isAborted,
|
|
4556
|
+
isAsync: () => isAsync,
|
|
4557
|
+
isDirty: () => isDirty,
|
|
4558
|
+
isValid: () => isValid,
|
|
4559
|
+
late: () => late,
|
|
4560
|
+
lazy: () => lazyType,
|
|
4561
|
+
literal: () => literalType,
|
|
4562
|
+
makeIssue: () => makeIssue,
|
|
4563
|
+
map: () => mapType,
|
|
4564
|
+
nan: () => nanType,
|
|
4565
|
+
nativeEnum: () => nativeEnumType,
|
|
4566
|
+
never: () => neverType,
|
|
4567
|
+
null: () => nullType,
|
|
4568
|
+
nullable: () => nullableType,
|
|
4569
|
+
number: () => numberType,
|
|
4570
|
+
object: () => objectType,
|
|
4571
|
+
objectUtil: () => objectUtil,
|
|
4572
|
+
oboolean: () => oboolean,
|
|
4573
|
+
onumber: () => onumber,
|
|
4574
|
+
optional: () => optionalType,
|
|
4575
|
+
ostring: () => ostring,
|
|
4576
|
+
pipeline: () => pipelineType,
|
|
4577
|
+
preprocess: () => preprocessType,
|
|
4578
|
+
promise: () => promiseType,
|
|
4579
|
+
quotelessJson: () => quotelessJson,
|
|
4580
|
+
record: () => recordType,
|
|
4581
|
+
set: () => setType,
|
|
4582
|
+
setErrorMap: () => setErrorMap,
|
|
4583
|
+
strictObject: () => strictObjectType,
|
|
4584
|
+
string: () => stringType,
|
|
4585
|
+
symbol: () => symbolType,
|
|
4586
|
+
transformer: () => effectsType,
|
|
4587
|
+
tuple: () => tupleType,
|
|
4588
|
+
undefined: () => undefinedType,
|
|
4589
|
+
union: () => unionType,
|
|
4590
|
+
unknown: () => unknownType,
|
|
4591
|
+
util: () => util,
|
|
4592
|
+
void: () => voidType
|
|
4593
|
+
});
|
|
4594
|
+
var init_external = __esm({
|
|
4595
|
+
"node_modules/zod/v3/external.js"() {
|
|
4596
|
+
init_errors();
|
|
4597
|
+
init_parseUtil();
|
|
4598
|
+
init_typeAliases();
|
|
4599
|
+
init_util();
|
|
4600
|
+
init_types();
|
|
4601
|
+
init_ZodError();
|
|
4602
|
+
}
|
|
4603
|
+
});
|
|
4604
|
+
|
|
4605
|
+
// node_modules/zod/index.js
|
|
4606
|
+
var init_zod = __esm({
|
|
4607
|
+
"node_modules/zod/index.js"() {
|
|
4608
|
+
init_external();
|
|
4609
|
+
init_external();
|
|
465
4610
|
}
|
|
466
4611
|
});
|
|
467
4612
|
|
|
468
4613
|
// src/config/schema.js
|
|
469
|
-
import { z } from "zod";
|
|
470
4614
|
var DomainSchema, SyncSchema, ConfigSchema;
|
|
471
4615
|
var init_schema2 = __esm({
|
|
472
4616
|
"src/config/schema.js"() {
|
|
473
4617
|
"use strict";
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
4618
|
+
init_zod();
|
|
4619
|
+
DomainSchema = external_exports.object({
|
|
4620
|
+
name: external_exports.string(),
|
|
4621
|
+
entityHints: external_exports.array(external_exports.string()).default([]),
|
|
4622
|
+
relationHints: external_exports.array(external_exports.string()).default([])
|
|
478
4623
|
});
|
|
479
|
-
SyncSchema =
|
|
480
|
-
memoryMd:
|
|
481
|
-
neuralMemory:
|
|
482
|
-
importOnStart:
|
|
4624
|
+
SyncSchema = external_exports.object({
|
|
4625
|
+
memoryMd: external_exports.string().nullable().default(null),
|
|
4626
|
+
neuralMemory: external_exports.string().nullable().default(null),
|
|
4627
|
+
importOnStart: external_exports.boolean().default(false)
|
|
483
4628
|
});
|
|
484
|
-
ConfigSchema =
|
|
485
|
-
storage:
|
|
486
|
-
path:
|
|
487
|
-
maxSizeMb:
|
|
4629
|
+
ConfigSchema = external_exports.object({
|
|
4630
|
+
storage: external_exports.object({
|
|
4631
|
+
path: external_exports.string().default("./memory-graph.db"),
|
|
4632
|
+
maxSizeMb: external_exports.number().default(500)
|
|
488
4633
|
}).default({}),
|
|
489
|
-
extraction:
|
|
490
|
-
provider:
|
|
491
|
-
model:
|
|
492
|
-
autoExtract:
|
|
493
|
-
minConfidence:
|
|
494
|
-
batchSize:
|
|
4634
|
+
extraction: external_exports.object({
|
|
4635
|
+
provider: external_exports.enum(["auto", "openai", "anthropic", "ollama"]).default("auto"),
|
|
4636
|
+
model: external_exports.string().default("auto"),
|
|
4637
|
+
autoExtract: external_exports.boolean().default(true),
|
|
4638
|
+
minConfidence: external_exports.number().min(0).max(1).default(0.7),
|
|
4639
|
+
batchSize: external_exports.number().default(5)
|
|
495
4640
|
}).default({}),
|
|
496
|
-
domains:
|
|
497
|
-
deduplication:
|
|
498
|
-
enabled:
|
|
499
|
-
similarityThreshold:
|
|
4641
|
+
domains: external_exports.array(DomainSchema).default([]),
|
|
4642
|
+
deduplication: external_exports.object({
|
|
4643
|
+
enabled: external_exports.boolean().default(true),
|
|
4644
|
+
similarityThreshold: external_exports.number().min(0).max(1).default(0.85)
|
|
500
4645
|
}).default({}),
|
|
501
4646
|
sync: SyncSchema.default({}),
|
|
502
|
-
query:
|
|
503
|
-
maxHops:
|
|
504
|
-
maxResults:
|
|
505
|
-
includeConfidence:
|
|
4647
|
+
query: external_exports.object({
|
|
4648
|
+
maxHops: external_exports.number().default(5),
|
|
4649
|
+
maxResults: external_exports.number().default(50),
|
|
4650
|
+
includeConfidence: external_exports.boolean().default(true)
|
|
506
4651
|
}).default({})
|
|
507
4652
|
});
|
|
508
4653
|
}
|
|
@@ -670,7 +4815,8 @@ async function extractFromText(text, config) {
|
|
|
670
4815
|
to: String(r.to).trim(),
|
|
671
4816
|
fromType: r.fromType?.trim(),
|
|
672
4817
|
toType: r.toType?.trim(),
|
|
673
|
-
confidence: Math.min(1, Math.max(0, Number(r.confidence) || 0.8))
|
|
4818
|
+
confidence: Math.min(1, Math.max(0, Number(r.confidence) || 0.8)),
|
|
4819
|
+
when: r.when ? String(r.when).trim() : void 0
|
|
674
4820
|
}));
|
|
675
4821
|
return { entities, relationships };
|
|
676
4822
|
} catch (err) {
|
|
@@ -1048,7 +5194,7 @@ async function queryAboutEntity(engine, entityName, config) {
|
|
|
1048
5194
|
const outgoing = engine.getRelationsFrom(entity.id);
|
|
1049
5195
|
const incoming = engine.getRelationsTo(entity.id);
|
|
1050
5196
|
const lines = [
|
|
1051
|
-
`${entity.name} (${entity.type})`
|
|
5197
|
+
`${entity.name} (${entity.type})${entity.mention_count && entity.mention_count > 1 ? ` [mentions: ${entity.mention_count}]` : ""}`
|
|
1052
5198
|
];
|
|
1053
5199
|
if (Object.keys(entity.properties).length > 0) {
|
|
1054
5200
|
lines.push(`Properties: ${JSON.stringify(entity.properties)}`);
|
|
@@ -1320,14 +5466,25 @@ function autoDedup(engine) {
|
|
|
1320
5466
|
return mergeCount;
|
|
1321
5467
|
}
|
|
1322
5468
|
function nameSimilarity(a, b) {
|
|
1323
|
-
const na = a.toLowerCase().trim();
|
|
1324
|
-
const nb = b.toLowerCase().trim();
|
|
5469
|
+
const na = stripTitles(a.toLowerCase().trim());
|
|
5470
|
+
const nb = stripTitles(b.toLowerCase().trim());
|
|
1325
5471
|
if (na === nb) return 1;
|
|
1326
5472
|
if (na.includes(nb) || nb.includes(na)) return 0.9;
|
|
5473
|
+
const rawA = a.toLowerCase().trim();
|
|
5474
|
+
const rawB = b.toLowerCase().trim();
|
|
5475
|
+
if (rawA !== na || rawB !== nb) {
|
|
5476
|
+
if (na === nb) return 1;
|
|
5477
|
+
if (na.includes(nb) || nb.includes(na)) return 0.92;
|
|
5478
|
+
}
|
|
1327
5479
|
const dist = levenshtein(na, nb);
|
|
1328
5480
|
const maxLen = Math.max(na.length, nb.length);
|
|
1329
5481
|
return maxLen === 0 ? 1 : 1 - dist / maxLen;
|
|
1330
5482
|
}
|
|
5483
|
+
function stripTitles(name) {
|
|
5484
|
+
const prefixes = /^(dr\.?|prof\.?|professor|mr\.?|mrs\.?|ms\.?|sir|lord|sếp|anh|chị|em)\s+/i;
|
|
5485
|
+
const suffixes = /\s+(inc\.?|corp\.?|ltd\.?|llc|co\.?|company|corporation|motors|labs|laboratory|laboratories|university|univ\.?)$/i;
|
|
5486
|
+
return name.replace(prefixes, "").replace(suffixes, "").trim();
|
|
5487
|
+
}
|
|
1331
5488
|
function levenshtein(a, b) {
|
|
1332
5489
|
const m = a.length;
|
|
1333
5490
|
const n = b.length;
|
|
@@ -1525,7 +5682,7 @@ var init_src = __esm({
|
|
|
1525
5682
|
}
|
|
1526
5683
|
});
|
|
1527
5684
|
|
|
1528
|
-
// plugin/entry.
|
|
5685
|
+
// plugin/entry.js
|
|
1529
5686
|
import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry";
|
|
1530
5687
|
import { resolve as resolve3 } from "node:path";
|
|
1531
5688
|
import { homedir } from "node:os";
|
|
@@ -1580,6 +5737,9 @@ var messageBuffer = [];
|
|
|
1580
5737
|
var batchTimer = null;
|
|
1581
5738
|
var BATCH_WINDOW_MS = 15e3;
|
|
1582
5739
|
var BATCH_MAX_MESSAGES = 5;
|
|
5740
|
+
var sessionMessages = /* @__PURE__ */ new Map();
|
|
5741
|
+
var SESSION_SUMMARY_MIN_MESSAGES = 5;
|
|
5742
|
+
var SESSION_SUMMARY_MAX_BUFFER = 50;
|
|
1583
5743
|
async function flushBatch(config) {
|
|
1584
5744
|
if (messageBuffer.length === 0) return;
|
|
1585
5745
|
const batch = messageBuffer.splice(0, messageBuffer.length);
|
|
@@ -1621,10 +5781,17 @@ var entry_default = definePluginEntry({
|
|
|
1621
5781
|
if (config?.autoIngest === false) return;
|
|
1622
5782
|
const text = typeof event.content === "string" ? event.content : event.content?.text || event.content?.body || "";
|
|
1623
5783
|
if (!text || !shouldIngest(text)) return;
|
|
5784
|
+
const sessionKey = event.sessionKey || "";
|
|
5785
|
+
if (sessionKey) {
|
|
5786
|
+
if (!sessionMessages.has(sessionKey)) sessionMessages.set(sessionKey, []);
|
|
5787
|
+
const msgs = sessionMessages.get(sessionKey);
|
|
5788
|
+
msgs.push({ text, role: "user", timestamp: Date.now() });
|
|
5789
|
+
if (msgs.length > SESSION_SUMMARY_MAX_BUFFER) msgs.shift();
|
|
5790
|
+
}
|
|
1624
5791
|
messageBuffer.push({
|
|
1625
5792
|
text,
|
|
1626
5793
|
senderId: event.senderId || "unknown",
|
|
1627
|
-
sessionKey
|
|
5794
|
+
sessionKey,
|
|
1628
5795
|
timestamp: Date.now()
|
|
1629
5796
|
});
|
|
1630
5797
|
if (messageBuffer.length >= BATCH_MAX_MESSAGES) {
|
|
@@ -1645,7 +5812,83 @@ var entry_default = definePluginEntry({
|
|
|
1645
5812
|
},
|
|
1646
5813
|
{ priority: 10 }
|
|
1647
5814
|
);
|
|
5815
|
+
api.on(
|
|
5816
|
+
"message_sent",
|
|
5817
|
+
async (event) => {
|
|
5818
|
+
const config = event.context?.pluginConfig;
|
|
5819
|
+
if (config?.sessionSummary === false) return;
|
|
5820
|
+
const text = typeof event.content === "string" ? event.content : event.content?.text || "";
|
|
5821
|
+
const sessionKey = event.sessionKey || "";
|
|
5822
|
+
if (!text || !sessionKey || text.length < MIN_MEANINGFUL_LENGTH) return;
|
|
5823
|
+
if (!sessionMessages.has(sessionKey)) sessionMessages.set(sessionKey, []);
|
|
5824
|
+
const msgs = sessionMessages.get(sessionKey);
|
|
5825
|
+
msgs.push({ text: text.slice(0, 500), role: "assistant", timestamp: Date.now() });
|
|
5826
|
+
if (msgs.length > SESSION_SUMMARY_MAX_BUFFER) msgs.shift();
|
|
5827
|
+
},
|
|
5828
|
+
{ priority: 10 }
|
|
5829
|
+
);
|
|
5830
|
+
api.on(
|
|
5831
|
+
"session_end",
|
|
5832
|
+
async (event, ctx) => {
|
|
5833
|
+
const config = ctx?.pluginConfig;
|
|
5834
|
+
if (config?.sessionSummary === false) return;
|
|
5835
|
+
const sessionKey = event.sessionKey || event.sessionId || "";
|
|
5836
|
+
const msgs = sessionMessages.get(sessionKey);
|
|
5837
|
+
sessionMessages.delete(sessionKey);
|
|
5838
|
+
if (!msgs || msgs.length < SESSION_SUMMARY_MIN_MESSAGES) return;
|
|
5839
|
+
try {
|
|
5840
|
+
const graph = await getGraph(config);
|
|
5841
|
+
const transcript = msgs.map((m) => `[${m.role}]: ${m.text}`).join("\n").slice(0, 3e3);
|
|
5842
|
+
const { OpenAI } = await import("openai");
|
|
5843
|
+
const client = new OpenAI({
|
|
5844
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
5845
|
+
baseURL: process.env.OPENAI_BASE_URL
|
|
5846
|
+
});
|
|
5847
|
+
const summaryResponse = await client.chat.completions.create({
|
|
5848
|
+
model: config?.extractionModel || process.env.MEMORY_GRAPH_MODEL || "kr/claude-haiku-4.5",
|
|
5849
|
+
messages: [
|
|
5850
|
+
{
|
|
5851
|
+
role: "system",
|
|
5852
|
+
content: `You are a session summarizer for a knowledge graph. Given a conversation transcript, extract a 2-4 sentence summary of KEY ACTIONS taken (what was built, fixed, published, decided, configured). Focus on concrete outcomes, versions, platforms, and decisions. Skip casual chat. If nothing meaningful happened, respond with "NO_SUMMARY". Output plain text only.`
|
|
5853
|
+
},
|
|
5854
|
+
{
|
|
5855
|
+
role: "user",
|
|
5856
|
+
content: transcript
|
|
5857
|
+
}
|
|
5858
|
+
],
|
|
5859
|
+
max_tokens: 300,
|
|
5860
|
+
temperature: 0.3
|
|
5861
|
+
});
|
|
5862
|
+
const summary = summaryResponse.choices?.[0]?.message?.content?.trim();
|
|
5863
|
+
if (!summary || summary === "NO_SUMMARY" || summary.length < 20) return;
|
|
5864
|
+
const today = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
|
|
5865
|
+
await graph.ingest(summary, {
|
|
5866
|
+
source: `session-summary-${today}`,
|
|
5867
|
+
sessionId: sessionKey
|
|
5868
|
+
});
|
|
5869
|
+
console.log(`[memory-graph] Session summary ingested (${msgs.length} messages \u2192 ${summary.length} chars)`);
|
|
5870
|
+
} catch (err) {
|
|
5871
|
+
console.warn("[memory-graph] Session summary ingestion failed:", err.message);
|
|
5872
|
+
}
|
|
5873
|
+
},
|
|
5874
|
+
{ priority: 10 }
|
|
5875
|
+
);
|
|
1648
5876
|
api.on("gateway_stop", async () => {
|
|
5877
|
+
for (const [key, msgs] of sessionMessages.entries()) {
|
|
5878
|
+
if (msgs.length >= SESSION_SUMMARY_MIN_MESSAGES) {
|
|
5879
|
+
try {
|
|
5880
|
+
const graph = await getGraph(null);
|
|
5881
|
+
const transcript = msgs.map((m) => `[${m.role}]: ${m.text}`).join("\n").slice(0, 3e3);
|
|
5882
|
+
const today = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
|
|
5883
|
+
await graph.ingest(`Session ended during shutdown. Messages: ${transcript.slice(0, 500)}`, {
|
|
5884
|
+
source: `session-summary-${today}-shutdown`,
|
|
5885
|
+
sessionId: key
|
|
5886
|
+
});
|
|
5887
|
+
} catch (_) {
|
|
5888
|
+
}
|
|
5889
|
+
}
|
|
5890
|
+
}
|
|
5891
|
+
sessionMessages.clear();
|
|
1649
5892
|
if (graphInstance) {
|
|
1650
5893
|
graphInstance.close();
|
|
1651
5894
|
graphInstance = null;
|