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