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