@hasna/brains 0.0.9 → 0.0.10
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/cli/index.js +1055 -632
- package/dist/db/index.d.ts.map +1 -1
- package/dist/index.js +4325 -127
- package/dist/lib/config.d.ts +11 -0
- package/dist/lib/config.d.ts.map +1 -0
- package/dist/lib/gatherers/assistants.d.ts +3 -0
- package/dist/lib/gatherers/assistants.d.ts.map +1 -0
- package/dist/lib/gatherers/economy.d.ts +3 -0
- package/dist/lib/gatherers/economy.d.ts.map +1 -0
- package/dist/lib/gatherers/index.d.ts +3 -0
- package/dist/lib/gatherers/index.d.ts.map +1 -1
- package/dist/lib/gatherers/protocol.d.ts +29 -0
- package/dist/lib/gatherers/protocol.d.ts.map +1 -0
- package/dist/lib/gatherers/recordings.d.ts +3 -0
- package/dist/lib/gatherers/recordings.d.ts.map +1 -0
- package/dist/lib/gatherers/registry.d.ts +7 -0
- package/dist/lib/gatherers/registry.d.ts.map +1 -0
- package/dist/lib/gatherers/researcher.d.ts +3 -0
- package/dist/lib/gatherers/researcher.d.ts.map +1 -0
- package/dist/lib/gatherers/styles.d.ts +3 -0
- package/dist/lib/gatherers/styles.d.ts.map +1 -0
- package/dist/lib/gatherers/tickets.d.ts +3 -0
- package/dist/lib/gatherers/tickets.d.ts.map +1 -0
- package/dist/lib/index.d.ts +3 -0
- package/dist/lib/index.d.ts.map +1 -1
- package/dist/lib/providers/openai.d.ts +2 -0
- package/dist/lib/providers/openai.d.ts.map +1 -1
- package/dist/lib/providers/thinker-labs.d.ts +1 -0
- package/dist/lib/providers/thinker-labs.d.ts.map +1 -1
- package/dist/lib/retry.d.ts +7 -0
- package/dist/lib/retry.d.ts.map +1 -0
- package/dist/lib/schemas.d.ts +87 -0
- package/dist/lib/schemas.d.ts.map +1 -0
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +4619 -199
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +4648 -9
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3650,10 +3650,48 @@ function drizzle(...params) {
|
|
|
3650
3650
|
drizzle2.mock = mock;
|
|
3651
3651
|
})(drizzle || (drizzle = {}));
|
|
3652
3652
|
|
|
3653
|
+
// node_modules/drizzle-orm/migrator.js
|
|
3654
|
+
import crypto from "crypto";
|
|
3655
|
+
import fs from "fs";
|
|
3656
|
+
function readMigrationFiles(config) {
|
|
3657
|
+
const migrationFolderTo = config.migrationsFolder;
|
|
3658
|
+
const migrationQueries = [];
|
|
3659
|
+
const journalPath = `${migrationFolderTo}/meta/_journal.json`;
|
|
3660
|
+
if (!fs.existsSync(journalPath)) {
|
|
3661
|
+
throw new Error(`Can't find meta/_journal.json file`);
|
|
3662
|
+
}
|
|
3663
|
+
const journalAsString = fs.readFileSync(`${migrationFolderTo}/meta/_journal.json`).toString();
|
|
3664
|
+
const journal = JSON.parse(journalAsString);
|
|
3665
|
+
for (const journalEntry of journal.entries) {
|
|
3666
|
+
const migrationPath = `${migrationFolderTo}/${journalEntry.tag}.sql`;
|
|
3667
|
+
try {
|
|
3668
|
+
const query = fs.readFileSync(`${migrationFolderTo}/${journalEntry.tag}.sql`).toString();
|
|
3669
|
+
const result = query.split("--> statement-breakpoint").map((it) => {
|
|
3670
|
+
return it;
|
|
3671
|
+
});
|
|
3672
|
+
migrationQueries.push({
|
|
3673
|
+
sql: result,
|
|
3674
|
+
bps: journalEntry.breakpoints,
|
|
3675
|
+
folderMillis: journalEntry.when,
|
|
3676
|
+
hash: crypto.createHash("sha256").update(query).digest("hex")
|
|
3677
|
+
});
|
|
3678
|
+
} catch {
|
|
3679
|
+
throw new Error(`No file ${migrationPath} found in ${migrationFolderTo} folder`);
|
|
3680
|
+
}
|
|
3681
|
+
}
|
|
3682
|
+
return migrationQueries;
|
|
3683
|
+
}
|
|
3684
|
+
|
|
3685
|
+
// node_modules/drizzle-orm/bun-sqlite/migrator.js
|
|
3686
|
+
function migrate(db, config) {
|
|
3687
|
+
const migrations = readMigrationFiles(config);
|
|
3688
|
+
db.dialect.migrate(migrations, db.session, config);
|
|
3689
|
+
}
|
|
3690
|
+
|
|
3653
3691
|
// src/db/index.ts
|
|
3654
3692
|
import { Database as Database2 } from "bun:sqlite";
|
|
3655
|
-
import { mkdirSync } from "fs";
|
|
3656
|
-
import { dirname, join } from "path";
|
|
3693
|
+
import { mkdirSync, existsSync, readdirSync, copyFileSync, statSync } from "fs";
|
|
3694
|
+
import { dirname, join, resolve } from "path";
|
|
3657
3695
|
import { homedir } from "os";
|
|
3658
3696
|
|
|
3659
3697
|
// src/db/schema.ts
|
|
@@ -3701,56 +3739,80 @@ var trainingDatasets = sqliteTable("training_datasets", {
|
|
|
3701
3739
|
});
|
|
3702
3740
|
|
|
3703
3741
|
// src/db/index.ts
|
|
3704
|
-
|
|
3742
|
+
function resolveDefaultDbPath() {
|
|
3743
|
+
const home = process.env["HOME"] || process.env["USERPROFILE"] || homedir();
|
|
3744
|
+
const newDir = join(home, ".hasna", "brains");
|
|
3745
|
+
const oldDir = join(home, ".brains");
|
|
3746
|
+
if (existsSync(oldDir) && !existsSync(newDir)) {
|
|
3747
|
+
mkdirSync(newDir, { recursive: true });
|
|
3748
|
+
try {
|
|
3749
|
+
for (const file of readdirSync(oldDir)) {
|
|
3750
|
+
const oldPath = join(oldDir, file);
|
|
3751
|
+
const newPath = join(newDir, file);
|
|
3752
|
+
try {
|
|
3753
|
+
if (statSync(oldPath).isFile()) {
|
|
3754
|
+
copyFileSync(oldPath, newPath);
|
|
3755
|
+
}
|
|
3756
|
+
} catch {}
|
|
3757
|
+
}
|
|
3758
|
+
} catch {}
|
|
3759
|
+
}
|
|
3760
|
+
mkdirSync(newDir, { recursive: true });
|
|
3761
|
+
return join(newDir, "brains.db");
|
|
3762
|
+
}
|
|
3763
|
+
var DEFAULT_DB_PATH = resolveDefaultDbPath();
|
|
3705
3764
|
function ensureDir(filePath) {
|
|
3706
3765
|
mkdirSync(dirname(filePath), { recursive: true });
|
|
3707
3766
|
}
|
|
3708
|
-
function createTables(sqlite) {
|
|
3709
|
-
sqlite.exec(`
|
|
3710
|
-
CREATE TABLE IF NOT EXISTS fine_tuned_models (
|
|
3711
|
-
id TEXT PRIMARY KEY,
|
|
3712
|
-
base_model TEXT NOT NULL,
|
|
3713
|
-
name TEXT NOT NULL,
|
|
3714
|
-
provider TEXT NOT NULL,
|
|
3715
|
-
status TEXT NOT NULL DEFAULT 'pending',
|
|
3716
|
-
fine_tune_job_id TEXT,
|
|
3717
|
-
display_name TEXT,
|
|
3718
|
-
description TEXT,
|
|
3719
|
-
collection TEXT,
|
|
3720
|
-
tags TEXT,
|
|
3721
|
-
created_at INTEGER NOT NULL,
|
|
3722
|
-
updated_at INTEGER NOT NULL
|
|
3723
|
-
);
|
|
3724
|
-
|
|
3725
|
-
CREATE TABLE IF NOT EXISTS training_jobs (
|
|
3726
|
-
id TEXT PRIMARY KEY,
|
|
3727
|
-
model_id TEXT NOT NULL REFERENCES fine_tuned_models(id),
|
|
3728
|
-
provider TEXT NOT NULL,
|
|
3729
|
-
status TEXT NOT NULL,
|
|
3730
|
-
started_at INTEGER NOT NULL,
|
|
3731
|
-
finished_at INTEGER,
|
|
3732
|
-
metrics TEXT,
|
|
3733
|
-
error TEXT
|
|
3734
|
-
);
|
|
3735
|
-
|
|
3736
|
-
CREATE TABLE IF NOT EXISTS training_datasets (
|
|
3737
|
-
id TEXT PRIMARY KEY,
|
|
3738
|
-
source TEXT NOT NULL,
|
|
3739
|
-
file_path TEXT NOT NULL,
|
|
3740
|
-
example_count INTEGER NOT NULL,
|
|
3741
|
-
created_at INTEGER NOT NULL,
|
|
3742
|
-
used_in_job_id TEXT REFERENCES training_jobs(id)
|
|
3743
|
-
);
|
|
3744
|
-
`);
|
|
3745
|
-
}
|
|
3746
3767
|
function getDb(dbPath) {
|
|
3747
3768
|
const resolvedPath = dbPath ?? DEFAULT_DB_PATH;
|
|
3748
3769
|
ensureDir(resolvedPath);
|
|
3749
3770
|
const sqlite = new Database2(resolvedPath);
|
|
3750
3771
|
sqlite.run("PRAGMA journal_mode = WAL");
|
|
3751
3772
|
sqlite.run("PRAGMA foreign_keys = ON");
|
|
3752
|
-
|
|
3753
|
-
|
|
3773
|
+
const db = drizzle(sqlite, { schema: exports_schema });
|
|
3774
|
+
try {
|
|
3775
|
+
const migrationsFolder = resolve(import.meta.dir, "../../drizzle");
|
|
3776
|
+
migrate(db, { migrationsFolder });
|
|
3777
|
+
} catch {
|
|
3778
|
+
sqlite.exec(`
|
|
3779
|
+
CREATE TABLE IF NOT EXISTS fine_tuned_models (
|
|
3780
|
+
id TEXT PRIMARY KEY,
|
|
3781
|
+
base_model TEXT NOT NULL,
|
|
3782
|
+
name TEXT NOT NULL,
|
|
3783
|
+
provider TEXT NOT NULL,
|
|
3784
|
+
status TEXT NOT NULL DEFAULT 'pending',
|
|
3785
|
+
fine_tune_job_id TEXT,
|
|
3786
|
+
display_name TEXT,
|
|
3787
|
+
description TEXT,
|
|
3788
|
+
collection TEXT,
|
|
3789
|
+
tags TEXT,
|
|
3790
|
+
created_at INTEGER NOT NULL,
|
|
3791
|
+
updated_at INTEGER NOT NULL
|
|
3792
|
+
);
|
|
3793
|
+
|
|
3794
|
+
CREATE TABLE IF NOT EXISTS training_jobs (
|
|
3795
|
+
id TEXT PRIMARY KEY,
|
|
3796
|
+
model_id TEXT NOT NULL REFERENCES fine_tuned_models(id),
|
|
3797
|
+
provider TEXT NOT NULL,
|
|
3798
|
+
status TEXT NOT NULL,
|
|
3799
|
+
started_at INTEGER NOT NULL,
|
|
3800
|
+
finished_at INTEGER,
|
|
3801
|
+
metrics TEXT,
|
|
3802
|
+
error TEXT
|
|
3803
|
+
);
|
|
3804
|
+
|
|
3805
|
+
CREATE TABLE IF NOT EXISTS training_datasets (
|
|
3806
|
+
id TEXT PRIMARY KEY,
|
|
3807
|
+
source TEXT NOT NULL,
|
|
3808
|
+
file_path TEXT NOT NULL,
|
|
3809
|
+
example_count INTEGER NOT NULL,
|
|
3810
|
+
created_at INTEGER NOT NULL,
|
|
3811
|
+
used_in_job_id TEXT REFERENCES training_jobs(id)
|
|
3812
|
+
);
|
|
3813
|
+
`);
|
|
3814
|
+
}
|
|
3815
|
+
return db;
|
|
3754
3816
|
}
|
|
3755
3817
|
// node_modules/openai/internal/qs/formats.mjs
|
|
3756
3818
|
var default_format = "RFC3986";
|
|
@@ -4840,8 +4902,8 @@ function _addRequestID(value, response) {
|
|
|
4840
4902
|
|
|
4841
4903
|
class APIPromise extends Promise {
|
|
4842
4904
|
constructor(responsePromise, parseResponse = defaultParseResponse) {
|
|
4843
|
-
super((
|
|
4844
|
-
|
|
4905
|
+
super((resolve2) => {
|
|
4906
|
+
resolve2(null);
|
|
4845
4907
|
});
|
|
4846
4908
|
this.responsePromise = responsePromise;
|
|
4847
4909
|
this.parseResponse = parseResponse;
|
|
@@ -5352,7 +5414,7 @@ var startsWithSchemeRegexp = /^[a-z][a-z0-9+.-]*:/i;
|
|
|
5352
5414
|
var isAbsoluteURL = (url) => {
|
|
5353
5415
|
return startsWithSchemeRegexp.test(url);
|
|
5354
5416
|
};
|
|
5355
|
-
var sleep = (ms) => new Promise((
|
|
5417
|
+
var sleep = (ms) => new Promise((resolve2) => setTimeout(resolve2, ms));
|
|
5356
5418
|
var validatePositiveInteger = (name, n) => {
|
|
5357
5419
|
if (typeof n !== "number" || !Number.isInteger(n)) {
|
|
5358
5420
|
throw new OpenAIError(`${name} must be an integer`);
|
|
@@ -5717,12 +5779,12 @@ class EventStream {
|
|
|
5717
5779
|
_EventStream_errored.set(this, false);
|
|
5718
5780
|
_EventStream_aborted.set(this, false);
|
|
5719
5781
|
_EventStream_catchingPromiseCreated.set(this, false);
|
|
5720
|
-
__classPrivateFieldSet3(this, _EventStream_connectedPromise, new Promise((
|
|
5721
|
-
__classPrivateFieldSet3(this, _EventStream_resolveConnectedPromise,
|
|
5782
|
+
__classPrivateFieldSet3(this, _EventStream_connectedPromise, new Promise((resolve2, reject) => {
|
|
5783
|
+
__classPrivateFieldSet3(this, _EventStream_resolveConnectedPromise, resolve2, "f");
|
|
5722
5784
|
__classPrivateFieldSet3(this, _EventStream_rejectConnectedPromise, reject, "f");
|
|
5723
5785
|
}), "f");
|
|
5724
|
-
__classPrivateFieldSet3(this, _EventStream_endPromise, new Promise((
|
|
5725
|
-
__classPrivateFieldSet3(this, _EventStream_resolveEndPromise,
|
|
5786
|
+
__classPrivateFieldSet3(this, _EventStream_endPromise, new Promise((resolve2, reject) => {
|
|
5787
|
+
__classPrivateFieldSet3(this, _EventStream_resolveEndPromise, resolve2, "f");
|
|
5726
5788
|
__classPrivateFieldSet3(this, _EventStream_rejectEndPromise, reject, "f");
|
|
5727
5789
|
}), "f");
|
|
5728
5790
|
__classPrivateFieldGet3(this, _EventStream_connectedPromise, "f").catch(() => {});
|
|
@@ -5774,11 +5836,11 @@ class EventStream {
|
|
|
5774
5836
|
return this;
|
|
5775
5837
|
}
|
|
5776
5838
|
emitted(event) {
|
|
5777
|
-
return new Promise((
|
|
5839
|
+
return new Promise((resolve2, reject) => {
|
|
5778
5840
|
__classPrivateFieldSet3(this, _EventStream_catchingPromiseCreated, true, "f");
|
|
5779
5841
|
if (event !== "error")
|
|
5780
5842
|
this.once("error", reject);
|
|
5781
|
-
this.once(event,
|
|
5843
|
+
this.once(event, resolve2);
|
|
5782
5844
|
});
|
|
5783
5845
|
}
|
|
5784
5846
|
async done() {
|
|
@@ -5936,7 +5998,7 @@ class AssistantStream extends EventStream {
|
|
|
5936
5998
|
if (done) {
|
|
5937
5999
|
return { value: undefined, done: true };
|
|
5938
6000
|
}
|
|
5939
|
-
return new Promise((
|
|
6001
|
+
return new Promise((resolve2, reject) => readQueue.push({ resolve: resolve2, reject })).then((chunk2) => chunk2 ? { value: chunk2, done: false } : { value: undefined, done: true });
|
|
5940
6002
|
}
|
|
5941
6003
|
const chunk = pushQueue.shift();
|
|
5942
6004
|
return { value: chunk, done: false };
|
|
@@ -7501,7 +7563,7 @@ class ChatCompletionStream extends AbstractChatCompletionRunner {
|
|
|
7501
7563
|
if (done) {
|
|
7502
7564
|
return { value: undefined, done: true };
|
|
7503
7565
|
}
|
|
7504
|
-
return new Promise((
|
|
7566
|
+
return new Promise((resolve2, reject) => readQueue.push({ resolve: resolve2, reject })).then((chunk2) => chunk2 ? { value: chunk2, done: false } : { value: undefined, done: true });
|
|
7505
7567
|
}
|
|
7506
7568
|
const chunk = pushQueue.shift();
|
|
7507
7569
|
return { value: chunk, done: false };
|
|
@@ -8750,7 +8812,7 @@ class ResponseStream extends EventStream {
|
|
|
8750
8812
|
if (done) {
|
|
8751
8813
|
return { value: undefined, done: true };
|
|
8752
8814
|
}
|
|
8753
|
-
return new Promise((
|
|
8815
|
+
return new Promise((resolve2, reject) => readQueue.push({ resolve: resolve2, reject })).then((event2) => event2 ? { value: event2, done: false } : { value: undefined, done: true });
|
|
8754
8816
|
}
|
|
8755
8817
|
const event = pushQueue.shift();
|
|
8756
8818
|
return { value: event, done: false };
|
|
@@ -9237,60 +9299,172 @@ var _deployments_endpoints = new Set([
|
|
|
9237
9299
|
var openai_default = OpenAI;
|
|
9238
9300
|
|
|
9239
9301
|
// src/lib/providers/openai.ts
|
|
9240
|
-
import { readFileSync } from "fs";
|
|
9302
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
9303
|
+
|
|
9304
|
+
// src/lib/config.ts
|
|
9305
|
+
import { readFileSync, writeFileSync, mkdirSync as mkdirSync2, existsSync as existsSync2, readdirSync as readdirSync2, copyFileSync as copyFileSync2, statSync as statSync2 } from "fs";
|
|
9306
|
+
import { join as join2, dirname as dirname2 } from "path";
|
|
9307
|
+
import { homedir as homedir2 } from "os";
|
|
9308
|
+
var CONFIG_KEYS = ["OPENAI_API_KEY", "THINKER_LABS_API_KEY", "THINKER_LABS_BASE_URL"];
|
|
9309
|
+
function resolveConfigPath() {
|
|
9310
|
+
const home = process.env["HOME"] || process.env["USERPROFILE"] || homedir2();
|
|
9311
|
+
const newDir = join2(home, ".hasna", "brains");
|
|
9312
|
+
const oldDir = join2(home, ".brains");
|
|
9313
|
+
if (existsSync2(oldDir) && !existsSync2(newDir)) {
|
|
9314
|
+
mkdirSync2(newDir, { recursive: true });
|
|
9315
|
+
try {
|
|
9316
|
+
for (const file of readdirSync2(oldDir)) {
|
|
9317
|
+
const oldPath = join2(oldDir, file);
|
|
9318
|
+
const newPath = join2(newDir, file);
|
|
9319
|
+
try {
|
|
9320
|
+
if (statSync2(oldPath).isFile()) {
|
|
9321
|
+
copyFileSync2(oldPath, newPath);
|
|
9322
|
+
}
|
|
9323
|
+
} catch {}
|
|
9324
|
+
}
|
|
9325
|
+
} catch {}
|
|
9326
|
+
}
|
|
9327
|
+
mkdirSync2(newDir, { recursive: true });
|
|
9328
|
+
return join2(newDir, "config.json");
|
|
9329
|
+
}
|
|
9330
|
+
var CONFIG_PATH = resolveConfigPath();
|
|
9331
|
+
function readConfigFile() {
|
|
9332
|
+
if (!existsSync2(CONFIG_PATH))
|
|
9333
|
+
return {};
|
|
9334
|
+
try {
|
|
9335
|
+
return JSON.parse(readFileSync(CONFIG_PATH, "utf-8"));
|
|
9336
|
+
} catch {
|
|
9337
|
+
return {};
|
|
9338
|
+
}
|
|
9339
|
+
}
|
|
9340
|
+
function writeConfigFile(data) {
|
|
9341
|
+
mkdirSync2(dirname2(CONFIG_PATH), { recursive: true });
|
|
9342
|
+
writeFileSync(CONFIG_PATH, JSON.stringify(data, null, 2) + `
|
|
9343
|
+
`, "utf-8");
|
|
9344
|
+
}
|
|
9345
|
+
function getConfigValue(key) {
|
|
9346
|
+
if (process.env[key])
|
|
9347
|
+
return process.env[key];
|
|
9348
|
+
return readConfigFile()[key];
|
|
9349
|
+
}
|
|
9350
|
+
function setConfigValue(key, value) {
|
|
9351
|
+
const data = readConfigFile();
|
|
9352
|
+
data[key] = value;
|
|
9353
|
+
writeConfigFile(data);
|
|
9354
|
+
}
|
|
9355
|
+
function listConfig() {
|
|
9356
|
+
const file = readConfigFile();
|
|
9357
|
+
return CONFIG_KEYS.map((key) => {
|
|
9358
|
+
if (process.env[key])
|
|
9359
|
+
return { key, value: process.env[key], source: "env" };
|
|
9360
|
+
if (file[key])
|
|
9361
|
+
return { key, value: file[key], source: "file" };
|
|
9362
|
+
return { key, value: "", source: "unset" };
|
|
9363
|
+
});
|
|
9364
|
+
}
|
|
9365
|
+
function deleteConfigValue(key) {
|
|
9366
|
+
const data = readConfigFile();
|
|
9367
|
+
delete data[key];
|
|
9368
|
+
writeConfigFile(data);
|
|
9369
|
+
}
|
|
9370
|
+
|
|
9371
|
+
// src/lib/retry.ts
|
|
9372
|
+
var RETRYABLE_STATUS_CODES = new Set([429, 500, 502, 503, 504]);
|
|
9373
|
+
function isRetryableError(err) {
|
|
9374
|
+
if (err instanceof Error) {
|
|
9375
|
+
if (err.message.includes("fetch failed") || err.message.includes("ECONNRESET") || err.message.includes("ETIMEDOUT")) {
|
|
9376
|
+
return true;
|
|
9377
|
+
}
|
|
9378
|
+
const match = err.message.match(/^(\d{3})\s/);
|
|
9379
|
+
if (match) {
|
|
9380
|
+
const status = parseInt(match[1], 10);
|
|
9381
|
+
return RETRYABLE_STATUS_CODES.has(status);
|
|
9382
|
+
}
|
|
9383
|
+
const tlMatch = err.message.match(/API error (\d{3})/);
|
|
9384
|
+
if (tlMatch) {
|
|
9385
|
+
const status = parseInt(tlMatch[1], 10);
|
|
9386
|
+
return RETRYABLE_STATUS_CODES.has(status);
|
|
9387
|
+
}
|
|
9388
|
+
}
|
|
9389
|
+
return false;
|
|
9390
|
+
}
|
|
9391
|
+
async function withRetry(fn, options = {}) {
|
|
9392
|
+
const { maxAttempts = 3, baseDelayMs = 1000, onRetry } = options;
|
|
9393
|
+
let lastError = new Error("Unknown error");
|
|
9394
|
+
for (let attempt = 1;attempt <= maxAttempts; attempt++) {
|
|
9395
|
+
try {
|
|
9396
|
+
return await fn();
|
|
9397
|
+
} catch (err) {
|
|
9398
|
+
lastError = err instanceof Error ? err : new Error(String(err));
|
|
9399
|
+
if (attempt === maxAttempts || !isRetryableError(lastError)) {
|
|
9400
|
+
throw lastError;
|
|
9401
|
+
}
|
|
9402
|
+
const delayMs = baseDelayMs * Math.pow(2, attempt - 1);
|
|
9403
|
+
onRetry?.(attempt, delayMs, lastError);
|
|
9404
|
+
await new Promise((resolve2) => setTimeout(resolve2, delayMs));
|
|
9405
|
+
}
|
|
9406
|
+
}
|
|
9407
|
+
throw lastError;
|
|
9408
|
+
}
|
|
9409
|
+
|
|
9410
|
+
// src/lib/providers/openai.ts
|
|
9241
9411
|
function getClient() {
|
|
9242
|
-
const apiKey =
|
|
9412
|
+
const apiKey = getConfigValue("OPENAI_API_KEY");
|
|
9243
9413
|
if (!apiKey) {
|
|
9244
|
-
throw new Error("OPENAI_API_KEY
|
|
9414
|
+
throw new Error("OPENAI_API_KEY is not set. Run: brains config set OPENAI_API_KEY <key>");
|
|
9245
9415
|
}
|
|
9246
9416
|
return new openai_default({ apiKey });
|
|
9247
9417
|
}
|
|
9248
9418
|
async function uploadTrainingFile(filePath) {
|
|
9249
|
-
|
|
9250
|
-
|
|
9251
|
-
|
|
9252
|
-
|
|
9253
|
-
|
|
9254
|
-
file,
|
|
9255
|
-
|
|
9419
|
+
return withRetry(async () => {
|
|
9420
|
+
const client = getClient();
|
|
9421
|
+
const fileContent = readFileSync2(filePath);
|
|
9422
|
+
const blob2 = new Blob([fileContent], { type: "application/jsonl" });
|
|
9423
|
+
const file = new File([blob2], filePath.split("/").pop() ?? "training.jsonl", { type: "application/jsonl" });
|
|
9424
|
+
const response = await client.files.create({ file, purpose: "fine-tune" });
|
|
9425
|
+
return { fileId: response.id };
|
|
9256
9426
|
});
|
|
9257
|
-
return { fileId: response.id };
|
|
9258
9427
|
}
|
|
9259
9428
|
async function createFineTuneJob(fileId, baseModel, suffix) {
|
|
9260
|
-
|
|
9261
|
-
|
|
9262
|
-
training_file: fileId,
|
|
9263
|
-
|
|
9264
|
-
|
|
9265
|
-
|
|
9266
|
-
|
|
9267
|
-
}
|
|
9268
|
-
const response = await client.fineTuning.jobs.create(params);
|
|
9269
|
-
return { jobId: response.id, status: response.status };
|
|
9429
|
+
return withRetry(async () => {
|
|
9430
|
+
const client = getClient();
|
|
9431
|
+
const params = { training_file: fileId, model: baseModel };
|
|
9432
|
+
if (suffix)
|
|
9433
|
+
params.suffix = suffix;
|
|
9434
|
+
const response = await client.fineTuning.jobs.create(params);
|
|
9435
|
+
return { jobId: response.id, status: response.status };
|
|
9436
|
+
});
|
|
9270
9437
|
}
|
|
9271
9438
|
async function getFineTuneStatus(jobId) {
|
|
9272
|
-
|
|
9273
|
-
|
|
9274
|
-
|
|
9275
|
-
|
|
9276
|
-
|
|
9277
|
-
|
|
9278
|
-
|
|
9279
|
-
|
|
9439
|
+
return withRetry(async () => {
|
|
9440
|
+
const client = getClient();
|
|
9441
|
+
const response = await client.fineTuning.jobs.retrieve(jobId);
|
|
9442
|
+
return {
|
|
9443
|
+
jobId: response.id,
|
|
9444
|
+
status: response.status,
|
|
9445
|
+
fineTunedModel: response.fine_tuned_model ?? undefined,
|
|
9446
|
+
baseModel: response.model ?? undefined,
|
|
9447
|
+
error: response.error?.message ?? undefined
|
|
9448
|
+
};
|
|
9449
|
+
});
|
|
9280
9450
|
}
|
|
9281
9451
|
async function listFineTunedModels() {
|
|
9282
|
-
|
|
9283
|
-
|
|
9284
|
-
|
|
9285
|
-
|
|
9286
|
-
|
|
9287
|
-
|
|
9288
|
-
|
|
9289
|
-
|
|
9452
|
+
return withRetry(async () => {
|
|
9453
|
+
const client = getClient();
|
|
9454
|
+
const jobs = await client.fineTuning.jobs.list();
|
|
9455
|
+
return jobs.data.map((job) => ({
|
|
9456
|
+
id: job.id,
|
|
9457
|
+
model: job.fine_tuned_model ?? job.model,
|
|
9458
|
+
status: job.status,
|
|
9459
|
+
created: job.created_at
|
|
9460
|
+
}));
|
|
9461
|
+
});
|
|
9290
9462
|
}
|
|
9291
9463
|
async function cancelFineTuneJob(jobId) {
|
|
9292
|
-
|
|
9293
|
-
|
|
9464
|
+
return withRetry(async () => {
|
|
9465
|
+
const client = getClient();
|
|
9466
|
+
await client.fineTuning.jobs.cancel(jobId);
|
|
9467
|
+
});
|
|
9294
9468
|
}
|
|
9295
9469
|
|
|
9296
9470
|
class OpenAIProvider {
|
|
@@ -9313,37 +9487,39 @@ class OpenAIProvider {
|
|
|
9313
9487
|
// src/lib/providers/thinker-labs.ts
|
|
9314
9488
|
var DEFAULT_BASE_URL = "https://api.thinkerlabs.ai/v1";
|
|
9315
9489
|
function getConfig() {
|
|
9316
|
-
const apiKey =
|
|
9317
|
-
const baseUrl =
|
|
9490
|
+
const apiKey = getConfigValue("THINKER_LABS_API_KEY");
|
|
9491
|
+
const baseUrl = getConfigValue("THINKER_LABS_BASE_URL") ?? DEFAULT_BASE_URL;
|
|
9318
9492
|
if (!apiKey)
|
|
9319
|
-
throw new Error("THINKER_LABS_API_KEY
|
|
9493
|
+
throw new Error("THINKER_LABS_API_KEY is not set. Run: brains config set THINKER_LABS_API_KEY <key>");
|
|
9320
9494
|
return { apiKey, baseUrl };
|
|
9321
9495
|
}
|
|
9322
9496
|
async function request(method, path, body, file) {
|
|
9323
|
-
|
|
9324
|
-
|
|
9325
|
-
|
|
9326
|
-
|
|
9327
|
-
|
|
9328
|
-
|
|
9329
|
-
|
|
9330
|
-
|
|
9331
|
-
|
|
9332
|
-
|
|
9333
|
-
|
|
9334
|
-
|
|
9335
|
-
|
|
9336
|
-
|
|
9337
|
-
|
|
9338
|
-
|
|
9339
|
-
|
|
9340
|
-
|
|
9341
|
-
|
|
9342
|
-
|
|
9343
|
-
|
|
9344
|
-
|
|
9345
|
-
|
|
9346
|
-
|
|
9497
|
+
return withRetry(async () => {
|
|
9498
|
+
const { apiKey, baseUrl } = getConfig();
|
|
9499
|
+
const headers = { Authorization: `Bearer ${apiKey}` };
|
|
9500
|
+
let fetchBody;
|
|
9501
|
+
if (file) {
|
|
9502
|
+
const form = new FormData;
|
|
9503
|
+
form.append("file", new Blob([file.data], { type: "text/plain" }), file.name);
|
|
9504
|
+
if (body) {
|
|
9505
|
+
for (const [k, v] of Object.entries(body)) {
|
|
9506
|
+
form.append(k, v);
|
|
9507
|
+
}
|
|
9508
|
+
}
|
|
9509
|
+
fetchBody = form;
|
|
9510
|
+
} else if (body) {
|
|
9511
|
+
headers["Content-Type"] = "application/json";
|
|
9512
|
+
fetchBody = JSON.stringify(body);
|
|
9513
|
+
}
|
|
9514
|
+
const res = await fetch(`${baseUrl}${path}`, { method, headers, body: fetchBody });
|
|
9515
|
+
if (!res.ok) {
|
|
9516
|
+
const text2 = await res.text();
|
|
9517
|
+
throw new Error(`Thinker Labs API error ${res.status}: ${text2}`);
|
|
9518
|
+
}
|
|
9519
|
+
if (res.status === 204)
|
|
9520
|
+
return;
|
|
9521
|
+
return res.json();
|
|
9522
|
+
});
|
|
9347
9523
|
}
|
|
9348
9524
|
async function uploadTrainingData(filePath) {
|
|
9349
9525
|
const fileContent = await Bun.file(filePath).text();
|
|
@@ -9408,47 +9584,4069 @@ class ThinkerLabsProvider {
|
|
|
9408
9584
|
}
|
|
9409
9585
|
}
|
|
9410
9586
|
// src/lib/package-metadata.ts
|
|
9411
|
-
import { existsSync, readFileSync as
|
|
9412
|
-
import { dirname as
|
|
9587
|
+
import { existsSync as existsSync3, readFileSync as readFileSync3 } from "fs";
|
|
9588
|
+
import { dirname as dirname3, resolve as resolve2 } from "path";
|
|
9413
9589
|
import { fileURLToPath } from "url";
|
|
9414
9590
|
var DEFAULT_VERSION = "0.0.0";
|
|
9415
9591
|
var cachedVersion;
|
|
9416
9592
|
function getPackageJsonPath() {
|
|
9417
|
-
return
|
|
9593
|
+
return resolve2(dirname3(fileURLToPath(import.meta.url)), "../../package.json");
|
|
9418
9594
|
}
|
|
9419
9595
|
function getPackageVersion() {
|
|
9420
9596
|
if (cachedVersion) {
|
|
9421
9597
|
return cachedVersion;
|
|
9422
9598
|
}
|
|
9423
9599
|
const packageJsonPath = getPackageJsonPath();
|
|
9424
|
-
if (!
|
|
9600
|
+
if (!existsSync3(packageJsonPath)) {
|
|
9425
9601
|
cachedVersion = DEFAULT_VERSION;
|
|
9426
9602
|
return cachedVersion;
|
|
9427
9603
|
}
|
|
9428
9604
|
try {
|
|
9429
|
-
const packageJson = JSON.parse(
|
|
9605
|
+
const packageJson = JSON.parse(readFileSync3(packageJsonPath, "utf-8"));
|
|
9430
9606
|
cachedVersion = typeof packageJson.version === "string" ? packageJson.version : DEFAULT_VERSION;
|
|
9431
9607
|
} catch {
|
|
9432
9608
|
cachedVersion = DEFAULT_VERSION;
|
|
9433
9609
|
}
|
|
9434
9610
|
return cachedVersion;
|
|
9435
9611
|
}
|
|
9612
|
+
// node_modules/zod/v3/external.js
|
|
9613
|
+
var exports_external = {};
|
|
9614
|
+
__export(exports_external, {
|
|
9615
|
+
void: () => voidType,
|
|
9616
|
+
util: () => util,
|
|
9617
|
+
unknown: () => unknownType,
|
|
9618
|
+
union: () => unionType,
|
|
9619
|
+
undefined: () => undefinedType,
|
|
9620
|
+
tuple: () => tupleType,
|
|
9621
|
+
transformer: () => effectsType,
|
|
9622
|
+
symbol: () => symbolType,
|
|
9623
|
+
string: () => stringType,
|
|
9624
|
+
strictObject: () => strictObjectType,
|
|
9625
|
+
setErrorMap: () => setErrorMap,
|
|
9626
|
+
set: () => setType,
|
|
9627
|
+
record: () => recordType,
|
|
9628
|
+
quotelessJson: () => quotelessJson,
|
|
9629
|
+
promise: () => promiseType,
|
|
9630
|
+
preprocess: () => preprocessType,
|
|
9631
|
+
pipeline: () => pipelineType,
|
|
9632
|
+
ostring: () => ostring,
|
|
9633
|
+
optional: () => optionalType,
|
|
9634
|
+
onumber: () => onumber,
|
|
9635
|
+
oboolean: () => oboolean,
|
|
9636
|
+
objectUtil: () => objectUtil,
|
|
9637
|
+
object: () => objectType,
|
|
9638
|
+
number: () => numberType,
|
|
9639
|
+
nullable: () => nullableType,
|
|
9640
|
+
null: () => nullType,
|
|
9641
|
+
never: () => neverType,
|
|
9642
|
+
nativeEnum: () => nativeEnumType,
|
|
9643
|
+
nan: () => nanType,
|
|
9644
|
+
map: () => mapType,
|
|
9645
|
+
makeIssue: () => makeIssue,
|
|
9646
|
+
literal: () => literalType,
|
|
9647
|
+
lazy: () => lazyType,
|
|
9648
|
+
late: () => late,
|
|
9649
|
+
isValid: () => isValid,
|
|
9650
|
+
isDirty: () => isDirty,
|
|
9651
|
+
isAsync: () => isAsync,
|
|
9652
|
+
isAborted: () => isAborted,
|
|
9653
|
+
intersection: () => intersectionType,
|
|
9654
|
+
instanceof: () => instanceOfType,
|
|
9655
|
+
getParsedType: () => getParsedType,
|
|
9656
|
+
getErrorMap: () => getErrorMap,
|
|
9657
|
+
function: () => functionType,
|
|
9658
|
+
enum: () => enumType,
|
|
9659
|
+
effect: () => effectsType,
|
|
9660
|
+
discriminatedUnion: () => discriminatedUnionType,
|
|
9661
|
+
defaultErrorMap: () => en_default,
|
|
9662
|
+
datetimeRegex: () => datetimeRegex,
|
|
9663
|
+
date: () => dateType,
|
|
9664
|
+
custom: () => custom,
|
|
9665
|
+
coerce: () => coerce,
|
|
9666
|
+
boolean: () => booleanType,
|
|
9667
|
+
bigint: () => bigIntType,
|
|
9668
|
+
array: () => arrayType,
|
|
9669
|
+
any: () => anyType,
|
|
9670
|
+
addIssueToContext: () => addIssueToContext,
|
|
9671
|
+
ZodVoid: () => ZodVoid,
|
|
9672
|
+
ZodUnknown: () => ZodUnknown,
|
|
9673
|
+
ZodUnion: () => ZodUnion,
|
|
9674
|
+
ZodUndefined: () => ZodUndefined,
|
|
9675
|
+
ZodType: () => ZodType,
|
|
9676
|
+
ZodTuple: () => ZodTuple,
|
|
9677
|
+
ZodTransformer: () => ZodEffects,
|
|
9678
|
+
ZodSymbol: () => ZodSymbol,
|
|
9679
|
+
ZodString: () => ZodString,
|
|
9680
|
+
ZodSet: () => ZodSet,
|
|
9681
|
+
ZodSchema: () => ZodType,
|
|
9682
|
+
ZodRecord: () => ZodRecord,
|
|
9683
|
+
ZodReadonly: () => ZodReadonly,
|
|
9684
|
+
ZodPromise: () => ZodPromise,
|
|
9685
|
+
ZodPipeline: () => ZodPipeline,
|
|
9686
|
+
ZodParsedType: () => ZodParsedType,
|
|
9687
|
+
ZodOptional: () => ZodOptional,
|
|
9688
|
+
ZodObject: () => ZodObject,
|
|
9689
|
+
ZodNumber: () => ZodNumber,
|
|
9690
|
+
ZodNullable: () => ZodNullable,
|
|
9691
|
+
ZodNull: () => ZodNull,
|
|
9692
|
+
ZodNever: () => ZodNever,
|
|
9693
|
+
ZodNativeEnum: () => ZodNativeEnum,
|
|
9694
|
+
ZodNaN: () => ZodNaN,
|
|
9695
|
+
ZodMap: () => ZodMap,
|
|
9696
|
+
ZodLiteral: () => ZodLiteral,
|
|
9697
|
+
ZodLazy: () => ZodLazy,
|
|
9698
|
+
ZodIssueCode: () => ZodIssueCode,
|
|
9699
|
+
ZodIntersection: () => ZodIntersection,
|
|
9700
|
+
ZodFunction: () => ZodFunction,
|
|
9701
|
+
ZodFirstPartyTypeKind: () => ZodFirstPartyTypeKind,
|
|
9702
|
+
ZodError: () => ZodError,
|
|
9703
|
+
ZodEnum: () => ZodEnum,
|
|
9704
|
+
ZodEffects: () => ZodEffects,
|
|
9705
|
+
ZodDiscriminatedUnion: () => ZodDiscriminatedUnion,
|
|
9706
|
+
ZodDefault: () => ZodDefault,
|
|
9707
|
+
ZodDate: () => ZodDate,
|
|
9708
|
+
ZodCatch: () => ZodCatch,
|
|
9709
|
+
ZodBranded: () => ZodBranded,
|
|
9710
|
+
ZodBoolean: () => ZodBoolean,
|
|
9711
|
+
ZodBigInt: () => ZodBigInt,
|
|
9712
|
+
ZodArray: () => ZodArray,
|
|
9713
|
+
ZodAny: () => ZodAny,
|
|
9714
|
+
Schema: () => ZodType,
|
|
9715
|
+
ParseStatus: () => ParseStatus,
|
|
9716
|
+
OK: () => OK,
|
|
9717
|
+
NEVER: () => NEVER,
|
|
9718
|
+
INVALID: () => INVALID,
|
|
9719
|
+
EMPTY_PATH: () => EMPTY_PATH,
|
|
9720
|
+
DIRTY: () => DIRTY,
|
|
9721
|
+
BRAND: () => BRAND
|
|
9722
|
+
});
|
|
9723
|
+
|
|
9724
|
+
// node_modules/zod/v3/helpers/util.js
|
|
9725
|
+
var util;
|
|
9726
|
+
(function(util2) {
|
|
9727
|
+
util2.assertEqual = (_) => {};
|
|
9728
|
+
function assertIs(_arg) {}
|
|
9729
|
+
util2.assertIs = assertIs;
|
|
9730
|
+
function assertNever3(_x) {
|
|
9731
|
+
throw new Error;
|
|
9732
|
+
}
|
|
9733
|
+
util2.assertNever = assertNever3;
|
|
9734
|
+
util2.arrayToEnum = (items) => {
|
|
9735
|
+
const obj = {};
|
|
9736
|
+
for (const item of items) {
|
|
9737
|
+
obj[item] = item;
|
|
9738
|
+
}
|
|
9739
|
+
return obj;
|
|
9740
|
+
};
|
|
9741
|
+
util2.getValidEnumValues = (obj) => {
|
|
9742
|
+
const validKeys = util2.objectKeys(obj).filter((k) => typeof obj[obj[k]] !== "number");
|
|
9743
|
+
const filtered = {};
|
|
9744
|
+
for (const k of validKeys) {
|
|
9745
|
+
filtered[k] = obj[k];
|
|
9746
|
+
}
|
|
9747
|
+
return util2.objectValues(filtered);
|
|
9748
|
+
};
|
|
9749
|
+
util2.objectValues = (obj) => {
|
|
9750
|
+
return util2.objectKeys(obj).map(function(e) {
|
|
9751
|
+
return obj[e];
|
|
9752
|
+
});
|
|
9753
|
+
};
|
|
9754
|
+
util2.objectKeys = typeof Object.keys === "function" ? (obj) => Object.keys(obj) : (object) => {
|
|
9755
|
+
const keys = [];
|
|
9756
|
+
for (const key in object) {
|
|
9757
|
+
if (Object.prototype.hasOwnProperty.call(object, key)) {
|
|
9758
|
+
keys.push(key);
|
|
9759
|
+
}
|
|
9760
|
+
}
|
|
9761
|
+
return keys;
|
|
9762
|
+
};
|
|
9763
|
+
util2.find = (arr, checker) => {
|
|
9764
|
+
for (const item of arr) {
|
|
9765
|
+
if (checker(item))
|
|
9766
|
+
return item;
|
|
9767
|
+
}
|
|
9768
|
+
return;
|
|
9769
|
+
};
|
|
9770
|
+
util2.isInteger = typeof Number.isInteger === "function" ? (val) => Number.isInteger(val) : (val) => typeof val === "number" && Number.isFinite(val) && Math.floor(val) === val;
|
|
9771
|
+
function joinValues(array, separator = " | ") {
|
|
9772
|
+
return array.map((val) => typeof val === "string" ? `'${val}'` : val).join(separator);
|
|
9773
|
+
}
|
|
9774
|
+
util2.joinValues = joinValues;
|
|
9775
|
+
util2.jsonStringifyReplacer = (_, value) => {
|
|
9776
|
+
if (typeof value === "bigint") {
|
|
9777
|
+
return value.toString();
|
|
9778
|
+
}
|
|
9779
|
+
return value;
|
|
9780
|
+
};
|
|
9781
|
+
})(util || (util = {}));
|
|
9782
|
+
var objectUtil;
|
|
9783
|
+
(function(objectUtil2) {
|
|
9784
|
+
objectUtil2.mergeShapes = (first, second) => {
|
|
9785
|
+
return {
|
|
9786
|
+
...first,
|
|
9787
|
+
...second
|
|
9788
|
+
};
|
|
9789
|
+
};
|
|
9790
|
+
})(objectUtil || (objectUtil = {}));
|
|
9791
|
+
var ZodParsedType = util.arrayToEnum([
|
|
9792
|
+
"string",
|
|
9793
|
+
"nan",
|
|
9794
|
+
"number",
|
|
9795
|
+
"integer",
|
|
9796
|
+
"float",
|
|
9797
|
+
"boolean",
|
|
9798
|
+
"date",
|
|
9799
|
+
"bigint",
|
|
9800
|
+
"symbol",
|
|
9801
|
+
"function",
|
|
9802
|
+
"undefined",
|
|
9803
|
+
"null",
|
|
9804
|
+
"array",
|
|
9805
|
+
"object",
|
|
9806
|
+
"unknown",
|
|
9807
|
+
"promise",
|
|
9808
|
+
"void",
|
|
9809
|
+
"never",
|
|
9810
|
+
"map",
|
|
9811
|
+
"set"
|
|
9812
|
+
]);
|
|
9813
|
+
var getParsedType = (data) => {
|
|
9814
|
+
const t = typeof data;
|
|
9815
|
+
switch (t) {
|
|
9816
|
+
case "undefined":
|
|
9817
|
+
return ZodParsedType.undefined;
|
|
9818
|
+
case "string":
|
|
9819
|
+
return ZodParsedType.string;
|
|
9820
|
+
case "number":
|
|
9821
|
+
return Number.isNaN(data) ? ZodParsedType.nan : ZodParsedType.number;
|
|
9822
|
+
case "boolean":
|
|
9823
|
+
return ZodParsedType.boolean;
|
|
9824
|
+
case "function":
|
|
9825
|
+
return ZodParsedType.function;
|
|
9826
|
+
case "bigint":
|
|
9827
|
+
return ZodParsedType.bigint;
|
|
9828
|
+
case "symbol":
|
|
9829
|
+
return ZodParsedType.symbol;
|
|
9830
|
+
case "object":
|
|
9831
|
+
if (Array.isArray(data)) {
|
|
9832
|
+
return ZodParsedType.array;
|
|
9833
|
+
}
|
|
9834
|
+
if (data === null) {
|
|
9835
|
+
return ZodParsedType.null;
|
|
9836
|
+
}
|
|
9837
|
+
if (data.then && typeof data.then === "function" && data.catch && typeof data.catch === "function") {
|
|
9838
|
+
return ZodParsedType.promise;
|
|
9839
|
+
}
|
|
9840
|
+
if (typeof Map !== "undefined" && data instanceof Map) {
|
|
9841
|
+
return ZodParsedType.map;
|
|
9842
|
+
}
|
|
9843
|
+
if (typeof Set !== "undefined" && data instanceof Set) {
|
|
9844
|
+
return ZodParsedType.set;
|
|
9845
|
+
}
|
|
9846
|
+
if (typeof Date !== "undefined" && data instanceof Date) {
|
|
9847
|
+
return ZodParsedType.date;
|
|
9848
|
+
}
|
|
9849
|
+
return ZodParsedType.object;
|
|
9850
|
+
default:
|
|
9851
|
+
return ZodParsedType.unknown;
|
|
9852
|
+
}
|
|
9853
|
+
};
|
|
9854
|
+
|
|
9855
|
+
// node_modules/zod/v3/ZodError.js
|
|
9856
|
+
var ZodIssueCode = util.arrayToEnum([
|
|
9857
|
+
"invalid_type",
|
|
9858
|
+
"invalid_literal",
|
|
9859
|
+
"custom",
|
|
9860
|
+
"invalid_union",
|
|
9861
|
+
"invalid_union_discriminator",
|
|
9862
|
+
"invalid_enum_value",
|
|
9863
|
+
"unrecognized_keys",
|
|
9864
|
+
"invalid_arguments",
|
|
9865
|
+
"invalid_return_type",
|
|
9866
|
+
"invalid_date",
|
|
9867
|
+
"invalid_string",
|
|
9868
|
+
"too_small",
|
|
9869
|
+
"too_big",
|
|
9870
|
+
"invalid_intersection_types",
|
|
9871
|
+
"not_multiple_of",
|
|
9872
|
+
"not_finite"
|
|
9873
|
+
]);
|
|
9874
|
+
var quotelessJson = (obj) => {
|
|
9875
|
+
const json = JSON.stringify(obj, null, 2);
|
|
9876
|
+
return json.replace(/"([^"]+)":/g, "$1:");
|
|
9877
|
+
};
|
|
9878
|
+
|
|
9879
|
+
class ZodError extends Error {
|
|
9880
|
+
get errors() {
|
|
9881
|
+
return this.issues;
|
|
9882
|
+
}
|
|
9883
|
+
constructor(issues) {
|
|
9884
|
+
super();
|
|
9885
|
+
this.issues = [];
|
|
9886
|
+
this.addIssue = (sub) => {
|
|
9887
|
+
this.issues = [...this.issues, sub];
|
|
9888
|
+
};
|
|
9889
|
+
this.addIssues = (subs = []) => {
|
|
9890
|
+
this.issues = [...this.issues, ...subs];
|
|
9891
|
+
};
|
|
9892
|
+
const actualProto = new.target.prototype;
|
|
9893
|
+
if (Object.setPrototypeOf) {
|
|
9894
|
+
Object.setPrototypeOf(this, actualProto);
|
|
9895
|
+
} else {
|
|
9896
|
+
this.__proto__ = actualProto;
|
|
9897
|
+
}
|
|
9898
|
+
this.name = "ZodError";
|
|
9899
|
+
this.issues = issues;
|
|
9900
|
+
}
|
|
9901
|
+
format(_mapper) {
|
|
9902
|
+
const mapper = _mapper || function(issue) {
|
|
9903
|
+
return issue.message;
|
|
9904
|
+
};
|
|
9905
|
+
const fieldErrors = { _errors: [] };
|
|
9906
|
+
const processError = (error) => {
|
|
9907
|
+
for (const issue of error.issues) {
|
|
9908
|
+
if (issue.code === "invalid_union") {
|
|
9909
|
+
issue.unionErrors.map(processError);
|
|
9910
|
+
} else if (issue.code === "invalid_return_type") {
|
|
9911
|
+
processError(issue.returnTypeError);
|
|
9912
|
+
} else if (issue.code === "invalid_arguments") {
|
|
9913
|
+
processError(issue.argumentsError);
|
|
9914
|
+
} else if (issue.path.length === 0) {
|
|
9915
|
+
fieldErrors._errors.push(mapper(issue));
|
|
9916
|
+
} else {
|
|
9917
|
+
let curr = fieldErrors;
|
|
9918
|
+
let i = 0;
|
|
9919
|
+
while (i < issue.path.length) {
|
|
9920
|
+
const el = issue.path[i];
|
|
9921
|
+
const terminal = i === issue.path.length - 1;
|
|
9922
|
+
if (!terminal) {
|
|
9923
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
9924
|
+
} else {
|
|
9925
|
+
curr[el] = curr[el] || { _errors: [] };
|
|
9926
|
+
curr[el]._errors.push(mapper(issue));
|
|
9927
|
+
}
|
|
9928
|
+
curr = curr[el];
|
|
9929
|
+
i++;
|
|
9930
|
+
}
|
|
9931
|
+
}
|
|
9932
|
+
}
|
|
9933
|
+
};
|
|
9934
|
+
processError(this);
|
|
9935
|
+
return fieldErrors;
|
|
9936
|
+
}
|
|
9937
|
+
static assert(value) {
|
|
9938
|
+
if (!(value instanceof ZodError)) {
|
|
9939
|
+
throw new Error(`Not a ZodError: ${value}`);
|
|
9940
|
+
}
|
|
9941
|
+
}
|
|
9942
|
+
toString() {
|
|
9943
|
+
return this.message;
|
|
9944
|
+
}
|
|
9945
|
+
get message() {
|
|
9946
|
+
return JSON.stringify(this.issues, util.jsonStringifyReplacer, 2);
|
|
9947
|
+
}
|
|
9948
|
+
get isEmpty() {
|
|
9949
|
+
return this.issues.length === 0;
|
|
9950
|
+
}
|
|
9951
|
+
flatten(mapper = (issue) => issue.message) {
|
|
9952
|
+
const fieldErrors = {};
|
|
9953
|
+
const formErrors = [];
|
|
9954
|
+
for (const sub of this.issues) {
|
|
9955
|
+
if (sub.path.length > 0) {
|
|
9956
|
+
const firstEl = sub.path[0];
|
|
9957
|
+
fieldErrors[firstEl] = fieldErrors[firstEl] || [];
|
|
9958
|
+
fieldErrors[firstEl].push(mapper(sub));
|
|
9959
|
+
} else {
|
|
9960
|
+
formErrors.push(mapper(sub));
|
|
9961
|
+
}
|
|
9962
|
+
}
|
|
9963
|
+
return { formErrors, fieldErrors };
|
|
9964
|
+
}
|
|
9965
|
+
get formErrors() {
|
|
9966
|
+
return this.flatten();
|
|
9967
|
+
}
|
|
9968
|
+
}
|
|
9969
|
+
ZodError.create = (issues) => {
|
|
9970
|
+
const error = new ZodError(issues);
|
|
9971
|
+
return error;
|
|
9972
|
+
};
|
|
9973
|
+
|
|
9974
|
+
// node_modules/zod/v3/locales/en.js
|
|
9975
|
+
var errorMap = (issue, _ctx) => {
|
|
9976
|
+
let message;
|
|
9977
|
+
switch (issue.code) {
|
|
9978
|
+
case ZodIssueCode.invalid_type:
|
|
9979
|
+
if (issue.received === ZodParsedType.undefined) {
|
|
9980
|
+
message = "Required";
|
|
9981
|
+
} else {
|
|
9982
|
+
message = `Expected ${issue.expected}, received ${issue.received}`;
|
|
9983
|
+
}
|
|
9984
|
+
break;
|
|
9985
|
+
case ZodIssueCode.invalid_literal:
|
|
9986
|
+
message = `Invalid literal value, expected ${JSON.stringify(issue.expected, util.jsonStringifyReplacer)}`;
|
|
9987
|
+
break;
|
|
9988
|
+
case ZodIssueCode.unrecognized_keys:
|
|
9989
|
+
message = `Unrecognized key(s) in object: ${util.joinValues(issue.keys, ", ")}`;
|
|
9990
|
+
break;
|
|
9991
|
+
case ZodIssueCode.invalid_union:
|
|
9992
|
+
message = `Invalid input`;
|
|
9993
|
+
break;
|
|
9994
|
+
case ZodIssueCode.invalid_union_discriminator:
|
|
9995
|
+
message = `Invalid discriminator value. Expected ${util.joinValues(issue.options)}`;
|
|
9996
|
+
break;
|
|
9997
|
+
case ZodIssueCode.invalid_enum_value:
|
|
9998
|
+
message = `Invalid enum value. Expected ${util.joinValues(issue.options)}, received '${issue.received}'`;
|
|
9999
|
+
break;
|
|
10000
|
+
case ZodIssueCode.invalid_arguments:
|
|
10001
|
+
message = `Invalid function arguments`;
|
|
10002
|
+
break;
|
|
10003
|
+
case ZodIssueCode.invalid_return_type:
|
|
10004
|
+
message = `Invalid function return type`;
|
|
10005
|
+
break;
|
|
10006
|
+
case ZodIssueCode.invalid_date:
|
|
10007
|
+
message = `Invalid date`;
|
|
10008
|
+
break;
|
|
10009
|
+
case ZodIssueCode.invalid_string:
|
|
10010
|
+
if (typeof issue.validation === "object") {
|
|
10011
|
+
if ("includes" in issue.validation) {
|
|
10012
|
+
message = `Invalid input: must include "${issue.validation.includes}"`;
|
|
10013
|
+
if (typeof issue.validation.position === "number") {
|
|
10014
|
+
message = `${message} at one or more positions greater than or equal to ${issue.validation.position}`;
|
|
10015
|
+
}
|
|
10016
|
+
} else if ("startsWith" in issue.validation) {
|
|
10017
|
+
message = `Invalid input: must start with "${issue.validation.startsWith}"`;
|
|
10018
|
+
} else if ("endsWith" in issue.validation) {
|
|
10019
|
+
message = `Invalid input: must end with "${issue.validation.endsWith}"`;
|
|
10020
|
+
} else {
|
|
10021
|
+
util.assertNever(issue.validation);
|
|
10022
|
+
}
|
|
10023
|
+
} else if (issue.validation !== "regex") {
|
|
10024
|
+
message = `Invalid ${issue.validation}`;
|
|
10025
|
+
} else {
|
|
10026
|
+
message = "Invalid";
|
|
10027
|
+
}
|
|
10028
|
+
break;
|
|
10029
|
+
case ZodIssueCode.too_small:
|
|
10030
|
+
if (issue.type === "array")
|
|
10031
|
+
message = `Array must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `more than`} ${issue.minimum} element(s)`;
|
|
10032
|
+
else if (issue.type === "string")
|
|
10033
|
+
message = `String must contain ${issue.exact ? "exactly" : issue.inclusive ? `at least` : `over`} ${issue.minimum} character(s)`;
|
|
10034
|
+
else if (issue.type === "number")
|
|
10035
|
+
message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
|
|
10036
|
+
else if (issue.type === "bigint")
|
|
10037
|
+
message = `Number must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${issue.minimum}`;
|
|
10038
|
+
else if (issue.type === "date")
|
|
10039
|
+
message = `Date must be ${issue.exact ? `exactly equal to ` : issue.inclusive ? `greater than or equal to ` : `greater than `}${new Date(Number(issue.minimum))}`;
|
|
10040
|
+
else
|
|
10041
|
+
message = "Invalid input";
|
|
10042
|
+
break;
|
|
10043
|
+
case ZodIssueCode.too_big:
|
|
10044
|
+
if (issue.type === "array")
|
|
10045
|
+
message = `Array must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `less than`} ${issue.maximum} element(s)`;
|
|
10046
|
+
else if (issue.type === "string")
|
|
10047
|
+
message = `String must contain ${issue.exact ? `exactly` : issue.inclusive ? `at most` : `under`} ${issue.maximum} character(s)`;
|
|
10048
|
+
else if (issue.type === "number")
|
|
10049
|
+
message = `Number must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
10050
|
+
else if (issue.type === "bigint")
|
|
10051
|
+
message = `BigInt must be ${issue.exact ? `exactly` : issue.inclusive ? `less than or equal to` : `less than`} ${issue.maximum}`;
|
|
10052
|
+
else if (issue.type === "date")
|
|
10053
|
+
message = `Date must be ${issue.exact ? `exactly` : issue.inclusive ? `smaller than or equal to` : `smaller than`} ${new Date(Number(issue.maximum))}`;
|
|
10054
|
+
else
|
|
10055
|
+
message = "Invalid input";
|
|
10056
|
+
break;
|
|
10057
|
+
case ZodIssueCode.custom:
|
|
10058
|
+
message = `Invalid input`;
|
|
10059
|
+
break;
|
|
10060
|
+
case ZodIssueCode.invalid_intersection_types:
|
|
10061
|
+
message = `Intersection results could not be merged`;
|
|
10062
|
+
break;
|
|
10063
|
+
case ZodIssueCode.not_multiple_of:
|
|
10064
|
+
message = `Number must be a multiple of ${issue.multipleOf}`;
|
|
10065
|
+
break;
|
|
10066
|
+
case ZodIssueCode.not_finite:
|
|
10067
|
+
message = "Number must be finite";
|
|
10068
|
+
break;
|
|
10069
|
+
default:
|
|
10070
|
+
message = _ctx.defaultError;
|
|
10071
|
+
util.assertNever(issue);
|
|
10072
|
+
}
|
|
10073
|
+
return { message };
|
|
10074
|
+
};
|
|
10075
|
+
var en_default = errorMap;
|
|
10076
|
+
|
|
10077
|
+
// node_modules/zod/v3/errors.js
|
|
10078
|
+
var overrideErrorMap = en_default;
|
|
10079
|
+
function setErrorMap(map) {
|
|
10080
|
+
overrideErrorMap = map;
|
|
10081
|
+
}
|
|
10082
|
+
function getErrorMap() {
|
|
10083
|
+
return overrideErrorMap;
|
|
10084
|
+
}
|
|
10085
|
+
// node_modules/zod/v3/helpers/parseUtil.js
|
|
10086
|
+
var makeIssue = (params) => {
|
|
10087
|
+
const { data, path, errorMaps, issueData } = params;
|
|
10088
|
+
const fullPath = [...path, ...issueData.path || []];
|
|
10089
|
+
const fullIssue = {
|
|
10090
|
+
...issueData,
|
|
10091
|
+
path: fullPath
|
|
10092
|
+
};
|
|
10093
|
+
if (issueData.message !== undefined) {
|
|
10094
|
+
return {
|
|
10095
|
+
...issueData,
|
|
10096
|
+
path: fullPath,
|
|
10097
|
+
message: issueData.message
|
|
10098
|
+
};
|
|
10099
|
+
}
|
|
10100
|
+
let errorMessage = "";
|
|
10101
|
+
const maps = errorMaps.filter((m) => !!m).slice().reverse();
|
|
10102
|
+
for (const map of maps) {
|
|
10103
|
+
errorMessage = map(fullIssue, { data, defaultError: errorMessage }).message;
|
|
10104
|
+
}
|
|
10105
|
+
return {
|
|
10106
|
+
...issueData,
|
|
10107
|
+
path: fullPath,
|
|
10108
|
+
message: errorMessage
|
|
10109
|
+
};
|
|
10110
|
+
};
|
|
10111
|
+
var EMPTY_PATH = [];
|
|
10112
|
+
function addIssueToContext(ctx, issueData) {
|
|
10113
|
+
const overrideMap = getErrorMap();
|
|
10114
|
+
const issue = makeIssue({
|
|
10115
|
+
issueData,
|
|
10116
|
+
data: ctx.data,
|
|
10117
|
+
path: ctx.path,
|
|
10118
|
+
errorMaps: [
|
|
10119
|
+
ctx.common.contextualErrorMap,
|
|
10120
|
+
ctx.schemaErrorMap,
|
|
10121
|
+
overrideMap,
|
|
10122
|
+
overrideMap === en_default ? undefined : en_default
|
|
10123
|
+
].filter((x) => !!x)
|
|
10124
|
+
});
|
|
10125
|
+
ctx.common.issues.push(issue);
|
|
10126
|
+
}
|
|
10127
|
+
|
|
10128
|
+
class ParseStatus {
|
|
10129
|
+
constructor() {
|
|
10130
|
+
this.value = "valid";
|
|
10131
|
+
}
|
|
10132
|
+
dirty() {
|
|
10133
|
+
if (this.value === "valid")
|
|
10134
|
+
this.value = "dirty";
|
|
10135
|
+
}
|
|
10136
|
+
abort() {
|
|
10137
|
+
if (this.value !== "aborted")
|
|
10138
|
+
this.value = "aborted";
|
|
10139
|
+
}
|
|
10140
|
+
static mergeArray(status, results) {
|
|
10141
|
+
const arrayValue = [];
|
|
10142
|
+
for (const s of results) {
|
|
10143
|
+
if (s.status === "aborted")
|
|
10144
|
+
return INVALID;
|
|
10145
|
+
if (s.status === "dirty")
|
|
10146
|
+
status.dirty();
|
|
10147
|
+
arrayValue.push(s.value);
|
|
10148
|
+
}
|
|
10149
|
+
return { status: status.value, value: arrayValue };
|
|
10150
|
+
}
|
|
10151
|
+
static async mergeObjectAsync(status, pairs) {
|
|
10152
|
+
const syncPairs = [];
|
|
10153
|
+
for (const pair of pairs) {
|
|
10154
|
+
const key = await pair.key;
|
|
10155
|
+
const value = await pair.value;
|
|
10156
|
+
syncPairs.push({
|
|
10157
|
+
key,
|
|
10158
|
+
value
|
|
10159
|
+
});
|
|
10160
|
+
}
|
|
10161
|
+
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
10162
|
+
}
|
|
10163
|
+
static mergeObjectSync(status, pairs) {
|
|
10164
|
+
const finalObject = {};
|
|
10165
|
+
for (const pair of pairs) {
|
|
10166
|
+
const { key, value } = pair;
|
|
10167
|
+
if (key.status === "aborted")
|
|
10168
|
+
return INVALID;
|
|
10169
|
+
if (value.status === "aborted")
|
|
10170
|
+
return INVALID;
|
|
10171
|
+
if (key.status === "dirty")
|
|
10172
|
+
status.dirty();
|
|
10173
|
+
if (value.status === "dirty")
|
|
10174
|
+
status.dirty();
|
|
10175
|
+
if (key.value !== "__proto__" && (typeof value.value !== "undefined" || pair.alwaysSet)) {
|
|
10176
|
+
finalObject[key.value] = value.value;
|
|
10177
|
+
}
|
|
10178
|
+
}
|
|
10179
|
+
return { status: status.value, value: finalObject };
|
|
10180
|
+
}
|
|
10181
|
+
}
|
|
10182
|
+
var INVALID = Object.freeze({
|
|
10183
|
+
status: "aborted"
|
|
10184
|
+
});
|
|
10185
|
+
var DIRTY = (value) => ({ status: "dirty", value });
|
|
10186
|
+
var OK = (value) => ({ status: "valid", value });
|
|
10187
|
+
var isAborted = (x) => x.status === "aborted";
|
|
10188
|
+
var isDirty = (x) => x.status === "dirty";
|
|
10189
|
+
var isValid = (x) => x.status === "valid";
|
|
10190
|
+
var isAsync = (x) => typeof Promise !== "undefined" && x instanceof Promise;
|
|
10191
|
+
// node_modules/zod/v3/helpers/errorUtil.js
|
|
10192
|
+
var errorUtil;
|
|
10193
|
+
(function(errorUtil2) {
|
|
10194
|
+
errorUtil2.errToObj = (message) => typeof message === "string" ? { message } : message || {};
|
|
10195
|
+
errorUtil2.toString = (message) => typeof message === "string" ? message : message?.message;
|
|
10196
|
+
})(errorUtil || (errorUtil = {}));
|
|
10197
|
+
|
|
10198
|
+
// node_modules/zod/v3/types.js
|
|
10199
|
+
class ParseInputLazyPath {
|
|
10200
|
+
constructor(parent, value, path, key) {
|
|
10201
|
+
this._cachedPath = [];
|
|
10202
|
+
this.parent = parent;
|
|
10203
|
+
this.data = value;
|
|
10204
|
+
this._path = path;
|
|
10205
|
+
this._key = key;
|
|
10206
|
+
}
|
|
10207
|
+
get path() {
|
|
10208
|
+
if (!this._cachedPath.length) {
|
|
10209
|
+
if (Array.isArray(this._key)) {
|
|
10210
|
+
this._cachedPath.push(...this._path, ...this._key);
|
|
10211
|
+
} else {
|
|
10212
|
+
this._cachedPath.push(...this._path, this._key);
|
|
10213
|
+
}
|
|
10214
|
+
}
|
|
10215
|
+
return this._cachedPath;
|
|
10216
|
+
}
|
|
10217
|
+
}
|
|
10218
|
+
var handleResult = (ctx, result) => {
|
|
10219
|
+
if (isValid(result)) {
|
|
10220
|
+
return { success: true, data: result.value };
|
|
10221
|
+
} else {
|
|
10222
|
+
if (!ctx.common.issues.length) {
|
|
10223
|
+
throw new Error("Validation failed but no issues detected.");
|
|
10224
|
+
}
|
|
10225
|
+
return {
|
|
10226
|
+
success: false,
|
|
10227
|
+
get error() {
|
|
10228
|
+
if (this._error)
|
|
10229
|
+
return this._error;
|
|
10230
|
+
const error = new ZodError(ctx.common.issues);
|
|
10231
|
+
this._error = error;
|
|
10232
|
+
return this._error;
|
|
10233
|
+
}
|
|
10234
|
+
};
|
|
10235
|
+
}
|
|
10236
|
+
};
|
|
10237
|
+
function processCreateParams(params) {
|
|
10238
|
+
if (!params)
|
|
10239
|
+
return {};
|
|
10240
|
+
const { errorMap: errorMap2, invalid_type_error, required_error, description } = params;
|
|
10241
|
+
if (errorMap2 && (invalid_type_error || required_error)) {
|
|
10242
|
+
throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`);
|
|
10243
|
+
}
|
|
10244
|
+
if (errorMap2)
|
|
10245
|
+
return { errorMap: errorMap2, description };
|
|
10246
|
+
const customMap = (iss, ctx) => {
|
|
10247
|
+
const { message } = params;
|
|
10248
|
+
if (iss.code === "invalid_enum_value") {
|
|
10249
|
+
return { message: message ?? ctx.defaultError };
|
|
10250
|
+
}
|
|
10251
|
+
if (typeof ctx.data === "undefined") {
|
|
10252
|
+
return { message: message ?? required_error ?? ctx.defaultError };
|
|
10253
|
+
}
|
|
10254
|
+
if (iss.code !== "invalid_type")
|
|
10255
|
+
return { message: ctx.defaultError };
|
|
10256
|
+
return { message: message ?? invalid_type_error ?? ctx.defaultError };
|
|
10257
|
+
};
|
|
10258
|
+
return { errorMap: customMap, description };
|
|
10259
|
+
}
|
|
10260
|
+
|
|
10261
|
+
class ZodType {
|
|
10262
|
+
get description() {
|
|
10263
|
+
return this._def.description;
|
|
10264
|
+
}
|
|
10265
|
+
_getType(input) {
|
|
10266
|
+
return getParsedType(input.data);
|
|
10267
|
+
}
|
|
10268
|
+
_getOrReturnCtx(input, ctx) {
|
|
10269
|
+
return ctx || {
|
|
10270
|
+
common: input.parent.common,
|
|
10271
|
+
data: input.data,
|
|
10272
|
+
parsedType: getParsedType(input.data),
|
|
10273
|
+
schemaErrorMap: this._def.errorMap,
|
|
10274
|
+
path: input.path,
|
|
10275
|
+
parent: input.parent
|
|
10276
|
+
};
|
|
10277
|
+
}
|
|
10278
|
+
_processInputParams(input) {
|
|
10279
|
+
return {
|
|
10280
|
+
status: new ParseStatus,
|
|
10281
|
+
ctx: {
|
|
10282
|
+
common: input.parent.common,
|
|
10283
|
+
data: input.data,
|
|
10284
|
+
parsedType: getParsedType(input.data),
|
|
10285
|
+
schemaErrorMap: this._def.errorMap,
|
|
10286
|
+
path: input.path,
|
|
10287
|
+
parent: input.parent
|
|
10288
|
+
}
|
|
10289
|
+
};
|
|
10290
|
+
}
|
|
10291
|
+
_parseSync(input) {
|
|
10292
|
+
const result = this._parse(input);
|
|
10293
|
+
if (isAsync(result)) {
|
|
10294
|
+
throw new Error("Synchronous parse encountered promise.");
|
|
10295
|
+
}
|
|
10296
|
+
return result;
|
|
10297
|
+
}
|
|
10298
|
+
_parseAsync(input) {
|
|
10299
|
+
const result = this._parse(input);
|
|
10300
|
+
return Promise.resolve(result);
|
|
10301
|
+
}
|
|
10302
|
+
parse(data, params) {
|
|
10303
|
+
const result = this.safeParse(data, params);
|
|
10304
|
+
if (result.success)
|
|
10305
|
+
return result.data;
|
|
10306
|
+
throw result.error;
|
|
10307
|
+
}
|
|
10308
|
+
safeParse(data, params) {
|
|
10309
|
+
const ctx = {
|
|
10310
|
+
common: {
|
|
10311
|
+
issues: [],
|
|
10312
|
+
async: params?.async ?? false,
|
|
10313
|
+
contextualErrorMap: params?.errorMap
|
|
10314
|
+
},
|
|
10315
|
+
path: params?.path || [],
|
|
10316
|
+
schemaErrorMap: this._def.errorMap,
|
|
10317
|
+
parent: null,
|
|
10318
|
+
data,
|
|
10319
|
+
parsedType: getParsedType(data)
|
|
10320
|
+
};
|
|
10321
|
+
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
|
|
10322
|
+
return handleResult(ctx, result);
|
|
10323
|
+
}
|
|
10324
|
+
"~validate"(data) {
|
|
10325
|
+
const ctx = {
|
|
10326
|
+
common: {
|
|
10327
|
+
issues: [],
|
|
10328
|
+
async: !!this["~standard"].async
|
|
10329
|
+
},
|
|
10330
|
+
path: [],
|
|
10331
|
+
schemaErrorMap: this._def.errorMap,
|
|
10332
|
+
parent: null,
|
|
10333
|
+
data,
|
|
10334
|
+
parsedType: getParsedType(data)
|
|
10335
|
+
};
|
|
10336
|
+
if (!this["~standard"].async) {
|
|
10337
|
+
try {
|
|
10338
|
+
const result = this._parseSync({ data, path: [], parent: ctx });
|
|
10339
|
+
return isValid(result) ? {
|
|
10340
|
+
value: result.value
|
|
10341
|
+
} : {
|
|
10342
|
+
issues: ctx.common.issues
|
|
10343
|
+
};
|
|
10344
|
+
} catch (err) {
|
|
10345
|
+
if (err?.message?.toLowerCase()?.includes("encountered")) {
|
|
10346
|
+
this["~standard"].async = true;
|
|
10347
|
+
}
|
|
10348
|
+
ctx.common = {
|
|
10349
|
+
issues: [],
|
|
10350
|
+
async: true
|
|
10351
|
+
};
|
|
10352
|
+
}
|
|
10353
|
+
}
|
|
10354
|
+
return this._parseAsync({ data, path: [], parent: ctx }).then((result) => isValid(result) ? {
|
|
10355
|
+
value: result.value
|
|
10356
|
+
} : {
|
|
10357
|
+
issues: ctx.common.issues
|
|
10358
|
+
});
|
|
10359
|
+
}
|
|
10360
|
+
async parseAsync(data, params) {
|
|
10361
|
+
const result = await this.safeParseAsync(data, params);
|
|
10362
|
+
if (result.success)
|
|
10363
|
+
return result.data;
|
|
10364
|
+
throw result.error;
|
|
10365
|
+
}
|
|
10366
|
+
async safeParseAsync(data, params) {
|
|
10367
|
+
const ctx = {
|
|
10368
|
+
common: {
|
|
10369
|
+
issues: [],
|
|
10370
|
+
contextualErrorMap: params?.errorMap,
|
|
10371
|
+
async: true
|
|
10372
|
+
},
|
|
10373
|
+
path: params?.path || [],
|
|
10374
|
+
schemaErrorMap: this._def.errorMap,
|
|
10375
|
+
parent: null,
|
|
10376
|
+
data,
|
|
10377
|
+
parsedType: getParsedType(data)
|
|
10378
|
+
};
|
|
10379
|
+
const maybeAsyncResult = this._parse({ data, path: ctx.path, parent: ctx });
|
|
10380
|
+
const result = await (isAsync(maybeAsyncResult) ? maybeAsyncResult : Promise.resolve(maybeAsyncResult));
|
|
10381
|
+
return handleResult(ctx, result);
|
|
10382
|
+
}
|
|
10383
|
+
refine(check, message) {
|
|
10384
|
+
const getIssueProperties = (val) => {
|
|
10385
|
+
if (typeof message === "string" || typeof message === "undefined") {
|
|
10386
|
+
return { message };
|
|
10387
|
+
} else if (typeof message === "function") {
|
|
10388
|
+
return message(val);
|
|
10389
|
+
} else {
|
|
10390
|
+
return message;
|
|
10391
|
+
}
|
|
10392
|
+
};
|
|
10393
|
+
return this._refinement((val, ctx) => {
|
|
10394
|
+
const result = check(val);
|
|
10395
|
+
const setError = () => ctx.addIssue({
|
|
10396
|
+
code: ZodIssueCode.custom,
|
|
10397
|
+
...getIssueProperties(val)
|
|
10398
|
+
});
|
|
10399
|
+
if (typeof Promise !== "undefined" && result instanceof Promise) {
|
|
10400
|
+
return result.then((data) => {
|
|
10401
|
+
if (!data) {
|
|
10402
|
+
setError();
|
|
10403
|
+
return false;
|
|
10404
|
+
} else {
|
|
10405
|
+
return true;
|
|
10406
|
+
}
|
|
10407
|
+
});
|
|
10408
|
+
}
|
|
10409
|
+
if (!result) {
|
|
10410
|
+
setError();
|
|
10411
|
+
return false;
|
|
10412
|
+
} else {
|
|
10413
|
+
return true;
|
|
10414
|
+
}
|
|
10415
|
+
});
|
|
10416
|
+
}
|
|
10417
|
+
refinement(check, refinementData) {
|
|
10418
|
+
return this._refinement((val, ctx) => {
|
|
10419
|
+
if (!check(val)) {
|
|
10420
|
+
ctx.addIssue(typeof refinementData === "function" ? refinementData(val, ctx) : refinementData);
|
|
10421
|
+
return false;
|
|
10422
|
+
} else {
|
|
10423
|
+
return true;
|
|
10424
|
+
}
|
|
10425
|
+
});
|
|
10426
|
+
}
|
|
10427
|
+
_refinement(refinement) {
|
|
10428
|
+
return new ZodEffects({
|
|
10429
|
+
schema: this,
|
|
10430
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
10431
|
+
effect: { type: "refinement", refinement }
|
|
10432
|
+
});
|
|
10433
|
+
}
|
|
10434
|
+
superRefine(refinement) {
|
|
10435
|
+
return this._refinement(refinement);
|
|
10436
|
+
}
|
|
10437
|
+
constructor(def) {
|
|
10438
|
+
this.spa = this.safeParseAsync;
|
|
10439
|
+
this._def = def;
|
|
10440
|
+
this.parse = this.parse.bind(this);
|
|
10441
|
+
this.safeParse = this.safeParse.bind(this);
|
|
10442
|
+
this.parseAsync = this.parseAsync.bind(this);
|
|
10443
|
+
this.safeParseAsync = this.safeParseAsync.bind(this);
|
|
10444
|
+
this.spa = this.spa.bind(this);
|
|
10445
|
+
this.refine = this.refine.bind(this);
|
|
10446
|
+
this.refinement = this.refinement.bind(this);
|
|
10447
|
+
this.superRefine = this.superRefine.bind(this);
|
|
10448
|
+
this.optional = this.optional.bind(this);
|
|
10449
|
+
this.nullable = this.nullable.bind(this);
|
|
10450
|
+
this.nullish = this.nullish.bind(this);
|
|
10451
|
+
this.array = this.array.bind(this);
|
|
10452
|
+
this.promise = this.promise.bind(this);
|
|
10453
|
+
this.or = this.or.bind(this);
|
|
10454
|
+
this.and = this.and.bind(this);
|
|
10455
|
+
this.transform = this.transform.bind(this);
|
|
10456
|
+
this.brand = this.brand.bind(this);
|
|
10457
|
+
this.default = this.default.bind(this);
|
|
10458
|
+
this.catch = this.catch.bind(this);
|
|
10459
|
+
this.describe = this.describe.bind(this);
|
|
10460
|
+
this.pipe = this.pipe.bind(this);
|
|
10461
|
+
this.readonly = this.readonly.bind(this);
|
|
10462
|
+
this.isNullable = this.isNullable.bind(this);
|
|
10463
|
+
this.isOptional = this.isOptional.bind(this);
|
|
10464
|
+
this["~standard"] = {
|
|
10465
|
+
version: 1,
|
|
10466
|
+
vendor: "zod",
|
|
10467
|
+
validate: (data) => this["~validate"](data)
|
|
10468
|
+
};
|
|
10469
|
+
}
|
|
10470
|
+
optional() {
|
|
10471
|
+
return ZodOptional.create(this, this._def);
|
|
10472
|
+
}
|
|
10473
|
+
nullable() {
|
|
10474
|
+
return ZodNullable.create(this, this._def);
|
|
10475
|
+
}
|
|
10476
|
+
nullish() {
|
|
10477
|
+
return this.nullable().optional();
|
|
10478
|
+
}
|
|
10479
|
+
array() {
|
|
10480
|
+
return ZodArray.create(this);
|
|
10481
|
+
}
|
|
10482
|
+
promise() {
|
|
10483
|
+
return ZodPromise.create(this, this._def);
|
|
10484
|
+
}
|
|
10485
|
+
or(option) {
|
|
10486
|
+
return ZodUnion.create([this, option], this._def);
|
|
10487
|
+
}
|
|
10488
|
+
and(incoming) {
|
|
10489
|
+
return ZodIntersection.create(this, incoming, this._def);
|
|
10490
|
+
}
|
|
10491
|
+
transform(transform) {
|
|
10492
|
+
return new ZodEffects({
|
|
10493
|
+
...processCreateParams(this._def),
|
|
10494
|
+
schema: this,
|
|
10495
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
10496
|
+
effect: { type: "transform", transform }
|
|
10497
|
+
});
|
|
10498
|
+
}
|
|
10499
|
+
default(def) {
|
|
10500
|
+
const defaultValueFunc = typeof def === "function" ? def : () => def;
|
|
10501
|
+
return new ZodDefault({
|
|
10502
|
+
...processCreateParams(this._def),
|
|
10503
|
+
innerType: this,
|
|
10504
|
+
defaultValue: defaultValueFunc,
|
|
10505
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault
|
|
10506
|
+
});
|
|
10507
|
+
}
|
|
10508
|
+
brand() {
|
|
10509
|
+
return new ZodBranded({
|
|
10510
|
+
typeName: ZodFirstPartyTypeKind.ZodBranded,
|
|
10511
|
+
type: this,
|
|
10512
|
+
...processCreateParams(this._def)
|
|
10513
|
+
});
|
|
10514
|
+
}
|
|
10515
|
+
catch(def) {
|
|
10516
|
+
const catchValueFunc = typeof def === "function" ? def : () => def;
|
|
10517
|
+
return new ZodCatch({
|
|
10518
|
+
...processCreateParams(this._def),
|
|
10519
|
+
innerType: this,
|
|
10520
|
+
catchValue: catchValueFunc,
|
|
10521
|
+
typeName: ZodFirstPartyTypeKind.ZodCatch
|
|
10522
|
+
});
|
|
10523
|
+
}
|
|
10524
|
+
describe(description) {
|
|
10525
|
+
const This = this.constructor;
|
|
10526
|
+
return new This({
|
|
10527
|
+
...this._def,
|
|
10528
|
+
description
|
|
10529
|
+
});
|
|
10530
|
+
}
|
|
10531
|
+
pipe(target) {
|
|
10532
|
+
return ZodPipeline.create(this, target);
|
|
10533
|
+
}
|
|
10534
|
+
readonly() {
|
|
10535
|
+
return ZodReadonly.create(this);
|
|
10536
|
+
}
|
|
10537
|
+
isOptional() {
|
|
10538
|
+
return this.safeParse(undefined).success;
|
|
10539
|
+
}
|
|
10540
|
+
isNullable() {
|
|
10541
|
+
return this.safeParse(null).success;
|
|
10542
|
+
}
|
|
10543
|
+
}
|
|
10544
|
+
var cuidRegex = /^c[^\s-]{8,}$/i;
|
|
10545
|
+
var cuid2Regex = /^[0-9a-z]+$/;
|
|
10546
|
+
var ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
|
|
10547
|
+
var uuidRegex = /^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$/i;
|
|
10548
|
+
var nanoidRegex = /^[a-z0-9_-]{21}$/i;
|
|
10549
|
+
var jwtRegex = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/;
|
|
10550
|
+
var durationRegex = /^[-+]?P(?!$)(?:(?:[-+]?\d+Y)|(?:[-+]?\d+[.,]\d+Y$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:(?:[-+]?\d+W)|(?:[-+]?\d+[.,]\d+W$))?(?:(?:[-+]?\d+D)|(?:[-+]?\d+[.,]\d+D$))?(?:T(?=[\d+-])(?:(?:[-+]?\d+H)|(?:[-+]?\d+[.,]\d+H$))?(?:(?:[-+]?\d+M)|(?:[-+]?\d+[.,]\d+M$))?(?:[-+]?\d+(?:[.,]\d+)?S)?)??$/;
|
|
10551
|
+
var emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
|
|
10552
|
+
var _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
10553
|
+
var emojiRegex;
|
|
10554
|
+
var ipv4Regex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
|
|
10555
|
+
var ipv4CidrRegex = /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\/(3[0-2]|[12]?[0-9])$/;
|
|
10556
|
+
var ipv6Regex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))$/;
|
|
10557
|
+
var ipv6CidrRegex = /^(([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,7}:|([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}|([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}|([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}|([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}|([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}|[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})|:((:[0-9a-fA-F]{1,4}){1,7}|:)|fe80:(:[0-9a-fA-F]{0,4}){0,4}%[0-9a-zA-Z]{1,}|::(ffff(:0{1,4}){0,1}:){0,1}((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])|([0-9a-fA-F]{1,4}:){1,4}:((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9]))\/(12[0-8]|1[01][0-9]|[1-9]?[0-9])$/;
|
|
10558
|
+
var base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
10559
|
+
var base64urlRegex = /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/;
|
|
10560
|
+
var dateRegexSource = `((\\d\\d[2468][048]|\\d\\d[13579][26]|\\d\\d0[48]|[02468][048]00|[13579][26]00)-02-29|\\d{4}-((0[13578]|1[02])-(0[1-9]|[12]\\d|3[01])|(0[469]|11)-(0[1-9]|[12]\\d|30)|(02)-(0[1-9]|1\\d|2[0-8])))`;
|
|
10561
|
+
var dateRegex = new RegExp(`^${dateRegexSource}$`);
|
|
10562
|
+
function timeRegexSource(args) {
|
|
10563
|
+
let secondsRegexSource = `[0-5]\\d`;
|
|
10564
|
+
if (args.precision) {
|
|
10565
|
+
secondsRegexSource = `${secondsRegexSource}\\.\\d{${args.precision}}`;
|
|
10566
|
+
} else if (args.precision == null) {
|
|
10567
|
+
secondsRegexSource = `${secondsRegexSource}(\\.\\d+)?`;
|
|
10568
|
+
}
|
|
10569
|
+
const secondsQuantifier = args.precision ? "+" : "?";
|
|
10570
|
+
return `([01]\\d|2[0-3]):[0-5]\\d(:${secondsRegexSource})${secondsQuantifier}`;
|
|
10571
|
+
}
|
|
10572
|
+
function timeRegex(args) {
|
|
10573
|
+
return new RegExp(`^${timeRegexSource(args)}$`);
|
|
10574
|
+
}
|
|
10575
|
+
function datetimeRegex(args) {
|
|
10576
|
+
let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
|
|
10577
|
+
const opts = [];
|
|
10578
|
+
opts.push(args.local ? `Z?` : `Z`);
|
|
10579
|
+
if (args.offset)
|
|
10580
|
+
opts.push(`([+-]\\d{2}:?\\d{2})`);
|
|
10581
|
+
regex = `${regex}(${opts.join("|")})`;
|
|
10582
|
+
return new RegExp(`^${regex}$`);
|
|
10583
|
+
}
|
|
10584
|
+
function isValidIP(ip, version2) {
|
|
10585
|
+
if ((version2 === "v4" || !version2) && ipv4Regex.test(ip)) {
|
|
10586
|
+
return true;
|
|
10587
|
+
}
|
|
10588
|
+
if ((version2 === "v6" || !version2) && ipv6Regex.test(ip)) {
|
|
10589
|
+
return true;
|
|
10590
|
+
}
|
|
10591
|
+
return false;
|
|
10592
|
+
}
|
|
10593
|
+
function isValidJWT(jwt, alg) {
|
|
10594
|
+
if (!jwtRegex.test(jwt))
|
|
10595
|
+
return false;
|
|
10596
|
+
try {
|
|
10597
|
+
const [header] = jwt.split(".");
|
|
10598
|
+
if (!header)
|
|
10599
|
+
return false;
|
|
10600
|
+
const base64 = header.replace(/-/g, "+").replace(/_/g, "/").padEnd(header.length + (4 - header.length % 4) % 4, "=");
|
|
10601
|
+
const decoded = JSON.parse(atob(base64));
|
|
10602
|
+
if (typeof decoded !== "object" || decoded === null)
|
|
10603
|
+
return false;
|
|
10604
|
+
if ("typ" in decoded && decoded?.typ !== "JWT")
|
|
10605
|
+
return false;
|
|
10606
|
+
if (!decoded.alg)
|
|
10607
|
+
return false;
|
|
10608
|
+
if (alg && decoded.alg !== alg)
|
|
10609
|
+
return false;
|
|
10610
|
+
return true;
|
|
10611
|
+
} catch {
|
|
10612
|
+
return false;
|
|
10613
|
+
}
|
|
10614
|
+
}
|
|
10615
|
+
function isValidCidr(ip, version2) {
|
|
10616
|
+
if ((version2 === "v4" || !version2) && ipv4CidrRegex.test(ip)) {
|
|
10617
|
+
return true;
|
|
10618
|
+
}
|
|
10619
|
+
if ((version2 === "v6" || !version2) && ipv6CidrRegex.test(ip)) {
|
|
10620
|
+
return true;
|
|
10621
|
+
}
|
|
10622
|
+
return false;
|
|
10623
|
+
}
|
|
10624
|
+
|
|
10625
|
+
class ZodString extends ZodType {
|
|
10626
|
+
_parse(input) {
|
|
10627
|
+
if (this._def.coerce) {
|
|
10628
|
+
input.data = String(input.data);
|
|
10629
|
+
}
|
|
10630
|
+
const parsedType = this._getType(input);
|
|
10631
|
+
if (parsedType !== ZodParsedType.string) {
|
|
10632
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
10633
|
+
addIssueToContext(ctx2, {
|
|
10634
|
+
code: ZodIssueCode.invalid_type,
|
|
10635
|
+
expected: ZodParsedType.string,
|
|
10636
|
+
received: ctx2.parsedType
|
|
10637
|
+
});
|
|
10638
|
+
return INVALID;
|
|
10639
|
+
}
|
|
10640
|
+
const status = new ParseStatus;
|
|
10641
|
+
let ctx = undefined;
|
|
10642
|
+
for (const check of this._def.checks) {
|
|
10643
|
+
if (check.kind === "min") {
|
|
10644
|
+
if (input.data.length < check.value) {
|
|
10645
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10646
|
+
addIssueToContext(ctx, {
|
|
10647
|
+
code: ZodIssueCode.too_small,
|
|
10648
|
+
minimum: check.value,
|
|
10649
|
+
type: "string",
|
|
10650
|
+
inclusive: true,
|
|
10651
|
+
exact: false,
|
|
10652
|
+
message: check.message
|
|
10653
|
+
});
|
|
10654
|
+
status.dirty();
|
|
10655
|
+
}
|
|
10656
|
+
} else if (check.kind === "max") {
|
|
10657
|
+
if (input.data.length > check.value) {
|
|
10658
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10659
|
+
addIssueToContext(ctx, {
|
|
10660
|
+
code: ZodIssueCode.too_big,
|
|
10661
|
+
maximum: check.value,
|
|
10662
|
+
type: "string",
|
|
10663
|
+
inclusive: true,
|
|
10664
|
+
exact: false,
|
|
10665
|
+
message: check.message
|
|
10666
|
+
});
|
|
10667
|
+
status.dirty();
|
|
10668
|
+
}
|
|
10669
|
+
} else if (check.kind === "length") {
|
|
10670
|
+
const tooBig = input.data.length > check.value;
|
|
10671
|
+
const tooSmall = input.data.length < check.value;
|
|
10672
|
+
if (tooBig || tooSmall) {
|
|
10673
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10674
|
+
if (tooBig) {
|
|
10675
|
+
addIssueToContext(ctx, {
|
|
10676
|
+
code: ZodIssueCode.too_big,
|
|
10677
|
+
maximum: check.value,
|
|
10678
|
+
type: "string",
|
|
10679
|
+
inclusive: true,
|
|
10680
|
+
exact: true,
|
|
10681
|
+
message: check.message
|
|
10682
|
+
});
|
|
10683
|
+
} else if (tooSmall) {
|
|
10684
|
+
addIssueToContext(ctx, {
|
|
10685
|
+
code: ZodIssueCode.too_small,
|
|
10686
|
+
minimum: check.value,
|
|
10687
|
+
type: "string",
|
|
10688
|
+
inclusive: true,
|
|
10689
|
+
exact: true,
|
|
10690
|
+
message: check.message
|
|
10691
|
+
});
|
|
10692
|
+
}
|
|
10693
|
+
status.dirty();
|
|
10694
|
+
}
|
|
10695
|
+
} else if (check.kind === "email") {
|
|
10696
|
+
if (!emailRegex.test(input.data)) {
|
|
10697
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10698
|
+
addIssueToContext(ctx, {
|
|
10699
|
+
validation: "email",
|
|
10700
|
+
code: ZodIssueCode.invalid_string,
|
|
10701
|
+
message: check.message
|
|
10702
|
+
});
|
|
10703
|
+
status.dirty();
|
|
10704
|
+
}
|
|
10705
|
+
} else if (check.kind === "emoji") {
|
|
10706
|
+
if (!emojiRegex) {
|
|
10707
|
+
emojiRegex = new RegExp(_emojiRegex, "u");
|
|
10708
|
+
}
|
|
10709
|
+
if (!emojiRegex.test(input.data)) {
|
|
10710
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10711
|
+
addIssueToContext(ctx, {
|
|
10712
|
+
validation: "emoji",
|
|
10713
|
+
code: ZodIssueCode.invalid_string,
|
|
10714
|
+
message: check.message
|
|
10715
|
+
});
|
|
10716
|
+
status.dirty();
|
|
10717
|
+
}
|
|
10718
|
+
} else if (check.kind === "uuid") {
|
|
10719
|
+
if (!uuidRegex.test(input.data)) {
|
|
10720
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10721
|
+
addIssueToContext(ctx, {
|
|
10722
|
+
validation: "uuid",
|
|
10723
|
+
code: ZodIssueCode.invalid_string,
|
|
10724
|
+
message: check.message
|
|
10725
|
+
});
|
|
10726
|
+
status.dirty();
|
|
10727
|
+
}
|
|
10728
|
+
} else if (check.kind === "nanoid") {
|
|
10729
|
+
if (!nanoidRegex.test(input.data)) {
|
|
10730
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10731
|
+
addIssueToContext(ctx, {
|
|
10732
|
+
validation: "nanoid",
|
|
10733
|
+
code: ZodIssueCode.invalid_string,
|
|
10734
|
+
message: check.message
|
|
10735
|
+
});
|
|
10736
|
+
status.dirty();
|
|
10737
|
+
}
|
|
10738
|
+
} else if (check.kind === "cuid") {
|
|
10739
|
+
if (!cuidRegex.test(input.data)) {
|
|
10740
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10741
|
+
addIssueToContext(ctx, {
|
|
10742
|
+
validation: "cuid",
|
|
10743
|
+
code: ZodIssueCode.invalid_string,
|
|
10744
|
+
message: check.message
|
|
10745
|
+
});
|
|
10746
|
+
status.dirty();
|
|
10747
|
+
}
|
|
10748
|
+
} else if (check.kind === "cuid2") {
|
|
10749
|
+
if (!cuid2Regex.test(input.data)) {
|
|
10750
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10751
|
+
addIssueToContext(ctx, {
|
|
10752
|
+
validation: "cuid2",
|
|
10753
|
+
code: ZodIssueCode.invalid_string,
|
|
10754
|
+
message: check.message
|
|
10755
|
+
});
|
|
10756
|
+
status.dirty();
|
|
10757
|
+
}
|
|
10758
|
+
} else if (check.kind === "ulid") {
|
|
10759
|
+
if (!ulidRegex.test(input.data)) {
|
|
10760
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10761
|
+
addIssueToContext(ctx, {
|
|
10762
|
+
validation: "ulid",
|
|
10763
|
+
code: ZodIssueCode.invalid_string,
|
|
10764
|
+
message: check.message
|
|
10765
|
+
});
|
|
10766
|
+
status.dirty();
|
|
10767
|
+
}
|
|
10768
|
+
} else if (check.kind === "url") {
|
|
10769
|
+
try {
|
|
10770
|
+
new URL(input.data);
|
|
10771
|
+
} catch {
|
|
10772
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10773
|
+
addIssueToContext(ctx, {
|
|
10774
|
+
validation: "url",
|
|
10775
|
+
code: ZodIssueCode.invalid_string,
|
|
10776
|
+
message: check.message
|
|
10777
|
+
});
|
|
10778
|
+
status.dirty();
|
|
10779
|
+
}
|
|
10780
|
+
} else if (check.kind === "regex") {
|
|
10781
|
+
check.regex.lastIndex = 0;
|
|
10782
|
+
const testResult = check.regex.test(input.data);
|
|
10783
|
+
if (!testResult) {
|
|
10784
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10785
|
+
addIssueToContext(ctx, {
|
|
10786
|
+
validation: "regex",
|
|
10787
|
+
code: ZodIssueCode.invalid_string,
|
|
10788
|
+
message: check.message
|
|
10789
|
+
});
|
|
10790
|
+
status.dirty();
|
|
10791
|
+
}
|
|
10792
|
+
} else if (check.kind === "trim") {
|
|
10793
|
+
input.data = input.data.trim();
|
|
10794
|
+
} else if (check.kind === "includes") {
|
|
10795
|
+
if (!input.data.includes(check.value, check.position)) {
|
|
10796
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10797
|
+
addIssueToContext(ctx, {
|
|
10798
|
+
code: ZodIssueCode.invalid_string,
|
|
10799
|
+
validation: { includes: check.value, position: check.position },
|
|
10800
|
+
message: check.message
|
|
10801
|
+
});
|
|
10802
|
+
status.dirty();
|
|
10803
|
+
}
|
|
10804
|
+
} else if (check.kind === "toLowerCase") {
|
|
10805
|
+
input.data = input.data.toLowerCase();
|
|
10806
|
+
} else if (check.kind === "toUpperCase") {
|
|
10807
|
+
input.data = input.data.toUpperCase();
|
|
10808
|
+
} else if (check.kind === "startsWith") {
|
|
10809
|
+
if (!input.data.startsWith(check.value)) {
|
|
10810
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10811
|
+
addIssueToContext(ctx, {
|
|
10812
|
+
code: ZodIssueCode.invalid_string,
|
|
10813
|
+
validation: { startsWith: check.value },
|
|
10814
|
+
message: check.message
|
|
10815
|
+
});
|
|
10816
|
+
status.dirty();
|
|
10817
|
+
}
|
|
10818
|
+
} else if (check.kind === "endsWith") {
|
|
10819
|
+
if (!input.data.endsWith(check.value)) {
|
|
10820
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10821
|
+
addIssueToContext(ctx, {
|
|
10822
|
+
code: ZodIssueCode.invalid_string,
|
|
10823
|
+
validation: { endsWith: check.value },
|
|
10824
|
+
message: check.message
|
|
10825
|
+
});
|
|
10826
|
+
status.dirty();
|
|
10827
|
+
}
|
|
10828
|
+
} else if (check.kind === "datetime") {
|
|
10829
|
+
const regex = datetimeRegex(check);
|
|
10830
|
+
if (!regex.test(input.data)) {
|
|
10831
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10832
|
+
addIssueToContext(ctx, {
|
|
10833
|
+
code: ZodIssueCode.invalid_string,
|
|
10834
|
+
validation: "datetime",
|
|
10835
|
+
message: check.message
|
|
10836
|
+
});
|
|
10837
|
+
status.dirty();
|
|
10838
|
+
}
|
|
10839
|
+
} else if (check.kind === "date") {
|
|
10840
|
+
const regex = dateRegex;
|
|
10841
|
+
if (!regex.test(input.data)) {
|
|
10842
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10843
|
+
addIssueToContext(ctx, {
|
|
10844
|
+
code: ZodIssueCode.invalid_string,
|
|
10845
|
+
validation: "date",
|
|
10846
|
+
message: check.message
|
|
10847
|
+
});
|
|
10848
|
+
status.dirty();
|
|
10849
|
+
}
|
|
10850
|
+
} else if (check.kind === "time") {
|
|
10851
|
+
const regex = timeRegex(check);
|
|
10852
|
+
if (!regex.test(input.data)) {
|
|
10853
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10854
|
+
addIssueToContext(ctx, {
|
|
10855
|
+
code: ZodIssueCode.invalid_string,
|
|
10856
|
+
validation: "time",
|
|
10857
|
+
message: check.message
|
|
10858
|
+
});
|
|
10859
|
+
status.dirty();
|
|
10860
|
+
}
|
|
10861
|
+
} else if (check.kind === "duration") {
|
|
10862
|
+
if (!durationRegex.test(input.data)) {
|
|
10863
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10864
|
+
addIssueToContext(ctx, {
|
|
10865
|
+
validation: "duration",
|
|
10866
|
+
code: ZodIssueCode.invalid_string,
|
|
10867
|
+
message: check.message
|
|
10868
|
+
});
|
|
10869
|
+
status.dirty();
|
|
10870
|
+
}
|
|
10871
|
+
} else if (check.kind === "ip") {
|
|
10872
|
+
if (!isValidIP(input.data, check.version)) {
|
|
10873
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10874
|
+
addIssueToContext(ctx, {
|
|
10875
|
+
validation: "ip",
|
|
10876
|
+
code: ZodIssueCode.invalid_string,
|
|
10877
|
+
message: check.message
|
|
10878
|
+
});
|
|
10879
|
+
status.dirty();
|
|
10880
|
+
}
|
|
10881
|
+
} else if (check.kind === "jwt") {
|
|
10882
|
+
if (!isValidJWT(input.data, check.alg)) {
|
|
10883
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10884
|
+
addIssueToContext(ctx, {
|
|
10885
|
+
validation: "jwt",
|
|
10886
|
+
code: ZodIssueCode.invalid_string,
|
|
10887
|
+
message: check.message
|
|
10888
|
+
});
|
|
10889
|
+
status.dirty();
|
|
10890
|
+
}
|
|
10891
|
+
} else if (check.kind === "cidr") {
|
|
10892
|
+
if (!isValidCidr(input.data, check.version)) {
|
|
10893
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10894
|
+
addIssueToContext(ctx, {
|
|
10895
|
+
validation: "cidr",
|
|
10896
|
+
code: ZodIssueCode.invalid_string,
|
|
10897
|
+
message: check.message
|
|
10898
|
+
});
|
|
10899
|
+
status.dirty();
|
|
10900
|
+
}
|
|
10901
|
+
} else if (check.kind === "base64") {
|
|
10902
|
+
if (!base64Regex.test(input.data)) {
|
|
10903
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10904
|
+
addIssueToContext(ctx, {
|
|
10905
|
+
validation: "base64",
|
|
10906
|
+
code: ZodIssueCode.invalid_string,
|
|
10907
|
+
message: check.message
|
|
10908
|
+
});
|
|
10909
|
+
status.dirty();
|
|
10910
|
+
}
|
|
10911
|
+
} else if (check.kind === "base64url") {
|
|
10912
|
+
if (!base64urlRegex.test(input.data)) {
|
|
10913
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
10914
|
+
addIssueToContext(ctx, {
|
|
10915
|
+
validation: "base64url",
|
|
10916
|
+
code: ZodIssueCode.invalid_string,
|
|
10917
|
+
message: check.message
|
|
10918
|
+
});
|
|
10919
|
+
status.dirty();
|
|
10920
|
+
}
|
|
10921
|
+
} else {
|
|
10922
|
+
util.assertNever(check);
|
|
10923
|
+
}
|
|
10924
|
+
}
|
|
10925
|
+
return { status: status.value, value: input.data };
|
|
10926
|
+
}
|
|
10927
|
+
_regex(regex, validation, message) {
|
|
10928
|
+
return this.refinement((data) => regex.test(data), {
|
|
10929
|
+
validation,
|
|
10930
|
+
code: ZodIssueCode.invalid_string,
|
|
10931
|
+
...errorUtil.errToObj(message)
|
|
10932
|
+
});
|
|
10933
|
+
}
|
|
10934
|
+
_addCheck(check) {
|
|
10935
|
+
return new ZodString({
|
|
10936
|
+
...this._def,
|
|
10937
|
+
checks: [...this._def.checks, check]
|
|
10938
|
+
});
|
|
10939
|
+
}
|
|
10940
|
+
email(message) {
|
|
10941
|
+
return this._addCheck({ kind: "email", ...errorUtil.errToObj(message) });
|
|
10942
|
+
}
|
|
10943
|
+
url(message) {
|
|
10944
|
+
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
|
|
10945
|
+
}
|
|
10946
|
+
emoji(message) {
|
|
10947
|
+
return this._addCheck({ kind: "emoji", ...errorUtil.errToObj(message) });
|
|
10948
|
+
}
|
|
10949
|
+
uuid(message) {
|
|
10950
|
+
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
|
|
10951
|
+
}
|
|
10952
|
+
nanoid(message) {
|
|
10953
|
+
return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) });
|
|
10954
|
+
}
|
|
10955
|
+
cuid(message) {
|
|
10956
|
+
return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
|
|
10957
|
+
}
|
|
10958
|
+
cuid2(message) {
|
|
10959
|
+
return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
|
|
10960
|
+
}
|
|
10961
|
+
ulid(message) {
|
|
10962
|
+
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
|
|
10963
|
+
}
|
|
10964
|
+
base64(message) {
|
|
10965
|
+
return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
|
|
10966
|
+
}
|
|
10967
|
+
base64url(message) {
|
|
10968
|
+
return this._addCheck({
|
|
10969
|
+
kind: "base64url",
|
|
10970
|
+
...errorUtil.errToObj(message)
|
|
10971
|
+
});
|
|
10972
|
+
}
|
|
10973
|
+
jwt(options) {
|
|
10974
|
+
return this._addCheck({ kind: "jwt", ...errorUtil.errToObj(options) });
|
|
10975
|
+
}
|
|
10976
|
+
ip(options) {
|
|
10977
|
+
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
|
|
10978
|
+
}
|
|
10979
|
+
cidr(options) {
|
|
10980
|
+
return this._addCheck({ kind: "cidr", ...errorUtil.errToObj(options) });
|
|
10981
|
+
}
|
|
10982
|
+
datetime(options) {
|
|
10983
|
+
if (typeof options === "string") {
|
|
10984
|
+
return this._addCheck({
|
|
10985
|
+
kind: "datetime",
|
|
10986
|
+
precision: null,
|
|
10987
|
+
offset: false,
|
|
10988
|
+
local: false,
|
|
10989
|
+
message: options
|
|
10990
|
+
});
|
|
10991
|
+
}
|
|
10992
|
+
return this._addCheck({
|
|
10993
|
+
kind: "datetime",
|
|
10994
|
+
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
10995
|
+
offset: options?.offset ?? false,
|
|
10996
|
+
local: options?.local ?? false,
|
|
10997
|
+
...errorUtil.errToObj(options?.message)
|
|
10998
|
+
});
|
|
10999
|
+
}
|
|
11000
|
+
date(message) {
|
|
11001
|
+
return this._addCheck({ kind: "date", message });
|
|
11002
|
+
}
|
|
11003
|
+
time(options) {
|
|
11004
|
+
if (typeof options === "string") {
|
|
11005
|
+
return this._addCheck({
|
|
11006
|
+
kind: "time",
|
|
11007
|
+
precision: null,
|
|
11008
|
+
message: options
|
|
11009
|
+
});
|
|
11010
|
+
}
|
|
11011
|
+
return this._addCheck({
|
|
11012
|
+
kind: "time",
|
|
11013
|
+
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
11014
|
+
...errorUtil.errToObj(options?.message)
|
|
11015
|
+
});
|
|
11016
|
+
}
|
|
11017
|
+
duration(message) {
|
|
11018
|
+
return this._addCheck({ kind: "duration", ...errorUtil.errToObj(message) });
|
|
11019
|
+
}
|
|
11020
|
+
regex(regex, message) {
|
|
11021
|
+
return this._addCheck({
|
|
11022
|
+
kind: "regex",
|
|
11023
|
+
regex,
|
|
11024
|
+
...errorUtil.errToObj(message)
|
|
11025
|
+
});
|
|
11026
|
+
}
|
|
11027
|
+
includes(value, options) {
|
|
11028
|
+
return this._addCheck({
|
|
11029
|
+
kind: "includes",
|
|
11030
|
+
value,
|
|
11031
|
+
position: options?.position,
|
|
11032
|
+
...errorUtil.errToObj(options?.message)
|
|
11033
|
+
});
|
|
11034
|
+
}
|
|
11035
|
+
startsWith(value, message) {
|
|
11036
|
+
return this._addCheck({
|
|
11037
|
+
kind: "startsWith",
|
|
11038
|
+
value,
|
|
11039
|
+
...errorUtil.errToObj(message)
|
|
11040
|
+
});
|
|
11041
|
+
}
|
|
11042
|
+
endsWith(value, message) {
|
|
11043
|
+
return this._addCheck({
|
|
11044
|
+
kind: "endsWith",
|
|
11045
|
+
value,
|
|
11046
|
+
...errorUtil.errToObj(message)
|
|
11047
|
+
});
|
|
11048
|
+
}
|
|
11049
|
+
min(minLength, message) {
|
|
11050
|
+
return this._addCheck({
|
|
11051
|
+
kind: "min",
|
|
11052
|
+
value: minLength,
|
|
11053
|
+
...errorUtil.errToObj(message)
|
|
11054
|
+
});
|
|
11055
|
+
}
|
|
11056
|
+
max(maxLength, message) {
|
|
11057
|
+
return this._addCheck({
|
|
11058
|
+
kind: "max",
|
|
11059
|
+
value: maxLength,
|
|
11060
|
+
...errorUtil.errToObj(message)
|
|
11061
|
+
});
|
|
11062
|
+
}
|
|
11063
|
+
length(len, message) {
|
|
11064
|
+
return this._addCheck({
|
|
11065
|
+
kind: "length",
|
|
11066
|
+
value: len,
|
|
11067
|
+
...errorUtil.errToObj(message)
|
|
11068
|
+
});
|
|
11069
|
+
}
|
|
11070
|
+
nonempty(message) {
|
|
11071
|
+
return this.min(1, errorUtil.errToObj(message));
|
|
11072
|
+
}
|
|
11073
|
+
trim() {
|
|
11074
|
+
return new ZodString({
|
|
11075
|
+
...this._def,
|
|
11076
|
+
checks: [...this._def.checks, { kind: "trim" }]
|
|
11077
|
+
});
|
|
11078
|
+
}
|
|
11079
|
+
toLowerCase() {
|
|
11080
|
+
return new ZodString({
|
|
11081
|
+
...this._def,
|
|
11082
|
+
checks: [...this._def.checks, { kind: "toLowerCase" }]
|
|
11083
|
+
});
|
|
11084
|
+
}
|
|
11085
|
+
toUpperCase() {
|
|
11086
|
+
return new ZodString({
|
|
11087
|
+
...this._def,
|
|
11088
|
+
checks: [...this._def.checks, { kind: "toUpperCase" }]
|
|
11089
|
+
});
|
|
11090
|
+
}
|
|
11091
|
+
get isDatetime() {
|
|
11092
|
+
return !!this._def.checks.find((ch) => ch.kind === "datetime");
|
|
11093
|
+
}
|
|
11094
|
+
get isDate() {
|
|
11095
|
+
return !!this._def.checks.find((ch) => ch.kind === "date");
|
|
11096
|
+
}
|
|
11097
|
+
get isTime() {
|
|
11098
|
+
return !!this._def.checks.find((ch) => ch.kind === "time");
|
|
11099
|
+
}
|
|
11100
|
+
get isDuration() {
|
|
11101
|
+
return !!this._def.checks.find((ch) => ch.kind === "duration");
|
|
11102
|
+
}
|
|
11103
|
+
get isEmail() {
|
|
11104
|
+
return !!this._def.checks.find((ch) => ch.kind === "email");
|
|
11105
|
+
}
|
|
11106
|
+
get isURL() {
|
|
11107
|
+
return !!this._def.checks.find((ch) => ch.kind === "url");
|
|
11108
|
+
}
|
|
11109
|
+
get isEmoji() {
|
|
11110
|
+
return !!this._def.checks.find((ch) => ch.kind === "emoji");
|
|
11111
|
+
}
|
|
11112
|
+
get isUUID() {
|
|
11113
|
+
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
11114
|
+
}
|
|
11115
|
+
get isNANOID() {
|
|
11116
|
+
return !!this._def.checks.find((ch) => ch.kind === "nanoid");
|
|
11117
|
+
}
|
|
11118
|
+
get isCUID() {
|
|
11119
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
11120
|
+
}
|
|
11121
|
+
get isCUID2() {
|
|
11122
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid2");
|
|
11123
|
+
}
|
|
11124
|
+
get isULID() {
|
|
11125
|
+
return !!this._def.checks.find((ch) => ch.kind === "ulid");
|
|
11126
|
+
}
|
|
11127
|
+
get isIP() {
|
|
11128
|
+
return !!this._def.checks.find((ch) => ch.kind === "ip");
|
|
11129
|
+
}
|
|
11130
|
+
get isCIDR() {
|
|
11131
|
+
return !!this._def.checks.find((ch) => ch.kind === "cidr");
|
|
11132
|
+
}
|
|
11133
|
+
get isBase64() {
|
|
11134
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64");
|
|
11135
|
+
}
|
|
11136
|
+
get isBase64url() {
|
|
11137
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64url");
|
|
11138
|
+
}
|
|
11139
|
+
get minLength() {
|
|
11140
|
+
let min = null;
|
|
11141
|
+
for (const ch of this._def.checks) {
|
|
11142
|
+
if (ch.kind === "min") {
|
|
11143
|
+
if (min === null || ch.value > min)
|
|
11144
|
+
min = ch.value;
|
|
11145
|
+
}
|
|
11146
|
+
}
|
|
11147
|
+
return min;
|
|
11148
|
+
}
|
|
11149
|
+
get maxLength() {
|
|
11150
|
+
let max = null;
|
|
11151
|
+
for (const ch of this._def.checks) {
|
|
11152
|
+
if (ch.kind === "max") {
|
|
11153
|
+
if (max === null || ch.value < max)
|
|
11154
|
+
max = ch.value;
|
|
11155
|
+
}
|
|
11156
|
+
}
|
|
11157
|
+
return max;
|
|
11158
|
+
}
|
|
11159
|
+
}
|
|
11160
|
+
ZodString.create = (params) => {
|
|
11161
|
+
return new ZodString({
|
|
11162
|
+
checks: [],
|
|
11163
|
+
typeName: ZodFirstPartyTypeKind.ZodString,
|
|
11164
|
+
coerce: params?.coerce ?? false,
|
|
11165
|
+
...processCreateParams(params)
|
|
11166
|
+
});
|
|
11167
|
+
};
|
|
11168
|
+
function floatSafeRemainder(val, step) {
|
|
11169
|
+
const valDecCount = (val.toString().split(".")[1] || "").length;
|
|
11170
|
+
const stepDecCount = (step.toString().split(".")[1] || "").length;
|
|
11171
|
+
const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
|
|
11172
|
+
const valInt = Number.parseInt(val.toFixed(decCount).replace(".", ""));
|
|
11173
|
+
const stepInt = Number.parseInt(step.toFixed(decCount).replace(".", ""));
|
|
11174
|
+
return valInt % stepInt / 10 ** decCount;
|
|
11175
|
+
}
|
|
11176
|
+
|
|
11177
|
+
class ZodNumber extends ZodType {
|
|
11178
|
+
constructor() {
|
|
11179
|
+
super(...arguments);
|
|
11180
|
+
this.min = this.gte;
|
|
11181
|
+
this.max = this.lte;
|
|
11182
|
+
this.step = this.multipleOf;
|
|
11183
|
+
}
|
|
11184
|
+
_parse(input) {
|
|
11185
|
+
if (this._def.coerce) {
|
|
11186
|
+
input.data = Number(input.data);
|
|
11187
|
+
}
|
|
11188
|
+
const parsedType = this._getType(input);
|
|
11189
|
+
if (parsedType !== ZodParsedType.number) {
|
|
11190
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
11191
|
+
addIssueToContext(ctx2, {
|
|
11192
|
+
code: ZodIssueCode.invalid_type,
|
|
11193
|
+
expected: ZodParsedType.number,
|
|
11194
|
+
received: ctx2.parsedType
|
|
11195
|
+
});
|
|
11196
|
+
return INVALID;
|
|
11197
|
+
}
|
|
11198
|
+
let ctx = undefined;
|
|
11199
|
+
const status = new ParseStatus;
|
|
11200
|
+
for (const check of this._def.checks) {
|
|
11201
|
+
if (check.kind === "int") {
|
|
11202
|
+
if (!util.isInteger(input.data)) {
|
|
11203
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
11204
|
+
addIssueToContext(ctx, {
|
|
11205
|
+
code: ZodIssueCode.invalid_type,
|
|
11206
|
+
expected: "integer",
|
|
11207
|
+
received: "float",
|
|
11208
|
+
message: check.message
|
|
11209
|
+
});
|
|
11210
|
+
status.dirty();
|
|
11211
|
+
}
|
|
11212
|
+
} else if (check.kind === "min") {
|
|
11213
|
+
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
11214
|
+
if (tooSmall) {
|
|
11215
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
11216
|
+
addIssueToContext(ctx, {
|
|
11217
|
+
code: ZodIssueCode.too_small,
|
|
11218
|
+
minimum: check.value,
|
|
11219
|
+
type: "number",
|
|
11220
|
+
inclusive: check.inclusive,
|
|
11221
|
+
exact: false,
|
|
11222
|
+
message: check.message
|
|
11223
|
+
});
|
|
11224
|
+
status.dirty();
|
|
11225
|
+
}
|
|
11226
|
+
} else if (check.kind === "max") {
|
|
11227
|
+
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
11228
|
+
if (tooBig) {
|
|
11229
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
11230
|
+
addIssueToContext(ctx, {
|
|
11231
|
+
code: ZodIssueCode.too_big,
|
|
11232
|
+
maximum: check.value,
|
|
11233
|
+
type: "number",
|
|
11234
|
+
inclusive: check.inclusive,
|
|
11235
|
+
exact: false,
|
|
11236
|
+
message: check.message
|
|
11237
|
+
});
|
|
11238
|
+
status.dirty();
|
|
11239
|
+
}
|
|
11240
|
+
} else if (check.kind === "multipleOf") {
|
|
11241
|
+
if (floatSafeRemainder(input.data, check.value) !== 0) {
|
|
11242
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
11243
|
+
addIssueToContext(ctx, {
|
|
11244
|
+
code: ZodIssueCode.not_multiple_of,
|
|
11245
|
+
multipleOf: check.value,
|
|
11246
|
+
message: check.message
|
|
11247
|
+
});
|
|
11248
|
+
status.dirty();
|
|
11249
|
+
}
|
|
11250
|
+
} else if (check.kind === "finite") {
|
|
11251
|
+
if (!Number.isFinite(input.data)) {
|
|
11252
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
11253
|
+
addIssueToContext(ctx, {
|
|
11254
|
+
code: ZodIssueCode.not_finite,
|
|
11255
|
+
message: check.message
|
|
11256
|
+
});
|
|
11257
|
+
status.dirty();
|
|
11258
|
+
}
|
|
11259
|
+
} else {
|
|
11260
|
+
util.assertNever(check);
|
|
11261
|
+
}
|
|
11262
|
+
}
|
|
11263
|
+
return { status: status.value, value: input.data };
|
|
11264
|
+
}
|
|
11265
|
+
gte(value, message) {
|
|
11266
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
11267
|
+
}
|
|
11268
|
+
gt(value, message) {
|
|
11269
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
11270
|
+
}
|
|
11271
|
+
lte(value, message) {
|
|
11272
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
11273
|
+
}
|
|
11274
|
+
lt(value, message) {
|
|
11275
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
11276
|
+
}
|
|
11277
|
+
setLimit(kind2, value, inclusive, message) {
|
|
11278
|
+
return new ZodNumber({
|
|
11279
|
+
...this._def,
|
|
11280
|
+
checks: [
|
|
11281
|
+
...this._def.checks,
|
|
11282
|
+
{
|
|
11283
|
+
kind: kind2,
|
|
11284
|
+
value,
|
|
11285
|
+
inclusive,
|
|
11286
|
+
message: errorUtil.toString(message)
|
|
11287
|
+
}
|
|
11288
|
+
]
|
|
11289
|
+
});
|
|
11290
|
+
}
|
|
11291
|
+
_addCheck(check) {
|
|
11292
|
+
return new ZodNumber({
|
|
11293
|
+
...this._def,
|
|
11294
|
+
checks: [...this._def.checks, check]
|
|
11295
|
+
});
|
|
11296
|
+
}
|
|
11297
|
+
int(message) {
|
|
11298
|
+
return this._addCheck({
|
|
11299
|
+
kind: "int",
|
|
11300
|
+
message: errorUtil.toString(message)
|
|
11301
|
+
});
|
|
11302
|
+
}
|
|
11303
|
+
positive(message) {
|
|
11304
|
+
return this._addCheck({
|
|
11305
|
+
kind: "min",
|
|
11306
|
+
value: 0,
|
|
11307
|
+
inclusive: false,
|
|
11308
|
+
message: errorUtil.toString(message)
|
|
11309
|
+
});
|
|
11310
|
+
}
|
|
11311
|
+
negative(message) {
|
|
11312
|
+
return this._addCheck({
|
|
11313
|
+
kind: "max",
|
|
11314
|
+
value: 0,
|
|
11315
|
+
inclusive: false,
|
|
11316
|
+
message: errorUtil.toString(message)
|
|
11317
|
+
});
|
|
11318
|
+
}
|
|
11319
|
+
nonpositive(message) {
|
|
11320
|
+
return this._addCheck({
|
|
11321
|
+
kind: "max",
|
|
11322
|
+
value: 0,
|
|
11323
|
+
inclusive: true,
|
|
11324
|
+
message: errorUtil.toString(message)
|
|
11325
|
+
});
|
|
11326
|
+
}
|
|
11327
|
+
nonnegative(message) {
|
|
11328
|
+
return this._addCheck({
|
|
11329
|
+
kind: "min",
|
|
11330
|
+
value: 0,
|
|
11331
|
+
inclusive: true,
|
|
11332
|
+
message: errorUtil.toString(message)
|
|
11333
|
+
});
|
|
11334
|
+
}
|
|
11335
|
+
multipleOf(value, message) {
|
|
11336
|
+
return this._addCheck({
|
|
11337
|
+
kind: "multipleOf",
|
|
11338
|
+
value,
|
|
11339
|
+
message: errorUtil.toString(message)
|
|
11340
|
+
});
|
|
11341
|
+
}
|
|
11342
|
+
finite(message) {
|
|
11343
|
+
return this._addCheck({
|
|
11344
|
+
kind: "finite",
|
|
11345
|
+
message: errorUtil.toString(message)
|
|
11346
|
+
});
|
|
11347
|
+
}
|
|
11348
|
+
safe(message) {
|
|
11349
|
+
return this._addCheck({
|
|
11350
|
+
kind: "min",
|
|
11351
|
+
inclusive: true,
|
|
11352
|
+
value: Number.MIN_SAFE_INTEGER,
|
|
11353
|
+
message: errorUtil.toString(message)
|
|
11354
|
+
})._addCheck({
|
|
11355
|
+
kind: "max",
|
|
11356
|
+
inclusive: true,
|
|
11357
|
+
value: Number.MAX_SAFE_INTEGER,
|
|
11358
|
+
message: errorUtil.toString(message)
|
|
11359
|
+
});
|
|
11360
|
+
}
|
|
11361
|
+
get minValue() {
|
|
11362
|
+
let min = null;
|
|
11363
|
+
for (const ch of this._def.checks) {
|
|
11364
|
+
if (ch.kind === "min") {
|
|
11365
|
+
if (min === null || ch.value > min)
|
|
11366
|
+
min = ch.value;
|
|
11367
|
+
}
|
|
11368
|
+
}
|
|
11369
|
+
return min;
|
|
11370
|
+
}
|
|
11371
|
+
get maxValue() {
|
|
11372
|
+
let max = null;
|
|
11373
|
+
for (const ch of this._def.checks) {
|
|
11374
|
+
if (ch.kind === "max") {
|
|
11375
|
+
if (max === null || ch.value < max)
|
|
11376
|
+
max = ch.value;
|
|
11377
|
+
}
|
|
11378
|
+
}
|
|
11379
|
+
return max;
|
|
11380
|
+
}
|
|
11381
|
+
get isInt() {
|
|
11382
|
+
return !!this._def.checks.find((ch) => ch.kind === "int" || ch.kind === "multipleOf" && util.isInteger(ch.value));
|
|
11383
|
+
}
|
|
11384
|
+
get isFinite() {
|
|
11385
|
+
let max = null;
|
|
11386
|
+
let min = null;
|
|
11387
|
+
for (const ch of this._def.checks) {
|
|
11388
|
+
if (ch.kind === "finite" || ch.kind === "int" || ch.kind === "multipleOf") {
|
|
11389
|
+
return true;
|
|
11390
|
+
} else if (ch.kind === "min") {
|
|
11391
|
+
if (min === null || ch.value > min)
|
|
11392
|
+
min = ch.value;
|
|
11393
|
+
} else if (ch.kind === "max") {
|
|
11394
|
+
if (max === null || ch.value < max)
|
|
11395
|
+
max = ch.value;
|
|
11396
|
+
}
|
|
11397
|
+
}
|
|
11398
|
+
return Number.isFinite(min) && Number.isFinite(max);
|
|
11399
|
+
}
|
|
11400
|
+
}
|
|
11401
|
+
ZodNumber.create = (params) => {
|
|
11402
|
+
return new ZodNumber({
|
|
11403
|
+
checks: [],
|
|
11404
|
+
typeName: ZodFirstPartyTypeKind.ZodNumber,
|
|
11405
|
+
coerce: params?.coerce || false,
|
|
11406
|
+
...processCreateParams(params)
|
|
11407
|
+
});
|
|
11408
|
+
};
|
|
11409
|
+
|
|
11410
|
+
class ZodBigInt extends ZodType {
|
|
11411
|
+
constructor() {
|
|
11412
|
+
super(...arguments);
|
|
11413
|
+
this.min = this.gte;
|
|
11414
|
+
this.max = this.lte;
|
|
11415
|
+
}
|
|
11416
|
+
_parse(input) {
|
|
11417
|
+
if (this._def.coerce) {
|
|
11418
|
+
try {
|
|
11419
|
+
input.data = BigInt(input.data);
|
|
11420
|
+
} catch {
|
|
11421
|
+
return this._getInvalidInput(input);
|
|
11422
|
+
}
|
|
11423
|
+
}
|
|
11424
|
+
const parsedType = this._getType(input);
|
|
11425
|
+
if (parsedType !== ZodParsedType.bigint) {
|
|
11426
|
+
return this._getInvalidInput(input);
|
|
11427
|
+
}
|
|
11428
|
+
let ctx = undefined;
|
|
11429
|
+
const status = new ParseStatus;
|
|
11430
|
+
for (const check of this._def.checks) {
|
|
11431
|
+
if (check.kind === "min") {
|
|
11432
|
+
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
11433
|
+
if (tooSmall) {
|
|
11434
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
11435
|
+
addIssueToContext(ctx, {
|
|
11436
|
+
code: ZodIssueCode.too_small,
|
|
11437
|
+
type: "bigint",
|
|
11438
|
+
minimum: check.value,
|
|
11439
|
+
inclusive: check.inclusive,
|
|
11440
|
+
message: check.message
|
|
11441
|
+
});
|
|
11442
|
+
status.dirty();
|
|
11443
|
+
}
|
|
11444
|
+
} else if (check.kind === "max") {
|
|
11445
|
+
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
11446
|
+
if (tooBig) {
|
|
11447
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
11448
|
+
addIssueToContext(ctx, {
|
|
11449
|
+
code: ZodIssueCode.too_big,
|
|
11450
|
+
type: "bigint",
|
|
11451
|
+
maximum: check.value,
|
|
11452
|
+
inclusive: check.inclusive,
|
|
11453
|
+
message: check.message
|
|
11454
|
+
});
|
|
11455
|
+
status.dirty();
|
|
11456
|
+
}
|
|
11457
|
+
} else if (check.kind === "multipleOf") {
|
|
11458
|
+
if (input.data % check.value !== BigInt(0)) {
|
|
11459
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
11460
|
+
addIssueToContext(ctx, {
|
|
11461
|
+
code: ZodIssueCode.not_multiple_of,
|
|
11462
|
+
multipleOf: check.value,
|
|
11463
|
+
message: check.message
|
|
11464
|
+
});
|
|
11465
|
+
status.dirty();
|
|
11466
|
+
}
|
|
11467
|
+
} else {
|
|
11468
|
+
util.assertNever(check);
|
|
11469
|
+
}
|
|
11470
|
+
}
|
|
11471
|
+
return { status: status.value, value: input.data };
|
|
11472
|
+
}
|
|
11473
|
+
_getInvalidInput(input) {
|
|
11474
|
+
const ctx = this._getOrReturnCtx(input);
|
|
11475
|
+
addIssueToContext(ctx, {
|
|
11476
|
+
code: ZodIssueCode.invalid_type,
|
|
11477
|
+
expected: ZodParsedType.bigint,
|
|
11478
|
+
received: ctx.parsedType
|
|
11479
|
+
});
|
|
11480
|
+
return INVALID;
|
|
11481
|
+
}
|
|
11482
|
+
gte(value, message) {
|
|
11483
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
11484
|
+
}
|
|
11485
|
+
gt(value, message) {
|
|
11486
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
11487
|
+
}
|
|
11488
|
+
lte(value, message) {
|
|
11489
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
11490
|
+
}
|
|
11491
|
+
lt(value, message) {
|
|
11492
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
11493
|
+
}
|
|
11494
|
+
setLimit(kind2, value, inclusive, message) {
|
|
11495
|
+
return new ZodBigInt({
|
|
11496
|
+
...this._def,
|
|
11497
|
+
checks: [
|
|
11498
|
+
...this._def.checks,
|
|
11499
|
+
{
|
|
11500
|
+
kind: kind2,
|
|
11501
|
+
value,
|
|
11502
|
+
inclusive,
|
|
11503
|
+
message: errorUtil.toString(message)
|
|
11504
|
+
}
|
|
11505
|
+
]
|
|
11506
|
+
});
|
|
11507
|
+
}
|
|
11508
|
+
_addCheck(check) {
|
|
11509
|
+
return new ZodBigInt({
|
|
11510
|
+
...this._def,
|
|
11511
|
+
checks: [...this._def.checks, check]
|
|
11512
|
+
});
|
|
11513
|
+
}
|
|
11514
|
+
positive(message) {
|
|
11515
|
+
return this._addCheck({
|
|
11516
|
+
kind: "min",
|
|
11517
|
+
value: BigInt(0),
|
|
11518
|
+
inclusive: false,
|
|
11519
|
+
message: errorUtil.toString(message)
|
|
11520
|
+
});
|
|
11521
|
+
}
|
|
11522
|
+
negative(message) {
|
|
11523
|
+
return this._addCheck({
|
|
11524
|
+
kind: "max",
|
|
11525
|
+
value: BigInt(0),
|
|
11526
|
+
inclusive: false,
|
|
11527
|
+
message: errorUtil.toString(message)
|
|
11528
|
+
});
|
|
11529
|
+
}
|
|
11530
|
+
nonpositive(message) {
|
|
11531
|
+
return this._addCheck({
|
|
11532
|
+
kind: "max",
|
|
11533
|
+
value: BigInt(0),
|
|
11534
|
+
inclusive: true,
|
|
11535
|
+
message: errorUtil.toString(message)
|
|
11536
|
+
});
|
|
11537
|
+
}
|
|
11538
|
+
nonnegative(message) {
|
|
11539
|
+
return this._addCheck({
|
|
11540
|
+
kind: "min",
|
|
11541
|
+
value: BigInt(0),
|
|
11542
|
+
inclusive: true,
|
|
11543
|
+
message: errorUtil.toString(message)
|
|
11544
|
+
});
|
|
11545
|
+
}
|
|
11546
|
+
multipleOf(value, message) {
|
|
11547
|
+
return this._addCheck({
|
|
11548
|
+
kind: "multipleOf",
|
|
11549
|
+
value,
|
|
11550
|
+
message: errorUtil.toString(message)
|
|
11551
|
+
});
|
|
11552
|
+
}
|
|
11553
|
+
get minValue() {
|
|
11554
|
+
let min = null;
|
|
11555
|
+
for (const ch of this._def.checks) {
|
|
11556
|
+
if (ch.kind === "min") {
|
|
11557
|
+
if (min === null || ch.value > min)
|
|
11558
|
+
min = ch.value;
|
|
11559
|
+
}
|
|
11560
|
+
}
|
|
11561
|
+
return min;
|
|
11562
|
+
}
|
|
11563
|
+
get maxValue() {
|
|
11564
|
+
let max = null;
|
|
11565
|
+
for (const ch of this._def.checks) {
|
|
11566
|
+
if (ch.kind === "max") {
|
|
11567
|
+
if (max === null || ch.value < max)
|
|
11568
|
+
max = ch.value;
|
|
11569
|
+
}
|
|
11570
|
+
}
|
|
11571
|
+
return max;
|
|
11572
|
+
}
|
|
11573
|
+
}
|
|
11574
|
+
ZodBigInt.create = (params) => {
|
|
11575
|
+
return new ZodBigInt({
|
|
11576
|
+
checks: [],
|
|
11577
|
+
typeName: ZodFirstPartyTypeKind.ZodBigInt,
|
|
11578
|
+
coerce: params?.coerce ?? false,
|
|
11579
|
+
...processCreateParams(params)
|
|
11580
|
+
});
|
|
11581
|
+
};
|
|
11582
|
+
|
|
11583
|
+
class ZodBoolean extends ZodType {
|
|
11584
|
+
_parse(input) {
|
|
11585
|
+
if (this._def.coerce) {
|
|
11586
|
+
input.data = Boolean(input.data);
|
|
11587
|
+
}
|
|
11588
|
+
const parsedType = this._getType(input);
|
|
11589
|
+
if (parsedType !== ZodParsedType.boolean) {
|
|
11590
|
+
const ctx = this._getOrReturnCtx(input);
|
|
11591
|
+
addIssueToContext(ctx, {
|
|
11592
|
+
code: ZodIssueCode.invalid_type,
|
|
11593
|
+
expected: ZodParsedType.boolean,
|
|
11594
|
+
received: ctx.parsedType
|
|
11595
|
+
});
|
|
11596
|
+
return INVALID;
|
|
11597
|
+
}
|
|
11598
|
+
return OK(input.data);
|
|
11599
|
+
}
|
|
11600
|
+
}
|
|
11601
|
+
ZodBoolean.create = (params) => {
|
|
11602
|
+
return new ZodBoolean({
|
|
11603
|
+
typeName: ZodFirstPartyTypeKind.ZodBoolean,
|
|
11604
|
+
coerce: params?.coerce || false,
|
|
11605
|
+
...processCreateParams(params)
|
|
11606
|
+
});
|
|
11607
|
+
};
|
|
11608
|
+
|
|
11609
|
+
class ZodDate extends ZodType {
|
|
11610
|
+
_parse(input) {
|
|
11611
|
+
if (this._def.coerce) {
|
|
11612
|
+
input.data = new Date(input.data);
|
|
11613
|
+
}
|
|
11614
|
+
const parsedType = this._getType(input);
|
|
11615
|
+
if (parsedType !== ZodParsedType.date) {
|
|
11616
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
11617
|
+
addIssueToContext(ctx2, {
|
|
11618
|
+
code: ZodIssueCode.invalid_type,
|
|
11619
|
+
expected: ZodParsedType.date,
|
|
11620
|
+
received: ctx2.parsedType
|
|
11621
|
+
});
|
|
11622
|
+
return INVALID;
|
|
11623
|
+
}
|
|
11624
|
+
if (Number.isNaN(input.data.getTime())) {
|
|
11625
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
11626
|
+
addIssueToContext(ctx2, {
|
|
11627
|
+
code: ZodIssueCode.invalid_date
|
|
11628
|
+
});
|
|
11629
|
+
return INVALID;
|
|
11630
|
+
}
|
|
11631
|
+
const status = new ParseStatus;
|
|
11632
|
+
let ctx = undefined;
|
|
11633
|
+
for (const check of this._def.checks) {
|
|
11634
|
+
if (check.kind === "min") {
|
|
11635
|
+
if (input.data.getTime() < check.value) {
|
|
11636
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
11637
|
+
addIssueToContext(ctx, {
|
|
11638
|
+
code: ZodIssueCode.too_small,
|
|
11639
|
+
message: check.message,
|
|
11640
|
+
inclusive: true,
|
|
11641
|
+
exact: false,
|
|
11642
|
+
minimum: check.value,
|
|
11643
|
+
type: "date"
|
|
11644
|
+
});
|
|
11645
|
+
status.dirty();
|
|
11646
|
+
}
|
|
11647
|
+
} else if (check.kind === "max") {
|
|
11648
|
+
if (input.data.getTime() > check.value) {
|
|
11649
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
11650
|
+
addIssueToContext(ctx, {
|
|
11651
|
+
code: ZodIssueCode.too_big,
|
|
11652
|
+
message: check.message,
|
|
11653
|
+
inclusive: true,
|
|
11654
|
+
exact: false,
|
|
11655
|
+
maximum: check.value,
|
|
11656
|
+
type: "date"
|
|
11657
|
+
});
|
|
11658
|
+
status.dirty();
|
|
11659
|
+
}
|
|
11660
|
+
} else {
|
|
11661
|
+
util.assertNever(check);
|
|
11662
|
+
}
|
|
11663
|
+
}
|
|
11664
|
+
return {
|
|
11665
|
+
status: status.value,
|
|
11666
|
+
value: new Date(input.data.getTime())
|
|
11667
|
+
};
|
|
11668
|
+
}
|
|
11669
|
+
_addCheck(check) {
|
|
11670
|
+
return new ZodDate({
|
|
11671
|
+
...this._def,
|
|
11672
|
+
checks: [...this._def.checks, check]
|
|
11673
|
+
});
|
|
11674
|
+
}
|
|
11675
|
+
min(minDate, message) {
|
|
11676
|
+
return this._addCheck({
|
|
11677
|
+
kind: "min",
|
|
11678
|
+
value: minDate.getTime(),
|
|
11679
|
+
message: errorUtil.toString(message)
|
|
11680
|
+
});
|
|
11681
|
+
}
|
|
11682
|
+
max(maxDate, message) {
|
|
11683
|
+
return this._addCheck({
|
|
11684
|
+
kind: "max",
|
|
11685
|
+
value: maxDate.getTime(),
|
|
11686
|
+
message: errorUtil.toString(message)
|
|
11687
|
+
});
|
|
11688
|
+
}
|
|
11689
|
+
get minDate() {
|
|
11690
|
+
let min = null;
|
|
11691
|
+
for (const ch of this._def.checks) {
|
|
11692
|
+
if (ch.kind === "min") {
|
|
11693
|
+
if (min === null || ch.value > min)
|
|
11694
|
+
min = ch.value;
|
|
11695
|
+
}
|
|
11696
|
+
}
|
|
11697
|
+
return min != null ? new Date(min) : null;
|
|
11698
|
+
}
|
|
11699
|
+
get maxDate() {
|
|
11700
|
+
let max = null;
|
|
11701
|
+
for (const ch of this._def.checks) {
|
|
11702
|
+
if (ch.kind === "max") {
|
|
11703
|
+
if (max === null || ch.value < max)
|
|
11704
|
+
max = ch.value;
|
|
11705
|
+
}
|
|
11706
|
+
}
|
|
11707
|
+
return max != null ? new Date(max) : null;
|
|
11708
|
+
}
|
|
11709
|
+
}
|
|
11710
|
+
ZodDate.create = (params) => {
|
|
11711
|
+
return new ZodDate({
|
|
11712
|
+
checks: [],
|
|
11713
|
+
coerce: params?.coerce || false,
|
|
11714
|
+
typeName: ZodFirstPartyTypeKind.ZodDate,
|
|
11715
|
+
...processCreateParams(params)
|
|
11716
|
+
});
|
|
11717
|
+
};
|
|
11718
|
+
|
|
11719
|
+
class ZodSymbol extends ZodType {
|
|
11720
|
+
_parse(input) {
|
|
11721
|
+
const parsedType = this._getType(input);
|
|
11722
|
+
if (parsedType !== ZodParsedType.symbol) {
|
|
11723
|
+
const ctx = this._getOrReturnCtx(input);
|
|
11724
|
+
addIssueToContext(ctx, {
|
|
11725
|
+
code: ZodIssueCode.invalid_type,
|
|
11726
|
+
expected: ZodParsedType.symbol,
|
|
11727
|
+
received: ctx.parsedType
|
|
11728
|
+
});
|
|
11729
|
+
return INVALID;
|
|
11730
|
+
}
|
|
11731
|
+
return OK(input.data);
|
|
11732
|
+
}
|
|
11733
|
+
}
|
|
11734
|
+
ZodSymbol.create = (params) => {
|
|
11735
|
+
return new ZodSymbol({
|
|
11736
|
+
typeName: ZodFirstPartyTypeKind.ZodSymbol,
|
|
11737
|
+
...processCreateParams(params)
|
|
11738
|
+
});
|
|
11739
|
+
};
|
|
11740
|
+
|
|
11741
|
+
class ZodUndefined extends ZodType {
|
|
11742
|
+
_parse(input) {
|
|
11743
|
+
const parsedType = this._getType(input);
|
|
11744
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
11745
|
+
const ctx = this._getOrReturnCtx(input);
|
|
11746
|
+
addIssueToContext(ctx, {
|
|
11747
|
+
code: ZodIssueCode.invalid_type,
|
|
11748
|
+
expected: ZodParsedType.undefined,
|
|
11749
|
+
received: ctx.parsedType
|
|
11750
|
+
});
|
|
11751
|
+
return INVALID;
|
|
11752
|
+
}
|
|
11753
|
+
return OK(input.data);
|
|
11754
|
+
}
|
|
11755
|
+
}
|
|
11756
|
+
ZodUndefined.create = (params) => {
|
|
11757
|
+
return new ZodUndefined({
|
|
11758
|
+
typeName: ZodFirstPartyTypeKind.ZodUndefined,
|
|
11759
|
+
...processCreateParams(params)
|
|
11760
|
+
});
|
|
11761
|
+
};
|
|
11762
|
+
|
|
11763
|
+
class ZodNull extends ZodType {
|
|
11764
|
+
_parse(input) {
|
|
11765
|
+
const parsedType = this._getType(input);
|
|
11766
|
+
if (parsedType !== ZodParsedType.null) {
|
|
11767
|
+
const ctx = this._getOrReturnCtx(input);
|
|
11768
|
+
addIssueToContext(ctx, {
|
|
11769
|
+
code: ZodIssueCode.invalid_type,
|
|
11770
|
+
expected: ZodParsedType.null,
|
|
11771
|
+
received: ctx.parsedType
|
|
11772
|
+
});
|
|
11773
|
+
return INVALID;
|
|
11774
|
+
}
|
|
11775
|
+
return OK(input.data);
|
|
11776
|
+
}
|
|
11777
|
+
}
|
|
11778
|
+
ZodNull.create = (params) => {
|
|
11779
|
+
return new ZodNull({
|
|
11780
|
+
typeName: ZodFirstPartyTypeKind.ZodNull,
|
|
11781
|
+
...processCreateParams(params)
|
|
11782
|
+
});
|
|
11783
|
+
};
|
|
11784
|
+
|
|
11785
|
+
class ZodAny extends ZodType {
|
|
11786
|
+
constructor() {
|
|
11787
|
+
super(...arguments);
|
|
11788
|
+
this._any = true;
|
|
11789
|
+
}
|
|
11790
|
+
_parse(input) {
|
|
11791
|
+
return OK(input.data);
|
|
11792
|
+
}
|
|
11793
|
+
}
|
|
11794
|
+
ZodAny.create = (params) => {
|
|
11795
|
+
return new ZodAny({
|
|
11796
|
+
typeName: ZodFirstPartyTypeKind.ZodAny,
|
|
11797
|
+
...processCreateParams(params)
|
|
11798
|
+
});
|
|
11799
|
+
};
|
|
11800
|
+
|
|
11801
|
+
class ZodUnknown extends ZodType {
|
|
11802
|
+
constructor() {
|
|
11803
|
+
super(...arguments);
|
|
11804
|
+
this._unknown = true;
|
|
11805
|
+
}
|
|
11806
|
+
_parse(input) {
|
|
11807
|
+
return OK(input.data);
|
|
11808
|
+
}
|
|
11809
|
+
}
|
|
11810
|
+
ZodUnknown.create = (params) => {
|
|
11811
|
+
return new ZodUnknown({
|
|
11812
|
+
typeName: ZodFirstPartyTypeKind.ZodUnknown,
|
|
11813
|
+
...processCreateParams(params)
|
|
11814
|
+
});
|
|
11815
|
+
};
|
|
11816
|
+
|
|
11817
|
+
class ZodNever extends ZodType {
|
|
11818
|
+
_parse(input) {
|
|
11819
|
+
const ctx = this._getOrReturnCtx(input);
|
|
11820
|
+
addIssueToContext(ctx, {
|
|
11821
|
+
code: ZodIssueCode.invalid_type,
|
|
11822
|
+
expected: ZodParsedType.never,
|
|
11823
|
+
received: ctx.parsedType
|
|
11824
|
+
});
|
|
11825
|
+
return INVALID;
|
|
11826
|
+
}
|
|
11827
|
+
}
|
|
11828
|
+
ZodNever.create = (params) => {
|
|
11829
|
+
return new ZodNever({
|
|
11830
|
+
typeName: ZodFirstPartyTypeKind.ZodNever,
|
|
11831
|
+
...processCreateParams(params)
|
|
11832
|
+
});
|
|
11833
|
+
};
|
|
11834
|
+
|
|
11835
|
+
class ZodVoid extends ZodType {
|
|
11836
|
+
_parse(input) {
|
|
11837
|
+
const parsedType = this._getType(input);
|
|
11838
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
11839
|
+
const ctx = this._getOrReturnCtx(input);
|
|
11840
|
+
addIssueToContext(ctx, {
|
|
11841
|
+
code: ZodIssueCode.invalid_type,
|
|
11842
|
+
expected: ZodParsedType.void,
|
|
11843
|
+
received: ctx.parsedType
|
|
11844
|
+
});
|
|
11845
|
+
return INVALID;
|
|
11846
|
+
}
|
|
11847
|
+
return OK(input.data);
|
|
11848
|
+
}
|
|
11849
|
+
}
|
|
11850
|
+
ZodVoid.create = (params) => {
|
|
11851
|
+
return new ZodVoid({
|
|
11852
|
+
typeName: ZodFirstPartyTypeKind.ZodVoid,
|
|
11853
|
+
...processCreateParams(params)
|
|
11854
|
+
});
|
|
11855
|
+
};
|
|
11856
|
+
|
|
11857
|
+
class ZodArray extends ZodType {
|
|
11858
|
+
_parse(input) {
|
|
11859
|
+
const { ctx, status } = this._processInputParams(input);
|
|
11860
|
+
const def = this._def;
|
|
11861
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
11862
|
+
addIssueToContext(ctx, {
|
|
11863
|
+
code: ZodIssueCode.invalid_type,
|
|
11864
|
+
expected: ZodParsedType.array,
|
|
11865
|
+
received: ctx.parsedType
|
|
11866
|
+
});
|
|
11867
|
+
return INVALID;
|
|
11868
|
+
}
|
|
11869
|
+
if (def.exactLength !== null) {
|
|
11870
|
+
const tooBig = ctx.data.length > def.exactLength.value;
|
|
11871
|
+
const tooSmall = ctx.data.length < def.exactLength.value;
|
|
11872
|
+
if (tooBig || tooSmall) {
|
|
11873
|
+
addIssueToContext(ctx, {
|
|
11874
|
+
code: tooBig ? ZodIssueCode.too_big : ZodIssueCode.too_small,
|
|
11875
|
+
minimum: tooSmall ? def.exactLength.value : undefined,
|
|
11876
|
+
maximum: tooBig ? def.exactLength.value : undefined,
|
|
11877
|
+
type: "array",
|
|
11878
|
+
inclusive: true,
|
|
11879
|
+
exact: true,
|
|
11880
|
+
message: def.exactLength.message
|
|
11881
|
+
});
|
|
11882
|
+
status.dirty();
|
|
11883
|
+
}
|
|
11884
|
+
}
|
|
11885
|
+
if (def.minLength !== null) {
|
|
11886
|
+
if (ctx.data.length < def.minLength.value) {
|
|
11887
|
+
addIssueToContext(ctx, {
|
|
11888
|
+
code: ZodIssueCode.too_small,
|
|
11889
|
+
minimum: def.minLength.value,
|
|
11890
|
+
type: "array",
|
|
11891
|
+
inclusive: true,
|
|
11892
|
+
exact: false,
|
|
11893
|
+
message: def.minLength.message
|
|
11894
|
+
});
|
|
11895
|
+
status.dirty();
|
|
11896
|
+
}
|
|
11897
|
+
}
|
|
11898
|
+
if (def.maxLength !== null) {
|
|
11899
|
+
if (ctx.data.length > def.maxLength.value) {
|
|
11900
|
+
addIssueToContext(ctx, {
|
|
11901
|
+
code: ZodIssueCode.too_big,
|
|
11902
|
+
maximum: def.maxLength.value,
|
|
11903
|
+
type: "array",
|
|
11904
|
+
inclusive: true,
|
|
11905
|
+
exact: false,
|
|
11906
|
+
message: def.maxLength.message
|
|
11907
|
+
});
|
|
11908
|
+
status.dirty();
|
|
11909
|
+
}
|
|
11910
|
+
}
|
|
11911
|
+
if (ctx.common.async) {
|
|
11912
|
+
return Promise.all([...ctx.data].map((item, i) => {
|
|
11913
|
+
return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
11914
|
+
})).then((result2) => {
|
|
11915
|
+
return ParseStatus.mergeArray(status, result2);
|
|
11916
|
+
});
|
|
11917
|
+
}
|
|
11918
|
+
const result = [...ctx.data].map((item, i) => {
|
|
11919
|
+
return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
11920
|
+
});
|
|
11921
|
+
return ParseStatus.mergeArray(status, result);
|
|
11922
|
+
}
|
|
11923
|
+
get element() {
|
|
11924
|
+
return this._def.type;
|
|
11925
|
+
}
|
|
11926
|
+
min(minLength, message) {
|
|
11927
|
+
return new ZodArray({
|
|
11928
|
+
...this._def,
|
|
11929
|
+
minLength: { value: minLength, message: errorUtil.toString(message) }
|
|
11930
|
+
});
|
|
11931
|
+
}
|
|
11932
|
+
max(maxLength, message) {
|
|
11933
|
+
return new ZodArray({
|
|
11934
|
+
...this._def,
|
|
11935
|
+
maxLength: { value: maxLength, message: errorUtil.toString(message) }
|
|
11936
|
+
});
|
|
11937
|
+
}
|
|
11938
|
+
length(len, message) {
|
|
11939
|
+
return new ZodArray({
|
|
11940
|
+
...this._def,
|
|
11941
|
+
exactLength: { value: len, message: errorUtil.toString(message) }
|
|
11942
|
+
});
|
|
11943
|
+
}
|
|
11944
|
+
nonempty(message) {
|
|
11945
|
+
return this.min(1, message);
|
|
11946
|
+
}
|
|
11947
|
+
}
|
|
11948
|
+
ZodArray.create = (schema2, params) => {
|
|
11949
|
+
return new ZodArray({
|
|
11950
|
+
type: schema2,
|
|
11951
|
+
minLength: null,
|
|
11952
|
+
maxLength: null,
|
|
11953
|
+
exactLength: null,
|
|
11954
|
+
typeName: ZodFirstPartyTypeKind.ZodArray,
|
|
11955
|
+
...processCreateParams(params)
|
|
11956
|
+
});
|
|
11957
|
+
};
|
|
11958
|
+
function deepPartialify(schema2) {
|
|
11959
|
+
if (schema2 instanceof ZodObject) {
|
|
11960
|
+
const newShape = {};
|
|
11961
|
+
for (const key in schema2.shape) {
|
|
11962
|
+
const fieldSchema = schema2.shape[key];
|
|
11963
|
+
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema));
|
|
11964
|
+
}
|
|
11965
|
+
return new ZodObject({
|
|
11966
|
+
...schema2._def,
|
|
11967
|
+
shape: () => newShape
|
|
11968
|
+
});
|
|
11969
|
+
} else if (schema2 instanceof ZodArray) {
|
|
11970
|
+
return new ZodArray({
|
|
11971
|
+
...schema2._def,
|
|
11972
|
+
type: deepPartialify(schema2.element)
|
|
11973
|
+
});
|
|
11974
|
+
} else if (schema2 instanceof ZodOptional) {
|
|
11975
|
+
return ZodOptional.create(deepPartialify(schema2.unwrap()));
|
|
11976
|
+
} else if (schema2 instanceof ZodNullable) {
|
|
11977
|
+
return ZodNullable.create(deepPartialify(schema2.unwrap()));
|
|
11978
|
+
} else if (schema2 instanceof ZodTuple) {
|
|
11979
|
+
return ZodTuple.create(schema2.items.map((item) => deepPartialify(item)));
|
|
11980
|
+
} else {
|
|
11981
|
+
return schema2;
|
|
11982
|
+
}
|
|
11983
|
+
}
|
|
11984
|
+
|
|
11985
|
+
class ZodObject extends ZodType {
|
|
11986
|
+
constructor() {
|
|
11987
|
+
super(...arguments);
|
|
11988
|
+
this._cached = null;
|
|
11989
|
+
this.nonstrict = this.passthrough;
|
|
11990
|
+
this.augment = this.extend;
|
|
11991
|
+
}
|
|
11992
|
+
_getCached() {
|
|
11993
|
+
if (this._cached !== null)
|
|
11994
|
+
return this._cached;
|
|
11995
|
+
const shape = this._def.shape();
|
|
11996
|
+
const keys = util.objectKeys(shape);
|
|
11997
|
+
this._cached = { shape, keys };
|
|
11998
|
+
return this._cached;
|
|
11999
|
+
}
|
|
12000
|
+
_parse(input) {
|
|
12001
|
+
const parsedType = this._getType(input);
|
|
12002
|
+
if (parsedType !== ZodParsedType.object) {
|
|
12003
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
12004
|
+
addIssueToContext(ctx2, {
|
|
12005
|
+
code: ZodIssueCode.invalid_type,
|
|
12006
|
+
expected: ZodParsedType.object,
|
|
12007
|
+
received: ctx2.parsedType
|
|
12008
|
+
});
|
|
12009
|
+
return INVALID;
|
|
12010
|
+
}
|
|
12011
|
+
const { status, ctx } = this._processInputParams(input);
|
|
12012
|
+
const { shape, keys: shapeKeys } = this._getCached();
|
|
12013
|
+
const extraKeys = [];
|
|
12014
|
+
if (!(this._def.catchall instanceof ZodNever && this._def.unknownKeys === "strip")) {
|
|
12015
|
+
for (const key in ctx.data) {
|
|
12016
|
+
if (!shapeKeys.includes(key)) {
|
|
12017
|
+
extraKeys.push(key);
|
|
12018
|
+
}
|
|
12019
|
+
}
|
|
12020
|
+
}
|
|
12021
|
+
const pairs = [];
|
|
12022
|
+
for (const key of shapeKeys) {
|
|
12023
|
+
const keyValidator = shape[key];
|
|
12024
|
+
const value = ctx.data[key];
|
|
12025
|
+
pairs.push({
|
|
12026
|
+
key: { status: "valid", value: key },
|
|
12027
|
+
value: keyValidator._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
|
|
12028
|
+
alwaysSet: key in ctx.data
|
|
12029
|
+
});
|
|
12030
|
+
}
|
|
12031
|
+
if (this._def.catchall instanceof ZodNever) {
|
|
12032
|
+
const unknownKeys = this._def.unknownKeys;
|
|
12033
|
+
if (unknownKeys === "passthrough") {
|
|
12034
|
+
for (const key of extraKeys) {
|
|
12035
|
+
pairs.push({
|
|
12036
|
+
key: { status: "valid", value: key },
|
|
12037
|
+
value: { status: "valid", value: ctx.data[key] }
|
|
12038
|
+
});
|
|
12039
|
+
}
|
|
12040
|
+
} else if (unknownKeys === "strict") {
|
|
12041
|
+
if (extraKeys.length > 0) {
|
|
12042
|
+
addIssueToContext(ctx, {
|
|
12043
|
+
code: ZodIssueCode.unrecognized_keys,
|
|
12044
|
+
keys: extraKeys
|
|
12045
|
+
});
|
|
12046
|
+
status.dirty();
|
|
12047
|
+
}
|
|
12048
|
+
} else if (unknownKeys === "strip") {} else {
|
|
12049
|
+
throw new Error(`Internal ZodObject error: invalid unknownKeys value.`);
|
|
12050
|
+
}
|
|
12051
|
+
} else {
|
|
12052
|
+
const catchall = this._def.catchall;
|
|
12053
|
+
for (const key of extraKeys) {
|
|
12054
|
+
const value = ctx.data[key];
|
|
12055
|
+
pairs.push({
|
|
12056
|
+
key: { status: "valid", value: key },
|
|
12057
|
+
value: catchall._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
|
|
12058
|
+
alwaysSet: key in ctx.data
|
|
12059
|
+
});
|
|
12060
|
+
}
|
|
12061
|
+
}
|
|
12062
|
+
if (ctx.common.async) {
|
|
12063
|
+
return Promise.resolve().then(async () => {
|
|
12064
|
+
const syncPairs = [];
|
|
12065
|
+
for (const pair of pairs) {
|
|
12066
|
+
const key = await pair.key;
|
|
12067
|
+
const value = await pair.value;
|
|
12068
|
+
syncPairs.push({
|
|
12069
|
+
key,
|
|
12070
|
+
value,
|
|
12071
|
+
alwaysSet: pair.alwaysSet
|
|
12072
|
+
});
|
|
12073
|
+
}
|
|
12074
|
+
return syncPairs;
|
|
12075
|
+
}).then((syncPairs) => {
|
|
12076
|
+
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
12077
|
+
});
|
|
12078
|
+
} else {
|
|
12079
|
+
return ParseStatus.mergeObjectSync(status, pairs);
|
|
12080
|
+
}
|
|
12081
|
+
}
|
|
12082
|
+
get shape() {
|
|
12083
|
+
return this._def.shape();
|
|
12084
|
+
}
|
|
12085
|
+
strict(message) {
|
|
12086
|
+
errorUtil.errToObj;
|
|
12087
|
+
return new ZodObject({
|
|
12088
|
+
...this._def,
|
|
12089
|
+
unknownKeys: "strict",
|
|
12090
|
+
...message !== undefined ? {
|
|
12091
|
+
errorMap: (issue, ctx) => {
|
|
12092
|
+
const defaultError = this._def.errorMap?.(issue, ctx).message ?? ctx.defaultError;
|
|
12093
|
+
if (issue.code === "unrecognized_keys")
|
|
12094
|
+
return {
|
|
12095
|
+
message: errorUtil.errToObj(message).message ?? defaultError
|
|
12096
|
+
};
|
|
12097
|
+
return {
|
|
12098
|
+
message: defaultError
|
|
12099
|
+
};
|
|
12100
|
+
}
|
|
12101
|
+
} : {}
|
|
12102
|
+
});
|
|
12103
|
+
}
|
|
12104
|
+
strip() {
|
|
12105
|
+
return new ZodObject({
|
|
12106
|
+
...this._def,
|
|
12107
|
+
unknownKeys: "strip"
|
|
12108
|
+
});
|
|
12109
|
+
}
|
|
12110
|
+
passthrough() {
|
|
12111
|
+
return new ZodObject({
|
|
12112
|
+
...this._def,
|
|
12113
|
+
unknownKeys: "passthrough"
|
|
12114
|
+
});
|
|
12115
|
+
}
|
|
12116
|
+
extend(augmentation) {
|
|
12117
|
+
return new ZodObject({
|
|
12118
|
+
...this._def,
|
|
12119
|
+
shape: () => ({
|
|
12120
|
+
...this._def.shape(),
|
|
12121
|
+
...augmentation
|
|
12122
|
+
})
|
|
12123
|
+
});
|
|
12124
|
+
}
|
|
12125
|
+
merge(merging) {
|
|
12126
|
+
const merged = new ZodObject({
|
|
12127
|
+
unknownKeys: merging._def.unknownKeys,
|
|
12128
|
+
catchall: merging._def.catchall,
|
|
12129
|
+
shape: () => ({
|
|
12130
|
+
...this._def.shape(),
|
|
12131
|
+
...merging._def.shape()
|
|
12132
|
+
}),
|
|
12133
|
+
typeName: ZodFirstPartyTypeKind.ZodObject
|
|
12134
|
+
});
|
|
12135
|
+
return merged;
|
|
12136
|
+
}
|
|
12137
|
+
setKey(key, schema2) {
|
|
12138
|
+
return this.augment({ [key]: schema2 });
|
|
12139
|
+
}
|
|
12140
|
+
catchall(index) {
|
|
12141
|
+
return new ZodObject({
|
|
12142
|
+
...this._def,
|
|
12143
|
+
catchall: index
|
|
12144
|
+
});
|
|
12145
|
+
}
|
|
12146
|
+
pick(mask) {
|
|
12147
|
+
const shape = {};
|
|
12148
|
+
for (const key of util.objectKeys(mask)) {
|
|
12149
|
+
if (mask[key] && this.shape[key]) {
|
|
12150
|
+
shape[key] = this.shape[key];
|
|
12151
|
+
}
|
|
12152
|
+
}
|
|
12153
|
+
return new ZodObject({
|
|
12154
|
+
...this._def,
|
|
12155
|
+
shape: () => shape
|
|
12156
|
+
});
|
|
12157
|
+
}
|
|
12158
|
+
omit(mask) {
|
|
12159
|
+
const shape = {};
|
|
12160
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
12161
|
+
if (!mask[key]) {
|
|
12162
|
+
shape[key] = this.shape[key];
|
|
12163
|
+
}
|
|
12164
|
+
}
|
|
12165
|
+
return new ZodObject({
|
|
12166
|
+
...this._def,
|
|
12167
|
+
shape: () => shape
|
|
12168
|
+
});
|
|
12169
|
+
}
|
|
12170
|
+
deepPartial() {
|
|
12171
|
+
return deepPartialify(this);
|
|
12172
|
+
}
|
|
12173
|
+
partial(mask) {
|
|
12174
|
+
const newShape = {};
|
|
12175
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
12176
|
+
const fieldSchema = this.shape[key];
|
|
12177
|
+
if (mask && !mask[key]) {
|
|
12178
|
+
newShape[key] = fieldSchema;
|
|
12179
|
+
} else {
|
|
12180
|
+
newShape[key] = fieldSchema.optional();
|
|
12181
|
+
}
|
|
12182
|
+
}
|
|
12183
|
+
return new ZodObject({
|
|
12184
|
+
...this._def,
|
|
12185
|
+
shape: () => newShape
|
|
12186
|
+
});
|
|
12187
|
+
}
|
|
12188
|
+
required(mask) {
|
|
12189
|
+
const newShape = {};
|
|
12190
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
12191
|
+
if (mask && !mask[key]) {
|
|
12192
|
+
newShape[key] = this.shape[key];
|
|
12193
|
+
} else {
|
|
12194
|
+
const fieldSchema = this.shape[key];
|
|
12195
|
+
let newField = fieldSchema;
|
|
12196
|
+
while (newField instanceof ZodOptional) {
|
|
12197
|
+
newField = newField._def.innerType;
|
|
12198
|
+
}
|
|
12199
|
+
newShape[key] = newField;
|
|
12200
|
+
}
|
|
12201
|
+
}
|
|
12202
|
+
return new ZodObject({
|
|
12203
|
+
...this._def,
|
|
12204
|
+
shape: () => newShape
|
|
12205
|
+
});
|
|
12206
|
+
}
|
|
12207
|
+
keyof() {
|
|
12208
|
+
return createZodEnum(util.objectKeys(this.shape));
|
|
12209
|
+
}
|
|
12210
|
+
}
|
|
12211
|
+
ZodObject.create = (shape, params) => {
|
|
12212
|
+
return new ZodObject({
|
|
12213
|
+
shape: () => shape,
|
|
12214
|
+
unknownKeys: "strip",
|
|
12215
|
+
catchall: ZodNever.create(),
|
|
12216
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
12217
|
+
...processCreateParams(params)
|
|
12218
|
+
});
|
|
12219
|
+
};
|
|
12220
|
+
ZodObject.strictCreate = (shape, params) => {
|
|
12221
|
+
return new ZodObject({
|
|
12222
|
+
shape: () => shape,
|
|
12223
|
+
unknownKeys: "strict",
|
|
12224
|
+
catchall: ZodNever.create(),
|
|
12225
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
12226
|
+
...processCreateParams(params)
|
|
12227
|
+
});
|
|
12228
|
+
};
|
|
12229
|
+
ZodObject.lazycreate = (shape, params) => {
|
|
12230
|
+
return new ZodObject({
|
|
12231
|
+
shape,
|
|
12232
|
+
unknownKeys: "strip",
|
|
12233
|
+
catchall: ZodNever.create(),
|
|
12234
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
12235
|
+
...processCreateParams(params)
|
|
12236
|
+
});
|
|
12237
|
+
};
|
|
12238
|
+
|
|
12239
|
+
class ZodUnion extends ZodType {
|
|
12240
|
+
_parse(input) {
|
|
12241
|
+
const { ctx } = this._processInputParams(input);
|
|
12242
|
+
const options = this._def.options;
|
|
12243
|
+
function handleResults(results) {
|
|
12244
|
+
for (const result of results) {
|
|
12245
|
+
if (result.result.status === "valid") {
|
|
12246
|
+
return result.result;
|
|
12247
|
+
}
|
|
12248
|
+
}
|
|
12249
|
+
for (const result of results) {
|
|
12250
|
+
if (result.result.status === "dirty") {
|
|
12251
|
+
ctx.common.issues.push(...result.ctx.common.issues);
|
|
12252
|
+
return result.result;
|
|
12253
|
+
}
|
|
12254
|
+
}
|
|
12255
|
+
const unionErrors = results.map((result) => new ZodError(result.ctx.common.issues));
|
|
12256
|
+
addIssueToContext(ctx, {
|
|
12257
|
+
code: ZodIssueCode.invalid_union,
|
|
12258
|
+
unionErrors
|
|
12259
|
+
});
|
|
12260
|
+
return INVALID;
|
|
12261
|
+
}
|
|
12262
|
+
if (ctx.common.async) {
|
|
12263
|
+
return Promise.all(options.map(async (option) => {
|
|
12264
|
+
const childCtx = {
|
|
12265
|
+
...ctx,
|
|
12266
|
+
common: {
|
|
12267
|
+
...ctx.common,
|
|
12268
|
+
issues: []
|
|
12269
|
+
},
|
|
12270
|
+
parent: null
|
|
12271
|
+
};
|
|
12272
|
+
return {
|
|
12273
|
+
result: await option._parseAsync({
|
|
12274
|
+
data: ctx.data,
|
|
12275
|
+
path: ctx.path,
|
|
12276
|
+
parent: childCtx
|
|
12277
|
+
}),
|
|
12278
|
+
ctx: childCtx
|
|
12279
|
+
};
|
|
12280
|
+
})).then(handleResults);
|
|
12281
|
+
} else {
|
|
12282
|
+
let dirty = undefined;
|
|
12283
|
+
const issues = [];
|
|
12284
|
+
for (const option of options) {
|
|
12285
|
+
const childCtx = {
|
|
12286
|
+
...ctx,
|
|
12287
|
+
common: {
|
|
12288
|
+
...ctx.common,
|
|
12289
|
+
issues: []
|
|
12290
|
+
},
|
|
12291
|
+
parent: null
|
|
12292
|
+
};
|
|
12293
|
+
const result = option._parseSync({
|
|
12294
|
+
data: ctx.data,
|
|
12295
|
+
path: ctx.path,
|
|
12296
|
+
parent: childCtx
|
|
12297
|
+
});
|
|
12298
|
+
if (result.status === "valid") {
|
|
12299
|
+
return result;
|
|
12300
|
+
} else if (result.status === "dirty" && !dirty) {
|
|
12301
|
+
dirty = { result, ctx: childCtx };
|
|
12302
|
+
}
|
|
12303
|
+
if (childCtx.common.issues.length) {
|
|
12304
|
+
issues.push(childCtx.common.issues);
|
|
12305
|
+
}
|
|
12306
|
+
}
|
|
12307
|
+
if (dirty) {
|
|
12308
|
+
ctx.common.issues.push(...dirty.ctx.common.issues);
|
|
12309
|
+
return dirty.result;
|
|
12310
|
+
}
|
|
12311
|
+
const unionErrors = issues.map((issues2) => new ZodError(issues2));
|
|
12312
|
+
addIssueToContext(ctx, {
|
|
12313
|
+
code: ZodIssueCode.invalid_union,
|
|
12314
|
+
unionErrors
|
|
12315
|
+
});
|
|
12316
|
+
return INVALID;
|
|
12317
|
+
}
|
|
12318
|
+
}
|
|
12319
|
+
get options() {
|
|
12320
|
+
return this._def.options;
|
|
12321
|
+
}
|
|
12322
|
+
}
|
|
12323
|
+
ZodUnion.create = (types, params) => {
|
|
12324
|
+
return new ZodUnion({
|
|
12325
|
+
options: types,
|
|
12326
|
+
typeName: ZodFirstPartyTypeKind.ZodUnion,
|
|
12327
|
+
...processCreateParams(params)
|
|
12328
|
+
});
|
|
12329
|
+
};
|
|
12330
|
+
var getDiscriminator = (type) => {
|
|
12331
|
+
if (type instanceof ZodLazy) {
|
|
12332
|
+
return getDiscriminator(type.schema);
|
|
12333
|
+
} else if (type instanceof ZodEffects) {
|
|
12334
|
+
return getDiscriminator(type.innerType());
|
|
12335
|
+
} else if (type instanceof ZodLiteral) {
|
|
12336
|
+
return [type.value];
|
|
12337
|
+
} else if (type instanceof ZodEnum) {
|
|
12338
|
+
return type.options;
|
|
12339
|
+
} else if (type instanceof ZodNativeEnum) {
|
|
12340
|
+
return util.objectValues(type.enum);
|
|
12341
|
+
} else if (type instanceof ZodDefault) {
|
|
12342
|
+
return getDiscriminator(type._def.innerType);
|
|
12343
|
+
} else if (type instanceof ZodUndefined) {
|
|
12344
|
+
return [undefined];
|
|
12345
|
+
} else if (type instanceof ZodNull) {
|
|
12346
|
+
return [null];
|
|
12347
|
+
} else if (type instanceof ZodOptional) {
|
|
12348
|
+
return [undefined, ...getDiscriminator(type.unwrap())];
|
|
12349
|
+
} else if (type instanceof ZodNullable) {
|
|
12350
|
+
return [null, ...getDiscriminator(type.unwrap())];
|
|
12351
|
+
} else if (type instanceof ZodBranded) {
|
|
12352
|
+
return getDiscriminator(type.unwrap());
|
|
12353
|
+
} else if (type instanceof ZodReadonly) {
|
|
12354
|
+
return getDiscriminator(type.unwrap());
|
|
12355
|
+
} else if (type instanceof ZodCatch) {
|
|
12356
|
+
return getDiscriminator(type._def.innerType);
|
|
12357
|
+
} else {
|
|
12358
|
+
return [];
|
|
12359
|
+
}
|
|
12360
|
+
};
|
|
12361
|
+
|
|
12362
|
+
class ZodDiscriminatedUnion extends ZodType {
|
|
12363
|
+
_parse(input) {
|
|
12364
|
+
const { ctx } = this._processInputParams(input);
|
|
12365
|
+
if (ctx.parsedType !== ZodParsedType.object) {
|
|
12366
|
+
addIssueToContext(ctx, {
|
|
12367
|
+
code: ZodIssueCode.invalid_type,
|
|
12368
|
+
expected: ZodParsedType.object,
|
|
12369
|
+
received: ctx.parsedType
|
|
12370
|
+
});
|
|
12371
|
+
return INVALID;
|
|
12372
|
+
}
|
|
12373
|
+
const discriminator = this.discriminator;
|
|
12374
|
+
const discriminatorValue = ctx.data[discriminator];
|
|
12375
|
+
const option = this.optionsMap.get(discriminatorValue);
|
|
12376
|
+
if (!option) {
|
|
12377
|
+
addIssueToContext(ctx, {
|
|
12378
|
+
code: ZodIssueCode.invalid_union_discriminator,
|
|
12379
|
+
options: Array.from(this.optionsMap.keys()),
|
|
12380
|
+
path: [discriminator]
|
|
12381
|
+
});
|
|
12382
|
+
return INVALID;
|
|
12383
|
+
}
|
|
12384
|
+
if (ctx.common.async) {
|
|
12385
|
+
return option._parseAsync({
|
|
12386
|
+
data: ctx.data,
|
|
12387
|
+
path: ctx.path,
|
|
12388
|
+
parent: ctx
|
|
12389
|
+
});
|
|
12390
|
+
} else {
|
|
12391
|
+
return option._parseSync({
|
|
12392
|
+
data: ctx.data,
|
|
12393
|
+
path: ctx.path,
|
|
12394
|
+
parent: ctx
|
|
12395
|
+
});
|
|
12396
|
+
}
|
|
12397
|
+
}
|
|
12398
|
+
get discriminator() {
|
|
12399
|
+
return this._def.discriminator;
|
|
12400
|
+
}
|
|
12401
|
+
get options() {
|
|
12402
|
+
return this._def.options;
|
|
12403
|
+
}
|
|
12404
|
+
get optionsMap() {
|
|
12405
|
+
return this._def.optionsMap;
|
|
12406
|
+
}
|
|
12407
|
+
static create(discriminator, options, params) {
|
|
12408
|
+
const optionsMap = new Map;
|
|
12409
|
+
for (const type of options) {
|
|
12410
|
+
const discriminatorValues = getDiscriminator(type.shape[discriminator]);
|
|
12411
|
+
if (!discriminatorValues.length) {
|
|
12412
|
+
throw new Error(`A discriminator value for key \`${discriminator}\` could not be extracted from all schema options`);
|
|
12413
|
+
}
|
|
12414
|
+
for (const value of discriminatorValues) {
|
|
12415
|
+
if (optionsMap.has(value)) {
|
|
12416
|
+
throw new Error(`Discriminator property ${String(discriminator)} has duplicate value ${String(value)}`);
|
|
12417
|
+
}
|
|
12418
|
+
optionsMap.set(value, type);
|
|
12419
|
+
}
|
|
12420
|
+
}
|
|
12421
|
+
return new ZodDiscriminatedUnion({
|
|
12422
|
+
typeName: ZodFirstPartyTypeKind.ZodDiscriminatedUnion,
|
|
12423
|
+
discriminator,
|
|
12424
|
+
options,
|
|
12425
|
+
optionsMap,
|
|
12426
|
+
...processCreateParams(params)
|
|
12427
|
+
});
|
|
12428
|
+
}
|
|
12429
|
+
}
|
|
12430
|
+
function mergeValues(a, b) {
|
|
12431
|
+
const aType = getParsedType(a);
|
|
12432
|
+
const bType = getParsedType(b);
|
|
12433
|
+
if (a === b) {
|
|
12434
|
+
return { valid: true, data: a };
|
|
12435
|
+
} else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
|
|
12436
|
+
const bKeys = util.objectKeys(b);
|
|
12437
|
+
const sharedKeys = util.objectKeys(a).filter((key) => bKeys.indexOf(key) !== -1);
|
|
12438
|
+
const newObj = { ...a, ...b };
|
|
12439
|
+
for (const key of sharedKeys) {
|
|
12440
|
+
const sharedValue = mergeValues(a[key], b[key]);
|
|
12441
|
+
if (!sharedValue.valid) {
|
|
12442
|
+
return { valid: false };
|
|
12443
|
+
}
|
|
12444
|
+
newObj[key] = sharedValue.data;
|
|
12445
|
+
}
|
|
12446
|
+
return { valid: true, data: newObj };
|
|
12447
|
+
} else if (aType === ZodParsedType.array && bType === ZodParsedType.array) {
|
|
12448
|
+
if (a.length !== b.length) {
|
|
12449
|
+
return { valid: false };
|
|
12450
|
+
}
|
|
12451
|
+
const newArray = [];
|
|
12452
|
+
for (let index = 0;index < a.length; index++) {
|
|
12453
|
+
const itemA = a[index];
|
|
12454
|
+
const itemB = b[index];
|
|
12455
|
+
const sharedValue = mergeValues(itemA, itemB);
|
|
12456
|
+
if (!sharedValue.valid) {
|
|
12457
|
+
return { valid: false };
|
|
12458
|
+
}
|
|
12459
|
+
newArray.push(sharedValue.data);
|
|
12460
|
+
}
|
|
12461
|
+
return { valid: true, data: newArray };
|
|
12462
|
+
} else if (aType === ZodParsedType.date && bType === ZodParsedType.date && +a === +b) {
|
|
12463
|
+
return { valid: true, data: a };
|
|
12464
|
+
} else {
|
|
12465
|
+
return { valid: false };
|
|
12466
|
+
}
|
|
12467
|
+
}
|
|
12468
|
+
|
|
12469
|
+
class ZodIntersection extends ZodType {
|
|
12470
|
+
_parse(input) {
|
|
12471
|
+
const { status, ctx } = this._processInputParams(input);
|
|
12472
|
+
const handleParsed = (parsedLeft, parsedRight) => {
|
|
12473
|
+
if (isAborted(parsedLeft) || isAborted(parsedRight)) {
|
|
12474
|
+
return INVALID;
|
|
12475
|
+
}
|
|
12476
|
+
const merged = mergeValues(parsedLeft.value, parsedRight.value);
|
|
12477
|
+
if (!merged.valid) {
|
|
12478
|
+
addIssueToContext(ctx, {
|
|
12479
|
+
code: ZodIssueCode.invalid_intersection_types
|
|
12480
|
+
});
|
|
12481
|
+
return INVALID;
|
|
12482
|
+
}
|
|
12483
|
+
if (isDirty(parsedLeft) || isDirty(parsedRight)) {
|
|
12484
|
+
status.dirty();
|
|
12485
|
+
}
|
|
12486
|
+
return { status: status.value, value: merged.data };
|
|
12487
|
+
};
|
|
12488
|
+
if (ctx.common.async) {
|
|
12489
|
+
return Promise.all([
|
|
12490
|
+
this._def.left._parseAsync({
|
|
12491
|
+
data: ctx.data,
|
|
12492
|
+
path: ctx.path,
|
|
12493
|
+
parent: ctx
|
|
12494
|
+
}),
|
|
12495
|
+
this._def.right._parseAsync({
|
|
12496
|
+
data: ctx.data,
|
|
12497
|
+
path: ctx.path,
|
|
12498
|
+
parent: ctx
|
|
12499
|
+
})
|
|
12500
|
+
]).then(([left, right]) => handleParsed(left, right));
|
|
12501
|
+
} else {
|
|
12502
|
+
return handleParsed(this._def.left._parseSync({
|
|
12503
|
+
data: ctx.data,
|
|
12504
|
+
path: ctx.path,
|
|
12505
|
+
parent: ctx
|
|
12506
|
+
}), this._def.right._parseSync({
|
|
12507
|
+
data: ctx.data,
|
|
12508
|
+
path: ctx.path,
|
|
12509
|
+
parent: ctx
|
|
12510
|
+
}));
|
|
12511
|
+
}
|
|
12512
|
+
}
|
|
12513
|
+
}
|
|
12514
|
+
ZodIntersection.create = (left, right, params) => {
|
|
12515
|
+
return new ZodIntersection({
|
|
12516
|
+
left,
|
|
12517
|
+
right,
|
|
12518
|
+
typeName: ZodFirstPartyTypeKind.ZodIntersection,
|
|
12519
|
+
...processCreateParams(params)
|
|
12520
|
+
});
|
|
12521
|
+
};
|
|
12522
|
+
|
|
12523
|
+
class ZodTuple extends ZodType {
|
|
12524
|
+
_parse(input) {
|
|
12525
|
+
const { status, ctx } = this._processInputParams(input);
|
|
12526
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
12527
|
+
addIssueToContext(ctx, {
|
|
12528
|
+
code: ZodIssueCode.invalid_type,
|
|
12529
|
+
expected: ZodParsedType.array,
|
|
12530
|
+
received: ctx.parsedType
|
|
12531
|
+
});
|
|
12532
|
+
return INVALID;
|
|
12533
|
+
}
|
|
12534
|
+
if (ctx.data.length < this._def.items.length) {
|
|
12535
|
+
addIssueToContext(ctx, {
|
|
12536
|
+
code: ZodIssueCode.too_small,
|
|
12537
|
+
minimum: this._def.items.length,
|
|
12538
|
+
inclusive: true,
|
|
12539
|
+
exact: false,
|
|
12540
|
+
type: "array"
|
|
12541
|
+
});
|
|
12542
|
+
return INVALID;
|
|
12543
|
+
}
|
|
12544
|
+
const rest = this._def.rest;
|
|
12545
|
+
if (!rest && ctx.data.length > this._def.items.length) {
|
|
12546
|
+
addIssueToContext(ctx, {
|
|
12547
|
+
code: ZodIssueCode.too_big,
|
|
12548
|
+
maximum: this._def.items.length,
|
|
12549
|
+
inclusive: true,
|
|
12550
|
+
exact: false,
|
|
12551
|
+
type: "array"
|
|
12552
|
+
});
|
|
12553
|
+
status.dirty();
|
|
12554
|
+
}
|
|
12555
|
+
const items = [...ctx.data].map((item, itemIndex) => {
|
|
12556
|
+
const schema2 = this._def.items[itemIndex] || this._def.rest;
|
|
12557
|
+
if (!schema2)
|
|
12558
|
+
return null;
|
|
12559
|
+
return schema2._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
|
|
12560
|
+
}).filter((x) => !!x);
|
|
12561
|
+
if (ctx.common.async) {
|
|
12562
|
+
return Promise.all(items).then((results) => {
|
|
12563
|
+
return ParseStatus.mergeArray(status, results);
|
|
12564
|
+
});
|
|
12565
|
+
} else {
|
|
12566
|
+
return ParseStatus.mergeArray(status, items);
|
|
12567
|
+
}
|
|
12568
|
+
}
|
|
12569
|
+
get items() {
|
|
12570
|
+
return this._def.items;
|
|
12571
|
+
}
|
|
12572
|
+
rest(rest) {
|
|
12573
|
+
return new ZodTuple({
|
|
12574
|
+
...this._def,
|
|
12575
|
+
rest
|
|
12576
|
+
});
|
|
12577
|
+
}
|
|
12578
|
+
}
|
|
12579
|
+
ZodTuple.create = (schemas, params) => {
|
|
12580
|
+
if (!Array.isArray(schemas)) {
|
|
12581
|
+
throw new Error("You must pass an array of schemas to z.tuple([ ... ])");
|
|
12582
|
+
}
|
|
12583
|
+
return new ZodTuple({
|
|
12584
|
+
items: schemas,
|
|
12585
|
+
typeName: ZodFirstPartyTypeKind.ZodTuple,
|
|
12586
|
+
rest: null,
|
|
12587
|
+
...processCreateParams(params)
|
|
12588
|
+
});
|
|
12589
|
+
};
|
|
12590
|
+
|
|
12591
|
+
class ZodRecord extends ZodType {
|
|
12592
|
+
get keySchema() {
|
|
12593
|
+
return this._def.keyType;
|
|
12594
|
+
}
|
|
12595
|
+
get valueSchema() {
|
|
12596
|
+
return this._def.valueType;
|
|
12597
|
+
}
|
|
12598
|
+
_parse(input) {
|
|
12599
|
+
const { status, ctx } = this._processInputParams(input);
|
|
12600
|
+
if (ctx.parsedType !== ZodParsedType.object) {
|
|
12601
|
+
addIssueToContext(ctx, {
|
|
12602
|
+
code: ZodIssueCode.invalid_type,
|
|
12603
|
+
expected: ZodParsedType.object,
|
|
12604
|
+
received: ctx.parsedType
|
|
12605
|
+
});
|
|
12606
|
+
return INVALID;
|
|
12607
|
+
}
|
|
12608
|
+
const pairs = [];
|
|
12609
|
+
const keyType = this._def.keyType;
|
|
12610
|
+
const valueType = this._def.valueType;
|
|
12611
|
+
for (const key in ctx.data) {
|
|
12612
|
+
pairs.push({
|
|
12613
|
+
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
|
|
12614
|
+
value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
|
|
12615
|
+
alwaysSet: key in ctx.data
|
|
12616
|
+
});
|
|
12617
|
+
}
|
|
12618
|
+
if (ctx.common.async) {
|
|
12619
|
+
return ParseStatus.mergeObjectAsync(status, pairs);
|
|
12620
|
+
} else {
|
|
12621
|
+
return ParseStatus.mergeObjectSync(status, pairs);
|
|
12622
|
+
}
|
|
12623
|
+
}
|
|
12624
|
+
get element() {
|
|
12625
|
+
return this._def.valueType;
|
|
12626
|
+
}
|
|
12627
|
+
static create(first, second, third) {
|
|
12628
|
+
if (second instanceof ZodType) {
|
|
12629
|
+
return new ZodRecord({
|
|
12630
|
+
keyType: first,
|
|
12631
|
+
valueType: second,
|
|
12632
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
12633
|
+
...processCreateParams(third)
|
|
12634
|
+
});
|
|
12635
|
+
}
|
|
12636
|
+
return new ZodRecord({
|
|
12637
|
+
keyType: ZodString.create(),
|
|
12638
|
+
valueType: first,
|
|
12639
|
+
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
12640
|
+
...processCreateParams(second)
|
|
12641
|
+
});
|
|
12642
|
+
}
|
|
12643
|
+
}
|
|
12644
|
+
|
|
12645
|
+
class ZodMap extends ZodType {
|
|
12646
|
+
get keySchema() {
|
|
12647
|
+
return this._def.keyType;
|
|
12648
|
+
}
|
|
12649
|
+
get valueSchema() {
|
|
12650
|
+
return this._def.valueType;
|
|
12651
|
+
}
|
|
12652
|
+
_parse(input) {
|
|
12653
|
+
const { status, ctx } = this._processInputParams(input);
|
|
12654
|
+
if (ctx.parsedType !== ZodParsedType.map) {
|
|
12655
|
+
addIssueToContext(ctx, {
|
|
12656
|
+
code: ZodIssueCode.invalid_type,
|
|
12657
|
+
expected: ZodParsedType.map,
|
|
12658
|
+
received: ctx.parsedType
|
|
12659
|
+
});
|
|
12660
|
+
return INVALID;
|
|
12661
|
+
}
|
|
12662
|
+
const keyType = this._def.keyType;
|
|
12663
|
+
const valueType = this._def.valueType;
|
|
12664
|
+
const pairs = [...ctx.data.entries()].map(([key, value], index) => {
|
|
12665
|
+
return {
|
|
12666
|
+
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, [index, "key"])),
|
|
12667
|
+
value: valueType._parse(new ParseInputLazyPath(ctx, value, ctx.path, [index, "value"]))
|
|
12668
|
+
};
|
|
12669
|
+
});
|
|
12670
|
+
if (ctx.common.async) {
|
|
12671
|
+
const finalMap = new Map;
|
|
12672
|
+
return Promise.resolve().then(async () => {
|
|
12673
|
+
for (const pair of pairs) {
|
|
12674
|
+
const key = await pair.key;
|
|
12675
|
+
const value = await pair.value;
|
|
12676
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
12677
|
+
return INVALID;
|
|
12678
|
+
}
|
|
12679
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
12680
|
+
status.dirty();
|
|
12681
|
+
}
|
|
12682
|
+
finalMap.set(key.value, value.value);
|
|
12683
|
+
}
|
|
12684
|
+
return { status: status.value, value: finalMap };
|
|
12685
|
+
});
|
|
12686
|
+
} else {
|
|
12687
|
+
const finalMap = new Map;
|
|
12688
|
+
for (const pair of pairs) {
|
|
12689
|
+
const key = pair.key;
|
|
12690
|
+
const value = pair.value;
|
|
12691
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
12692
|
+
return INVALID;
|
|
12693
|
+
}
|
|
12694
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
12695
|
+
status.dirty();
|
|
12696
|
+
}
|
|
12697
|
+
finalMap.set(key.value, value.value);
|
|
12698
|
+
}
|
|
12699
|
+
return { status: status.value, value: finalMap };
|
|
12700
|
+
}
|
|
12701
|
+
}
|
|
12702
|
+
}
|
|
12703
|
+
ZodMap.create = (keyType, valueType, params) => {
|
|
12704
|
+
return new ZodMap({
|
|
12705
|
+
valueType,
|
|
12706
|
+
keyType,
|
|
12707
|
+
typeName: ZodFirstPartyTypeKind.ZodMap,
|
|
12708
|
+
...processCreateParams(params)
|
|
12709
|
+
});
|
|
12710
|
+
};
|
|
12711
|
+
|
|
12712
|
+
class ZodSet extends ZodType {
|
|
12713
|
+
_parse(input) {
|
|
12714
|
+
const { status, ctx } = this._processInputParams(input);
|
|
12715
|
+
if (ctx.parsedType !== ZodParsedType.set) {
|
|
12716
|
+
addIssueToContext(ctx, {
|
|
12717
|
+
code: ZodIssueCode.invalid_type,
|
|
12718
|
+
expected: ZodParsedType.set,
|
|
12719
|
+
received: ctx.parsedType
|
|
12720
|
+
});
|
|
12721
|
+
return INVALID;
|
|
12722
|
+
}
|
|
12723
|
+
const def = this._def;
|
|
12724
|
+
if (def.minSize !== null) {
|
|
12725
|
+
if (ctx.data.size < def.minSize.value) {
|
|
12726
|
+
addIssueToContext(ctx, {
|
|
12727
|
+
code: ZodIssueCode.too_small,
|
|
12728
|
+
minimum: def.minSize.value,
|
|
12729
|
+
type: "set",
|
|
12730
|
+
inclusive: true,
|
|
12731
|
+
exact: false,
|
|
12732
|
+
message: def.minSize.message
|
|
12733
|
+
});
|
|
12734
|
+
status.dirty();
|
|
12735
|
+
}
|
|
12736
|
+
}
|
|
12737
|
+
if (def.maxSize !== null) {
|
|
12738
|
+
if (ctx.data.size > def.maxSize.value) {
|
|
12739
|
+
addIssueToContext(ctx, {
|
|
12740
|
+
code: ZodIssueCode.too_big,
|
|
12741
|
+
maximum: def.maxSize.value,
|
|
12742
|
+
type: "set",
|
|
12743
|
+
inclusive: true,
|
|
12744
|
+
exact: false,
|
|
12745
|
+
message: def.maxSize.message
|
|
12746
|
+
});
|
|
12747
|
+
status.dirty();
|
|
12748
|
+
}
|
|
12749
|
+
}
|
|
12750
|
+
const valueType = this._def.valueType;
|
|
12751
|
+
function finalizeSet(elements2) {
|
|
12752
|
+
const parsedSet = new Set;
|
|
12753
|
+
for (const element of elements2) {
|
|
12754
|
+
if (element.status === "aborted")
|
|
12755
|
+
return INVALID;
|
|
12756
|
+
if (element.status === "dirty")
|
|
12757
|
+
status.dirty();
|
|
12758
|
+
parsedSet.add(element.value);
|
|
12759
|
+
}
|
|
12760
|
+
return { status: status.value, value: parsedSet };
|
|
12761
|
+
}
|
|
12762
|
+
const elements = [...ctx.data.values()].map((item, i) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i)));
|
|
12763
|
+
if (ctx.common.async) {
|
|
12764
|
+
return Promise.all(elements).then((elements2) => finalizeSet(elements2));
|
|
12765
|
+
} else {
|
|
12766
|
+
return finalizeSet(elements);
|
|
12767
|
+
}
|
|
12768
|
+
}
|
|
12769
|
+
min(minSize, message) {
|
|
12770
|
+
return new ZodSet({
|
|
12771
|
+
...this._def,
|
|
12772
|
+
minSize: { value: minSize, message: errorUtil.toString(message) }
|
|
12773
|
+
});
|
|
12774
|
+
}
|
|
12775
|
+
max(maxSize, message) {
|
|
12776
|
+
return new ZodSet({
|
|
12777
|
+
...this._def,
|
|
12778
|
+
maxSize: { value: maxSize, message: errorUtil.toString(message) }
|
|
12779
|
+
});
|
|
12780
|
+
}
|
|
12781
|
+
size(size, message) {
|
|
12782
|
+
return this.min(size, message).max(size, message);
|
|
12783
|
+
}
|
|
12784
|
+
nonempty(message) {
|
|
12785
|
+
return this.min(1, message);
|
|
12786
|
+
}
|
|
12787
|
+
}
|
|
12788
|
+
ZodSet.create = (valueType, params) => {
|
|
12789
|
+
return new ZodSet({
|
|
12790
|
+
valueType,
|
|
12791
|
+
minSize: null,
|
|
12792
|
+
maxSize: null,
|
|
12793
|
+
typeName: ZodFirstPartyTypeKind.ZodSet,
|
|
12794
|
+
...processCreateParams(params)
|
|
12795
|
+
});
|
|
12796
|
+
};
|
|
12797
|
+
|
|
12798
|
+
class ZodFunction extends ZodType {
|
|
12799
|
+
constructor() {
|
|
12800
|
+
super(...arguments);
|
|
12801
|
+
this.validate = this.implement;
|
|
12802
|
+
}
|
|
12803
|
+
_parse(input) {
|
|
12804
|
+
const { ctx } = this._processInputParams(input);
|
|
12805
|
+
if (ctx.parsedType !== ZodParsedType.function) {
|
|
12806
|
+
addIssueToContext(ctx, {
|
|
12807
|
+
code: ZodIssueCode.invalid_type,
|
|
12808
|
+
expected: ZodParsedType.function,
|
|
12809
|
+
received: ctx.parsedType
|
|
12810
|
+
});
|
|
12811
|
+
return INVALID;
|
|
12812
|
+
}
|
|
12813
|
+
function makeArgsIssue(args, error) {
|
|
12814
|
+
return makeIssue({
|
|
12815
|
+
data: args,
|
|
12816
|
+
path: ctx.path,
|
|
12817
|
+
errorMaps: [ctx.common.contextualErrorMap, ctx.schemaErrorMap, getErrorMap(), en_default].filter((x) => !!x),
|
|
12818
|
+
issueData: {
|
|
12819
|
+
code: ZodIssueCode.invalid_arguments,
|
|
12820
|
+
argumentsError: error
|
|
12821
|
+
}
|
|
12822
|
+
});
|
|
12823
|
+
}
|
|
12824
|
+
function makeReturnsIssue(returns, error) {
|
|
12825
|
+
return makeIssue({
|
|
12826
|
+
data: returns,
|
|
12827
|
+
path: ctx.path,
|
|
12828
|
+
errorMaps: [ctx.common.contextualErrorMap, ctx.schemaErrorMap, getErrorMap(), en_default].filter((x) => !!x),
|
|
12829
|
+
issueData: {
|
|
12830
|
+
code: ZodIssueCode.invalid_return_type,
|
|
12831
|
+
returnTypeError: error
|
|
12832
|
+
}
|
|
12833
|
+
});
|
|
12834
|
+
}
|
|
12835
|
+
const params = { errorMap: ctx.common.contextualErrorMap };
|
|
12836
|
+
const fn = ctx.data;
|
|
12837
|
+
if (this._def.returns instanceof ZodPromise) {
|
|
12838
|
+
const me = this;
|
|
12839
|
+
return OK(async function(...args) {
|
|
12840
|
+
const error = new ZodError([]);
|
|
12841
|
+
const parsedArgs = await me._def.args.parseAsync(args, params).catch((e) => {
|
|
12842
|
+
error.addIssue(makeArgsIssue(args, e));
|
|
12843
|
+
throw error;
|
|
12844
|
+
});
|
|
12845
|
+
const result = await Reflect.apply(fn, this, parsedArgs);
|
|
12846
|
+
const parsedReturns = await me._def.returns._def.type.parseAsync(result, params).catch((e) => {
|
|
12847
|
+
error.addIssue(makeReturnsIssue(result, e));
|
|
12848
|
+
throw error;
|
|
12849
|
+
});
|
|
12850
|
+
return parsedReturns;
|
|
12851
|
+
});
|
|
12852
|
+
} else {
|
|
12853
|
+
const me = this;
|
|
12854
|
+
return OK(function(...args) {
|
|
12855
|
+
const parsedArgs = me._def.args.safeParse(args, params);
|
|
12856
|
+
if (!parsedArgs.success) {
|
|
12857
|
+
throw new ZodError([makeArgsIssue(args, parsedArgs.error)]);
|
|
12858
|
+
}
|
|
12859
|
+
const result = Reflect.apply(fn, this, parsedArgs.data);
|
|
12860
|
+
const parsedReturns = me._def.returns.safeParse(result, params);
|
|
12861
|
+
if (!parsedReturns.success) {
|
|
12862
|
+
throw new ZodError([makeReturnsIssue(result, parsedReturns.error)]);
|
|
12863
|
+
}
|
|
12864
|
+
return parsedReturns.data;
|
|
12865
|
+
});
|
|
12866
|
+
}
|
|
12867
|
+
}
|
|
12868
|
+
parameters() {
|
|
12869
|
+
return this._def.args;
|
|
12870
|
+
}
|
|
12871
|
+
returnType() {
|
|
12872
|
+
return this._def.returns;
|
|
12873
|
+
}
|
|
12874
|
+
args(...items) {
|
|
12875
|
+
return new ZodFunction({
|
|
12876
|
+
...this._def,
|
|
12877
|
+
args: ZodTuple.create(items).rest(ZodUnknown.create())
|
|
12878
|
+
});
|
|
12879
|
+
}
|
|
12880
|
+
returns(returnType) {
|
|
12881
|
+
return new ZodFunction({
|
|
12882
|
+
...this._def,
|
|
12883
|
+
returns: returnType
|
|
12884
|
+
});
|
|
12885
|
+
}
|
|
12886
|
+
implement(func) {
|
|
12887
|
+
const validatedFunc = this.parse(func);
|
|
12888
|
+
return validatedFunc;
|
|
12889
|
+
}
|
|
12890
|
+
strictImplement(func) {
|
|
12891
|
+
const validatedFunc = this.parse(func);
|
|
12892
|
+
return validatedFunc;
|
|
12893
|
+
}
|
|
12894
|
+
static create(args, returns, params) {
|
|
12895
|
+
return new ZodFunction({
|
|
12896
|
+
args: args ? args : ZodTuple.create([]).rest(ZodUnknown.create()),
|
|
12897
|
+
returns: returns || ZodUnknown.create(),
|
|
12898
|
+
typeName: ZodFirstPartyTypeKind.ZodFunction,
|
|
12899
|
+
...processCreateParams(params)
|
|
12900
|
+
});
|
|
12901
|
+
}
|
|
12902
|
+
}
|
|
12903
|
+
|
|
12904
|
+
class ZodLazy extends ZodType {
|
|
12905
|
+
get schema() {
|
|
12906
|
+
return this._def.getter();
|
|
12907
|
+
}
|
|
12908
|
+
_parse(input) {
|
|
12909
|
+
const { ctx } = this._processInputParams(input);
|
|
12910
|
+
const lazySchema = this._def.getter();
|
|
12911
|
+
return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx });
|
|
12912
|
+
}
|
|
12913
|
+
}
|
|
12914
|
+
ZodLazy.create = (getter, params) => {
|
|
12915
|
+
return new ZodLazy({
|
|
12916
|
+
getter,
|
|
12917
|
+
typeName: ZodFirstPartyTypeKind.ZodLazy,
|
|
12918
|
+
...processCreateParams(params)
|
|
12919
|
+
});
|
|
12920
|
+
};
|
|
12921
|
+
|
|
12922
|
+
class ZodLiteral extends ZodType {
|
|
12923
|
+
_parse(input) {
|
|
12924
|
+
if (input.data !== this._def.value) {
|
|
12925
|
+
const ctx = this._getOrReturnCtx(input);
|
|
12926
|
+
addIssueToContext(ctx, {
|
|
12927
|
+
received: ctx.data,
|
|
12928
|
+
code: ZodIssueCode.invalid_literal,
|
|
12929
|
+
expected: this._def.value
|
|
12930
|
+
});
|
|
12931
|
+
return INVALID;
|
|
12932
|
+
}
|
|
12933
|
+
return { status: "valid", value: input.data };
|
|
12934
|
+
}
|
|
12935
|
+
get value() {
|
|
12936
|
+
return this._def.value;
|
|
12937
|
+
}
|
|
12938
|
+
}
|
|
12939
|
+
ZodLiteral.create = (value, params) => {
|
|
12940
|
+
return new ZodLiteral({
|
|
12941
|
+
value,
|
|
12942
|
+
typeName: ZodFirstPartyTypeKind.ZodLiteral,
|
|
12943
|
+
...processCreateParams(params)
|
|
12944
|
+
});
|
|
12945
|
+
};
|
|
12946
|
+
function createZodEnum(values, params) {
|
|
12947
|
+
return new ZodEnum({
|
|
12948
|
+
values,
|
|
12949
|
+
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
12950
|
+
...processCreateParams(params)
|
|
12951
|
+
});
|
|
12952
|
+
}
|
|
12953
|
+
|
|
12954
|
+
class ZodEnum extends ZodType {
|
|
12955
|
+
_parse(input) {
|
|
12956
|
+
if (typeof input.data !== "string") {
|
|
12957
|
+
const ctx = this._getOrReturnCtx(input);
|
|
12958
|
+
const expectedValues = this._def.values;
|
|
12959
|
+
addIssueToContext(ctx, {
|
|
12960
|
+
expected: util.joinValues(expectedValues),
|
|
12961
|
+
received: ctx.parsedType,
|
|
12962
|
+
code: ZodIssueCode.invalid_type
|
|
12963
|
+
});
|
|
12964
|
+
return INVALID;
|
|
12965
|
+
}
|
|
12966
|
+
if (!this._cache) {
|
|
12967
|
+
this._cache = new Set(this._def.values);
|
|
12968
|
+
}
|
|
12969
|
+
if (!this._cache.has(input.data)) {
|
|
12970
|
+
const ctx = this._getOrReturnCtx(input);
|
|
12971
|
+
const expectedValues = this._def.values;
|
|
12972
|
+
addIssueToContext(ctx, {
|
|
12973
|
+
received: ctx.data,
|
|
12974
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
12975
|
+
options: expectedValues
|
|
12976
|
+
});
|
|
12977
|
+
return INVALID;
|
|
12978
|
+
}
|
|
12979
|
+
return OK(input.data);
|
|
12980
|
+
}
|
|
12981
|
+
get options() {
|
|
12982
|
+
return this._def.values;
|
|
12983
|
+
}
|
|
12984
|
+
get enum() {
|
|
12985
|
+
const enumValues = {};
|
|
12986
|
+
for (const val of this._def.values) {
|
|
12987
|
+
enumValues[val] = val;
|
|
12988
|
+
}
|
|
12989
|
+
return enumValues;
|
|
12990
|
+
}
|
|
12991
|
+
get Values() {
|
|
12992
|
+
const enumValues = {};
|
|
12993
|
+
for (const val of this._def.values) {
|
|
12994
|
+
enumValues[val] = val;
|
|
12995
|
+
}
|
|
12996
|
+
return enumValues;
|
|
12997
|
+
}
|
|
12998
|
+
get Enum() {
|
|
12999
|
+
const enumValues = {};
|
|
13000
|
+
for (const val of this._def.values) {
|
|
13001
|
+
enumValues[val] = val;
|
|
13002
|
+
}
|
|
13003
|
+
return enumValues;
|
|
13004
|
+
}
|
|
13005
|
+
extract(values, newDef = this._def) {
|
|
13006
|
+
return ZodEnum.create(values, {
|
|
13007
|
+
...this._def,
|
|
13008
|
+
...newDef
|
|
13009
|
+
});
|
|
13010
|
+
}
|
|
13011
|
+
exclude(values, newDef = this._def) {
|
|
13012
|
+
return ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
|
|
13013
|
+
...this._def,
|
|
13014
|
+
...newDef
|
|
13015
|
+
});
|
|
13016
|
+
}
|
|
13017
|
+
}
|
|
13018
|
+
ZodEnum.create = createZodEnum;
|
|
13019
|
+
|
|
13020
|
+
class ZodNativeEnum extends ZodType {
|
|
13021
|
+
_parse(input) {
|
|
13022
|
+
const nativeEnumValues = util.getValidEnumValues(this._def.values);
|
|
13023
|
+
const ctx = this._getOrReturnCtx(input);
|
|
13024
|
+
if (ctx.parsedType !== ZodParsedType.string && ctx.parsedType !== ZodParsedType.number) {
|
|
13025
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
13026
|
+
addIssueToContext(ctx, {
|
|
13027
|
+
expected: util.joinValues(expectedValues),
|
|
13028
|
+
received: ctx.parsedType,
|
|
13029
|
+
code: ZodIssueCode.invalid_type
|
|
13030
|
+
});
|
|
13031
|
+
return INVALID;
|
|
13032
|
+
}
|
|
13033
|
+
if (!this._cache) {
|
|
13034
|
+
this._cache = new Set(util.getValidEnumValues(this._def.values));
|
|
13035
|
+
}
|
|
13036
|
+
if (!this._cache.has(input.data)) {
|
|
13037
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
13038
|
+
addIssueToContext(ctx, {
|
|
13039
|
+
received: ctx.data,
|
|
13040
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
13041
|
+
options: expectedValues
|
|
13042
|
+
});
|
|
13043
|
+
return INVALID;
|
|
13044
|
+
}
|
|
13045
|
+
return OK(input.data);
|
|
13046
|
+
}
|
|
13047
|
+
get enum() {
|
|
13048
|
+
return this._def.values;
|
|
13049
|
+
}
|
|
13050
|
+
}
|
|
13051
|
+
ZodNativeEnum.create = (values, params) => {
|
|
13052
|
+
return new ZodNativeEnum({
|
|
13053
|
+
values,
|
|
13054
|
+
typeName: ZodFirstPartyTypeKind.ZodNativeEnum,
|
|
13055
|
+
...processCreateParams(params)
|
|
13056
|
+
});
|
|
13057
|
+
};
|
|
13058
|
+
|
|
13059
|
+
class ZodPromise extends ZodType {
|
|
13060
|
+
unwrap() {
|
|
13061
|
+
return this._def.type;
|
|
13062
|
+
}
|
|
13063
|
+
_parse(input) {
|
|
13064
|
+
const { ctx } = this._processInputParams(input);
|
|
13065
|
+
if (ctx.parsedType !== ZodParsedType.promise && ctx.common.async === false) {
|
|
13066
|
+
addIssueToContext(ctx, {
|
|
13067
|
+
code: ZodIssueCode.invalid_type,
|
|
13068
|
+
expected: ZodParsedType.promise,
|
|
13069
|
+
received: ctx.parsedType
|
|
13070
|
+
});
|
|
13071
|
+
return INVALID;
|
|
13072
|
+
}
|
|
13073
|
+
const promisified = ctx.parsedType === ZodParsedType.promise ? ctx.data : Promise.resolve(ctx.data);
|
|
13074
|
+
return OK(promisified.then((data) => {
|
|
13075
|
+
return this._def.type.parseAsync(data, {
|
|
13076
|
+
path: ctx.path,
|
|
13077
|
+
errorMap: ctx.common.contextualErrorMap
|
|
13078
|
+
});
|
|
13079
|
+
}));
|
|
13080
|
+
}
|
|
13081
|
+
}
|
|
13082
|
+
ZodPromise.create = (schema2, params) => {
|
|
13083
|
+
return new ZodPromise({
|
|
13084
|
+
type: schema2,
|
|
13085
|
+
typeName: ZodFirstPartyTypeKind.ZodPromise,
|
|
13086
|
+
...processCreateParams(params)
|
|
13087
|
+
});
|
|
13088
|
+
};
|
|
13089
|
+
|
|
13090
|
+
class ZodEffects extends ZodType {
|
|
13091
|
+
innerType() {
|
|
13092
|
+
return this._def.schema;
|
|
13093
|
+
}
|
|
13094
|
+
sourceType() {
|
|
13095
|
+
return this._def.schema._def.typeName === ZodFirstPartyTypeKind.ZodEffects ? this._def.schema.sourceType() : this._def.schema;
|
|
13096
|
+
}
|
|
13097
|
+
_parse(input) {
|
|
13098
|
+
const { status, ctx } = this._processInputParams(input);
|
|
13099
|
+
const effect = this._def.effect || null;
|
|
13100
|
+
const checkCtx = {
|
|
13101
|
+
addIssue: (arg) => {
|
|
13102
|
+
addIssueToContext(ctx, arg);
|
|
13103
|
+
if (arg.fatal) {
|
|
13104
|
+
status.abort();
|
|
13105
|
+
} else {
|
|
13106
|
+
status.dirty();
|
|
13107
|
+
}
|
|
13108
|
+
},
|
|
13109
|
+
get path() {
|
|
13110
|
+
return ctx.path;
|
|
13111
|
+
}
|
|
13112
|
+
};
|
|
13113
|
+
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
|
|
13114
|
+
if (effect.type === "preprocess") {
|
|
13115
|
+
const processed = effect.transform(ctx.data, checkCtx);
|
|
13116
|
+
if (ctx.common.async) {
|
|
13117
|
+
return Promise.resolve(processed).then(async (processed2) => {
|
|
13118
|
+
if (status.value === "aborted")
|
|
13119
|
+
return INVALID;
|
|
13120
|
+
const result = await this._def.schema._parseAsync({
|
|
13121
|
+
data: processed2,
|
|
13122
|
+
path: ctx.path,
|
|
13123
|
+
parent: ctx
|
|
13124
|
+
});
|
|
13125
|
+
if (result.status === "aborted")
|
|
13126
|
+
return INVALID;
|
|
13127
|
+
if (result.status === "dirty")
|
|
13128
|
+
return DIRTY(result.value);
|
|
13129
|
+
if (status.value === "dirty")
|
|
13130
|
+
return DIRTY(result.value);
|
|
13131
|
+
return result;
|
|
13132
|
+
});
|
|
13133
|
+
} else {
|
|
13134
|
+
if (status.value === "aborted")
|
|
13135
|
+
return INVALID;
|
|
13136
|
+
const result = this._def.schema._parseSync({
|
|
13137
|
+
data: processed,
|
|
13138
|
+
path: ctx.path,
|
|
13139
|
+
parent: ctx
|
|
13140
|
+
});
|
|
13141
|
+
if (result.status === "aborted")
|
|
13142
|
+
return INVALID;
|
|
13143
|
+
if (result.status === "dirty")
|
|
13144
|
+
return DIRTY(result.value);
|
|
13145
|
+
if (status.value === "dirty")
|
|
13146
|
+
return DIRTY(result.value);
|
|
13147
|
+
return result;
|
|
13148
|
+
}
|
|
13149
|
+
}
|
|
13150
|
+
if (effect.type === "refinement") {
|
|
13151
|
+
const executeRefinement = (acc) => {
|
|
13152
|
+
const result = effect.refinement(acc, checkCtx);
|
|
13153
|
+
if (ctx.common.async) {
|
|
13154
|
+
return Promise.resolve(result);
|
|
13155
|
+
}
|
|
13156
|
+
if (result instanceof Promise) {
|
|
13157
|
+
throw new Error("Async refinement encountered during synchronous parse operation. Use .parseAsync instead.");
|
|
13158
|
+
}
|
|
13159
|
+
return acc;
|
|
13160
|
+
};
|
|
13161
|
+
if (ctx.common.async === false) {
|
|
13162
|
+
const inner = this._def.schema._parseSync({
|
|
13163
|
+
data: ctx.data,
|
|
13164
|
+
path: ctx.path,
|
|
13165
|
+
parent: ctx
|
|
13166
|
+
});
|
|
13167
|
+
if (inner.status === "aborted")
|
|
13168
|
+
return INVALID;
|
|
13169
|
+
if (inner.status === "dirty")
|
|
13170
|
+
status.dirty();
|
|
13171
|
+
executeRefinement(inner.value);
|
|
13172
|
+
return { status: status.value, value: inner.value };
|
|
13173
|
+
} else {
|
|
13174
|
+
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((inner) => {
|
|
13175
|
+
if (inner.status === "aborted")
|
|
13176
|
+
return INVALID;
|
|
13177
|
+
if (inner.status === "dirty")
|
|
13178
|
+
status.dirty();
|
|
13179
|
+
return executeRefinement(inner.value).then(() => {
|
|
13180
|
+
return { status: status.value, value: inner.value };
|
|
13181
|
+
});
|
|
13182
|
+
});
|
|
13183
|
+
}
|
|
13184
|
+
}
|
|
13185
|
+
if (effect.type === "transform") {
|
|
13186
|
+
if (ctx.common.async === false) {
|
|
13187
|
+
const base = this._def.schema._parseSync({
|
|
13188
|
+
data: ctx.data,
|
|
13189
|
+
path: ctx.path,
|
|
13190
|
+
parent: ctx
|
|
13191
|
+
});
|
|
13192
|
+
if (!isValid(base))
|
|
13193
|
+
return INVALID;
|
|
13194
|
+
const result = effect.transform(base.value, checkCtx);
|
|
13195
|
+
if (result instanceof Promise) {
|
|
13196
|
+
throw new Error(`Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.`);
|
|
13197
|
+
}
|
|
13198
|
+
return { status: status.value, value: result };
|
|
13199
|
+
} else {
|
|
13200
|
+
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((base) => {
|
|
13201
|
+
if (!isValid(base))
|
|
13202
|
+
return INVALID;
|
|
13203
|
+
return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({
|
|
13204
|
+
status: status.value,
|
|
13205
|
+
value: result
|
|
13206
|
+
}));
|
|
13207
|
+
});
|
|
13208
|
+
}
|
|
13209
|
+
}
|
|
13210
|
+
util.assertNever(effect);
|
|
13211
|
+
}
|
|
13212
|
+
}
|
|
13213
|
+
ZodEffects.create = (schema2, effect, params) => {
|
|
13214
|
+
return new ZodEffects({
|
|
13215
|
+
schema: schema2,
|
|
13216
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
13217
|
+
effect,
|
|
13218
|
+
...processCreateParams(params)
|
|
13219
|
+
});
|
|
13220
|
+
};
|
|
13221
|
+
ZodEffects.createWithPreprocess = (preprocess, schema2, params) => {
|
|
13222
|
+
return new ZodEffects({
|
|
13223
|
+
schema: schema2,
|
|
13224
|
+
effect: { type: "preprocess", transform: preprocess },
|
|
13225
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
13226
|
+
...processCreateParams(params)
|
|
13227
|
+
});
|
|
13228
|
+
};
|
|
13229
|
+
class ZodOptional extends ZodType {
|
|
13230
|
+
_parse(input) {
|
|
13231
|
+
const parsedType = this._getType(input);
|
|
13232
|
+
if (parsedType === ZodParsedType.undefined) {
|
|
13233
|
+
return OK(undefined);
|
|
13234
|
+
}
|
|
13235
|
+
return this._def.innerType._parse(input);
|
|
13236
|
+
}
|
|
13237
|
+
unwrap() {
|
|
13238
|
+
return this._def.innerType;
|
|
13239
|
+
}
|
|
13240
|
+
}
|
|
13241
|
+
ZodOptional.create = (type, params) => {
|
|
13242
|
+
return new ZodOptional({
|
|
13243
|
+
innerType: type,
|
|
13244
|
+
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
13245
|
+
...processCreateParams(params)
|
|
13246
|
+
});
|
|
13247
|
+
};
|
|
13248
|
+
|
|
13249
|
+
class ZodNullable extends ZodType {
|
|
13250
|
+
_parse(input) {
|
|
13251
|
+
const parsedType = this._getType(input);
|
|
13252
|
+
if (parsedType === ZodParsedType.null) {
|
|
13253
|
+
return OK(null);
|
|
13254
|
+
}
|
|
13255
|
+
return this._def.innerType._parse(input);
|
|
13256
|
+
}
|
|
13257
|
+
unwrap() {
|
|
13258
|
+
return this._def.innerType;
|
|
13259
|
+
}
|
|
13260
|
+
}
|
|
13261
|
+
ZodNullable.create = (type, params) => {
|
|
13262
|
+
return new ZodNullable({
|
|
13263
|
+
innerType: type,
|
|
13264
|
+
typeName: ZodFirstPartyTypeKind.ZodNullable,
|
|
13265
|
+
...processCreateParams(params)
|
|
13266
|
+
});
|
|
13267
|
+
};
|
|
13268
|
+
|
|
13269
|
+
class ZodDefault extends ZodType {
|
|
13270
|
+
_parse(input) {
|
|
13271
|
+
const { ctx } = this._processInputParams(input);
|
|
13272
|
+
let data = ctx.data;
|
|
13273
|
+
if (ctx.parsedType === ZodParsedType.undefined) {
|
|
13274
|
+
data = this._def.defaultValue();
|
|
13275
|
+
}
|
|
13276
|
+
return this._def.innerType._parse({
|
|
13277
|
+
data,
|
|
13278
|
+
path: ctx.path,
|
|
13279
|
+
parent: ctx
|
|
13280
|
+
});
|
|
13281
|
+
}
|
|
13282
|
+
removeDefault() {
|
|
13283
|
+
return this._def.innerType;
|
|
13284
|
+
}
|
|
13285
|
+
}
|
|
13286
|
+
ZodDefault.create = (type, params) => {
|
|
13287
|
+
return new ZodDefault({
|
|
13288
|
+
innerType: type,
|
|
13289
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
13290
|
+
defaultValue: typeof params.default === "function" ? params.default : () => params.default,
|
|
13291
|
+
...processCreateParams(params)
|
|
13292
|
+
});
|
|
13293
|
+
};
|
|
13294
|
+
|
|
13295
|
+
class ZodCatch extends ZodType {
|
|
13296
|
+
_parse(input) {
|
|
13297
|
+
const { ctx } = this._processInputParams(input);
|
|
13298
|
+
const newCtx = {
|
|
13299
|
+
...ctx,
|
|
13300
|
+
common: {
|
|
13301
|
+
...ctx.common,
|
|
13302
|
+
issues: []
|
|
13303
|
+
}
|
|
13304
|
+
};
|
|
13305
|
+
const result = this._def.innerType._parse({
|
|
13306
|
+
data: newCtx.data,
|
|
13307
|
+
path: newCtx.path,
|
|
13308
|
+
parent: {
|
|
13309
|
+
...newCtx
|
|
13310
|
+
}
|
|
13311
|
+
});
|
|
13312
|
+
if (isAsync(result)) {
|
|
13313
|
+
return result.then((result2) => {
|
|
13314
|
+
return {
|
|
13315
|
+
status: "valid",
|
|
13316
|
+
value: result2.status === "valid" ? result2.value : this._def.catchValue({
|
|
13317
|
+
get error() {
|
|
13318
|
+
return new ZodError(newCtx.common.issues);
|
|
13319
|
+
},
|
|
13320
|
+
input: newCtx.data
|
|
13321
|
+
})
|
|
13322
|
+
};
|
|
13323
|
+
});
|
|
13324
|
+
} else {
|
|
13325
|
+
return {
|
|
13326
|
+
status: "valid",
|
|
13327
|
+
value: result.status === "valid" ? result.value : this._def.catchValue({
|
|
13328
|
+
get error() {
|
|
13329
|
+
return new ZodError(newCtx.common.issues);
|
|
13330
|
+
},
|
|
13331
|
+
input: newCtx.data
|
|
13332
|
+
})
|
|
13333
|
+
};
|
|
13334
|
+
}
|
|
13335
|
+
}
|
|
13336
|
+
removeCatch() {
|
|
13337
|
+
return this._def.innerType;
|
|
13338
|
+
}
|
|
13339
|
+
}
|
|
13340
|
+
ZodCatch.create = (type, params) => {
|
|
13341
|
+
return new ZodCatch({
|
|
13342
|
+
innerType: type,
|
|
13343
|
+
typeName: ZodFirstPartyTypeKind.ZodCatch,
|
|
13344
|
+
catchValue: typeof params.catch === "function" ? params.catch : () => params.catch,
|
|
13345
|
+
...processCreateParams(params)
|
|
13346
|
+
});
|
|
13347
|
+
};
|
|
13348
|
+
|
|
13349
|
+
class ZodNaN extends ZodType {
|
|
13350
|
+
_parse(input) {
|
|
13351
|
+
const parsedType = this._getType(input);
|
|
13352
|
+
if (parsedType !== ZodParsedType.nan) {
|
|
13353
|
+
const ctx = this._getOrReturnCtx(input);
|
|
13354
|
+
addIssueToContext(ctx, {
|
|
13355
|
+
code: ZodIssueCode.invalid_type,
|
|
13356
|
+
expected: ZodParsedType.nan,
|
|
13357
|
+
received: ctx.parsedType
|
|
13358
|
+
});
|
|
13359
|
+
return INVALID;
|
|
13360
|
+
}
|
|
13361
|
+
return { status: "valid", value: input.data };
|
|
13362
|
+
}
|
|
13363
|
+
}
|
|
13364
|
+
ZodNaN.create = (params) => {
|
|
13365
|
+
return new ZodNaN({
|
|
13366
|
+
typeName: ZodFirstPartyTypeKind.ZodNaN,
|
|
13367
|
+
...processCreateParams(params)
|
|
13368
|
+
});
|
|
13369
|
+
};
|
|
13370
|
+
var BRAND = Symbol("zod_brand");
|
|
13371
|
+
|
|
13372
|
+
class ZodBranded extends ZodType {
|
|
13373
|
+
_parse(input) {
|
|
13374
|
+
const { ctx } = this._processInputParams(input);
|
|
13375
|
+
const data = ctx.data;
|
|
13376
|
+
return this._def.type._parse({
|
|
13377
|
+
data,
|
|
13378
|
+
path: ctx.path,
|
|
13379
|
+
parent: ctx
|
|
13380
|
+
});
|
|
13381
|
+
}
|
|
13382
|
+
unwrap() {
|
|
13383
|
+
return this._def.type;
|
|
13384
|
+
}
|
|
13385
|
+
}
|
|
13386
|
+
|
|
13387
|
+
class ZodPipeline extends ZodType {
|
|
13388
|
+
_parse(input) {
|
|
13389
|
+
const { status, ctx } = this._processInputParams(input);
|
|
13390
|
+
if (ctx.common.async) {
|
|
13391
|
+
const handleAsync = async () => {
|
|
13392
|
+
const inResult = await this._def.in._parseAsync({
|
|
13393
|
+
data: ctx.data,
|
|
13394
|
+
path: ctx.path,
|
|
13395
|
+
parent: ctx
|
|
13396
|
+
});
|
|
13397
|
+
if (inResult.status === "aborted")
|
|
13398
|
+
return INVALID;
|
|
13399
|
+
if (inResult.status === "dirty") {
|
|
13400
|
+
status.dirty();
|
|
13401
|
+
return DIRTY(inResult.value);
|
|
13402
|
+
} else {
|
|
13403
|
+
return this._def.out._parseAsync({
|
|
13404
|
+
data: inResult.value,
|
|
13405
|
+
path: ctx.path,
|
|
13406
|
+
parent: ctx
|
|
13407
|
+
});
|
|
13408
|
+
}
|
|
13409
|
+
};
|
|
13410
|
+
return handleAsync();
|
|
13411
|
+
} else {
|
|
13412
|
+
const inResult = this._def.in._parseSync({
|
|
13413
|
+
data: ctx.data,
|
|
13414
|
+
path: ctx.path,
|
|
13415
|
+
parent: ctx
|
|
13416
|
+
});
|
|
13417
|
+
if (inResult.status === "aborted")
|
|
13418
|
+
return INVALID;
|
|
13419
|
+
if (inResult.status === "dirty") {
|
|
13420
|
+
status.dirty();
|
|
13421
|
+
return {
|
|
13422
|
+
status: "dirty",
|
|
13423
|
+
value: inResult.value
|
|
13424
|
+
};
|
|
13425
|
+
} else {
|
|
13426
|
+
return this._def.out._parseSync({
|
|
13427
|
+
data: inResult.value,
|
|
13428
|
+
path: ctx.path,
|
|
13429
|
+
parent: ctx
|
|
13430
|
+
});
|
|
13431
|
+
}
|
|
13432
|
+
}
|
|
13433
|
+
}
|
|
13434
|
+
static create(a, b) {
|
|
13435
|
+
return new ZodPipeline({
|
|
13436
|
+
in: a,
|
|
13437
|
+
out: b,
|
|
13438
|
+
typeName: ZodFirstPartyTypeKind.ZodPipeline
|
|
13439
|
+
});
|
|
13440
|
+
}
|
|
13441
|
+
}
|
|
13442
|
+
|
|
13443
|
+
class ZodReadonly extends ZodType {
|
|
13444
|
+
_parse(input) {
|
|
13445
|
+
const result = this._def.innerType._parse(input);
|
|
13446
|
+
const freeze = (data) => {
|
|
13447
|
+
if (isValid(data)) {
|
|
13448
|
+
data.value = Object.freeze(data.value);
|
|
13449
|
+
}
|
|
13450
|
+
return data;
|
|
13451
|
+
};
|
|
13452
|
+
return isAsync(result) ? result.then((data) => freeze(data)) : freeze(result);
|
|
13453
|
+
}
|
|
13454
|
+
unwrap() {
|
|
13455
|
+
return this._def.innerType;
|
|
13456
|
+
}
|
|
13457
|
+
}
|
|
13458
|
+
ZodReadonly.create = (type, params) => {
|
|
13459
|
+
return new ZodReadonly({
|
|
13460
|
+
innerType: type,
|
|
13461
|
+
typeName: ZodFirstPartyTypeKind.ZodReadonly,
|
|
13462
|
+
...processCreateParams(params)
|
|
13463
|
+
});
|
|
13464
|
+
};
|
|
13465
|
+
function cleanParams(params, data) {
|
|
13466
|
+
const p = typeof params === "function" ? params(data) : typeof params === "string" ? { message: params } : params;
|
|
13467
|
+
const p2 = typeof p === "string" ? { message: p } : p;
|
|
13468
|
+
return p2;
|
|
13469
|
+
}
|
|
13470
|
+
function custom(check, _params = {}, fatal) {
|
|
13471
|
+
if (check)
|
|
13472
|
+
return ZodAny.create().superRefine((data, ctx) => {
|
|
13473
|
+
const r = check(data);
|
|
13474
|
+
if (r instanceof Promise) {
|
|
13475
|
+
return r.then((r2) => {
|
|
13476
|
+
if (!r2) {
|
|
13477
|
+
const params = cleanParams(_params, data);
|
|
13478
|
+
const _fatal = params.fatal ?? fatal ?? true;
|
|
13479
|
+
ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
|
|
13480
|
+
}
|
|
13481
|
+
});
|
|
13482
|
+
}
|
|
13483
|
+
if (!r) {
|
|
13484
|
+
const params = cleanParams(_params, data);
|
|
13485
|
+
const _fatal = params.fatal ?? fatal ?? true;
|
|
13486
|
+
ctx.addIssue({ code: "custom", ...params, fatal: _fatal });
|
|
13487
|
+
}
|
|
13488
|
+
return;
|
|
13489
|
+
});
|
|
13490
|
+
return ZodAny.create();
|
|
13491
|
+
}
|
|
13492
|
+
var late = {
|
|
13493
|
+
object: ZodObject.lazycreate
|
|
13494
|
+
};
|
|
13495
|
+
var ZodFirstPartyTypeKind;
|
|
13496
|
+
(function(ZodFirstPartyTypeKind2) {
|
|
13497
|
+
ZodFirstPartyTypeKind2["ZodString"] = "ZodString";
|
|
13498
|
+
ZodFirstPartyTypeKind2["ZodNumber"] = "ZodNumber";
|
|
13499
|
+
ZodFirstPartyTypeKind2["ZodNaN"] = "ZodNaN";
|
|
13500
|
+
ZodFirstPartyTypeKind2["ZodBigInt"] = "ZodBigInt";
|
|
13501
|
+
ZodFirstPartyTypeKind2["ZodBoolean"] = "ZodBoolean";
|
|
13502
|
+
ZodFirstPartyTypeKind2["ZodDate"] = "ZodDate";
|
|
13503
|
+
ZodFirstPartyTypeKind2["ZodSymbol"] = "ZodSymbol";
|
|
13504
|
+
ZodFirstPartyTypeKind2["ZodUndefined"] = "ZodUndefined";
|
|
13505
|
+
ZodFirstPartyTypeKind2["ZodNull"] = "ZodNull";
|
|
13506
|
+
ZodFirstPartyTypeKind2["ZodAny"] = "ZodAny";
|
|
13507
|
+
ZodFirstPartyTypeKind2["ZodUnknown"] = "ZodUnknown";
|
|
13508
|
+
ZodFirstPartyTypeKind2["ZodNever"] = "ZodNever";
|
|
13509
|
+
ZodFirstPartyTypeKind2["ZodVoid"] = "ZodVoid";
|
|
13510
|
+
ZodFirstPartyTypeKind2["ZodArray"] = "ZodArray";
|
|
13511
|
+
ZodFirstPartyTypeKind2["ZodObject"] = "ZodObject";
|
|
13512
|
+
ZodFirstPartyTypeKind2["ZodUnion"] = "ZodUnion";
|
|
13513
|
+
ZodFirstPartyTypeKind2["ZodDiscriminatedUnion"] = "ZodDiscriminatedUnion";
|
|
13514
|
+
ZodFirstPartyTypeKind2["ZodIntersection"] = "ZodIntersection";
|
|
13515
|
+
ZodFirstPartyTypeKind2["ZodTuple"] = "ZodTuple";
|
|
13516
|
+
ZodFirstPartyTypeKind2["ZodRecord"] = "ZodRecord";
|
|
13517
|
+
ZodFirstPartyTypeKind2["ZodMap"] = "ZodMap";
|
|
13518
|
+
ZodFirstPartyTypeKind2["ZodSet"] = "ZodSet";
|
|
13519
|
+
ZodFirstPartyTypeKind2["ZodFunction"] = "ZodFunction";
|
|
13520
|
+
ZodFirstPartyTypeKind2["ZodLazy"] = "ZodLazy";
|
|
13521
|
+
ZodFirstPartyTypeKind2["ZodLiteral"] = "ZodLiteral";
|
|
13522
|
+
ZodFirstPartyTypeKind2["ZodEnum"] = "ZodEnum";
|
|
13523
|
+
ZodFirstPartyTypeKind2["ZodEffects"] = "ZodEffects";
|
|
13524
|
+
ZodFirstPartyTypeKind2["ZodNativeEnum"] = "ZodNativeEnum";
|
|
13525
|
+
ZodFirstPartyTypeKind2["ZodOptional"] = "ZodOptional";
|
|
13526
|
+
ZodFirstPartyTypeKind2["ZodNullable"] = "ZodNullable";
|
|
13527
|
+
ZodFirstPartyTypeKind2["ZodDefault"] = "ZodDefault";
|
|
13528
|
+
ZodFirstPartyTypeKind2["ZodCatch"] = "ZodCatch";
|
|
13529
|
+
ZodFirstPartyTypeKind2["ZodPromise"] = "ZodPromise";
|
|
13530
|
+
ZodFirstPartyTypeKind2["ZodBranded"] = "ZodBranded";
|
|
13531
|
+
ZodFirstPartyTypeKind2["ZodPipeline"] = "ZodPipeline";
|
|
13532
|
+
ZodFirstPartyTypeKind2["ZodReadonly"] = "ZodReadonly";
|
|
13533
|
+
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
|
|
13534
|
+
var instanceOfType = (cls, params = {
|
|
13535
|
+
message: `Input not instance of ${cls.name}`
|
|
13536
|
+
}) => custom((data) => data instanceof cls, params);
|
|
13537
|
+
var stringType = ZodString.create;
|
|
13538
|
+
var numberType = ZodNumber.create;
|
|
13539
|
+
var nanType = ZodNaN.create;
|
|
13540
|
+
var bigIntType = ZodBigInt.create;
|
|
13541
|
+
var booleanType = ZodBoolean.create;
|
|
13542
|
+
var dateType = ZodDate.create;
|
|
13543
|
+
var symbolType = ZodSymbol.create;
|
|
13544
|
+
var undefinedType = ZodUndefined.create;
|
|
13545
|
+
var nullType = ZodNull.create;
|
|
13546
|
+
var anyType = ZodAny.create;
|
|
13547
|
+
var unknownType = ZodUnknown.create;
|
|
13548
|
+
var neverType = ZodNever.create;
|
|
13549
|
+
var voidType = ZodVoid.create;
|
|
13550
|
+
var arrayType = ZodArray.create;
|
|
13551
|
+
var objectType = ZodObject.create;
|
|
13552
|
+
var strictObjectType = ZodObject.strictCreate;
|
|
13553
|
+
var unionType = ZodUnion.create;
|
|
13554
|
+
var discriminatedUnionType = ZodDiscriminatedUnion.create;
|
|
13555
|
+
var intersectionType = ZodIntersection.create;
|
|
13556
|
+
var tupleType = ZodTuple.create;
|
|
13557
|
+
var recordType = ZodRecord.create;
|
|
13558
|
+
var mapType = ZodMap.create;
|
|
13559
|
+
var setType = ZodSet.create;
|
|
13560
|
+
var functionType = ZodFunction.create;
|
|
13561
|
+
var lazyType = ZodLazy.create;
|
|
13562
|
+
var literalType = ZodLiteral.create;
|
|
13563
|
+
var enumType = ZodEnum.create;
|
|
13564
|
+
var nativeEnumType = ZodNativeEnum.create;
|
|
13565
|
+
var promiseType = ZodPromise.create;
|
|
13566
|
+
var effectsType = ZodEffects.create;
|
|
13567
|
+
var optionalType = ZodOptional.create;
|
|
13568
|
+
var nullableType = ZodNullable.create;
|
|
13569
|
+
var preprocessType = ZodEffects.createWithPreprocess;
|
|
13570
|
+
var pipelineType = ZodPipeline.create;
|
|
13571
|
+
var ostring = () => stringType().optional();
|
|
13572
|
+
var onumber = () => numberType().optional();
|
|
13573
|
+
var oboolean = () => booleanType().optional();
|
|
13574
|
+
var coerce = {
|
|
13575
|
+
string: (arg) => ZodString.create({ ...arg, coerce: true }),
|
|
13576
|
+
number: (arg) => ZodNumber.create({ ...arg, coerce: true }),
|
|
13577
|
+
boolean: (arg) => ZodBoolean.create({
|
|
13578
|
+
...arg,
|
|
13579
|
+
coerce: true
|
|
13580
|
+
}),
|
|
13581
|
+
bigint: (arg) => ZodBigInt.create({ ...arg, coerce: true }),
|
|
13582
|
+
date: (arg) => ZodDate.create({ ...arg, coerce: true })
|
|
13583
|
+
};
|
|
13584
|
+
var NEVER = INVALID;
|
|
13585
|
+
// src/lib/schemas.ts
|
|
13586
|
+
var ProviderSchema = exports_external.enum(["openai", "thinker-labs"], {
|
|
13587
|
+
errorMap: () => ({ message: "Invalid provider. Must be one of: openai, thinker-labs" })
|
|
13588
|
+
});
|
|
13589
|
+
var FinetuneStartSchema = exports_external.object({
|
|
13590
|
+
provider: ProviderSchema,
|
|
13591
|
+
baseModel: exports_external.string().min(1, "Base model is required"),
|
|
13592
|
+
dataset: exports_external.string().optional(),
|
|
13593
|
+
name: exports_external.string().min(1, "Name is required")
|
|
13594
|
+
});
|
|
13595
|
+
var GatherOptionsSchema = exports_external.object({
|
|
13596
|
+
source: exports_external.enum(["todos", "mementos", "conversations", "sessions", "all"]).default("all"),
|
|
13597
|
+
output: exports_external.string().optional(),
|
|
13598
|
+
limit: exports_external.coerce.number().int().positive("Limit must be a positive integer").default(500)
|
|
13599
|
+
});
|
|
13600
|
+
var ModelUpdateSchema = exports_external.object({
|
|
13601
|
+
displayName: exports_external.string().optional(),
|
|
13602
|
+
description: exports_external.string().optional(),
|
|
13603
|
+
collection: exports_external.string().optional(),
|
|
13604
|
+
tags: exports_external.array(exports_external.string()).optional()
|
|
13605
|
+
});
|
|
13606
|
+
var McpGatherSchema = exports_external.object({
|
|
13607
|
+
sources: exports_external.array(exports_external.enum(["todos", "mementos", "conversations", "sessions"])).min(1, "At least one source required"),
|
|
13608
|
+
limit: exports_external.number().int().positive().optional(),
|
|
13609
|
+
output_dir: exports_external.string().optional()
|
|
13610
|
+
});
|
|
13611
|
+
var McpFinetuneStartSchema = exports_external.object({
|
|
13612
|
+
provider: ProviderSchema,
|
|
13613
|
+
base_model: exports_external.string().min(1, "Base model is required"),
|
|
13614
|
+
dataset_path: exports_external.string().optional(),
|
|
13615
|
+
name: exports_external.string().optional()
|
|
13616
|
+
});
|
|
13617
|
+
var McpFinetuneStatusSchema = exports_external.object({
|
|
13618
|
+
job_id: exports_external.string().min(1, "Job ID is required"),
|
|
13619
|
+
provider: ProviderSchema
|
|
13620
|
+
});
|
|
9436
13621
|
export {
|
|
13622
|
+
withRetry,
|
|
9437
13623
|
uploadTrainingFile,
|
|
9438
13624
|
uploadTrainingData,
|
|
9439
13625
|
trainingJobs,
|
|
9440
13626
|
trainingDatasets,
|
|
9441
13627
|
startFineTune,
|
|
13628
|
+
setConfigValue,
|
|
9442
13629
|
listModels,
|
|
9443
13630
|
listFineTunedModels,
|
|
13631
|
+
listConfig,
|
|
9444
13632
|
getStatus,
|
|
9445
13633
|
getPackageVersion,
|
|
9446
13634
|
getFineTuneStatus,
|
|
9447
13635
|
getDb,
|
|
13636
|
+
getConfigValue,
|
|
9448
13637
|
fineTunedModels,
|
|
13638
|
+
deleteConfigValue,
|
|
9449
13639
|
createFineTuneJob,
|
|
9450
13640
|
cancelJob,
|
|
9451
13641
|
cancelFineTuneJob,
|
|
9452
13642
|
ThinkerLabsProvider,
|
|
9453
|
-
|
|
13643
|
+
ProviderSchema,
|
|
13644
|
+
OpenAIProvider,
|
|
13645
|
+
ModelUpdateSchema,
|
|
13646
|
+
McpGatherSchema,
|
|
13647
|
+
McpFinetuneStatusSchema,
|
|
13648
|
+
McpFinetuneStartSchema,
|
|
13649
|
+
GatherOptionsSchema,
|
|
13650
|
+
FinetuneStartSchema,
|
|
13651
|
+
CONFIG_KEYS
|
|
9454
13652
|
};
|