@rpgjs/client 5.0.0-alpha.21 → 5.0.0-alpha.22
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/Game/Object.d.ts +109 -0
- package/dist/RpgClientEngine.d.ts +111 -0
- package/dist/index.d.ts +2 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/index10.js +1 -1
- package/dist/index15.js +42 -3
- package/dist/index15.js.map +1 -1
- package/dist/index19.js.map +1 -1
- package/dist/index2.js +142 -2
- package/dist/index2.js.map +1 -1
- package/dist/index20.js +15 -2
- package/dist/index20.js.map +1 -1
- package/dist/index23.js.map +1 -1
- package/dist/index26.js +2 -2
- package/dist/index26.js.map +1 -1
- package/dist/index27.js +2 -2
- package/dist/index27.js.map +1 -1
- package/dist/index34.js.map +1 -1
- package/dist/index35.js.map +1 -1
- package/dist/index36.js +8 -1
- package/dist/index36.js.map +1 -1
- package/dist/index37.js +1 -1
- package/dist/index38.js +1 -1
- package/dist/index40.js +6 -6
- package/dist/index41.js +320 -3681
- package/dist/index41.js.map +1 -1
- package/dist/index42.js +3686 -183
- package/dist/index42.js.map +1 -1
- package/dist/index43.js +71 -498
- package/dist/index43.js.map +1 -1
- package/dist/index44.js +182 -72
- package/dist/index44.js.map +1 -1
- package/dist/index45.js +500 -2
- package/dist/index45.js.map +1 -1
- package/dist/index46.js +3 -17
- package/dist/index46.js.map +1 -1
- package/dist/index47.js +16 -142
- package/dist/index47.js.map +1 -1
- package/dist/index48.js +206 -8
- package/dist/index48.js.map +1 -1
- package/dist/index49.js +7 -108
- package/dist/index49.js.map +1 -1
- package/dist/index50.js +104 -127
- package/dist/index50.js.map +1 -1
- package/dist/index51.js +122 -123
- package/dist/index51.js.map +1 -1
- package/dist/index52.js +123 -98
- package/dist/index52.js.map +1 -1
- package/dist/index53.js +107 -136
- package/dist/index53.js.map +1 -1
- package/dist/index54.js +139 -7
- package/dist/index54.js.map +1 -1
- package/dist/index55.js +7 -52
- package/dist/index55.js.map +1 -1
- package/dist/index56.js +54 -0
- package/dist/index56.js.map +1 -0
- package/dist/index8.js +8 -0
- package/dist/index8.js.map +1 -1
- package/dist/module.d.ts +43 -4
- package/dist/services/keyboardControls.d.ts +13 -2
- package/dist/services/mmorpg.d.ts +1 -1
- package/dist/services/standalone.d.ts +1 -1
- package/package.json +11 -10
- package/src/Game/Object.ts +82 -8
- package/src/RpgClientEngine.ts +173 -2
- package/src/components/character.ce +67 -4
- package/src/components/prebuilt/index.ts +1 -0
- package/src/components/scenes/draw-map.ce +12 -3
- package/src/index.ts +2 -1
- package/src/module.ts +56 -2
- package/src/services/keyboardControls.ts +13 -1
package/dist/index41.js
CHANGED
|
@@ -1,3690 +1,329 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { getErrorMap } from './index48.js';
|
|
3
|
-
import { errorUtil } from './index54.js';
|
|
4
|
-
import { INVALID, ParseStatus, isAsync, isValid, addIssueToContext, OK, DIRTY, isAborted, isDirty, makeIssue } from './index49.js';
|
|
5
|
-
import { getParsedType, ZodParsedType, util } from './index50.js';
|
|
6
|
-
import errorMap from './index52.js';
|
|
1
|
+
import { BehaviorSubject, filter, combineLatest, map, finalize } from 'rxjs';
|
|
7
2
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
_getOrReturnCtx(input, ctx) {
|
|
79
|
-
return (ctx || {
|
|
80
|
-
common: input.parent.common,
|
|
81
|
-
data: input.data,
|
|
82
|
-
parsedType: getParsedType(input.data),
|
|
83
|
-
schemaErrorMap: this._def.errorMap,
|
|
84
|
-
path: input.path,
|
|
85
|
-
parent: input.parent,
|
|
86
|
-
});
|
|
87
|
-
}
|
|
88
|
-
_processInputParams(input) {
|
|
89
|
-
return {
|
|
90
|
-
status: new ParseStatus(),
|
|
91
|
-
ctx: {
|
|
92
|
-
common: input.parent.common,
|
|
93
|
-
data: input.data,
|
|
94
|
-
parsedType: getParsedType(input.data),
|
|
95
|
-
schemaErrorMap: this._def.errorMap,
|
|
96
|
-
path: input.path,
|
|
97
|
-
parent: input.parent,
|
|
98
|
-
},
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
_parseSync(input) {
|
|
102
|
-
const result = this._parse(input);
|
|
103
|
-
if (isAsync(result)) {
|
|
104
|
-
throw new Error("Synchronous parse encountered promise.");
|
|
105
|
-
}
|
|
106
|
-
return result;
|
|
107
|
-
}
|
|
108
|
-
_parseAsync(input) {
|
|
109
|
-
const result = this._parse(input);
|
|
110
|
-
return Promise.resolve(result);
|
|
111
|
-
}
|
|
112
|
-
parse(data, params) {
|
|
113
|
-
const result = this.safeParse(data, params);
|
|
114
|
-
if (result.success)
|
|
115
|
-
return result.data;
|
|
116
|
-
throw result.error;
|
|
117
|
-
}
|
|
118
|
-
safeParse(data, params) {
|
|
119
|
-
const ctx = {
|
|
120
|
-
common: {
|
|
121
|
-
issues: [],
|
|
122
|
-
async: params?.async ?? false,
|
|
123
|
-
contextualErrorMap: params?.errorMap,
|
|
124
|
-
},
|
|
125
|
-
path: params?.path || [],
|
|
126
|
-
schemaErrorMap: this._def.errorMap,
|
|
127
|
-
parent: null,
|
|
128
|
-
data,
|
|
129
|
-
parsedType: getParsedType(data),
|
|
130
|
-
};
|
|
131
|
-
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
|
|
132
|
-
return handleResult(ctx, result);
|
|
133
|
-
}
|
|
134
|
-
"~validate"(data) {
|
|
135
|
-
const ctx = {
|
|
136
|
-
common: {
|
|
137
|
-
issues: [],
|
|
138
|
-
async: !!this["~standard"].async,
|
|
139
|
-
},
|
|
140
|
-
path: [],
|
|
141
|
-
schemaErrorMap: this._def.errorMap,
|
|
142
|
-
parent: null,
|
|
143
|
-
data,
|
|
144
|
-
parsedType: getParsedType(data),
|
|
145
|
-
};
|
|
146
|
-
if (!this["~standard"].async) {
|
|
147
|
-
try {
|
|
148
|
-
const result = this._parseSync({ data, path: [], parent: ctx });
|
|
149
|
-
return isValid(result)
|
|
150
|
-
? {
|
|
151
|
-
value: result.value,
|
|
152
|
-
}
|
|
153
|
-
: {
|
|
154
|
-
issues: ctx.common.issues,
|
|
155
|
-
};
|
|
156
|
-
}
|
|
157
|
-
catch (err) {
|
|
158
|
-
if (err?.message?.toLowerCase()?.includes("encountered")) {
|
|
159
|
-
this["~standard"].async = true;
|
|
160
|
-
}
|
|
161
|
-
ctx.common = {
|
|
162
|
-
issues: [],
|
|
163
|
-
async: true,
|
|
164
|
-
};
|
|
165
|
-
}
|
|
166
|
-
}
|
|
167
|
-
return this._parseAsync({ data, path: [], parent: ctx }).then((result) => isValid(result)
|
|
168
|
-
? {
|
|
169
|
-
value: result.value,
|
|
170
|
-
}
|
|
171
|
-
: {
|
|
172
|
-
issues: ctx.common.issues,
|
|
173
|
-
});
|
|
174
|
-
}
|
|
175
|
-
async parseAsync(data, params) {
|
|
176
|
-
const result = await this.safeParseAsync(data, params);
|
|
177
|
-
if (result.success)
|
|
178
|
-
return result.data;
|
|
179
|
-
throw result.error;
|
|
180
|
-
}
|
|
181
|
-
async safeParseAsync(data, params) {
|
|
182
|
-
const ctx = {
|
|
183
|
-
common: {
|
|
184
|
-
issues: [],
|
|
185
|
-
contextualErrorMap: params?.errorMap,
|
|
186
|
-
async: true,
|
|
187
|
-
},
|
|
188
|
-
path: params?.path || [],
|
|
189
|
-
schemaErrorMap: this._def.errorMap,
|
|
190
|
-
parent: null,
|
|
191
|
-
data,
|
|
192
|
-
parsedType: getParsedType(data),
|
|
193
|
-
};
|
|
194
|
-
const maybeAsyncResult = this._parse({ data, path: ctx.path, parent: ctx });
|
|
195
|
-
const result = await (isAsync(maybeAsyncResult) ? maybeAsyncResult : Promise.resolve(maybeAsyncResult));
|
|
196
|
-
return handleResult(ctx, result);
|
|
197
|
-
}
|
|
198
|
-
refine(check, message) {
|
|
199
|
-
const getIssueProperties = (val) => {
|
|
200
|
-
if (typeof message === "string" || typeof message === "undefined") {
|
|
201
|
-
return { message };
|
|
202
|
-
}
|
|
203
|
-
else if (typeof message === "function") {
|
|
204
|
-
return message(val);
|
|
205
|
-
}
|
|
206
|
-
else {
|
|
207
|
-
return message;
|
|
208
|
-
}
|
|
209
|
-
};
|
|
210
|
-
return this._refinement((val, ctx) => {
|
|
211
|
-
const result = check(val);
|
|
212
|
-
const setError = () => ctx.addIssue({
|
|
213
|
-
code: ZodIssueCode.custom,
|
|
214
|
-
...getIssueProperties(val),
|
|
215
|
-
});
|
|
216
|
-
if (typeof Promise !== "undefined" && result instanceof Promise) {
|
|
217
|
-
return result.then((data) => {
|
|
218
|
-
if (!data) {
|
|
219
|
-
setError();
|
|
220
|
-
return false;
|
|
221
|
-
}
|
|
222
|
-
else {
|
|
223
|
-
return true;
|
|
224
|
-
}
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
if (!result) {
|
|
228
|
-
setError();
|
|
229
|
-
return false;
|
|
230
|
-
}
|
|
231
|
-
else {
|
|
232
|
-
return true;
|
|
233
|
-
}
|
|
234
|
-
});
|
|
235
|
-
}
|
|
236
|
-
refinement(check, refinementData) {
|
|
237
|
-
return this._refinement((val, ctx) => {
|
|
238
|
-
if (!check(val)) {
|
|
239
|
-
ctx.addIssue(typeof refinementData === "function" ? refinementData(val, ctx) : refinementData);
|
|
240
|
-
return false;
|
|
241
|
-
}
|
|
242
|
-
else {
|
|
243
|
-
return true;
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
_refinement(refinement) {
|
|
248
|
-
return new ZodEffects({
|
|
249
|
-
schema: this,
|
|
250
|
-
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
251
|
-
effect: { type: "refinement", refinement },
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
superRefine(refinement) {
|
|
255
|
-
return this._refinement(refinement);
|
|
256
|
-
}
|
|
257
|
-
constructor(def) {
|
|
258
|
-
/** Alias of safeParseAsync */
|
|
259
|
-
this.spa = this.safeParseAsync;
|
|
260
|
-
this._def = def;
|
|
261
|
-
this.parse = this.parse.bind(this);
|
|
262
|
-
this.safeParse = this.safeParse.bind(this);
|
|
263
|
-
this.parseAsync = this.parseAsync.bind(this);
|
|
264
|
-
this.safeParseAsync = this.safeParseAsync.bind(this);
|
|
265
|
-
this.spa = this.spa.bind(this);
|
|
266
|
-
this.refine = this.refine.bind(this);
|
|
267
|
-
this.refinement = this.refinement.bind(this);
|
|
268
|
-
this.superRefine = this.superRefine.bind(this);
|
|
269
|
-
this.optional = this.optional.bind(this);
|
|
270
|
-
this.nullable = this.nullable.bind(this);
|
|
271
|
-
this.nullish = this.nullish.bind(this);
|
|
272
|
-
this.array = this.array.bind(this);
|
|
273
|
-
this.promise = this.promise.bind(this);
|
|
274
|
-
this.or = this.or.bind(this);
|
|
275
|
-
this.and = this.and.bind(this);
|
|
276
|
-
this.transform = this.transform.bind(this);
|
|
277
|
-
this.brand = this.brand.bind(this);
|
|
278
|
-
this.default = this.default.bind(this);
|
|
279
|
-
this.catch = this.catch.bind(this);
|
|
280
|
-
this.describe = this.describe.bind(this);
|
|
281
|
-
this.pipe = this.pipe.bind(this);
|
|
282
|
-
this.readonly = this.readonly.bind(this);
|
|
283
|
-
this.isNullable = this.isNullable.bind(this);
|
|
284
|
-
this.isOptional = this.isOptional.bind(this);
|
|
285
|
-
this["~standard"] = {
|
|
286
|
-
version: 1,
|
|
287
|
-
vendor: "zod",
|
|
288
|
-
validate: (data) => this["~validate"](data),
|
|
289
|
-
};
|
|
290
|
-
}
|
|
291
|
-
optional() {
|
|
292
|
-
return ZodOptional.create(this, this._def);
|
|
293
|
-
}
|
|
294
|
-
nullable() {
|
|
295
|
-
return ZodNullable.create(this, this._def);
|
|
296
|
-
}
|
|
297
|
-
nullish() {
|
|
298
|
-
return this.nullable().optional();
|
|
299
|
-
}
|
|
300
|
-
array() {
|
|
301
|
-
return ZodArray.create(this);
|
|
302
|
-
}
|
|
303
|
-
promise() {
|
|
304
|
-
return ZodPromise.create(this, this._def);
|
|
305
|
-
}
|
|
306
|
-
or(option) {
|
|
307
|
-
return ZodUnion.create([this, option], this._def);
|
|
308
|
-
}
|
|
309
|
-
and(incoming) {
|
|
310
|
-
return ZodIntersection.create(this, incoming, this._def);
|
|
311
|
-
}
|
|
312
|
-
transform(transform) {
|
|
313
|
-
return new ZodEffects({
|
|
314
|
-
...processCreateParams(this._def),
|
|
315
|
-
schema: this,
|
|
316
|
-
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
317
|
-
effect: { type: "transform", transform },
|
|
318
|
-
});
|
|
319
|
-
}
|
|
320
|
-
default(def) {
|
|
321
|
-
const defaultValueFunc = typeof def === "function" ? def : () => def;
|
|
322
|
-
return new ZodDefault({
|
|
323
|
-
...processCreateParams(this._def),
|
|
324
|
-
innerType: this,
|
|
325
|
-
defaultValue: defaultValueFunc,
|
|
326
|
-
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
327
|
-
});
|
|
328
|
-
}
|
|
329
|
-
brand() {
|
|
330
|
-
return new ZodBranded({
|
|
331
|
-
typeName: ZodFirstPartyTypeKind.ZodBranded,
|
|
332
|
-
type: this,
|
|
333
|
-
...processCreateParams(this._def),
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
|
-
catch(def) {
|
|
337
|
-
const catchValueFunc = typeof def === "function" ? def : () => def;
|
|
338
|
-
return new ZodCatch({
|
|
339
|
-
...processCreateParams(this._def),
|
|
340
|
-
innerType: this,
|
|
341
|
-
catchValue: catchValueFunc,
|
|
342
|
-
typeName: ZodFirstPartyTypeKind.ZodCatch,
|
|
343
|
-
});
|
|
344
|
-
}
|
|
345
|
-
describe(description) {
|
|
346
|
-
const This = this.constructor;
|
|
347
|
-
return new This({
|
|
348
|
-
...this._def,
|
|
349
|
-
description,
|
|
350
|
-
});
|
|
351
|
-
}
|
|
352
|
-
pipe(target) {
|
|
353
|
-
return ZodPipeline.create(this, target);
|
|
354
|
-
}
|
|
355
|
-
readonly() {
|
|
356
|
-
return ZodReadonly.create(this);
|
|
357
|
-
}
|
|
358
|
-
isOptional() {
|
|
359
|
-
return this.safeParse(undefined).success;
|
|
360
|
-
}
|
|
361
|
-
isNullable() {
|
|
362
|
-
return this.safeParse(null).success;
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
const cuidRegex = /^c[^\s-]{8,}$/i;
|
|
366
|
-
const cuid2Regex = /^[0-9a-z]+$/;
|
|
367
|
-
const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
|
|
368
|
-
// const uuidRegex =
|
|
369
|
-
// /^([a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[a-f0-9]{4}-[a-f0-9]{12}|00000000-0000-0000-0000-000000000000)$/i;
|
|
370
|
-
const 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;
|
|
371
|
-
const nanoidRegex = /^[a-z0-9_-]{21}$/i;
|
|
372
|
-
const jwtRegex = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/;
|
|
373
|
-
const 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)?)??$/;
|
|
374
|
-
// from https://stackoverflow.com/a/46181/1550155
|
|
375
|
-
// old version: too slow, didn't support unicode
|
|
376
|
-
// const emailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
|
|
377
|
-
//old email regex
|
|
378
|
-
// const emailRegex = /^(([^<>()[\].,;:\s@"]+(\.[^<>()[\].,;:\s@"]+)*)|(".+"))@((?!-)([^<>()[\].,;:\s@"]+\.)+[^<>()[\].,;:\s@"]{1,})[^-<>()[\].,;:\s@"]$/i;
|
|
379
|
-
// eslint-disable-next-line
|
|
380
|
-
// const emailRegex =
|
|
381
|
-
// /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\])|(\[IPv6:(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))\])|([A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])*(\.[A-Za-z]{2,})+))$/;
|
|
382
|
-
// const emailRegex =
|
|
383
|
-
// /^[a-zA-Z0-9\.\!\#\$\%\&\'\*\+\/\=\?\^\_\`\{\|\}\~\-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
|
|
384
|
-
// const emailRegex =
|
|
385
|
-
// /^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])$/i;
|
|
386
|
-
const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
|
|
387
|
-
// const emailRegex =
|
|
388
|
-
// /^[a-z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-z0-9-]+(?:\.[a-z0-9\-]+)*$/i;
|
|
389
|
-
// from https://thekevinscott.com/emojis-in-javascript/#writing-a-regular-expression
|
|
390
|
-
const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
391
|
-
let emojiRegex;
|
|
392
|
-
// faster, simpler, safer
|
|
393
|
-
const 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])$/;
|
|
394
|
-
const 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])$/;
|
|
395
|
-
// const ipv6Regex =
|
|
396
|
-
// /^(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))$/;
|
|
397
|
-
const 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]))$/;
|
|
398
|
-
const 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])$/;
|
|
399
|
-
// https://stackoverflow.com/questions/7860392/determine-if-string-is-in-base64-using-javascript
|
|
400
|
-
const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
401
|
-
// https://base64.guru/standards/base64url
|
|
402
|
-
const base64urlRegex = /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/;
|
|
403
|
-
// simple
|
|
404
|
-
// const dateRegexSource = `\\d{4}-\\d{2}-\\d{2}`;
|
|
405
|
-
// no leap year validation
|
|
406
|
-
// const dateRegexSource = `\\d{4}-((0[13578]|10|12)-31|(0[13-9]|1[0-2])-30|(0[1-9]|1[0-2])-(0[1-9]|1\\d|2\\d))`;
|
|
407
|
-
// with leap year validation
|
|
408
|
-
const 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])))`;
|
|
409
|
-
const dateRegex = new RegExp(`^${dateRegexSource}$`);
|
|
410
|
-
function timeRegexSource(args) {
|
|
411
|
-
let secondsRegexSource = `[0-5]\\d`;
|
|
412
|
-
if (args.precision) {
|
|
413
|
-
secondsRegexSource = `${secondsRegexSource}\\.\\d{${args.precision}}`;
|
|
414
|
-
}
|
|
415
|
-
else if (args.precision == null) {
|
|
416
|
-
secondsRegexSource = `${secondsRegexSource}(\\.\\d+)?`;
|
|
417
|
-
}
|
|
418
|
-
const secondsQuantifier = args.precision ? "+" : "?"; // require seconds if precision is nonzero
|
|
419
|
-
return `([01]\\d|2[0-3]):[0-5]\\d(:${secondsRegexSource})${secondsQuantifier}`;
|
|
420
|
-
}
|
|
421
|
-
function timeRegex(args) {
|
|
422
|
-
return new RegExp(`^${timeRegexSource(args)}$`);
|
|
423
|
-
}
|
|
424
|
-
// Adapted from https://stackoverflow.com/a/3143231
|
|
425
|
-
function datetimeRegex(args) {
|
|
426
|
-
let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
|
|
427
|
-
const opts = [];
|
|
428
|
-
opts.push(args.local ? `Z?` : `Z`);
|
|
429
|
-
if (args.offset)
|
|
430
|
-
opts.push(`([+-]\\d{2}:?\\d{2})`);
|
|
431
|
-
regex = `${regex}(${opts.join("|")})`;
|
|
432
|
-
return new RegExp(`^${regex}$`);
|
|
433
|
-
}
|
|
434
|
-
function isValidIP(ip, version) {
|
|
435
|
-
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
|
|
436
|
-
return true;
|
|
437
|
-
}
|
|
438
|
-
if ((version === "v6" || !version) && ipv6Regex.test(ip)) {
|
|
439
|
-
return true;
|
|
440
|
-
}
|
|
441
|
-
return false;
|
|
442
|
-
}
|
|
443
|
-
function isValidJWT(jwt, alg) {
|
|
444
|
-
if (!jwtRegex.test(jwt))
|
|
445
|
-
return false;
|
|
446
|
-
try {
|
|
447
|
-
const [header] = jwt.split(".");
|
|
448
|
-
if (!header)
|
|
449
|
-
return false;
|
|
450
|
-
// Convert base64url to base64
|
|
451
|
-
const base64 = header
|
|
452
|
-
.replace(/-/g, "+")
|
|
453
|
-
.replace(/_/g, "/")
|
|
454
|
-
.padEnd(header.length + ((4 - (header.length % 4)) % 4), "=");
|
|
455
|
-
const decoded = JSON.parse(atob(base64));
|
|
456
|
-
if (typeof decoded !== "object" || decoded === null)
|
|
457
|
-
return false;
|
|
458
|
-
if ("typ" in decoded && decoded?.typ !== "JWT")
|
|
459
|
-
return false;
|
|
460
|
-
if (!decoded.alg)
|
|
461
|
-
return false;
|
|
462
|
-
if (alg && decoded.alg !== alg)
|
|
463
|
-
return false;
|
|
464
|
-
return true;
|
|
465
|
-
}
|
|
466
|
-
catch {
|
|
467
|
-
return false;
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
function isValidCidr(ip, version) {
|
|
471
|
-
if ((version === "v4" || !version) && ipv4CidrRegex.test(ip)) {
|
|
472
|
-
return true;
|
|
473
|
-
}
|
|
474
|
-
if ((version === "v6" || !version) && ipv6CidrRegex.test(ip)) {
|
|
475
|
-
return true;
|
|
476
|
-
}
|
|
477
|
-
return false;
|
|
478
|
-
}
|
|
479
|
-
class ZodString extends ZodType {
|
|
480
|
-
_parse(input) {
|
|
481
|
-
if (this._def.coerce) {
|
|
482
|
-
input.data = String(input.data);
|
|
483
|
-
}
|
|
484
|
-
const parsedType = this._getType(input);
|
|
485
|
-
if (parsedType !== ZodParsedType.string) {
|
|
486
|
-
const ctx = this._getOrReturnCtx(input);
|
|
487
|
-
addIssueToContext(ctx, {
|
|
488
|
-
code: ZodIssueCode.invalid_type,
|
|
489
|
-
expected: ZodParsedType.string,
|
|
490
|
-
received: ctx.parsedType,
|
|
491
|
-
});
|
|
492
|
-
return INVALID;
|
|
493
|
-
}
|
|
494
|
-
const status = new ParseStatus();
|
|
495
|
-
let ctx = undefined;
|
|
496
|
-
for (const check of this._def.checks) {
|
|
497
|
-
if (check.kind === "min") {
|
|
498
|
-
if (input.data.length < check.value) {
|
|
499
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
500
|
-
addIssueToContext(ctx, {
|
|
501
|
-
code: ZodIssueCode.too_small,
|
|
502
|
-
minimum: check.value,
|
|
503
|
-
type: "string",
|
|
504
|
-
inclusive: true,
|
|
505
|
-
exact: false,
|
|
506
|
-
message: check.message,
|
|
507
|
-
});
|
|
508
|
-
status.dirty();
|
|
509
|
-
}
|
|
510
|
-
}
|
|
511
|
-
else if (check.kind === "max") {
|
|
512
|
-
if (input.data.length > check.value) {
|
|
513
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
514
|
-
addIssueToContext(ctx, {
|
|
515
|
-
code: ZodIssueCode.too_big,
|
|
516
|
-
maximum: check.value,
|
|
517
|
-
type: "string",
|
|
518
|
-
inclusive: true,
|
|
519
|
-
exact: false,
|
|
520
|
-
message: check.message,
|
|
521
|
-
});
|
|
522
|
-
status.dirty();
|
|
523
|
-
}
|
|
524
|
-
}
|
|
525
|
-
else if (check.kind === "length") {
|
|
526
|
-
const tooBig = input.data.length > check.value;
|
|
527
|
-
const tooSmall = input.data.length < check.value;
|
|
528
|
-
if (tooBig || tooSmall) {
|
|
529
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
530
|
-
if (tooBig) {
|
|
531
|
-
addIssueToContext(ctx, {
|
|
532
|
-
code: ZodIssueCode.too_big,
|
|
533
|
-
maximum: check.value,
|
|
534
|
-
type: "string",
|
|
535
|
-
inclusive: true,
|
|
536
|
-
exact: true,
|
|
537
|
-
message: check.message,
|
|
538
|
-
});
|
|
539
|
-
}
|
|
540
|
-
else if (tooSmall) {
|
|
541
|
-
addIssueToContext(ctx, {
|
|
542
|
-
code: ZodIssueCode.too_small,
|
|
543
|
-
minimum: check.value,
|
|
544
|
-
type: "string",
|
|
545
|
-
inclusive: true,
|
|
546
|
-
exact: true,
|
|
547
|
-
message: check.message,
|
|
548
|
-
});
|
|
549
|
-
}
|
|
550
|
-
status.dirty();
|
|
551
|
-
}
|
|
552
|
-
}
|
|
553
|
-
else if (check.kind === "email") {
|
|
554
|
-
if (!emailRegex.test(input.data)) {
|
|
555
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
556
|
-
addIssueToContext(ctx, {
|
|
557
|
-
validation: "email",
|
|
558
|
-
code: ZodIssueCode.invalid_string,
|
|
559
|
-
message: check.message,
|
|
560
|
-
});
|
|
561
|
-
status.dirty();
|
|
562
|
-
}
|
|
563
|
-
}
|
|
564
|
-
else if (check.kind === "emoji") {
|
|
565
|
-
if (!emojiRegex) {
|
|
566
|
-
emojiRegex = new RegExp(_emojiRegex, "u");
|
|
567
|
-
}
|
|
568
|
-
if (!emojiRegex.test(input.data)) {
|
|
569
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
570
|
-
addIssueToContext(ctx, {
|
|
571
|
-
validation: "emoji",
|
|
572
|
-
code: ZodIssueCode.invalid_string,
|
|
573
|
-
message: check.message,
|
|
574
|
-
});
|
|
575
|
-
status.dirty();
|
|
576
|
-
}
|
|
577
|
-
}
|
|
578
|
-
else if (check.kind === "uuid") {
|
|
579
|
-
if (!uuidRegex.test(input.data)) {
|
|
580
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
581
|
-
addIssueToContext(ctx, {
|
|
582
|
-
validation: "uuid",
|
|
583
|
-
code: ZodIssueCode.invalid_string,
|
|
584
|
-
message: check.message,
|
|
585
|
-
});
|
|
586
|
-
status.dirty();
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
else if (check.kind === "nanoid") {
|
|
590
|
-
if (!nanoidRegex.test(input.data)) {
|
|
591
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
592
|
-
addIssueToContext(ctx, {
|
|
593
|
-
validation: "nanoid",
|
|
594
|
-
code: ZodIssueCode.invalid_string,
|
|
595
|
-
message: check.message,
|
|
596
|
-
});
|
|
597
|
-
status.dirty();
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
|
-
else if (check.kind === "cuid") {
|
|
601
|
-
if (!cuidRegex.test(input.data)) {
|
|
602
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
603
|
-
addIssueToContext(ctx, {
|
|
604
|
-
validation: "cuid",
|
|
605
|
-
code: ZodIssueCode.invalid_string,
|
|
606
|
-
message: check.message,
|
|
607
|
-
});
|
|
608
|
-
status.dirty();
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
else if (check.kind === "cuid2") {
|
|
612
|
-
if (!cuid2Regex.test(input.data)) {
|
|
613
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
614
|
-
addIssueToContext(ctx, {
|
|
615
|
-
validation: "cuid2",
|
|
616
|
-
code: ZodIssueCode.invalid_string,
|
|
617
|
-
message: check.message,
|
|
618
|
-
});
|
|
619
|
-
status.dirty();
|
|
620
|
-
}
|
|
621
|
-
}
|
|
622
|
-
else if (check.kind === "ulid") {
|
|
623
|
-
if (!ulidRegex.test(input.data)) {
|
|
624
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
625
|
-
addIssueToContext(ctx, {
|
|
626
|
-
validation: "ulid",
|
|
627
|
-
code: ZodIssueCode.invalid_string,
|
|
628
|
-
message: check.message,
|
|
629
|
-
});
|
|
630
|
-
status.dirty();
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
else if (check.kind === "url") {
|
|
634
|
-
try {
|
|
635
|
-
new URL(input.data);
|
|
636
|
-
}
|
|
637
|
-
catch {
|
|
638
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
639
|
-
addIssueToContext(ctx, {
|
|
640
|
-
validation: "url",
|
|
641
|
-
code: ZodIssueCode.invalid_string,
|
|
642
|
-
message: check.message,
|
|
643
|
-
});
|
|
644
|
-
status.dirty();
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
else if (check.kind === "regex") {
|
|
648
|
-
check.regex.lastIndex = 0;
|
|
649
|
-
const testResult = check.regex.test(input.data);
|
|
650
|
-
if (!testResult) {
|
|
651
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
652
|
-
addIssueToContext(ctx, {
|
|
653
|
-
validation: "regex",
|
|
654
|
-
code: ZodIssueCode.invalid_string,
|
|
655
|
-
message: check.message,
|
|
656
|
-
});
|
|
657
|
-
status.dirty();
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
else if (check.kind === "trim") {
|
|
661
|
-
input.data = input.data.trim();
|
|
662
|
-
}
|
|
663
|
-
else if (check.kind === "includes") {
|
|
664
|
-
if (!input.data.includes(check.value, check.position)) {
|
|
665
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
666
|
-
addIssueToContext(ctx, {
|
|
667
|
-
code: ZodIssueCode.invalid_string,
|
|
668
|
-
validation: { includes: check.value, position: check.position },
|
|
669
|
-
message: check.message,
|
|
670
|
-
});
|
|
671
|
-
status.dirty();
|
|
672
|
-
}
|
|
673
|
-
}
|
|
674
|
-
else if (check.kind === "toLowerCase") {
|
|
675
|
-
input.data = input.data.toLowerCase();
|
|
676
|
-
}
|
|
677
|
-
else if (check.kind === "toUpperCase") {
|
|
678
|
-
input.data = input.data.toUpperCase();
|
|
679
|
-
}
|
|
680
|
-
else if (check.kind === "startsWith") {
|
|
681
|
-
if (!input.data.startsWith(check.value)) {
|
|
682
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
683
|
-
addIssueToContext(ctx, {
|
|
684
|
-
code: ZodIssueCode.invalid_string,
|
|
685
|
-
validation: { startsWith: check.value },
|
|
686
|
-
message: check.message,
|
|
687
|
-
});
|
|
688
|
-
status.dirty();
|
|
689
|
-
}
|
|
690
|
-
}
|
|
691
|
-
else if (check.kind === "endsWith") {
|
|
692
|
-
if (!input.data.endsWith(check.value)) {
|
|
693
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
694
|
-
addIssueToContext(ctx, {
|
|
695
|
-
code: ZodIssueCode.invalid_string,
|
|
696
|
-
validation: { endsWith: check.value },
|
|
697
|
-
message: check.message,
|
|
698
|
-
});
|
|
699
|
-
status.dirty();
|
|
700
|
-
}
|
|
701
|
-
}
|
|
702
|
-
else if (check.kind === "datetime") {
|
|
703
|
-
const regex = datetimeRegex(check);
|
|
704
|
-
if (!regex.test(input.data)) {
|
|
705
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
706
|
-
addIssueToContext(ctx, {
|
|
707
|
-
code: ZodIssueCode.invalid_string,
|
|
708
|
-
validation: "datetime",
|
|
709
|
-
message: check.message,
|
|
710
|
-
});
|
|
711
|
-
status.dirty();
|
|
712
|
-
}
|
|
713
|
-
}
|
|
714
|
-
else if (check.kind === "date") {
|
|
715
|
-
const regex = dateRegex;
|
|
716
|
-
if (!regex.test(input.data)) {
|
|
717
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
718
|
-
addIssueToContext(ctx, {
|
|
719
|
-
code: ZodIssueCode.invalid_string,
|
|
720
|
-
validation: "date",
|
|
721
|
-
message: check.message,
|
|
722
|
-
});
|
|
723
|
-
status.dirty();
|
|
724
|
-
}
|
|
725
|
-
}
|
|
726
|
-
else if (check.kind === "time") {
|
|
727
|
-
const regex = timeRegex(check);
|
|
728
|
-
if (!regex.test(input.data)) {
|
|
729
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
730
|
-
addIssueToContext(ctx, {
|
|
731
|
-
code: ZodIssueCode.invalid_string,
|
|
732
|
-
validation: "time",
|
|
733
|
-
message: check.message,
|
|
734
|
-
});
|
|
735
|
-
status.dirty();
|
|
736
|
-
}
|
|
737
|
-
}
|
|
738
|
-
else if (check.kind === "duration") {
|
|
739
|
-
if (!durationRegex.test(input.data)) {
|
|
740
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
741
|
-
addIssueToContext(ctx, {
|
|
742
|
-
validation: "duration",
|
|
743
|
-
code: ZodIssueCode.invalid_string,
|
|
744
|
-
message: check.message,
|
|
745
|
-
});
|
|
746
|
-
status.dirty();
|
|
747
|
-
}
|
|
748
|
-
}
|
|
749
|
-
else if (check.kind === "ip") {
|
|
750
|
-
if (!isValidIP(input.data, check.version)) {
|
|
751
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
752
|
-
addIssueToContext(ctx, {
|
|
753
|
-
validation: "ip",
|
|
754
|
-
code: ZodIssueCode.invalid_string,
|
|
755
|
-
message: check.message,
|
|
756
|
-
});
|
|
757
|
-
status.dirty();
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
else if (check.kind === "jwt") {
|
|
761
|
-
if (!isValidJWT(input.data, check.alg)) {
|
|
762
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
763
|
-
addIssueToContext(ctx, {
|
|
764
|
-
validation: "jwt",
|
|
765
|
-
code: ZodIssueCode.invalid_string,
|
|
766
|
-
message: check.message,
|
|
767
|
-
});
|
|
768
|
-
status.dirty();
|
|
769
|
-
}
|
|
770
|
-
}
|
|
771
|
-
else if (check.kind === "cidr") {
|
|
772
|
-
if (!isValidCidr(input.data, check.version)) {
|
|
773
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
774
|
-
addIssueToContext(ctx, {
|
|
775
|
-
validation: "cidr",
|
|
776
|
-
code: ZodIssueCode.invalid_string,
|
|
777
|
-
message: check.message,
|
|
778
|
-
});
|
|
779
|
-
status.dirty();
|
|
780
|
-
}
|
|
781
|
-
}
|
|
782
|
-
else if (check.kind === "base64") {
|
|
783
|
-
if (!base64Regex.test(input.data)) {
|
|
784
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
785
|
-
addIssueToContext(ctx, {
|
|
786
|
-
validation: "base64",
|
|
787
|
-
code: ZodIssueCode.invalid_string,
|
|
788
|
-
message: check.message,
|
|
789
|
-
});
|
|
790
|
-
status.dirty();
|
|
791
|
-
}
|
|
792
|
-
}
|
|
793
|
-
else if (check.kind === "base64url") {
|
|
794
|
-
if (!base64urlRegex.test(input.data)) {
|
|
795
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
796
|
-
addIssueToContext(ctx, {
|
|
797
|
-
validation: "base64url",
|
|
798
|
-
code: ZodIssueCode.invalid_string,
|
|
799
|
-
message: check.message,
|
|
800
|
-
});
|
|
801
|
-
status.dirty();
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
else {
|
|
805
|
-
util.assertNever(check);
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
return { status: status.value, value: input.data };
|
|
809
|
-
}
|
|
810
|
-
_regex(regex, validation, message) {
|
|
811
|
-
return this.refinement((data) => regex.test(data), {
|
|
812
|
-
validation,
|
|
813
|
-
code: ZodIssueCode.invalid_string,
|
|
814
|
-
...errorUtil.errToObj(message),
|
|
815
|
-
});
|
|
816
|
-
}
|
|
817
|
-
_addCheck(check) {
|
|
818
|
-
return new ZodString({
|
|
819
|
-
...this._def,
|
|
820
|
-
checks: [...this._def.checks, check],
|
|
821
|
-
});
|
|
822
|
-
}
|
|
823
|
-
email(message) {
|
|
824
|
-
return this._addCheck({ kind: "email", ...errorUtil.errToObj(message) });
|
|
825
|
-
}
|
|
826
|
-
url(message) {
|
|
827
|
-
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
|
|
828
|
-
}
|
|
829
|
-
emoji(message) {
|
|
830
|
-
return this._addCheck({ kind: "emoji", ...errorUtil.errToObj(message) });
|
|
831
|
-
}
|
|
832
|
-
uuid(message) {
|
|
833
|
-
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
|
|
834
|
-
}
|
|
835
|
-
nanoid(message) {
|
|
836
|
-
return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) });
|
|
837
|
-
}
|
|
838
|
-
cuid(message) {
|
|
839
|
-
return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
|
|
840
|
-
}
|
|
841
|
-
cuid2(message) {
|
|
842
|
-
return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
|
|
843
|
-
}
|
|
844
|
-
ulid(message) {
|
|
845
|
-
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
|
|
846
|
-
}
|
|
847
|
-
base64(message) {
|
|
848
|
-
return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
|
|
849
|
-
}
|
|
850
|
-
base64url(message) {
|
|
851
|
-
// base64url encoding is a modification of base64 that can safely be used in URLs and filenames
|
|
852
|
-
return this._addCheck({
|
|
853
|
-
kind: "base64url",
|
|
854
|
-
...errorUtil.errToObj(message),
|
|
855
|
-
});
|
|
856
|
-
}
|
|
857
|
-
jwt(options) {
|
|
858
|
-
return this._addCheck({ kind: "jwt", ...errorUtil.errToObj(options) });
|
|
859
|
-
}
|
|
860
|
-
ip(options) {
|
|
861
|
-
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
|
|
862
|
-
}
|
|
863
|
-
cidr(options) {
|
|
864
|
-
return this._addCheck({ kind: "cidr", ...errorUtil.errToObj(options) });
|
|
865
|
-
}
|
|
866
|
-
datetime(options) {
|
|
867
|
-
if (typeof options === "string") {
|
|
868
|
-
return this._addCheck({
|
|
869
|
-
kind: "datetime",
|
|
870
|
-
precision: null,
|
|
871
|
-
offset: false,
|
|
872
|
-
local: false,
|
|
873
|
-
message: options,
|
|
874
|
-
});
|
|
875
|
-
}
|
|
876
|
-
return this._addCheck({
|
|
877
|
-
kind: "datetime",
|
|
878
|
-
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
879
|
-
offset: options?.offset ?? false,
|
|
880
|
-
local: options?.local ?? false,
|
|
881
|
-
...errorUtil.errToObj(options?.message),
|
|
882
|
-
});
|
|
883
|
-
}
|
|
884
|
-
date(message) {
|
|
885
|
-
return this._addCheck({ kind: "date", message });
|
|
886
|
-
}
|
|
887
|
-
time(options) {
|
|
888
|
-
if (typeof options === "string") {
|
|
889
|
-
return this._addCheck({
|
|
890
|
-
kind: "time",
|
|
891
|
-
precision: null,
|
|
892
|
-
message: options,
|
|
893
|
-
});
|
|
894
|
-
}
|
|
895
|
-
return this._addCheck({
|
|
896
|
-
kind: "time",
|
|
897
|
-
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
898
|
-
...errorUtil.errToObj(options?.message),
|
|
899
|
-
});
|
|
900
|
-
}
|
|
901
|
-
duration(message) {
|
|
902
|
-
return this._addCheck({ kind: "duration", ...errorUtil.errToObj(message) });
|
|
903
|
-
}
|
|
904
|
-
regex(regex, message) {
|
|
905
|
-
return this._addCheck({
|
|
906
|
-
kind: "regex",
|
|
907
|
-
regex: regex,
|
|
908
|
-
...errorUtil.errToObj(message),
|
|
909
|
-
});
|
|
910
|
-
}
|
|
911
|
-
includes(value, options) {
|
|
912
|
-
return this._addCheck({
|
|
913
|
-
kind: "includes",
|
|
914
|
-
value: value,
|
|
915
|
-
position: options?.position,
|
|
916
|
-
...errorUtil.errToObj(options?.message),
|
|
917
|
-
});
|
|
918
|
-
}
|
|
919
|
-
startsWith(value, message) {
|
|
920
|
-
return this._addCheck({
|
|
921
|
-
kind: "startsWith",
|
|
922
|
-
value: value,
|
|
923
|
-
...errorUtil.errToObj(message),
|
|
924
|
-
});
|
|
925
|
-
}
|
|
926
|
-
endsWith(value, message) {
|
|
927
|
-
return this._addCheck({
|
|
928
|
-
kind: "endsWith",
|
|
929
|
-
value: value,
|
|
930
|
-
...errorUtil.errToObj(message),
|
|
931
|
-
});
|
|
932
|
-
}
|
|
933
|
-
min(minLength, message) {
|
|
934
|
-
return this._addCheck({
|
|
935
|
-
kind: "min",
|
|
936
|
-
value: minLength,
|
|
937
|
-
...errorUtil.errToObj(message),
|
|
938
|
-
});
|
|
939
|
-
}
|
|
940
|
-
max(maxLength, message) {
|
|
941
|
-
return this._addCheck({
|
|
942
|
-
kind: "max",
|
|
943
|
-
value: maxLength,
|
|
944
|
-
...errorUtil.errToObj(message),
|
|
945
|
-
});
|
|
946
|
-
}
|
|
947
|
-
length(len, message) {
|
|
948
|
-
return this._addCheck({
|
|
949
|
-
kind: "length",
|
|
950
|
-
value: len,
|
|
951
|
-
...errorUtil.errToObj(message),
|
|
952
|
-
});
|
|
953
|
-
}
|
|
954
|
-
/**
|
|
955
|
-
* Equivalent to `.min(1)`
|
|
956
|
-
*/
|
|
957
|
-
nonempty(message) {
|
|
958
|
-
return this.min(1, errorUtil.errToObj(message));
|
|
959
|
-
}
|
|
960
|
-
trim() {
|
|
961
|
-
return new ZodString({
|
|
962
|
-
...this._def,
|
|
963
|
-
checks: [...this._def.checks, { kind: "trim" }],
|
|
964
|
-
});
|
|
965
|
-
}
|
|
966
|
-
toLowerCase() {
|
|
967
|
-
return new ZodString({
|
|
968
|
-
...this._def,
|
|
969
|
-
checks: [...this._def.checks, { kind: "toLowerCase" }],
|
|
970
|
-
});
|
|
971
|
-
}
|
|
972
|
-
toUpperCase() {
|
|
973
|
-
return new ZodString({
|
|
974
|
-
...this._def,
|
|
975
|
-
checks: [...this._def.checks, { kind: "toUpperCase" }],
|
|
976
|
-
});
|
|
977
|
-
}
|
|
978
|
-
get isDatetime() {
|
|
979
|
-
return !!this._def.checks.find((ch) => ch.kind === "datetime");
|
|
980
|
-
}
|
|
981
|
-
get isDate() {
|
|
982
|
-
return !!this._def.checks.find((ch) => ch.kind === "date");
|
|
983
|
-
}
|
|
984
|
-
get isTime() {
|
|
985
|
-
return !!this._def.checks.find((ch) => ch.kind === "time");
|
|
986
|
-
}
|
|
987
|
-
get isDuration() {
|
|
988
|
-
return !!this._def.checks.find((ch) => ch.kind === "duration");
|
|
989
|
-
}
|
|
990
|
-
get isEmail() {
|
|
991
|
-
return !!this._def.checks.find((ch) => ch.kind === "email");
|
|
992
|
-
}
|
|
993
|
-
get isURL() {
|
|
994
|
-
return !!this._def.checks.find((ch) => ch.kind === "url");
|
|
995
|
-
}
|
|
996
|
-
get isEmoji() {
|
|
997
|
-
return !!this._def.checks.find((ch) => ch.kind === "emoji");
|
|
998
|
-
}
|
|
999
|
-
get isUUID() {
|
|
1000
|
-
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
1001
|
-
}
|
|
1002
|
-
get isNANOID() {
|
|
1003
|
-
return !!this._def.checks.find((ch) => ch.kind === "nanoid");
|
|
1004
|
-
}
|
|
1005
|
-
get isCUID() {
|
|
1006
|
-
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
1007
|
-
}
|
|
1008
|
-
get isCUID2() {
|
|
1009
|
-
return !!this._def.checks.find((ch) => ch.kind === "cuid2");
|
|
1010
|
-
}
|
|
1011
|
-
get isULID() {
|
|
1012
|
-
return !!this._def.checks.find((ch) => ch.kind === "ulid");
|
|
1013
|
-
}
|
|
1014
|
-
get isIP() {
|
|
1015
|
-
return !!this._def.checks.find((ch) => ch.kind === "ip");
|
|
1016
|
-
}
|
|
1017
|
-
get isCIDR() {
|
|
1018
|
-
return !!this._def.checks.find((ch) => ch.kind === "cidr");
|
|
1019
|
-
}
|
|
1020
|
-
get isBase64() {
|
|
1021
|
-
return !!this._def.checks.find((ch) => ch.kind === "base64");
|
|
1022
|
-
}
|
|
1023
|
-
get isBase64url() {
|
|
1024
|
-
// base64url encoding is a modification of base64 that can safely be used in URLs and filenames
|
|
1025
|
-
return !!this._def.checks.find((ch) => ch.kind === "base64url");
|
|
1026
|
-
}
|
|
1027
|
-
get minLength() {
|
|
1028
|
-
let min = null;
|
|
1029
|
-
for (const ch of this._def.checks) {
|
|
1030
|
-
if (ch.kind === "min") {
|
|
1031
|
-
if (min === null || ch.value > min)
|
|
1032
|
-
min = ch.value;
|
|
1033
|
-
}
|
|
1034
|
-
}
|
|
1035
|
-
return min;
|
|
1036
|
-
}
|
|
1037
|
-
get maxLength() {
|
|
1038
|
-
let max = null;
|
|
1039
|
-
for (const ch of this._def.checks) {
|
|
1040
|
-
if (ch.kind === "max") {
|
|
1041
|
-
if (max === null || ch.value < max)
|
|
1042
|
-
max = ch.value;
|
|
1043
|
-
}
|
|
1044
|
-
}
|
|
1045
|
-
return max;
|
|
1046
|
-
}
|
|
1047
|
-
}
|
|
1048
|
-
ZodString.create = (params) => {
|
|
1049
|
-
return new ZodString({
|
|
1050
|
-
checks: [],
|
|
1051
|
-
typeName: ZodFirstPartyTypeKind.ZodString,
|
|
1052
|
-
coerce: params?.coerce ?? false,
|
|
1053
|
-
...processCreateParams(params),
|
|
1054
|
-
});
|
|
1055
|
-
};
|
|
1056
|
-
// https://stackoverflow.com/questions/3966484/why-does-modulus-operator-return-fractional-number-in-javascript/31711034#31711034
|
|
1057
|
-
function floatSafeRemainder(val, step) {
|
|
1058
|
-
const valDecCount = (val.toString().split(".")[1] || "").length;
|
|
1059
|
-
const stepDecCount = (step.toString().split(".")[1] || "").length;
|
|
1060
|
-
const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
|
|
1061
|
-
const valInt = Number.parseInt(val.toFixed(decCount).replace(".", ""));
|
|
1062
|
-
const stepInt = Number.parseInt(step.toFixed(decCount).replace(".", ""));
|
|
1063
|
-
return (valInt % stepInt) / 10 ** decCount;
|
|
1064
|
-
}
|
|
1065
|
-
class ZodNumber extends ZodType {
|
|
1066
|
-
constructor() {
|
|
1067
|
-
super(...arguments);
|
|
1068
|
-
this.min = this.gte;
|
|
1069
|
-
this.max = this.lte;
|
|
1070
|
-
this.step = this.multipleOf;
|
|
1071
|
-
}
|
|
1072
|
-
_parse(input) {
|
|
1073
|
-
if (this._def.coerce) {
|
|
1074
|
-
input.data = Number(input.data);
|
|
1075
|
-
}
|
|
1076
|
-
const parsedType = this._getType(input);
|
|
1077
|
-
if (parsedType !== ZodParsedType.number) {
|
|
1078
|
-
const ctx = this._getOrReturnCtx(input);
|
|
1079
|
-
addIssueToContext(ctx, {
|
|
1080
|
-
code: ZodIssueCode.invalid_type,
|
|
1081
|
-
expected: ZodParsedType.number,
|
|
1082
|
-
received: ctx.parsedType,
|
|
1083
|
-
});
|
|
1084
|
-
return INVALID;
|
|
1085
|
-
}
|
|
1086
|
-
let ctx = undefined;
|
|
1087
|
-
const status = new ParseStatus();
|
|
1088
|
-
for (const check of this._def.checks) {
|
|
1089
|
-
if (check.kind === "int") {
|
|
1090
|
-
if (!util.isInteger(input.data)) {
|
|
1091
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1092
|
-
addIssueToContext(ctx, {
|
|
1093
|
-
code: ZodIssueCode.invalid_type,
|
|
1094
|
-
expected: "integer",
|
|
1095
|
-
received: "float",
|
|
1096
|
-
message: check.message,
|
|
1097
|
-
});
|
|
1098
|
-
status.dirty();
|
|
1099
|
-
}
|
|
1100
|
-
}
|
|
1101
|
-
else if (check.kind === "min") {
|
|
1102
|
-
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
1103
|
-
if (tooSmall) {
|
|
1104
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1105
|
-
addIssueToContext(ctx, {
|
|
1106
|
-
code: ZodIssueCode.too_small,
|
|
1107
|
-
minimum: check.value,
|
|
1108
|
-
type: "number",
|
|
1109
|
-
inclusive: check.inclusive,
|
|
1110
|
-
exact: false,
|
|
1111
|
-
message: check.message,
|
|
1112
|
-
});
|
|
1113
|
-
status.dirty();
|
|
1114
|
-
}
|
|
1115
|
-
}
|
|
1116
|
-
else if (check.kind === "max") {
|
|
1117
|
-
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
1118
|
-
if (tooBig) {
|
|
1119
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1120
|
-
addIssueToContext(ctx, {
|
|
1121
|
-
code: ZodIssueCode.too_big,
|
|
1122
|
-
maximum: check.value,
|
|
1123
|
-
type: "number",
|
|
1124
|
-
inclusive: check.inclusive,
|
|
1125
|
-
exact: false,
|
|
1126
|
-
message: check.message,
|
|
1127
|
-
});
|
|
1128
|
-
status.dirty();
|
|
1129
|
-
}
|
|
1130
|
-
}
|
|
1131
|
-
else if (check.kind === "multipleOf") {
|
|
1132
|
-
if (floatSafeRemainder(input.data, check.value) !== 0) {
|
|
1133
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1134
|
-
addIssueToContext(ctx, {
|
|
1135
|
-
code: ZodIssueCode.not_multiple_of,
|
|
1136
|
-
multipleOf: check.value,
|
|
1137
|
-
message: check.message,
|
|
1138
|
-
});
|
|
1139
|
-
status.dirty();
|
|
1140
|
-
}
|
|
1141
|
-
}
|
|
1142
|
-
else if (check.kind === "finite") {
|
|
1143
|
-
if (!Number.isFinite(input.data)) {
|
|
1144
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1145
|
-
addIssueToContext(ctx, {
|
|
1146
|
-
code: ZodIssueCode.not_finite,
|
|
1147
|
-
message: check.message,
|
|
1148
|
-
});
|
|
1149
|
-
status.dirty();
|
|
1150
|
-
}
|
|
1151
|
-
}
|
|
1152
|
-
else {
|
|
1153
|
-
util.assertNever(check);
|
|
1154
|
-
}
|
|
1155
|
-
}
|
|
1156
|
-
return { status: status.value, value: input.data };
|
|
1157
|
-
}
|
|
1158
|
-
gte(value, message) {
|
|
1159
|
-
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
1160
|
-
}
|
|
1161
|
-
gt(value, message) {
|
|
1162
|
-
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
1163
|
-
}
|
|
1164
|
-
lte(value, message) {
|
|
1165
|
-
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
1166
|
-
}
|
|
1167
|
-
lt(value, message) {
|
|
1168
|
-
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
1169
|
-
}
|
|
1170
|
-
setLimit(kind, value, inclusive, message) {
|
|
1171
|
-
return new ZodNumber({
|
|
1172
|
-
...this._def,
|
|
1173
|
-
checks: [
|
|
1174
|
-
...this._def.checks,
|
|
1175
|
-
{
|
|
1176
|
-
kind,
|
|
1177
|
-
value,
|
|
1178
|
-
inclusive,
|
|
1179
|
-
message: errorUtil.toString(message),
|
|
1180
|
-
},
|
|
1181
|
-
],
|
|
1182
|
-
});
|
|
1183
|
-
}
|
|
1184
|
-
_addCheck(check) {
|
|
1185
|
-
return new ZodNumber({
|
|
1186
|
-
...this._def,
|
|
1187
|
-
checks: [...this._def.checks, check],
|
|
1188
|
-
});
|
|
1189
|
-
}
|
|
1190
|
-
int(message) {
|
|
1191
|
-
return this._addCheck({
|
|
1192
|
-
kind: "int",
|
|
1193
|
-
message: errorUtil.toString(message),
|
|
1194
|
-
});
|
|
1195
|
-
}
|
|
1196
|
-
positive(message) {
|
|
1197
|
-
return this._addCheck({
|
|
1198
|
-
kind: "min",
|
|
1199
|
-
value: 0,
|
|
1200
|
-
inclusive: false,
|
|
1201
|
-
message: errorUtil.toString(message),
|
|
1202
|
-
});
|
|
1203
|
-
}
|
|
1204
|
-
negative(message) {
|
|
1205
|
-
return this._addCheck({
|
|
1206
|
-
kind: "max",
|
|
1207
|
-
value: 0,
|
|
1208
|
-
inclusive: false,
|
|
1209
|
-
message: errorUtil.toString(message),
|
|
1210
|
-
});
|
|
1211
|
-
}
|
|
1212
|
-
nonpositive(message) {
|
|
1213
|
-
return this._addCheck({
|
|
1214
|
-
kind: "max",
|
|
1215
|
-
value: 0,
|
|
1216
|
-
inclusive: true,
|
|
1217
|
-
message: errorUtil.toString(message),
|
|
1218
|
-
});
|
|
1219
|
-
}
|
|
1220
|
-
nonnegative(message) {
|
|
1221
|
-
return this._addCheck({
|
|
1222
|
-
kind: "min",
|
|
1223
|
-
value: 0,
|
|
1224
|
-
inclusive: true,
|
|
1225
|
-
message: errorUtil.toString(message),
|
|
1226
|
-
});
|
|
1227
|
-
}
|
|
1228
|
-
multipleOf(value, message) {
|
|
1229
|
-
return this._addCheck({
|
|
1230
|
-
kind: "multipleOf",
|
|
1231
|
-
value: value,
|
|
1232
|
-
message: errorUtil.toString(message),
|
|
1233
|
-
});
|
|
1234
|
-
}
|
|
1235
|
-
finite(message) {
|
|
1236
|
-
return this._addCheck({
|
|
1237
|
-
kind: "finite",
|
|
1238
|
-
message: errorUtil.toString(message),
|
|
1239
|
-
});
|
|
1240
|
-
}
|
|
1241
|
-
safe(message) {
|
|
1242
|
-
return this._addCheck({
|
|
1243
|
-
kind: "min",
|
|
1244
|
-
inclusive: true,
|
|
1245
|
-
value: Number.MIN_SAFE_INTEGER,
|
|
1246
|
-
message: errorUtil.toString(message),
|
|
1247
|
-
})._addCheck({
|
|
1248
|
-
kind: "max",
|
|
1249
|
-
inclusive: true,
|
|
1250
|
-
value: Number.MAX_SAFE_INTEGER,
|
|
1251
|
-
message: errorUtil.toString(message),
|
|
1252
|
-
});
|
|
1253
|
-
}
|
|
1254
|
-
get minValue() {
|
|
1255
|
-
let min = null;
|
|
1256
|
-
for (const ch of this._def.checks) {
|
|
1257
|
-
if (ch.kind === "min") {
|
|
1258
|
-
if (min === null || ch.value > min)
|
|
1259
|
-
min = ch.value;
|
|
1260
|
-
}
|
|
1261
|
-
}
|
|
1262
|
-
return min;
|
|
1263
|
-
}
|
|
1264
|
-
get maxValue() {
|
|
1265
|
-
let max = null;
|
|
1266
|
-
for (const ch of this._def.checks) {
|
|
1267
|
-
if (ch.kind === "max") {
|
|
1268
|
-
if (max === null || ch.value < max)
|
|
1269
|
-
max = ch.value;
|
|
1270
|
-
}
|
|
1271
|
-
}
|
|
1272
|
-
return max;
|
|
1273
|
-
}
|
|
1274
|
-
get isInt() {
|
|
1275
|
-
return !!this._def.checks.find((ch) => ch.kind === "int" || (ch.kind === "multipleOf" && util.isInteger(ch.value)));
|
|
1276
|
-
}
|
|
1277
|
-
get isFinite() {
|
|
1278
|
-
let max = null;
|
|
1279
|
-
let min = null;
|
|
1280
|
-
for (const ch of this._def.checks) {
|
|
1281
|
-
if (ch.kind === "finite" || ch.kind === "int" || ch.kind === "multipleOf") {
|
|
1282
|
-
return true;
|
|
1283
|
-
}
|
|
1284
|
-
else if (ch.kind === "min") {
|
|
1285
|
-
if (min === null || ch.value > min)
|
|
1286
|
-
min = ch.value;
|
|
1287
|
-
}
|
|
1288
|
-
else if (ch.kind === "max") {
|
|
1289
|
-
if (max === null || ch.value < max)
|
|
1290
|
-
max = ch.value;
|
|
1291
|
-
}
|
|
1292
|
-
}
|
|
1293
|
-
return Number.isFinite(min) && Number.isFinite(max);
|
|
1294
|
-
}
|
|
1295
|
-
}
|
|
1296
|
-
ZodNumber.create = (params) => {
|
|
1297
|
-
return new ZodNumber({
|
|
1298
|
-
checks: [],
|
|
1299
|
-
typeName: ZodFirstPartyTypeKind.ZodNumber,
|
|
1300
|
-
coerce: params?.coerce || false,
|
|
1301
|
-
...processCreateParams(params),
|
|
1302
|
-
});
|
|
1303
|
-
};
|
|
1304
|
-
class ZodBigInt extends ZodType {
|
|
1305
|
-
constructor() {
|
|
1306
|
-
super(...arguments);
|
|
1307
|
-
this.min = this.gte;
|
|
1308
|
-
this.max = this.lte;
|
|
1309
|
-
}
|
|
1310
|
-
_parse(input) {
|
|
1311
|
-
if (this._def.coerce) {
|
|
1312
|
-
try {
|
|
1313
|
-
input.data = BigInt(input.data);
|
|
1314
|
-
}
|
|
1315
|
-
catch {
|
|
1316
|
-
return this._getInvalidInput(input);
|
|
1317
|
-
}
|
|
1318
|
-
}
|
|
1319
|
-
const parsedType = this._getType(input);
|
|
1320
|
-
if (parsedType !== ZodParsedType.bigint) {
|
|
1321
|
-
return this._getInvalidInput(input);
|
|
1322
|
-
}
|
|
1323
|
-
let ctx = undefined;
|
|
1324
|
-
const status = new ParseStatus();
|
|
1325
|
-
for (const check of this._def.checks) {
|
|
1326
|
-
if (check.kind === "min") {
|
|
1327
|
-
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
1328
|
-
if (tooSmall) {
|
|
1329
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1330
|
-
addIssueToContext(ctx, {
|
|
1331
|
-
code: ZodIssueCode.too_small,
|
|
1332
|
-
type: "bigint",
|
|
1333
|
-
minimum: check.value,
|
|
1334
|
-
inclusive: check.inclusive,
|
|
1335
|
-
message: check.message,
|
|
1336
|
-
});
|
|
1337
|
-
status.dirty();
|
|
1338
|
-
}
|
|
1339
|
-
}
|
|
1340
|
-
else if (check.kind === "max") {
|
|
1341
|
-
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
1342
|
-
if (tooBig) {
|
|
1343
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1344
|
-
addIssueToContext(ctx, {
|
|
1345
|
-
code: ZodIssueCode.too_big,
|
|
1346
|
-
type: "bigint",
|
|
1347
|
-
maximum: check.value,
|
|
1348
|
-
inclusive: check.inclusive,
|
|
1349
|
-
message: check.message,
|
|
1350
|
-
});
|
|
1351
|
-
status.dirty();
|
|
1352
|
-
}
|
|
1353
|
-
}
|
|
1354
|
-
else if (check.kind === "multipleOf") {
|
|
1355
|
-
if (input.data % check.value !== BigInt(0)) {
|
|
1356
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1357
|
-
addIssueToContext(ctx, {
|
|
1358
|
-
code: ZodIssueCode.not_multiple_of,
|
|
1359
|
-
multipleOf: check.value,
|
|
1360
|
-
message: check.message,
|
|
1361
|
-
});
|
|
1362
|
-
status.dirty();
|
|
1363
|
-
}
|
|
1364
|
-
}
|
|
1365
|
-
else {
|
|
1366
|
-
util.assertNever(check);
|
|
1367
|
-
}
|
|
1368
|
-
}
|
|
1369
|
-
return { status: status.value, value: input.data };
|
|
1370
|
-
}
|
|
1371
|
-
_getInvalidInput(input) {
|
|
1372
|
-
const ctx = this._getOrReturnCtx(input);
|
|
1373
|
-
addIssueToContext(ctx, {
|
|
1374
|
-
code: ZodIssueCode.invalid_type,
|
|
1375
|
-
expected: ZodParsedType.bigint,
|
|
1376
|
-
received: ctx.parsedType,
|
|
1377
|
-
});
|
|
1378
|
-
return INVALID;
|
|
1379
|
-
}
|
|
1380
|
-
gte(value, message) {
|
|
1381
|
-
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
1382
|
-
}
|
|
1383
|
-
gt(value, message) {
|
|
1384
|
-
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
1385
|
-
}
|
|
1386
|
-
lte(value, message) {
|
|
1387
|
-
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
1388
|
-
}
|
|
1389
|
-
lt(value, message) {
|
|
1390
|
-
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
1391
|
-
}
|
|
1392
|
-
setLimit(kind, value, inclusive, message) {
|
|
1393
|
-
return new ZodBigInt({
|
|
1394
|
-
...this._def,
|
|
1395
|
-
checks: [
|
|
1396
|
-
...this._def.checks,
|
|
1397
|
-
{
|
|
1398
|
-
kind,
|
|
1399
|
-
value,
|
|
1400
|
-
inclusive,
|
|
1401
|
-
message: errorUtil.toString(message),
|
|
1402
|
-
},
|
|
1403
|
-
],
|
|
1404
|
-
});
|
|
1405
|
-
}
|
|
1406
|
-
_addCheck(check) {
|
|
1407
|
-
return new ZodBigInt({
|
|
1408
|
-
...this._def,
|
|
1409
|
-
checks: [...this._def.checks, check],
|
|
1410
|
-
});
|
|
1411
|
-
}
|
|
1412
|
-
positive(message) {
|
|
1413
|
-
return this._addCheck({
|
|
1414
|
-
kind: "min",
|
|
1415
|
-
value: BigInt(0),
|
|
1416
|
-
inclusive: false,
|
|
1417
|
-
message: errorUtil.toString(message),
|
|
1418
|
-
});
|
|
1419
|
-
}
|
|
1420
|
-
negative(message) {
|
|
1421
|
-
return this._addCheck({
|
|
1422
|
-
kind: "max",
|
|
1423
|
-
value: BigInt(0),
|
|
1424
|
-
inclusive: false,
|
|
1425
|
-
message: errorUtil.toString(message),
|
|
1426
|
-
});
|
|
1427
|
-
}
|
|
1428
|
-
nonpositive(message) {
|
|
1429
|
-
return this._addCheck({
|
|
1430
|
-
kind: "max",
|
|
1431
|
-
value: BigInt(0),
|
|
1432
|
-
inclusive: true,
|
|
1433
|
-
message: errorUtil.toString(message),
|
|
1434
|
-
});
|
|
1435
|
-
}
|
|
1436
|
-
nonnegative(message) {
|
|
1437
|
-
return this._addCheck({
|
|
1438
|
-
kind: "min",
|
|
1439
|
-
value: BigInt(0),
|
|
1440
|
-
inclusive: true,
|
|
1441
|
-
message: errorUtil.toString(message),
|
|
1442
|
-
});
|
|
1443
|
-
}
|
|
1444
|
-
multipleOf(value, message) {
|
|
1445
|
-
return this._addCheck({
|
|
1446
|
-
kind: "multipleOf",
|
|
1447
|
-
value,
|
|
1448
|
-
message: errorUtil.toString(message),
|
|
1449
|
-
});
|
|
1450
|
-
}
|
|
1451
|
-
get minValue() {
|
|
1452
|
-
let min = null;
|
|
1453
|
-
for (const ch of this._def.checks) {
|
|
1454
|
-
if (ch.kind === "min") {
|
|
1455
|
-
if (min === null || ch.value > min)
|
|
1456
|
-
min = ch.value;
|
|
1457
|
-
}
|
|
1458
|
-
}
|
|
1459
|
-
return min;
|
|
1460
|
-
}
|
|
1461
|
-
get maxValue() {
|
|
1462
|
-
let max = null;
|
|
1463
|
-
for (const ch of this._def.checks) {
|
|
1464
|
-
if (ch.kind === "max") {
|
|
1465
|
-
if (max === null || ch.value < max)
|
|
1466
|
-
max = ch.value;
|
|
1467
|
-
}
|
|
1468
|
-
}
|
|
1469
|
-
return max;
|
|
1470
|
-
}
|
|
1471
|
-
}
|
|
1472
|
-
ZodBigInt.create = (params) => {
|
|
1473
|
-
return new ZodBigInt({
|
|
1474
|
-
checks: [],
|
|
1475
|
-
typeName: ZodFirstPartyTypeKind.ZodBigInt,
|
|
1476
|
-
coerce: params?.coerce ?? false,
|
|
1477
|
-
...processCreateParams(params),
|
|
1478
|
-
});
|
|
1479
|
-
};
|
|
1480
|
-
class ZodBoolean extends ZodType {
|
|
1481
|
-
_parse(input) {
|
|
1482
|
-
if (this._def.coerce) {
|
|
1483
|
-
input.data = Boolean(input.data);
|
|
1484
|
-
}
|
|
1485
|
-
const parsedType = this._getType(input);
|
|
1486
|
-
if (parsedType !== ZodParsedType.boolean) {
|
|
1487
|
-
const ctx = this._getOrReturnCtx(input);
|
|
1488
|
-
addIssueToContext(ctx, {
|
|
1489
|
-
code: ZodIssueCode.invalid_type,
|
|
1490
|
-
expected: ZodParsedType.boolean,
|
|
1491
|
-
received: ctx.parsedType,
|
|
1492
|
-
});
|
|
1493
|
-
return INVALID;
|
|
1494
|
-
}
|
|
1495
|
-
return OK(input.data);
|
|
1496
|
-
}
|
|
1497
|
-
}
|
|
1498
|
-
ZodBoolean.create = (params) => {
|
|
1499
|
-
return new ZodBoolean({
|
|
1500
|
-
typeName: ZodFirstPartyTypeKind.ZodBoolean,
|
|
1501
|
-
coerce: params?.coerce || false,
|
|
1502
|
-
...processCreateParams(params),
|
|
1503
|
-
});
|
|
1504
|
-
};
|
|
1505
|
-
class ZodDate extends ZodType {
|
|
1506
|
-
_parse(input) {
|
|
1507
|
-
if (this._def.coerce) {
|
|
1508
|
-
input.data = new Date(input.data);
|
|
1509
|
-
}
|
|
1510
|
-
const parsedType = this._getType(input);
|
|
1511
|
-
if (parsedType !== ZodParsedType.date) {
|
|
1512
|
-
const ctx = this._getOrReturnCtx(input);
|
|
1513
|
-
addIssueToContext(ctx, {
|
|
1514
|
-
code: ZodIssueCode.invalid_type,
|
|
1515
|
-
expected: ZodParsedType.date,
|
|
1516
|
-
received: ctx.parsedType,
|
|
1517
|
-
});
|
|
1518
|
-
return INVALID;
|
|
1519
|
-
}
|
|
1520
|
-
if (Number.isNaN(input.data.getTime())) {
|
|
1521
|
-
const ctx = this._getOrReturnCtx(input);
|
|
1522
|
-
addIssueToContext(ctx, {
|
|
1523
|
-
code: ZodIssueCode.invalid_date,
|
|
1524
|
-
});
|
|
1525
|
-
return INVALID;
|
|
1526
|
-
}
|
|
1527
|
-
const status = new ParseStatus();
|
|
1528
|
-
let ctx = undefined;
|
|
1529
|
-
for (const check of this._def.checks) {
|
|
1530
|
-
if (check.kind === "min") {
|
|
1531
|
-
if (input.data.getTime() < check.value) {
|
|
1532
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1533
|
-
addIssueToContext(ctx, {
|
|
1534
|
-
code: ZodIssueCode.too_small,
|
|
1535
|
-
message: check.message,
|
|
1536
|
-
inclusive: true,
|
|
1537
|
-
exact: false,
|
|
1538
|
-
minimum: check.value,
|
|
1539
|
-
type: "date",
|
|
1540
|
-
});
|
|
1541
|
-
status.dirty();
|
|
1542
|
-
}
|
|
1543
|
-
}
|
|
1544
|
-
else if (check.kind === "max") {
|
|
1545
|
-
if (input.data.getTime() > check.value) {
|
|
1546
|
-
ctx = this._getOrReturnCtx(input, ctx);
|
|
1547
|
-
addIssueToContext(ctx, {
|
|
1548
|
-
code: ZodIssueCode.too_big,
|
|
1549
|
-
message: check.message,
|
|
1550
|
-
inclusive: true,
|
|
1551
|
-
exact: false,
|
|
1552
|
-
maximum: check.value,
|
|
1553
|
-
type: "date",
|
|
1554
|
-
});
|
|
1555
|
-
status.dirty();
|
|
1556
|
-
}
|
|
1557
|
-
}
|
|
1558
|
-
else {
|
|
1559
|
-
util.assertNever(check);
|
|
1560
|
-
}
|
|
1561
|
-
}
|
|
1562
|
-
return {
|
|
1563
|
-
status: status.value,
|
|
1564
|
-
value: new Date(input.data.getTime()),
|
|
1565
|
-
};
|
|
1566
|
-
}
|
|
1567
|
-
_addCheck(check) {
|
|
1568
|
-
return new ZodDate({
|
|
1569
|
-
...this._def,
|
|
1570
|
-
checks: [...this._def.checks, check],
|
|
1571
|
-
});
|
|
1572
|
-
}
|
|
1573
|
-
min(minDate, message) {
|
|
1574
|
-
return this._addCheck({
|
|
1575
|
-
kind: "min",
|
|
1576
|
-
value: minDate.getTime(),
|
|
1577
|
-
message: errorUtil.toString(message),
|
|
1578
|
-
});
|
|
1579
|
-
}
|
|
1580
|
-
max(maxDate, message) {
|
|
1581
|
-
return this._addCheck({
|
|
1582
|
-
kind: "max",
|
|
1583
|
-
value: maxDate.getTime(),
|
|
1584
|
-
message: errorUtil.toString(message),
|
|
1585
|
-
});
|
|
1586
|
-
}
|
|
1587
|
-
get minDate() {
|
|
1588
|
-
let min = null;
|
|
1589
|
-
for (const ch of this._def.checks) {
|
|
1590
|
-
if (ch.kind === "min") {
|
|
1591
|
-
if (min === null || ch.value > min)
|
|
1592
|
-
min = ch.value;
|
|
1593
|
-
}
|
|
1594
|
-
}
|
|
1595
|
-
return min != null ? new Date(min) : null;
|
|
1596
|
-
}
|
|
1597
|
-
get maxDate() {
|
|
1598
|
-
let max = null;
|
|
1599
|
-
for (const ch of this._def.checks) {
|
|
1600
|
-
if (ch.kind === "max") {
|
|
1601
|
-
if (max === null || ch.value < max)
|
|
1602
|
-
max = ch.value;
|
|
1603
|
-
}
|
|
1604
|
-
}
|
|
1605
|
-
return max != null ? new Date(max) : null;
|
|
1606
|
-
}
|
|
1607
|
-
}
|
|
1608
|
-
ZodDate.create = (params) => {
|
|
1609
|
-
return new ZodDate({
|
|
1610
|
-
checks: [],
|
|
1611
|
-
coerce: params?.coerce || false,
|
|
1612
|
-
typeName: ZodFirstPartyTypeKind.ZodDate,
|
|
1613
|
-
...processCreateParams(params),
|
|
1614
|
-
});
|
|
1615
|
-
};
|
|
1616
|
-
class ZodSymbol extends ZodType {
|
|
1617
|
-
_parse(input) {
|
|
1618
|
-
const parsedType = this._getType(input);
|
|
1619
|
-
if (parsedType !== ZodParsedType.symbol) {
|
|
1620
|
-
const ctx = this._getOrReturnCtx(input);
|
|
1621
|
-
addIssueToContext(ctx, {
|
|
1622
|
-
code: ZodIssueCode.invalid_type,
|
|
1623
|
-
expected: ZodParsedType.symbol,
|
|
1624
|
-
received: ctx.parsedType,
|
|
1625
|
-
});
|
|
1626
|
-
return INVALID;
|
|
1627
|
-
}
|
|
1628
|
-
return OK(input.data);
|
|
1629
|
-
}
|
|
1630
|
-
}
|
|
1631
|
-
ZodSymbol.create = (params) => {
|
|
1632
|
-
return new ZodSymbol({
|
|
1633
|
-
typeName: ZodFirstPartyTypeKind.ZodSymbol,
|
|
1634
|
-
...processCreateParams(params),
|
|
1635
|
-
});
|
|
1636
|
-
};
|
|
1637
|
-
class ZodUndefined extends ZodType {
|
|
1638
|
-
_parse(input) {
|
|
1639
|
-
const parsedType = this._getType(input);
|
|
1640
|
-
if (parsedType !== ZodParsedType.undefined) {
|
|
1641
|
-
const ctx = this._getOrReturnCtx(input);
|
|
1642
|
-
addIssueToContext(ctx, {
|
|
1643
|
-
code: ZodIssueCode.invalid_type,
|
|
1644
|
-
expected: ZodParsedType.undefined,
|
|
1645
|
-
received: ctx.parsedType,
|
|
1646
|
-
});
|
|
1647
|
-
return INVALID;
|
|
1648
|
-
}
|
|
1649
|
-
return OK(input.data);
|
|
1650
|
-
}
|
|
1651
|
-
}
|
|
1652
|
-
ZodUndefined.create = (params) => {
|
|
1653
|
-
return new ZodUndefined({
|
|
1654
|
-
typeName: ZodFirstPartyTypeKind.ZodUndefined,
|
|
1655
|
-
...processCreateParams(params),
|
|
1656
|
-
});
|
|
1657
|
-
};
|
|
1658
|
-
class ZodNull extends ZodType {
|
|
1659
|
-
_parse(input) {
|
|
1660
|
-
const parsedType = this._getType(input);
|
|
1661
|
-
if (parsedType !== ZodParsedType.null) {
|
|
1662
|
-
const ctx = this._getOrReturnCtx(input);
|
|
1663
|
-
addIssueToContext(ctx, {
|
|
1664
|
-
code: ZodIssueCode.invalid_type,
|
|
1665
|
-
expected: ZodParsedType.null,
|
|
1666
|
-
received: ctx.parsedType,
|
|
1667
|
-
});
|
|
1668
|
-
return INVALID;
|
|
1669
|
-
}
|
|
1670
|
-
return OK(input.data);
|
|
1671
|
-
}
|
|
1672
|
-
}
|
|
1673
|
-
ZodNull.create = (params) => {
|
|
1674
|
-
return new ZodNull({
|
|
1675
|
-
typeName: ZodFirstPartyTypeKind.ZodNull,
|
|
1676
|
-
...processCreateParams(params),
|
|
1677
|
-
});
|
|
1678
|
-
};
|
|
1679
|
-
class ZodAny extends ZodType {
|
|
1680
|
-
constructor() {
|
|
1681
|
-
super(...arguments);
|
|
1682
|
-
// to prevent instances of other classes from extending ZodAny. this causes issues with catchall in ZodObject.
|
|
1683
|
-
this._any = true;
|
|
1684
|
-
}
|
|
1685
|
-
_parse(input) {
|
|
1686
|
-
return OK(input.data);
|
|
1687
|
-
}
|
|
1688
|
-
}
|
|
1689
|
-
ZodAny.create = (params) => {
|
|
1690
|
-
return new ZodAny({
|
|
1691
|
-
typeName: ZodFirstPartyTypeKind.ZodAny,
|
|
1692
|
-
...processCreateParams(params),
|
|
1693
|
-
});
|
|
1694
|
-
};
|
|
1695
|
-
class ZodUnknown extends ZodType {
|
|
1696
|
-
constructor() {
|
|
1697
|
-
super(...arguments);
|
|
1698
|
-
// required
|
|
1699
|
-
this._unknown = true;
|
|
1700
|
-
}
|
|
1701
|
-
_parse(input) {
|
|
1702
|
-
return OK(input.data);
|
|
1703
|
-
}
|
|
1704
|
-
}
|
|
1705
|
-
ZodUnknown.create = (params) => {
|
|
1706
|
-
return new ZodUnknown({
|
|
1707
|
-
typeName: ZodFirstPartyTypeKind.ZodUnknown,
|
|
1708
|
-
...processCreateParams(params),
|
|
1709
|
-
});
|
|
1710
|
-
};
|
|
1711
|
-
class ZodNever extends ZodType {
|
|
1712
|
-
_parse(input) {
|
|
1713
|
-
const ctx = this._getOrReturnCtx(input);
|
|
1714
|
-
addIssueToContext(ctx, {
|
|
1715
|
-
code: ZodIssueCode.invalid_type,
|
|
1716
|
-
expected: ZodParsedType.never,
|
|
1717
|
-
received: ctx.parsedType,
|
|
1718
|
-
});
|
|
1719
|
-
return INVALID;
|
|
1720
|
-
}
|
|
1721
|
-
}
|
|
1722
|
-
ZodNever.create = (params) => {
|
|
1723
|
-
return new ZodNever({
|
|
1724
|
-
typeName: ZodFirstPartyTypeKind.ZodNever,
|
|
1725
|
-
...processCreateParams(params),
|
|
1726
|
-
});
|
|
1727
|
-
};
|
|
1728
|
-
class ZodVoid extends ZodType {
|
|
1729
|
-
_parse(input) {
|
|
1730
|
-
const parsedType = this._getType(input);
|
|
1731
|
-
if (parsedType !== ZodParsedType.undefined) {
|
|
1732
|
-
const ctx = this._getOrReturnCtx(input);
|
|
1733
|
-
addIssueToContext(ctx, {
|
|
1734
|
-
code: ZodIssueCode.invalid_type,
|
|
1735
|
-
expected: ZodParsedType.void,
|
|
1736
|
-
received: ctx.parsedType,
|
|
1737
|
-
});
|
|
1738
|
-
return INVALID;
|
|
1739
|
-
}
|
|
1740
|
-
return OK(input.data);
|
|
1741
|
-
}
|
|
1742
|
-
}
|
|
1743
|
-
ZodVoid.create = (params) => {
|
|
1744
|
-
return new ZodVoid({
|
|
1745
|
-
typeName: ZodFirstPartyTypeKind.ZodVoid,
|
|
1746
|
-
...processCreateParams(params),
|
|
1747
|
-
});
|
|
1748
|
-
};
|
|
1749
|
-
class ZodArray extends ZodType {
|
|
1750
|
-
_parse(input) {
|
|
1751
|
-
const { ctx, status } = this._processInputParams(input);
|
|
1752
|
-
const def = this._def;
|
|
1753
|
-
if (ctx.parsedType !== ZodParsedType.array) {
|
|
1754
|
-
addIssueToContext(ctx, {
|
|
1755
|
-
code: ZodIssueCode.invalid_type,
|
|
1756
|
-
expected: ZodParsedType.array,
|
|
1757
|
-
received: ctx.parsedType,
|
|
1758
|
-
});
|
|
1759
|
-
return INVALID;
|
|
1760
|
-
}
|
|
1761
|
-
if (def.exactLength !== null) {
|
|
1762
|
-
const tooBig = ctx.data.length > def.exactLength.value;
|
|
1763
|
-
const tooSmall = ctx.data.length < def.exactLength.value;
|
|
1764
|
-
if (tooBig || tooSmall) {
|
|
1765
|
-
addIssueToContext(ctx, {
|
|
1766
|
-
code: tooBig ? ZodIssueCode.too_big : ZodIssueCode.too_small,
|
|
1767
|
-
minimum: (tooSmall ? def.exactLength.value : undefined),
|
|
1768
|
-
maximum: (tooBig ? def.exactLength.value : undefined),
|
|
1769
|
-
type: "array",
|
|
1770
|
-
inclusive: true,
|
|
1771
|
-
exact: true,
|
|
1772
|
-
message: def.exactLength.message,
|
|
1773
|
-
});
|
|
1774
|
-
status.dirty();
|
|
1775
|
-
}
|
|
1776
|
-
}
|
|
1777
|
-
if (def.minLength !== null) {
|
|
1778
|
-
if (ctx.data.length < def.minLength.value) {
|
|
1779
|
-
addIssueToContext(ctx, {
|
|
1780
|
-
code: ZodIssueCode.too_small,
|
|
1781
|
-
minimum: def.minLength.value,
|
|
1782
|
-
type: "array",
|
|
1783
|
-
inclusive: true,
|
|
1784
|
-
exact: false,
|
|
1785
|
-
message: def.minLength.message,
|
|
1786
|
-
});
|
|
1787
|
-
status.dirty();
|
|
1788
|
-
}
|
|
1789
|
-
}
|
|
1790
|
-
if (def.maxLength !== null) {
|
|
1791
|
-
if (ctx.data.length > def.maxLength.value) {
|
|
1792
|
-
addIssueToContext(ctx, {
|
|
1793
|
-
code: ZodIssueCode.too_big,
|
|
1794
|
-
maximum: def.maxLength.value,
|
|
1795
|
-
type: "array",
|
|
1796
|
-
inclusive: true,
|
|
1797
|
-
exact: false,
|
|
1798
|
-
message: def.maxLength.message,
|
|
1799
|
-
});
|
|
1800
|
-
status.dirty();
|
|
1801
|
-
}
|
|
1802
|
-
}
|
|
1803
|
-
if (ctx.common.async) {
|
|
1804
|
-
return Promise.all([...ctx.data].map((item, i) => {
|
|
1805
|
-
return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
1806
|
-
})).then((result) => {
|
|
1807
|
-
return ParseStatus.mergeArray(status, result);
|
|
1808
|
-
});
|
|
1809
|
-
}
|
|
1810
|
-
const result = [...ctx.data].map((item, i) => {
|
|
1811
|
-
return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
1812
|
-
});
|
|
1813
|
-
return ParseStatus.mergeArray(status, result);
|
|
1814
|
-
}
|
|
1815
|
-
get element() {
|
|
1816
|
-
return this._def.type;
|
|
1817
|
-
}
|
|
1818
|
-
min(minLength, message) {
|
|
1819
|
-
return new ZodArray({
|
|
1820
|
-
...this._def,
|
|
1821
|
-
minLength: { value: minLength, message: errorUtil.toString(message) },
|
|
1822
|
-
});
|
|
1823
|
-
}
|
|
1824
|
-
max(maxLength, message) {
|
|
1825
|
-
return new ZodArray({
|
|
1826
|
-
...this._def,
|
|
1827
|
-
maxLength: { value: maxLength, message: errorUtil.toString(message) },
|
|
1828
|
-
});
|
|
1829
|
-
}
|
|
1830
|
-
length(len, message) {
|
|
1831
|
-
return new ZodArray({
|
|
1832
|
-
...this._def,
|
|
1833
|
-
exactLength: { value: len, message: errorUtil.toString(message) },
|
|
1834
|
-
});
|
|
1835
|
-
}
|
|
1836
|
-
nonempty(message) {
|
|
1837
|
-
return this.min(1, message);
|
|
1838
|
-
}
|
|
1839
|
-
}
|
|
1840
|
-
ZodArray.create = (schema, params) => {
|
|
1841
|
-
return new ZodArray({
|
|
1842
|
-
type: schema,
|
|
1843
|
-
minLength: null,
|
|
1844
|
-
maxLength: null,
|
|
1845
|
-
exactLength: null,
|
|
1846
|
-
typeName: ZodFirstPartyTypeKind.ZodArray,
|
|
1847
|
-
...processCreateParams(params),
|
|
1848
|
-
});
|
|
1849
|
-
};
|
|
1850
|
-
function deepPartialify(schema) {
|
|
1851
|
-
if (schema instanceof ZodObject) {
|
|
1852
|
-
const newShape = {};
|
|
1853
|
-
for (const key in schema.shape) {
|
|
1854
|
-
const fieldSchema = schema.shape[key];
|
|
1855
|
-
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema));
|
|
1856
|
-
}
|
|
1857
|
-
return new ZodObject({
|
|
1858
|
-
...schema._def,
|
|
1859
|
-
shape: () => newShape,
|
|
1860
|
-
});
|
|
1861
|
-
}
|
|
1862
|
-
else if (schema instanceof ZodArray) {
|
|
1863
|
-
return new ZodArray({
|
|
1864
|
-
...schema._def,
|
|
1865
|
-
type: deepPartialify(schema.element),
|
|
1866
|
-
});
|
|
1867
|
-
}
|
|
1868
|
-
else if (schema instanceof ZodOptional) {
|
|
1869
|
-
return ZodOptional.create(deepPartialify(schema.unwrap()));
|
|
1870
|
-
}
|
|
1871
|
-
else if (schema instanceof ZodNullable) {
|
|
1872
|
-
return ZodNullable.create(deepPartialify(schema.unwrap()));
|
|
1873
|
-
}
|
|
1874
|
-
else if (schema instanceof ZodTuple) {
|
|
1875
|
-
return ZodTuple.create(schema.items.map((item) => deepPartialify(item)));
|
|
1876
|
-
}
|
|
1877
|
-
else {
|
|
1878
|
-
return schema;
|
|
1879
|
-
}
|
|
1880
|
-
}
|
|
1881
|
-
class ZodObject extends ZodType {
|
|
1882
|
-
constructor() {
|
|
1883
|
-
super(...arguments);
|
|
1884
|
-
this._cached = null;
|
|
1885
|
-
/**
|
|
1886
|
-
* @deprecated In most cases, this is no longer needed - unknown properties are now silently stripped.
|
|
1887
|
-
* If you want to pass through unknown properties, use `.passthrough()` instead.
|
|
1888
|
-
*/
|
|
1889
|
-
this.nonstrict = this.passthrough;
|
|
1890
|
-
// extend<
|
|
1891
|
-
// Augmentation extends ZodRawShape,
|
|
1892
|
-
// NewOutput extends util.flatten<{
|
|
1893
|
-
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
|
|
1894
|
-
// ? Augmentation[k]["_output"]
|
|
1895
|
-
// : k extends keyof Output
|
|
1896
|
-
// ? Output[k]
|
|
1897
|
-
// : never;
|
|
1898
|
-
// }>,
|
|
1899
|
-
// NewInput extends util.flatten<{
|
|
1900
|
-
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
|
|
1901
|
-
// ? Augmentation[k]["_input"]
|
|
1902
|
-
// : k extends keyof Input
|
|
1903
|
-
// ? Input[k]
|
|
1904
|
-
// : never;
|
|
1905
|
-
// }>
|
|
1906
|
-
// >(
|
|
1907
|
-
// augmentation: Augmentation
|
|
1908
|
-
// ): ZodObject<
|
|
1909
|
-
// extendShape<T, Augmentation>,
|
|
1910
|
-
// UnknownKeys,
|
|
1911
|
-
// Catchall,
|
|
1912
|
-
// NewOutput,
|
|
1913
|
-
// NewInput
|
|
1914
|
-
// > {
|
|
1915
|
-
// return new ZodObject({
|
|
1916
|
-
// ...this._def,
|
|
1917
|
-
// shape: () => ({
|
|
1918
|
-
// ...this._def.shape(),
|
|
1919
|
-
// ...augmentation,
|
|
1920
|
-
// }),
|
|
1921
|
-
// }) as any;
|
|
1922
|
-
// }
|
|
1923
|
-
/**
|
|
1924
|
-
* @deprecated Use `.extend` instead
|
|
1925
|
-
* */
|
|
1926
|
-
this.augment = this.extend;
|
|
1927
|
-
}
|
|
1928
|
-
_getCached() {
|
|
1929
|
-
if (this._cached !== null)
|
|
1930
|
-
return this._cached;
|
|
1931
|
-
const shape = this._def.shape();
|
|
1932
|
-
const keys = util.objectKeys(shape);
|
|
1933
|
-
this._cached = { shape, keys };
|
|
1934
|
-
return this._cached;
|
|
1935
|
-
}
|
|
1936
|
-
_parse(input) {
|
|
1937
|
-
const parsedType = this._getType(input);
|
|
1938
|
-
if (parsedType !== ZodParsedType.object) {
|
|
1939
|
-
const ctx = this._getOrReturnCtx(input);
|
|
1940
|
-
addIssueToContext(ctx, {
|
|
1941
|
-
code: ZodIssueCode.invalid_type,
|
|
1942
|
-
expected: ZodParsedType.object,
|
|
1943
|
-
received: ctx.parsedType,
|
|
1944
|
-
});
|
|
1945
|
-
return INVALID;
|
|
1946
|
-
}
|
|
1947
|
-
const { status, ctx } = this._processInputParams(input);
|
|
1948
|
-
const { shape, keys: shapeKeys } = this._getCached();
|
|
1949
|
-
const extraKeys = [];
|
|
1950
|
-
if (!(this._def.catchall instanceof ZodNever && this._def.unknownKeys === "strip")) {
|
|
1951
|
-
for (const key in ctx.data) {
|
|
1952
|
-
if (!shapeKeys.includes(key)) {
|
|
1953
|
-
extraKeys.push(key);
|
|
1954
|
-
}
|
|
1955
|
-
}
|
|
1956
|
-
}
|
|
1957
|
-
const pairs = [];
|
|
1958
|
-
for (const key of shapeKeys) {
|
|
1959
|
-
const keyValidator = shape[key];
|
|
1960
|
-
const value = ctx.data[key];
|
|
1961
|
-
pairs.push({
|
|
1962
|
-
key: { status: "valid", value: key },
|
|
1963
|
-
value: keyValidator._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
|
|
1964
|
-
alwaysSet: key in ctx.data,
|
|
1965
|
-
});
|
|
1966
|
-
}
|
|
1967
|
-
if (this._def.catchall instanceof ZodNever) {
|
|
1968
|
-
const unknownKeys = this._def.unknownKeys;
|
|
1969
|
-
if (unknownKeys === "passthrough") {
|
|
1970
|
-
for (const key of extraKeys) {
|
|
1971
|
-
pairs.push({
|
|
1972
|
-
key: { status: "valid", value: key },
|
|
1973
|
-
value: { status: "valid", value: ctx.data[key] },
|
|
1974
|
-
});
|
|
1975
|
-
}
|
|
1976
|
-
}
|
|
1977
|
-
else if (unknownKeys === "strict") {
|
|
1978
|
-
if (extraKeys.length > 0) {
|
|
1979
|
-
addIssueToContext(ctx, {
|
|
1980
|
-
code: ZodIssueCode.unrecognized_keys,
|
|
1981
|
-
keys: extraKeys,
|
|
1982
|
-
});
|
|
1983
|
-
status.dirty();
|
|
1984
|
-
}
|
|
1985
|
-
}
|
|
1986
|
-
else if (unknownKeys === "strip") ;
|
|
1987
|
-
else {
|
|
1988
|
-
throw new Error(`Internal ZodObject error: invalid unknownKeys value.`);
|
|
1989
|
-
}
|
|
1990
|
-
}
|
|
1991
|
-
else {
|
|
1992
|
-
// run catchall validation
|
|
1993
|
-
const catchall = this._def.catchall;
|
|
1994
|
-
for (const key of extraKeys) {
|
|
1995
|
-
const value = ctx.data[key];
|
|
1996
|
-
pairs.push({
|
|
1997
|
-
key: { status: "valid", value: key },
|
|
1998
|
-
value: catchall._parse(new ParseInputLazyPath(ctx, value, ctx.path, key) //, ctx.child(key), value, getParsedType(value)
|
|
1999
|
-
),
|
|
2000
|
-
alwaysSet: key in ctx.data,
|
|
2001
|
-
});
|
|
2002
|
-
}
|
|
2003
|
-
}
|
|
2004
|
-
if (ctx.common.async) {
|
|
2005
|
-
return Promise.resolve()
|
|
2006
|
-
.then(async () => {
|
|
2007
|
-
const syncPairs = [];
|
|
2008
|
-
for (const pair of pairs) {
|
|
2009
|
-
const key = await pair.key;
|
|
2010
|
-
const value = await pair.value;
|
|
2011
|
-
syncPairs.push({
|
|
2012
|
-
key,
|
|
2013
|
-
value,
|
|
2014
|
-
alwaysSet: pair.alwaysSet,
|
|
2015
|
-
});
|
|
2016
|
-
}
|
|
2017
|
-
return syncPairs;
|
|
2018
|
-
})
|
|
2019
|
-
.then((syncPairs) => {
|
|
2020
|
-
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
2021
|
-
});
|
|
2022
|
-
}
|
|
2023
|
-
else {
|
|
2024
|
-
return ParseStatus.mergeObjectSync(status, pairs);
|
|
2025
|
-
}
|
|
2026
|
-
}
|
|
2027
|
-
get shape() {
|
|
2028
|
-
return this._def.shape();
|
|
2029
|
-
}
|
|
2030
|
-
strict(message) {
|
|
2031
|
-
errorUtil.errToObj;
|
|
2032
|
-
return new ZodObject({
|
|
2033
|
-
...this._def,
|
|
2034
|
-
unknownKeys: "strict",
|
|
2035
|
-
...(message !== undefined
|
|
2036
|
-
? {
|
|
2037
|
-
errorMap: (issue, ctx) => {
|
|
2038
|
-
const defaultError = this._def.errorMap?.(issue, ctx).message ?? ctx.defaultError;
|
|
2039
|
-
if (issue.code === "unrecognized_keys")
|
|
2040
|
-
return {
|
|
2041
|
-
message: errorUtil.errToObj(message).message ?? defaultError,
|
|
2042
|
-
};
|
|
2043
|
-
return {
|
|
2044
|
-
message: defaultError,
|
|
2045
|
-
};
|
|
2046
|
-
},
|
|
2047
|
-
}
|
|
2048
|
-
: {}),
|
|
2049
|
-
});
|
|
2050
|
-
}
|
|
2051
|
-
strip() {
|
|
2052
|
-
return new ZodObject({
|
|
2053
|
-
...this._def,
|
|
2054
|
-
unknownKeys: "strip",
|
|
2055
|
-
});
|
|
2056
|
-
}
|
|
2057
|
-
passthrough() {
|
|
2058
|
-
return new ZodObject({
|
|
2059
|
-
...this._def,
|
|
2060
|
-
unknownKeys: "passthrough",
|
|
2061
|
-
});
|
|
2062
|
-
}
|
|
2063
|
-
// const AugmentFactory =
|
|
2064
|
-
// <Def extends ZodObjectDef>(def: Def) =>
|
|
2065
|
-
// <Augmentation extends ZodRawShape>(
|
|
2066
|
-
// augmentation: Augmentation
|
|
2067
|
-
// ): ZodObject<
|
|
2068
|
-
// extendShape<ReturnType<Def["shape"]>, Augmentation>,
|
|
2069
|
-
// Def["unknownKeys"],
|
|
2070
|
-
// Def["catchall"]
|
|
2071
|
-
// > => {
|
|
2072
|
-
// return new ZodObject({
|
|
2073
|
-
// ...def,
|
|
2074
|
-
// shape: () => ({
|
|
2075
|
-
// ...def.shape(),
|
|
2076
|
-
// ...augmentation,
|
|
2077
|
-
// }),
|
|
2078
|
-
// }) as any;
|
|
2079
|
-
// };
|
|
2080
|
-
extend(augmentation) {
|
|
2081
|
-
return new ZodObject({
|
|
2082
|
-
...this._def,
|
|
2083
|
-
shape: () => ({
|
|
2084
|
-
...this._def.shape(),
|
|
2085
|
-
...augmentation,
|
|
2086
|
-
}),
|
|
2087
|
-
});
|
|
2088
|
-
}
|
|
2089
|
-
/**
|
|
2090
|
-
* Prior to zod@1.0.12 there was a bug in the
|
|
2091
|
-
* inferred type of merged objects. Please
|
|
2092
|
-
* upgrade if you are experiencing issues.
|
|
2093
|
-
*/
|
|
2094
|
-
merge(merging) {
|
|
2095
|
-
const merged = new ZodObject({
|
|
2096
|
-
unknownKeys: merging._def.unknownKeys,
|
|
2097
|
-
catchall: merging._def.catchall,
|
|
2098
|
-
shape: () => ({
|
|
2099
|
-
...this._def.shape(),
|
|
2100
|
-
...merging._def.shape(),
|
|
2101
|
-
}),
|
|
2102
|
-
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2103
|
-
});
|
|
2104
|
-
return merged;
|
|
2105
|
-
}
|
|
2106
|
-
// merge<
|
|
2107
|
-
// Incoming extends AnyZodObject,
|
|
2108
|
-
// Augmentation extends Incoming["shape"],
|
|
2109
|
-
// NewOutput extends {
|
|
2110
|
-
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
|
|
2111
|
-
// ? Augmentation[k]["_output"]
|
|
2112
|
-
// : k extends keyof Output
|
|
2113
|
-
// ? Output[k]
|
|
2114
|
-
// : never;
|
|
2115
|
-
// },
|
|
2116
|
-
// NewInput extends {
|
|
2117
|
-
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
|
|
2118
|
-
// ? Augmentation[k]["_input"]
|
|
2119
|
-
// : k extends keyof Input
|
|
2120
|
-
// ? Input[k]
|
|
2121
|
-
// : never;
|
|
2122
|
-
// }
|
|
2123
|
-
// >(
|
|
2124
|
-
// merging: Incoming
|
|
2125
|
-
// ): ZodObject<
|
|
2126
|
-
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
2127
|
-
// Incoming["_def"]["unknownKeys"],
|
|
2128
|
-
// Incoming["_def"]["catchall"],
|
|
2129
|
-
// NewOutput,
|
|
2130
|
-
// NewInput
|
|
2131
|
-
// > {
|
|
2132
|
-
// const merged: any = new ZodObject({
|
|
2133
|
-
// unknownKeys: merging._def.unknownKeys,
|
|
2134
|
-
// catchall: merging._def.catchall,
|
|
2135
|
-
// shape: () =>
|
|
2136
|
-
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
2137
|
-
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2138
|
-
// }) as any;
|
|
2139
|
-
// return merged;
|
|
2140
|
-
// }
|
|
2141
|
-
setKey(key, schema) {
|
|
2142
|
-
return this.augment({ [key]: schema });
|
|
2143
|
-
}
|
|
2144
|
-
// merge<Incoming extends AnyZodObject>(
|
|
2145
|
-
// merging: Incoming
|
|
2146
|
-
// ): //ZodObject<T & Incoming["_shape"], UnknownKeys, Catchall> = (merging) => {
|
|
2147
|
-
// ZodObject<
|
|
2148
|
-
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
2149
|
-
// Incoming["_def"]["unknownKeys"],
|
|
2150
|
-
// Incoming["_def"]["catchall"]
|
|
2151
|
-
// > {
|
|
2152
|
-
// // const mergedShape = objectUtil.mergeShapes(
|
|
2153
|
-
// // this._def.shape(),
|
|
2154
|
-
// // merging._def.shape()
|
|
2155
|
-
// // );
|
|
2156
|
-
// const merged: any = new ZodObject({
|
|
2157
|
-
// unknownKeys: merging._def.unknownKeys,
|
|
2158
|
-
// catchall: merging._def.catchall,
|
|
2159
|
-
// shape: () =>
|
|
2160
|
-
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
2161
|
-
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2162
|
-
// }) as any;
|
|
2163
|
-
// return merged;
|
|
2164
|
-
// }
|
|
2165
|
-
catchall(index) {
|
|
2166
|
-
return new ZodObject({
|
|
2167
|
-
...this._def,
|
|
2168
|
-
catchall: index,
|
|
2169
|
-
});
|
|
2170
|
-
}
|
|
2171
|
-
pick(mask) {
|
|
2172
|
-
const shape = {};
|
|
2173
|
-
for (const key of util.objectKeys(mask)) {
|
|
2174
|
-
if (mask[key] && this.shape[key]) {
|
|
2175
|
-
shape[key] = this.shape[key];
|
|
2176
|
-
}
|
|
2177
|
-
}
|
|
2178
|
-
return new ZodObject({
|
|
2179
|
-
...this._def,
|
|
2180
|
-
shape: () => shape,
|
|
2181
|
-
});
|
|
2182
|
-
}
|
|
2183
|
-
omit(mask) {
|
|
2184
|
-
const shape = {};
|
|
2185
|
-
for (const key of util.objectKeys(this.shape)) {
|
|
2186
|
-
if (!mask[key]) {
|
|
2187
|
-
shape[key] = this.shape[key];
|
|
2188
|
-
}
|
|
2189
|
-
}
|
|
2190
|
-
return new ZodObject({
|
|
2191
|
-
...this._def,
|
|
2192
|
-
shape: () => shape,
|
|
2193
|
-
});
|
|
2194
|
-
}
|
|
2195
|
-
/**
|
|
2196
|
-
* @deprecated
|
|
2197
|
-
*/
|
|
2198
|
-
deepPartial() {
|
|
2199
|
-
return deepPartialify(this);
|
|
2200
|
-
}
|
|
2201
|
-
partial(mask) {
|
|
2202
|
-
const newShape = {};
|
|
2203
|
-
for (const key of util.objectKeys(this.shape)) {
|
|
2204
|
-
const fieldSchema = this.shape[key];
|
|
2205
|
-
if (mask && !mask[key]) {
|
|
2206
|
-
newShape[key] = fieldSchema;
|
|
2207
|
-
}
|
|
2208
|
-
else {
|
|
2209
|
-
newShape[key] = fieldSchema.optional();
|
|
2210
|
-
}
|
|
2211
|
-
}
|
|
2212
|
-
return new ZodObject({
|
|
2213
|
-
...this._def,
|
|
2214
|
-
shape: () => newShape,
|
|
2215
|
-
});
|
|
2216
|
-
}
|
|
2217
|
-
required(mask) {
|
|
2218
|
-
const newShape = {};
|
|
2219
|
-
for (const key of util.objectKeys(this.shape)) {
|
|
2220
|
-
if (mask && !mask[key]) {
|
|
2221
|
-
newShape[key] = this.shape[key];
|
|
2222
|
-
}
|
|
2223
|
-
else {
|
|
2224
|
-
const fieldSchema = this.shape[key];
|
|
2225
|
-
let newField = fieldSchema;
|
|
2226
|
-
while (newField instanceof ZodOptional) {
|
|
2227
|
-
newField = newField._def.innerType;
|
|
2228
|
-
}
|
|
2229
|
-
newShape[key] = newField;
|
|
2230
|
-
}
|
|
2231
|
-
}
|
|
2232
|
-
return new ZodObject({
|
|
2233
|
-
...this._def,
|
|
2234
|
-
shape: () => newShape,
|
|
2235
|
-
});
|
|
2236
|
-
}
|
|
2237
|
-
keyof() {
|
|
2238
|
-
return createZodEnum(util.objectKeys(this.shape));
|
|
2239
|
-
}
|
|
2240
|
-
}
|
|
2241
|
-
ZodObject.create = (shape, params) => {
|
|
2242
|
-
return new ZodObject({
|
|
2243
|
-
shape: () => shape,
|
|
2244
|
-
unknownKeys: "strip",
|
|
2245
|
-
catchall: ZodNever.create(),
|
|
2246
|
-
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2247
|
-
...processCreateParams(params),
|
|
2248
|
-
});
|
|
2249
|
-
};
|
|
2250
|
-
ZodObject.strictCreate = (shape, params) => {
|
|
2251
|
-
return new ZodObject({
|
|
2252
|
-
shape: () => shape,
|
|
2253
|
-
unknownKeys: "strict",
|
|
2254
|
-
catchall: ZodNever.create(),
|
|
2255
|
-
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2256
|
-
...processCreateParams(params),
|
|
2257
|
-
});
|
|
2258
|
-
};
|
|
2259
|
-
ZodObject.lazycreate = (shape, params) => {
|
|
2260
|
-
return new ZodObject({
|
|
2261
|
-
shape,
|
|
2262
|
-
unknownKeys: "strip",
|
|
2263
|
-
catchall: ZodNever.create(),
|
|
2264
|
-
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2265
|
-
...processCreateParams(params),
|
|
2266
|
-
});
|
|
2267
|
-
};
|
|
2268
|
-
class ZodUnion extends ZodType {
|
|
2269
|
-
_parse(input) {
|
|
2270
|
-
const { ctx } = this._processInputParams(input);
|
|
2271
|
-
const options = this._def.options;
|
|
2272
|
-
function handleResults(results) {
|
|
2273
|
-
// return first issue-free validation if it exists
|
|
2274
|
-
for (const result of results) {
|
|
2275
|
-
if (result.result.status === "valid") {
|
|
2276
|
-
return result.result;
|
|
2277
|
-
}
|
|
2278
|
-
}
|
|
2279
|
-
for (const result of results) {
|
|
2280
|
-
if (result.result.status === "dirty") {
|
|
2281
|
-
// add issues from dirty option
|
|
2282
|
-
ctx.common.issues.push(...result.ctx.common.issues);
|
|
2283
|
-
return result.result;
|
|
2284
|
-
}
|
|
2285
|
-
}
|
|
2286
|
-
// return invalid
|
|
2287
|
-
const unionErrors = results.map((result) => new ZodError(result.ctx.common.issues));
|
|
2288
|
-
addIssueToContext(ctx, {
|
|
2289
|
-
code: ZodIssueCode.invalid_union,
|
|
2290
|
-
unionErrors,
|
|
2291
|
-
});
|
|
2292
|
-
return INVALID;
|
|
2293
|
-
}
|
|
2294
|
-
if (ctx.common.async) {
|
|
2295
|
-
return Promise.all(options.map(async (option) => {
|
|
2296
|
-
const childCtx = {
|
|
2297
|
-
...ctx,
|
|
2298
|
-
common: {
|
|
2299
|
-
...ctx.common,
|
|
2300
|
-
issues: [],
|
|
2301
|
-
},
|
|
2302
|
-
parent: null,
|
|
2303
|
-
};
|
|
2304
|
-
return {
|
|
2305
|
-
result: await option._parseAsync({
|
|
2306
|
-
data: ctx.data,
|
|
2307
|
-
path: ctx.path,
|
|
2308
|
-
parent: childCtx,
|
|
2309
|
-
}),
|
|
2310
|
-
ctx: childCtx,
|
|
2311
|
-
};
|
|
2312
|
-
})).then(handleResults);
|
|
2313
|
-
}
|
|
2314
|
-
else {
|
|
2315
|
-
let dirty = undefined;
|
|
2316
|
-
const issues = [];
|
|
2317
|
-
for (const option of options) {
|
|
2318
|
-
const childCtx = {
|
|
2319
|
-
...ctx,
|
|
2320
|
-
common: {
|
|
2321
|
-
...ctx.common,
|
|
2322
|
-
issues: [],
|
|
2323
|
-
},
|
|
2324
|
-
parent: null,
|
|
2325
|
-
};
|
|
2326
|
-
const result = option._parseSync({
|
|
2327
|
-
data: ctx.data,
|
|
2328
|
-
path: ctx.path,
|
|
2329
|
-
parent: childCtx,
|
|
2330
|
-
});
|
|
2331
|
-
if (result.status === "valid") {
|
|
2332
|
-
return result;
|
|
2333
|
-
}
|
|
2334
|
-
else if (result.status === "dirty" && !dirty) {
|
|
2335
|
-
dirty = { result, ctx: childCtx };
|
|
2336
|
-
}
|
|
2337
|
-
if (childCtx.common.issues.length) {
|
|
2338
|
-
issues.push(childCtx.common.issues);
|
|
2339
|
-
}
|
|
2340
|
-
}
|
|
2341
|
-
if (dirty) {
|
|
2342
|
-
ctx.common.issues.push(...dirty.ctx.common.issues);
|
|
2343
|
-
return dirty.result;
|
|
2344
|
-
}
|
|
2345
|
-
const unionErrors = issues.map((issues) => new ZodError(issues));
|
|
2346
|
-
addIssueToContext(ctx, {
|
|
2347
|
-
code: ZodIssueCode.invalid_union,
|
|
2348
|
-
unionErrors,
|
|
2349
|
-
});
|
|
2350
|
-
return INVALID;
|
|
2351
|
-
}
|
|
2352
|
-
}
|
|
2353
|
-
get options() {
|
|
2354
|
-
return this._def.options;
|
|
2355
|
-
}
|
|
2356
|
-
}
|
|
2357
|
-
ZodUnion.create = (types, params) => {
|
|
2358
|
-
return new ZodUnion({
|
|
2359
|
-
options: types,
|
|
2360
|
-
typeName: ZodFirstPartyTypeKind.ZodUnion,
|
|
2361
|
-
...processCreateParams(params),
|
|
2362
|
-
});
|
|
2363
|
-
};
|
|
2364
|
-
/////////////////////////////////////////////////////
|
|
2365
|
-
/////////////////////////////////////////////////////
|
|
2366
|
-
////////// //////////
|
|
2367
|
-
////////// ZodDiscriminatedUnion //////////
|
|
2368
|
-
////////// //////////
|
|
2369
|
-
/////////////////////////////////////////////////////
|
|
2370
|
-
/////////////////////////////////////////////////////
|
|
2371
|
-
const getDiscriminator = (type) => {
|
|
2372
|
-
if (type instanceof ZodLazy) {
|
|
2373
|
-
return getDiscriminator(type.schema);
|
|
2374
|
-
}
|
|
2375
|
-
else if (type instanceof ZodEffects) {
|
|
2376
|
-
return getDiscriminator(type.innerType());
|
|
2377
|
-
}
|
|
2378
|
-
else if (type instanceof ZodLiteral) {
|
|
2379
|
-
return [type.value];
|
|
2380
|
-
}
|
|
2381
|
-
else if (type instanceof ZodEnum) {
|
|
2382
|
-
return type.options;
|
|
2383
|
-
}
|
|
2384
|
-
else if (type instanceof ZodNativeEnum) {
|
|
2385
|
-
// eslint-disable-next-line ban/ban
|
|
2386
|
-
return util.objectValues(type.enum);
|
|
2387
|
-
}
|
|
2388
|
-
else if (type instanceof ZodDefault) {
|
|
2389
|
-
return getDiscriminator(type._def.innerType);
|
|
2390
|
-
}
|
|
2391
|
-
else if (type instanceof ZodUndefined) {
|
|
2392
|
-
return [undefined];
|
|
2393
|
-
}
|
|
2394
|
-
else if (type instanceof ZodNull) {
|
|
2395
|
-
return [null];
|
|
2396
|
-
}
|
|
2397
|
-
else if (type instanceof ZodOptional) {
|
|
2398
|
-
return [undefined, ...getDiscriminator(type.unwrap())];
|
|
2399
|
-
}
|
|
2400
|
-
else if (type instanceof ZodNullable) {
|
|
2401
|
-
return [null, ...getDiscriminator(type.unwrap())];
|
|
2402
|
-
}
|
|
2403
|
-
else if (type instanceof ZodBranded) {
|
|
2404
|
-
return getDiscriminator(type.unwrap());
|
|
2405
|
-
}
|
|
2406
|
-
else if (type instanceof ZodReadonly) {
|
|
2407
|
-
return getDiscriminator(type.unwrap());
|
|
2408
|
-
}
|
|
2409
|
-
else if (type instanceof ZodCatch) {
|
|
2410
|
-
return getDiscriminator(type._def.innerType);
|
|
2411
|
-
}
|
|
2412
|
-
else {
|
|
2413
|
-
return [];
|
|
2414
|
-
}
|
|
2415
|
-
};
|
|
2416
|
-
class ZodDiscriminatedUnion extends ZodType {
|
|
2417
|
-
_parse(input) {
|
|
2418
|
-
const { ctx } = this._processInputParams(input);
|
|
2419
|
-
if (ctx.parsedType !== ZodParsedType.object) {
|
|
2420
|
-
addIssueToContext(ctx, {
|
|
2421
|
-
code: ZodIssueCode.invalid_type,
|
|
2422
|
-
expected: ZodParsedType.object,
|
|
2423
|
-
received: ctx.parsedType,
|
|
2424
|
-
});
|
|
2425
|
-
return INVALID;
|
|
2426
|
-
}
|
|
2427
|
-
const discriminator = this.discriminator;
|
|
2428
|
-
const discriminatorValue = ctx.data[discriminator];
|
|
2429
|
-
const option = this.optionsMap.get(discriminatorValue);
|
|
2430
|
-
if (!option) {
|
|
2431
|
-
addIssueToContext(ctx, {
|
|
2432
|
-
code: ZodIssueCode.invalid_union_discriminator,
|
|
2433
|
-
options: Array.from(this.optionsMap.keys()),
|
|
2434
|
-
path: [discriminator],
|
|
2435
|
-
});
|
|
2436
|
-
return INVALID;
|
|
2437
|
-
}
|
|
2438
|
-
if (ctx.common.async) {
|
|
2439
|
-
return option._parseAsync({
|
|
2440
|
-
data: ctx.data,
|
|
2441
|
-
path: ctx.path,
|
|
2442
|
-
parent: ctx,
|
|
2443
|
-
});
|
|
2444
|
-
}
|
|
2445
|
-
else {
|
|
2446
|
-
return option._parseSync({
|
|
2447
|
-
data: ctx.data,
|
|
2448
|
-
path: ctx.path,
|
|
2449
|
-
parent: ctx,
|
|
2450
|
-
});
|
|
2451
|
-
}
|
|
2452
|
-
}
|
|
2453
|
-
get discriminator() {
|
|
2454
|
-
return this._def.discriminator;
|
|
2455
|
-
}
|
|
2456
|
-
get options() {
|
|
2457
|
-
return this._def.options;
|
|
2458
|
-
}
|
|
2459
|
-
get optionsMap() {
|
|
2460
|
-
return this._def.optionsMap;
|
|
2461
|
-
}
|
|
2462
|
-
/**
|
|
2463
|
-
* The constructor of the discriminated union schema. Its behaviour is very similar to that of the normal z.union() constructor.
|
|
2464
|
-
* However, it only allows a union of objects, all of which need to share a discriminator property. This property must
|
|
2465
|
-
* have a different value for each object in the union.
|
|
2466
|
-
* @param discriminator the name of the discriminator property
|
|
2467
|
-
* @param types an array of object schemas
|
|
2468
|
-
* @param params
|
|
2469
|
-
*/
|
|
2470
|
-
static create(discriminator, options, params) {
|
|
2471
|
-
// Get all the valid discriminator values
|
|
2472
|
-
const optionsMap = new Map();
|
|
2473
|
-
// try {
|
|
2474
|
-
for (const type of options) {
|
|
2475
|
-
const discriminatorValues = getDiscriminator(type.shape[discriminator]);
|
|
2476
|
-
if (!discriminatorValues.length) {
|
|
2477
|
-
throw new Error(`A discriminator value for key \`${discriminator}\` could not be extracted from all schema options`);
|
|
2478
|
-
}
|
|
2479
|
-
for (const value of discriminatorValues) {
|
|
2480
|
-
if (optionsMap.has(value)) {
|
|
2481
|
-
throw new Error(`Discriminator property ${String(discriminator)} has duplicate value ${String(value)}`);
|
|
2482
|
-
}
|
|
2483
|
-
optionsMap.set(value, type);
|
|
2484
|
-
}
|
|
2485
|
-
}
|
|
2486
|
-
return new ZodDiscriminatedUnion({
|
|
2487
|
-
typeName: ZodFirstPartyTypeKind.ZodDiscriminatedUnion,
|
|
2488
|
-
discriminator,
|
|
2489
|
-
options,
|
|
2490
|
-
optionsMap,
|
|
2491
|
-
...processCreateParams(params),
|
|
2492
|
-
});
|
|
2493
|
-
}
|
|
2494
|
-
}
|
|
2495
|
-
function mergeValues(a, b) {
|
|
2496
|
-
const aType = getParsedType(a);
|
|
2497
|
-
const bType = getParsedType(b);
|
|
2498
|
-
if (a === b) {
|
|
2499
|
-
return { valid: true, data: a };
|
|
2500
|
-
}
|
|
2501
|
-
else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
|
|
2502
|
-
const bKeys = util.objectKeys(b);
|
|
2503
|
-
const sharedKeys = util.objectKeys(a).filter((key) => bKeys.indexOf(key) !== -1);
|
|
2504
|
-
const newObj = { ...a, ...b };
|
|
2505
|
-
for (const key of sharedKeys) {
|
|
2506
|
-
const sharedValue = mergeValues(a[key], b[key]);
|
|
2507
|
-
if (!sharedValue.valid) {
|
|
2508
|
-
return { valid: false };
|
|
2509
|
-
}
|
|
2510
|
-
newObj[key] = sharedValue.data;
|
|
2511
|
-
}
|
|
2512
|
-
return { valid: true, data: newObj };
|
|
2513
|
-
}
|
|
2514
|
-
else if (aType === ZodParsedType.array && bType === ZodParsedType.array) {
|
|
2515
|
-
if (a.length !== b.length) {
|
|
2516
|
-
return { valid: false };
|
|
2517
|
-
}
|
|
2518
|
-
const newArray = [];
|
|
2519
|
-
for (let index = 0; index < a.length; index++) {
|
|
2520
|
-
const itemA = a[index];
|
|
2521
|
-
const itemB = b[index];
|
|
2522
|
-
const sharedValue = mergeValues(itemA, itemB);
|
|
2523
|
-
if (!sharedValue.valid) {
|
|
2524
|
-
return { valid: false };
|
|
2525
|
-
}
|
|
2526
|
-
newArray.push(sharedValue.data);
|
|
2527
|
-
}
|
|
2528
|
-
return { valid: true, data: newArray };
|
|
2529
|
-
}
|
|
2530
|
-
else if (aType === ZodParsedType.date && bType === ZodParsedType.date && +a === +b) {
|
|
2531
|
-
return { valid: true, data: a };
|
|
2532
|
-
}
|
|
2533
|
-
else {
|
|
2534
|
-
return { valid: false };
|
|
2535
|
-
}
|
|
2536
|
-
}
|
|
2537
|
-
class ZodIntersection extends ZodType {
|
|
2538
|
-
_parse(input) {
|
|
2539
|
-
const { status, ctx } = this._processInputParams(input);
|
|
2540
|
-
const handleParsed = (parsedLeft, parsedRight) => {
|
|
2541
|
-
if (isAborted(parsedLeft) || isAborted(parsedRight)) {
|
|
2542
|
-
return INVALID;
|
|
2543
|
-
}
|
|
2544
|
-
const merged = mergeValues(parsedLeft.value, parsedRight.value);
|
|
2545
|
-
if (!merged.valid) {
|
|
2546
|
-
addIssueToContext(ctx, {
|
|
2547
|
-
code: ZodIssueCode.invalid_intersection_types,
|
|
2548
|
-
});
|
|
2549
|
-
return INVALID;
|
|
2550
|
-
}
|
|
2551
|
-
if (isDirty(parsedLeft) || isDirty(parsedRight)) {
|
|
2552
|
-
status.dirty();
|
|
2553
|
-
}
|
|
2554
|
-
return { status: status.value, value: merged.data };
|
|
2555
|
-
};
|
|
2556
|
-
if (ctx.common.async) {
|
|
2557
|
-
return Promise.all([
|
|
2558
|
-
this._def.left._parseAsync({
|
|
2559
|
-
data: ctx.data,
|
|
2560
|
-
path: ctx.path,
|
|
2561
|
-
parent: ctx,
|
|
2562
|
-
}),
|
|
2563
|
-
this._def.right._parseAsync({
|
|
2564
|
-
data: ctx.data,
|
|
2565
|
-
path: ctx.path,
|
|
2566
|
-
parent: ctx,
|
|
2567
|
-
}),
|
|
2568
|
-
]).then(([left, right]) => handleParsed(left, right));
|
|
2569
|
-
}
|
|
2570
|
-
else {
|
|
2571
|
-
return handleParsed(this._def.left._parseSync({
|
|
2572
|
-
data: ctx.data,
|
|
2573
|
-
path: ctx.path,
|
|
2574
|
-
parent: ctx,
|
|
2575
|
-
}), this._def.right._parseSync({
|
|
2576
|
-
data: ctx.data,
|
|
2577
|
-
path: ctx.path,
|
|
2578
|
-
parent: ctx,
|
|
2579
|
-
}));
|
|
2580
|
-
}
|
|
2581
|
-
}
|
|
2582
|
-
}
|
|
2583
|
-
ZodIntersection.create = (left, right, params) => {
|
|
2584
|
-
return new ZodIntersection({
|
|
2585
|
-
left: left,
|
|
2586
|
-
right: right,
|
|
2587
|
-
typeName: ZodFirstPartyTypeKind.ZodIntersection,
|
|
2588
|
-
...processCreateParams(params),
|
|
2589
|
-
});
|
|
2590
|
-
};
|
|
2591
|
-
// type ZodTupleItems = [ZodTypeAny, ...ZodTypeAny[]];
|
|
2592
|
-
class ZodTuple extends ZodType {
|
|
2593
|
-
_parse(input) {
|
|
2594
|
-
const { status, ctx } = this._processInputParams(input);
|
|
2595
|
-
if (ctx.parsedType !== ZodParsedType.array) {
|
|
2596
|
-
addIssueToContext(ctx, {
|
|
2597
|
-
code: ZodIssueCode.invalid_type,
|
|
2598
|
-
expected: ZodParsedType.array,
|
|
2599
|
-
received: ctx.parsedType,
|
|
2600
|
-
});
|
|
2601
|
-
return INVALID;
|
|
2602
|
-
}
|
|
2603
|
-
if (ctx.data.length < this._def.items.length) {
|
|
2604
|
-
addIssueToContext(ctx, {
|
|
2605
|
-
code: ZodIssueCode.too_small,
|
|
2606
|
-
minimum: this._def.items.length,
|
|
2607
|
-
inclusive: true,
|
|
2608
|
-
exact: false,
|
|
2609
|
-
type: "array",
|
|
2610
|
-
});
|
|
2611
|
-
return INVALID;
|
|
2612
|
-
}
|
|
2613
|
-
const rest = this._def.rest;
|
|
2614
|
-
if (!rest && ctx.data.length > this._def.items.length) {
|
|
2615
|
-
addIssueToContext(ctx, {
|
|
2616
|
-
code: ZodIssueCode.too_big,
|
|
2617
|
-
maximum: this._def.items.length,
|
|
2618
|
-
inclusive: true,
|
|
2619
|
-
exact: false,
|
|
2620
|
-
type: "array",
|
|
2621
|
-
});
|
|
2622
|
-
status.dirty();
|
|
2623
|
-
}
|
|
2624
|
-
const items = [...ctx.data]
|
|
2625
|
-
.map((item, itemIndex) => {
|
|
2626
|
-
const schema = this._def.items[itemIndex] || this._def.rest;
|
|
2627
|
-
if (!schema)
|
|
2628
|
-
return null;
|
|
2629
|
-
return schema._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
|
|
2630
|
-
})
|
|
2631
|
-
.filter((x) => !!x); // filter nulls
|
|
2632
|
-
if (ctx.common.async) {
|
|
2633
|
-
return Promise.all(items).then((results) => {
|
|
2634
|
-
return ParseStatus.mergeArray(status, results);
|
|
2635
|
-
});
|
|
2636
|
-
}
|
|
2637
|
-
else {
|
|
2638
|
-
return ParseStatus.mergeArray(status, items);
|
|
2639
|
-
}
|
|
2640
|
-
}
|
|
2641
|
-
get items() {
|
|
2642
|
-
return this._def.items;
|
|
2643
|
-
}
|
|
2644
|
-
rest(rest) {
|
|
2645
|
-
return new ZodTuple({
|
|
2646
|
-
...this._def,
|
|
2647
|
-
rest,
|
|
2648
|
-
});
|
|
2649
|
-
}
|
|
2650
|
-
}
|
|
2651
|
-
ZodTuple.create = (schemas, params) => {
|
|
2652
|
-
if (!Array.isArray(schemas)) {
|
|
2653
|
-
throw new Error("You must pass an array of schemas to z.tuple([ ... ])");
|
|
2654
|
-
}
|
|
2655
|
-
return new ZodTuple({
|
|
2656
|
-
items: schemas,
|
|
2657
|
-
typeName: ZodFirstPartyTypeKind.ZodTuple,
|
|
2658
|
-
rest: null,
|
|
2659
|
-
...processCreateParams(params),
|
|
2660
|
-
});
|
|
2661
|
-
};
|
|
2662
|
-
class ZodRecord extends ZodType {
|
|
2663
|
-
get keySchema() {
|
|
2664
|
-
return this._def.keyType;
|
|
2665
|
-
}
|
|
2666
|
-
get valueSchema() {
|
|
2667
|
-
return this._def.valueType;
|
|
2668
|
-
}
|
|
2669
|
-
_parse(input) {
|
|
2670
|
-
const { status, ctx } = this._processInputParams(input);
|
|
2671
|
-
if (ctx.parsedType !== ZodParsedType.object) {
|
|
2672
|
-
addIssueToContext(ctx, {
|
|
2673
|
-
code: ZodIssueCode.invalid_type,
|
|
2674
|
-
expected: ZodParsedType.object,
|
|
2675
|
-
received: ctx.parsedType,
|
|
2676
|
-
});
|
|
2677
|
-
return INVALID;
|
|
2678
|
-
}
|
|
2679
|
-
const pairs = [];
|
|
2680
|
-
const keyType = this._def.keyType;
|
|
2681
|
-
const valueType = this._def.valueType;
|
|
2682
|
-
for (const key in ctx.data) {
|
|
2683
|
-
pairs.push({
|
|
2684
|
-
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, key)),
|
|
2685
|
-
value: valueType._parse(new ParseInputLazyPath(ctx, ctx.data[key], ctx.path, key)),
|
|
2686
|
-
alwaysSet: key in ctx.data,
|
|
2687
|
-
});
|
|
2688
|
-
}
|
|
2689
|
-
if (ctx.common.async) {
|
|
2690
|
-
return ParseStatus.mergeObjectAsync(status, pairs);
|
|
2691
|
-
}
|
|
2692
|
-
else {
|
|
2693
|
-
return ParseStatus.mergeObjectSync(status, pairs);
|
|
2694
|
-
}
|
|
2695
|
-
}
|
|
2696
|
-
get element() {
|
|
2697
|
-
return this._def.valueType;
|
|
2698
|
-
}
|
|
2699
|
-
static create(first, second, third) {
|
|
2700
|
-
if (second instanceof ZodType) {
|
|
2701
|
-
return new ZodRecord({
|
|
2702
|
-
keyType: first,
|
|
2703
|
-
valueType: second,
|
|
2704
|
-
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
2705
|
-
...processCreateParams(third),
|
|
2706
|
-
});
|
|
2707
|
-
}
|
|
2708
|
-
return new ZodRecord({
|
|
2709
|
-
keyType: ZodString.create(),
|
|
2710
|
-
valueType: first,
|
|
2711
|
-
typeName: ZodFirstPartyTypeKind.ZodRecord,
|
|
2712
|
-
...processCreateParams(second),
|
|
2713
|
-
});
|
|
2714
|
-
}
|
|
2715
|
-
}
|
|
2716
|
-
class ZodMap extends ZodType {
|
|
2717
|
-
get keySchema() {
|
|
2718
|
-
return this._def.keyType;
|
|
2719
|
-
}
|
|
2720
|
-
get valueSchema() {
|
|
2721
|
-
return this._def.valueType;
|
|
2722
|
-
}
|
|
2723
|
-
_parse(input) {
|
|
2724
|
-
const { status, ctx } = this._processInputParams(input);
|
|
2725
|
-
if (ctx.parsedType !== ZodParsedType.map) {
|
|
2726
|
-
addIssueToContext(ctx, {
|
|
2727
|
-
code: ZodIssueCode.invalid_type,
|
|
2728
|
-
expected: ZodParsedType.map,
|
|
2729
|
-
received: ctx.parsedType,
|
|
2730
|
-
});
|
|
2731
|
-
return INVALID;
|
|
2732
|
-
}
|
|
2733
|
-
const keyType = this._def.keyType;
|
|
2734
|
-
const valueType = this._def.valueType;
|
|
2735
|
-
const pairs = [...ctx.data.entries()].map(([key, value], index) => {
|
|
2736
|
-
return {
|
|
2737
|
-
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, [index, "key"])),
|
|
2738
|
-
value: valueType._parse(new ParseInputLazyPath(ctx, value, ctx.path, [index, "value"])),
|
|
2739
|
-
};
|
|
2740
|
-
});
|
|
2741
|
-
if (ctx.common.async) {
|
|
2742
|
-
const finalMap = new Map();
|
|
2743
|
-
return Promise.resolve().then(async () => {
|
|
2744
|
-
for (const pair of pairs) {
|
|
2745
|
-
const key = await pair.key;
|
|
2746
|
-
const value = await pair.value;
|
|
2747
|
-
if (key.status === "aborted" || value.status === "aborted") {
|
|
2748
|
-
return INVALID;
|
|
2749
|
-
}
|
|
2750
|
-
if (key.status === "dirty" || value.status === "dirty") {
|
|
2751
|
-
status.dirty();
|
|
2752
|
-
}
|
|
2753
|
-
finalMap.set(key.value, value.value);
|
|
2754
|
-
}
|
|
2755
|
-
return { status: status.value, value: finalMap };
|
|
2756
|
-
});
|
|
2757
|
-
}
|
|
2758
|
-
else {
|
|
2759
|
-
const finalMap = new Map();
|
|
2760
|
-
for (const pair of pairs) {
|
|
2761
|
-
const key = pair.key;
|
|
2762
|
-
const value = pair.value;
|
|
2763
|
-
if (key.status === "aborted" || value.status === "aborted") {
|
|
2764
|
-
return INVALID;
|
|
2765
|
-
}
|
|
2766
|
-
if (key.status === "dirty" || value.status === "dirty") {
|
|
2767
|
-
status.dirty();
|
|
2768
|
-
}
|
|
2769
|
-
finalMap.set(key.value, value.value);
|
|
2770
|
-
}
|
|
2771
|
-
return { status: status.value, value: finalMap };
|
|
2772
|
-
}
|
|
2773
|
-
}
|
|
2774
|
-
}
|
|
2775
|
-
ZodMap.create = (keyType, valueType, params) => {
|
|
2776
|
-
return new ZodMap({
|
|
2777
|
-
valueType,
|
|
2778
|
-
keyType,
|
|
2779
|
-
typeName: ZodFirstPartyTypeKind.ZodMap,
|
|
2780
|
-
...processCreateParams(params),
|
|
2781
|
-
});
|
|
2782
|
-
};
|
|
2783
|
-
class ZodSet extends ZodType {
|
|
2784
|
-
_parse(input) {
|
|
2785
|
-
const { status, ctx } = this._processInputParams(input);
|
|
2786
|
-
if (ctx.parsedType !== ZodParsedType.set) {
|
|
2787
|
-
addIssueToContext(ctx, {
|
|
2788
|
-
code: ZodIssueCode.invalid_type,
|
|
2789
|
-
expected: ZodParsedType.set,
|
|
2790
|
-
received: ctx.parsedType,
|
|
2791
|
-
});
|
|
2792
|
-
return INVALID;
|
|
2793
|
-
}
|
|
2794
|
-
const def = this._def;
|
|
2795
|
-
if (def.minSize !== null) {
|
|
2796
|
-
if (ctx.data.size < def.minSize.value) {
|
|
2797
|
-
addIssueToContext(ctx, {
|
|
2798
|
-
code: ZodIssueCode.too_small,
|
|
2799
|
-
minimum: def.minSize.value,
|
|
2800
|
-
type: "set",
|
|
2801
|
-
inclusive: true,
|
|
2802
|
-
exact: false,
|
|
2803
|
-
message: def.minSize.message,
|
|
2804
|
-
});
|
|
2805
|
-
status.dirty();
|
|
2806
|
-
}
|
|
2807
|
-
}
|
|
2808
|
-
if (def.maxSize !== null) {
|
|
2809
|
-
if (ctx.data.size > def.maxSize.value) {
|
|
2810
|
-
addIssueToContext(ctx, {
|
|
2811
|
-
code: ZodIssueCode.too_big,
|
|
2812
|
-
maximum: def.maxSize.value,
|
|
2813
|
-
type: "set",
|
|
2814
|
-
inclusive: true,
|
|
2815
|
-
exact: false,
|
|
2816
|
-
message: def.maxSize.message,
|
|
2817
|
-
});
|
|
2818
|
-
status.dirty();
|
|
2819
|
-
}
|
|
2820
|
-
}
|
|
2821
|
-
const valueType = this._def.valueType;
|
|
2822
|
-
function finalizeSet(elements) {
|
|
2823
|
-
const parsedSet = new Set();
|
|
2824
|
-
for (const element of elements) {
|
|
2825
|
-
if (element.status === "aborted")
|
|
2826
|
-
return INVALID;
|
|
2827
|
-
if (element.status === "dirty")
|
|
2828
|
-
status.dirty();
|
|
2829
|
-
parsedSet.add(element.value);
|
|
2830
|
-
}
|
|
2831
|
-
return { status: status.value, value: parsedSet };
|
|
2832
|
-
}
|
|
2833
|
-
const elements = [...ctx.data.values()].map((item, i) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i)));
|
|
2834
|
-
if (ctx.common.async) {
|
|
2835
|
-
return Promise.all(elements).then((elements) => finalizeSet(elements));
|
|
2836
|
-
}
|
|
2837
|
-
else {
|
|
2838
|
-
return finalizeSet(elements);
|
|
2839
|
-
}
|
|
2840
|
-
}
|
|
2841
|
-
min(minSize, message) {
|
|
2842
|
-
return new ZodSet({
|
|
2843
|
-
...this._def,
|
|
2844
|
-
minSize: { value: minSize, message: errorUtil.toString(message) },
|
|
2845
|
-
});
|
|
2846
|
-
}
|
|
2847
|
-
max(maxSize, message) {
|
|
2848
|
-
return new ZodSet({
|
|
2849
|
-
...this._def,
|
|
2850
|
-
maxSize: { value: maxSize, message: errorUtil.toString(message) },
|
|
2851
|
-
});
|
|
2852
|
-
}
|
|
2853
|
-
size(size, message) {
|
|
2854
|
-
return this.min(size, message).max(size, message);
|
|
2855
|
-
}
|
|
2856
|
-
nonempty(message) {
|
|
2857
|
-
return this.min(1, message);
|
|
2858
|
-
}
|
|
2859
|
-
}
|
|
2860
|
-
ZodSet.create = (valueType, params) => {
|
|
2861
|
-
return new ZodSet({
|
|
2862
|
-
valueType,
|
|
2863
|
-
minSize: null,
|
|
2864
|
-
maxSize: null,
|
|
2865
|
-
typeName: ZodFirstPartyTypeKind.ZodSet,
|
|
2866
|
-
...processCreateParams(params),
|
|
2867
|
-
});
|
|
2868
|
-
};
|
|
2869
|
-
class ZodFunction extends ZodType {
|
|
2870
|
-
constructor() {
|
|
2871
|
-
super(...arguments);
|
|
2872
|
-
this.validate = this.implement;
|
|
2873
|
-
}
|
|
2874
|
-
_parse(input) {
|
|
2875
|
-
const { ctx } = this._processInputParams(input);
|
|
2876
|
-
if (ctx.parsedType !== ZodParsedType.function) {
|
|
2877
|
-
addIssueToContext(ctx, {
|
|
2878
|
-
code: ZodIssueCode.invalid_type,
|
|
2879
|
-
expected: ZodParsedType.function,
|
|
2880
|
-
received: ctx.parsedType,
|
|
2881
|
-
});
|
|
2882
|
-
return INVALID;
|
|
2883
|
-
}
|
|
2884
|
-
function makeArgsIssue(args, error) {
|
|
2885
|
-
return makeIssue({
|
|
2886
|
-
data: args,
|
|
2887
|
-
path: ctx.path,
|
|
2888
|
-
errorMaps: [ctx.common.contextualErrorMap, ctx.schemaErrorMap, getErrorMap(), errorMap].filter((x) => !!x),
|
|
2889
|
-
issueData: {
|
|
2890
|
-
code: ZodIssueCode.invalid_arguments,
|
|
2891
|
-
argumentsError: error,
|
|
2892
|
-
},
|
|
2893
|
-
});
|
|
2894
|
-
}
|
|
2895
|
-
function makeReturnsIssue(returns, error) {
|
|
2896
|
-
return makeIssue({
|
|
2897
|
-
data: returns,
|
|
2898
|
-
path: ctx.path,
|
|
2899
|
-
errorMaps: [ctx.common.contextualErrorMap, ctx.schemaErrorMap, getErrorMap(), errorMap].filter((x) => !!x),
|
|
2900
|
-
issueData: {
|
|
2901
|
-
code: ZodIssueCode.invalid_return_type,
|
|
2902
|
-
returnTypeError: error,
|
|
2903
|
-
},
|
|
2904
|
-
});
|
|
2905
|
-
}
|
|
2906
|
-
const params = { errorMap: ctx.common.contextualErrorMap };
|
|
2907
|
-
const fn = ctx.data;
|
|
2908
|
-
if (this._def.returns instanceof ZodPromise) {
|
|
2909
|
-
// Would love a way to avoid disabling this rule, but we need
|
|
2910
|
-
// an alias (using an arrow function was what caused 2651).
|
|
2911
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
2912
|
-
const me = this;
|
|
2913
|
-
return OK(async function (...args) {
|
|
2914
|
-
const error = new ZodError([]);
|
|
2915
|
-
const parsedArgs = await me._def.args.parseAsync(args, params).catch((e) => {
|
|
2916
|
-
error.addIssue(makeArgsIssue(args, e));
|
|
2917
|
-
throw error;
|
|
2918
|
-
});
|
|
2919
|
-
const result = await Reflect.apply(fn, this, parsedArgs);
|
|
2920
|
-
const parsedReturns = await me._def.returns._def.type
|
|
2921
|
-
.parseAsync(result, params)
|
|
2922
|
-
.catch((e) => {
|
|
2923
|
-
error.addIssue(makeReturnsIssue(result, e));
|
|
2924
|
-
throw error;
|
|
2925
|
-
});
|
|
2926
|
-
return parsedReturns;
|
|
2927
|
-
});
|
|
2928
|
-
}
|
|
2929
|
-
else {
|
|
2930
|
-
// Would love a way to avoid disabling this rule, but we need
|
|
2931
|
-
// an alias (using an arrow function was what caused 2651).
|
|
2932
|
-
// eslint-disable-next-line @typescript-eslint/no-this-alias
|
|
2933
|
-
const me = this;
|
|
2934
|
-
return OK(function (...args) {
|
|
2935
|
-
const parsedArgs = me._def.args.safeParse(args, params);
|
|
2936
|
-
if (!parsedArgs.success) {
|
|
2937
|
-
throw new ZodError([makeArgsIssue(args, parsedArgs.error)]);
|
|
2938
|
-
}
|
|
2939
|
-
const result = Reflect.apply(fn, this, parsedArgs.data);
|
|
2940
|
-
const parsedReturns = me._def.returns.safeParse(result, params);
|
|
2941
|
-
if (!parsedReturns.success) {
|
|
2942
|
-
throw new ZodError([makeReturnsIssue(result, parsedReturns.error)]);
|
|
2943
|
-
}
|
|
2944
|
-
return parsedReturns.data;
|
|
2945
|
-
});
|
|
2946
|
-
}
|
|
2947
|
-
}
|
|
2948
|
-
parameters() {
|
|
2949
|
-
return this._def.args;
|
|
2950
|
-
}
|
|
2951
|
-
returnType() {
|
|
2952
|
-
return this._def.returns;
|
|
2953
|
-
}
|
|
2954
|
-
args(...items) {
|
|
2955
|
-
return new ZodFunction({
|
|
2956
|
-
...this._def,
|
|
2957
|
-
args: ZodTuple.create(items).rest(ZodUnknown.create()),
|
|
2958
|
-
});
|
|
2959
|
-
}
|
|
2960
|
-
returns(returnType) {
|
|
2961
|
-
return new ZodFunction({
|
|
2962
|
-
...this._def,
|
|
2963
|
-
returns: returnType,
|
|
2964
|
-
});
|
|
2965
|
-
}
|
|
2966
|
-
implement(func) {
|
|
2967
|
-
const validatedFunc = this.parse(func);
|
|
2968
|
-
return validatedFunc;
|
|
2969
|
-
}
|
|
2970
|
-
strictImplement(func) {
|
|
2971
|
-
const validatedFunc = this.parse(func);
|
|
2972
|
-
return validatedFunc;
|
|
2973
|
-
}
|
|
2974
|
-
static create(args, returns, params) {
|
|
2975
|
-
return new ZodFunction({
|
|
2976
|
-
args: (args ? args : ZodTuple.create([]).rest(ZodUnknown.create())),
|
|
2977
|
-
returns: returns || ZodUnknown.create(),
|
|
2978
|
-
typeName: ZodFirstPartyTypeKind.ZodFunction,
|
|
2979
|
-
...processCreateParams(params),
|
|
2980
|
-
});
|
|
2981
|
-
}
|
|
2982
|
-
}
|
|
2983
|
-
class ZodLazy extends ZodType {
|
|
2984
|
-
get schema() {
|
|
2985
|
-
return this._def.getter();
|
|
2986
|
-
}
|
|
2987
|
-
_parse(input) {
|
|
2988
|
-
const { ctx } = this._processInputParams(input);
|
|
2989
|
-
const lazySchema = this._def.getter();
|
|
2990
|
-
return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx });
|
|
2991
|
-
}
|
|
2992
|
-
}
|
|
2993
|
-
ZodLazy.create = (getter, params) => {
|
|
2994
|
-
return new ZodLazy({
|
|
2995
|
-
getter: getter,
|
|
2996
|
-
typeName: ZodFirstPartyTypeKind.ZodLazy,
|
|
2997
|
-
...processCreateParams(params),
|
|
2998
|
-
});
|
|
2999
|
-
};
|
|
3000
|
-
class ZodLiteral extends ZodType {
|
|
3001
|
-
_parse(input) {
|
|
3002
|
-
if (input.data !== this._def.value) {
|
|
3003
|
-
const ctx = this._getOrReturnCtx(input);
|
|
3004
|
-
addIssueToContext(ctx, {
|
|
3005
|
-
received: ctx.data,
|
|
3006
|
-
code: ZodIssueCode.invalid_literal,
|
|
3007
|
-
expected: this._def.value,
|
|
3008
|
-
});
|
|
3009
|
-
return INVALID;
|
|
3010
|
-
}
|
|
3011
|
-
return { status: "valid", value: input.data };
|
|
3012
|
-
}
|
|
3013
|
-
get value() {
|
|
3014
|
-
return this._def.value;
|
|
3015
|
-
}
|
|
3016
|
-
}
|
|
3017
|
-
ZodLiteral.create = (value, params) => {
|
|
3018
|
-
return new ZodLiteral({
|
|
3019
|
-
value: value,
|
|
3020
|
-
typeName: ZodFirstPartyTypeKind.ZodLiteral,
|
|
3021
|
-
...processCreateParams(params),
|
|
3022
|
-
});
|
|
3023
|
-
};
|
|
3024
|
-
function createZodEnum(values, params) {
|
|
3025
|
-
return new ZodEnum({
|
|
3026
|
-
values,
|
|
3027
|
-
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
3028
|
-
...processCreateParams(params),
|
|
3029
|
-
});
|
|
3030
|
-
}
|
|
3031
|
-
class ZodEnum extends ZodType {
|
|
3032
|
-
_parse(input) {
|
|
3033
|
-
if (typeof input.data !== "string") {
|
|
3034
|
-
const ctx = this._getOrReturnCtx(input);
|
|
3035
|
-
const expectedValues = this._def.values;
|
|
3036
|
-
addIssueToContext(ctx, {
|
|
3037
|
-
expected: util.joinValues(expectedValues),
|
|
3038
|
-
received: ctx.parsedType,
|
|
3039
|
-
code: ZodIssueCode.invalid_type,
|
|
3040
|
-
});
|
|
3041
|
-
return INVALID;
|
|
3042
|
-
}
|
|
3043
|
-
if (!this._cache) {
|
|
3044
|
-
this._cache = new Set(this._def.values);
|
|
3045
|
-
}
|
|
3046
|
-
if (!this._cache.has(input.data)) {
|
|
3047
|
-
const ctx = this._getOrReturnCtx(input);
|
|
3048
|
-
const expectedValues = this._def.values;
|
|
3049
|
-
addIssueToContext(ctx, {
|
|
3050
|
-
received: ctx.data,
|
|
3051
|
-
code: ZodIssueCode.invalid_enum_value,
|
|
3052
|
-
options: expectedValues,
|
|
3053
|
-
});
|
|
3054
|
-
return INVALID;
|
|
3055
|
-
}
|
|
3056
|
-
return OK(input.data);
|
|
3057
|
-
}
|
|
3058
|
-
get options() {
|
|
3059
|
-
return this._def.values;
|
|
3060
|
-
}
|
|
3061
|
-
get enum() {
|
|
3062
|
-
const enumValues = {};
|
|
3063
|
-
for (const val of this._def.values) {
|
|
3064
|
-
enumValues[val] = val;
|
|
3065
|
-
}
|
|
3066
|
-
return enumValues;
|
|
3067
|
-
}
|
|
3068
|
-
get Values() {
|
|
3069
|
-
const enumValues = {};
|
|
3070
|
-
for (const val of this._def.values) {
|
|
3071
|
-
enumValues[val] = val;
|
|
3072
|
-
}
|
|
3073
|
-
return enumValues;
|
|
3074
|
-
}
|
|
3075
|
-
get Enum() {
|
|
3076
|
-
const enumValues = {};
|
|
3077
|
-
for (const val of this._def.values) {
|
|
3078
|
-
enumValues[val] = val;
|
|
3079
|
-
}
|
|
3080
|
-
return enumValues;
|
|
3081
|
-
}
|
|
3082
|
-
extract(values, newDef = this._def) {
|
|
3083
|
-
return ZodEnum.create(values, {
|
|
3084
|
-
...this._def,
|
|
3085
|
-
...newDef,
|
|
3086
|
-
});
|
|
3087
|
-
}
|
|
3088
|
-
exclude(values, newDef = this._def) {
|
|
3089
|
-
return ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
|
|
3090
|
-
...this._def,
|
|
3091
|
-
...newDef,
|
|
3092
|
-
});
|
|
3093
|
-
}
|
|
3094
|
-
}
|
|
3095
|
-
ZodEnum.create = createZodEnum;
|
|
3096
|
-
class ZodNativeEnum extends ZodType {
|
|
3097
|
-
_parse(input) {
|
|
3098
|
-
const nativeEnumValues = util.getValidEnumValues(this._def.values);
|
|
3099
|
-
const ctx = this._getOrReturnCtx(input);
|
|
3100
|
-
if (ctx.parsedType !== ZodParsedType.string && ctx.parsedType !== ZodParsedType.number) {
|
|
3101
|
-
const expectedValues = util.objectValues(nativeEnumValues);
|
|
3102
|
-
addIssueToContext(ctx, {
|
|
3103
|
-
expected: util.joinValues(expectedValues),
|
|
3104
|
-
received: ctx.parsedType,
|
|
3105
|
-
code: ZodIssueCode.invalid_type,
|
|
3106
|
-
});
|
|
3107
|
-
return INVALID;
|
|
3108
|
-
}
|
|
3109
|
-
if (!this._cache) {
|
|
3110
|
-
this._cache = new Set(util.getValidEnumValues(this._def.values));
|
|
3111
|
-
}
|
|
3112
|
-
if (!this._cache.has(input.data)) {
|
|
3113
|
-
const expectedValues = util.objectValues(nativeEnumValues);
|
|
3114
|
-
addIssueToContext(ctx, {
|
|
3115
|
-
received: ctx.data,
|
|
3116
|
-
code: ZodIssueCode.invalid_enum_value,
|
|
3117
|
-
options: expectedValues,
|
|
3118
|
-
});
|
|
3119
|
-
return INVALID;
|
|
3120
|
-
}
|
|
3121
|
-
return OK(input.data);
|
|
3122
|
-
}
|
|
3123
|
-
get enum() {
|
|
3124
|
-
return this._def.values;
|
|
3125
|
-
}
|
|
3126
|
-
}
|
|
3127
|
-
ZodNativeEnum.create = (values, params) => {
|
|
3128
|
-
return new ZodNativeEnum({
|
|
3129
|
-
values: values,
|
|
3130
|
-
typeName: ZodFirstPartyTypeKind.ZodNativeEnum,
|
|
3131
|
-
...processCreateParams(params),
|
|
3132
|
-
});
|
|
3133
|
-
};
|
|
3134
|
-
class ZodPromise extends ZodType {
|
|
3135
|
-
unwrap() {
|
|
3136
|
-
return this._def.type;
|
|
3137
|
-
}
|
|
3138
|
-
_parse(input) {
|
|
3139
|
-
const { ctx } = this._processInputParams(input);
|
|
3140
|
-
if (ctx.parsedType !== ZodParsedType.promise && ctx.common.async === false) {
|
|
3141
|
-
addIssueToContext(ctx, {
|
|
3142
|
-
code: ZodIssueCode.invalid_type,
|
|
3143
|
-
expected: ZodParsedType.promise,
|
|
3144
|
-
received: ctx.parsedType,
|
|
3145
|
-
});
|
|
3146
|
-
return INVALID;
|
|
3147
|
-
}
|
|
3148
|
-
const promisified = ctx.parsedType === ZodParsedType.promise ? ctx.data : Promise.resolve(ctx.data);
|
|
3149
|
-
return OK(promisified.then((data) => {
|
|
3150
|
-
return this._def.type.parseAsync(data, {
|
|
3151
|
-
path: ctx.path,
|
|
3152
|
-
errorMap: ctx.common.contextualErrorMap,
|
|
3153
|
-
});
|
|
3154
|
-
}));
|
|
3155
|
-
}
|
|
3156
|
-
}
|
|
3157
|
-
ZodPromise.create = (schema, params) => {
|
|
3158
|
-
return new ZodPromise({
|
|
3159
|
-
type: schema,
|
|
3160
|
-
typeName: ZodFirstPartyTypeKind.ZodPromise,
|
|
3161
|
-
...processCreateParams(params),
|
|
3162
|
-
});
|
|
3163
|
-
};
|
|
3164
|
-
class ZodEffects extends ZodType {
|
|
3165
|
-
innerType() {
|
|
3166
|
-
return this._def.schema;
|
|
3167
|
-
}
|
|
3168
|
-
sourceType() {
|
|
3169
|
-
return this._def.schema._def.typeName === ZodFirstPartyTypeKind.ZodEffects
|
|
3170
|
-
? this._def.schema.sourceType()
|
|
3171
|
-
: this._def.schema;
|
|
3172
|
-
}
|
|
3173
|
-
_parse(input) {
|
|
3174
|
-
const { status, ctx } = this._processInputParams(input);
|
|
3175
|
-
const effect = this._def.effect || null;
|
|
3176
|
-
const checkCtx = {
|
|
3177
|
-
addIssue: (arg) => {
|
|
3178
|
-
addIssueToContext(ctx, arg);
|
|
3179
|
-
if (arg.fatal) {
|
|
3180
|
-
status.abort();
|
|
3181
|
-
}
|
|
3182
|
-
else {
|
|
3183
|
-
status.dirty();
|
|
3184
|
-
}
|
|
3185
|
-
},
|
|
3186
|
-
get path() {
|
|
3187
|
-
return ctx.path;
|
|
3188
|
-
},
|
|
3189
|
-
};
|
|
3190
|
-
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
|
|
3191
|
-
if (effect.type === "preprocess") {
|
|
3192
|
-
const processed = effect.transform(ctx.data, checkCtx);
|
|
3193
|
-
if (ctx.common.async) {
|
|
3194
|
-
return Promise.resolve(processed).then(async (processed) => {
|
|
3195
|
-
if (status.value === "aborted")
|
|
3196
|
-
return INVALID;
|
|
3197
|
-
const result = await this._def.schema._parseAsync({
|
|
3198
|
-
data: processed,
|
|
3199
|
-
path: ctx.path,
|
|
3200
|
-
parent: ctx,
|
|
3201
|
-
});
|
|
3202
|
-
if (result.status === "aborted")
|
|
3203
|
-
return INVALID;
|
|
3204
|
-
if (result.status === "dirty")
|
|
3205
|
-
return DIRTY(result.value);
|
|
3206
|
-
if (status.value === "dirty")
|
|
3207
|
-
return DIRTY(result.value);
|
|
3208
|
-
return result;
|
|
3209
|
-
});
|
|
3210
|
-
}
|
|
3211
|
-
else {
|
|
3212
|
-
if (status.value === "aborted")
|
|
3213
|
-
return INVALID;
|
|
3214
|
-
const result = this._def.schema._parseSync({
|
|
3215
|
-
data: processed,
|
|
3216
|
-
path: ctx.path,
|
|
3217
|
-
parent: ctx,
|
|
3218
|
-
});
|
|
3219
|
-
if (result.status === "aborted")
|
|
3220
|
-
return INVALID;
|
|
3221
|
-
if (result.status === "dirty")
|
|
3222
|
-
return DIRTY(result.value);
|
|
3223
|
-
if (status.value === "dirty")
|
|
3224
|
-
return DIRTY(result.value);
|
|
3225
|
-
return result;
|
|
3226
|
-
}
|
|
3227
|
-
}
|
|
3228
|
-
if (effect.type === "refinement") {
|
|
3229
|
-
const executeRefinement = (acc) => {
|
|
3230
|
-
const result = effect.refinement(acc, checkCtx);
|
|
3231
|
-
if (ctx.common.async) {
|
|
3232
|
-
return Promise.resolve(result);
|
|
3233
|
-
}
|
|
3234
|
-
if (result instanceof Promise) {
|
|
3235
|
-
throw new Error("Async refinement encountered during synchronous parse operation. Use .parseAsync instead.");
|
|
3236
|
-
}
|
|
3237
|
-
return acc;
|
|
3238
|
-
};
|
|
3239
|
-
if (ctx.common.async === false) {
|
|
3240
|
-
const inner = this._def.schema._parseSync({
|
|
3241
|
-
data: ctx.data,
|
|
3242
|
-
path: ctx.path,
|
|
3243
|
-
parent: ctx,
|
|
3244
|
-
});
|
|
3245
|
-
if (inner.status === "aborted")
|
|
3246
|
-
return INVALID;
|
|
3247
|
-
if (inner.status === "dirty")
|
|
3248
|
-
status.dirty();
|
|
3249
|
-
// return value is ignored
|
|
3250
|
-
executeRefinement(inner.value);
|
|
3251
|
-
return { status: status.value, value: inner.value };
|
|
3252
|
-
}
|
|
3253
|
-
else {
|
|
3254
|
-
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((inner) => {
|
|
3255
|
-
if (inner.status === "aborted")
|
|
3256
|
-
return INVALID;
|
|
3257
|
-
if (inner.status === "dirty")
|
|
3258
|
-
status.dirty();
|
|
3259
|
-
return executeRefinement(inner.value).then(() => {
|
|
3260
|
-
return { status: status.value, value: inner.value };
|
|
3261
|
-
});
|
|
3262
|
-
});
|
|
3263
|
-
}
|
|
3264
|
-
}
|
|
3265
|
-
if (effect.type === "transform") {
|
|
3266
|
-
if (ctx.common.async === false) {
|
|
3267
|
-
const base = this._def.schema._parseSync({
|
|
3268
|
-
data: ctx.data,
|
|
3269
|
-
path: ctx.path,
|
|
3270
|
-
parent: ctx,
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
5
|
+
var ArraySubject = class extends BehaviorSubject {
|
|
6
|
+
static {
|
|
7
|
+
__name(this, "ArraySubject");
|
|
8
|
+
}
|
|
9
|
+
_items = [];
|
|
10
|
+
constructor(items = []) {
|
|
11
|
+
super({
|
|
12
|
+
type: "init",
|
|
13
|
+
items
|
|
14
|
+
});
|
|
15
|
+
this.createProxy(items);
|
|
16
|
+
}
|
|
17
|
+
createProxy(items) {
|
|
18
|
+
this._items = new Proxy(items, {
|
|
19
|
+
get: /* @__PURE__ */ __name((target, prop, receiver) => {
|
|
20
|
+
const origMethod = target[prop];
|
|
21
|
+
if (typeof origMethod === "function") {
|
|
22
|
+
return (...args) => {
|
|
23
|
+
let changeType = "update";
|
|
24
|
+
let index = void 0;
|
|
25
|
+
let isMutateFn = false;
|
|
26
|
+
let itemsToEmit = [];
|
|
27
|
+
let changeSplice = true;
|
|
28
|
+
switch (prop) {
|
|
29
|
+
case "push":
|
|
30
|
+
index = target.length;
|
|
31
|
+
changeType = "add";
|
|
32
|
+
isMutateFn = true;
|
|
33
|
+
break;
|
|
34
|
+
case "pop":
|
|
35
|
+
index = target.length - 1;
|
|
36
|
+
changeType = "remove";
|
|
37
|
+
isMutateFn = true;
|
|
38
|
+
break;
|
|
39
|
+
case "unshift":
|
|
40
|
+
index = 0;
|
|
41
|
+
changeType = "add";
|
|
42
|
+
isMutateFn = true;
|
|
43
|
+
break;
|
|
44
|
+
case "shift":
|
|
45
|
+
index = 0;
|
|
46
|
+
changeType = "remove";
|
|
47
|
+
isMutateFn = true;
|
|
48
|
+
break;
|
|
49
|
+
case "splice":
|
|
50
|
+
index = args[0];
|
|
51
|
+
const deleteCount = args[1];
|
|
52
|
+
const newItems = args.slice(2);
|
|
53
|
+
itemsToEmit = newItems;
|
|
54
|
+
if (deleteCount > 0 && newItems.length === 0) {
|
|
55
|
+
changeType = "remove";
|
|
56
|
+
} else if (deleteCount === 0 && newItems.length > 0) {
|
|
57
|
+
changeType = "add";
|
|
58
|
+
} else if (deleteCount === 0 && newItems.length === 0) {
|
|
59
|
+
changeSplice = false;
|
|
60
|
+
} else {
|
|
61
|
+
changeType = "update";
|
|
62
|
+
}
|
|
63
|
+
isMutateFn = true;
|
|
64
|
+
break;
|
|
65
|
+
}
|
|
66
|
+
const result = origMethod.apply(target, args);
|
|
67
|
+
if (isMutateFn && changeSplice) {
|
|
68
|
+
if (prop === "splice") {
|
|
69
|
+
this.next({
|
|
70
|
+
type: changeType,
|
|
71
|
+
index,
|
|
72
|
+
items: itemsToEmit
|
|
3271
73
|
});
|
|
3272
|
-
|
|
3273
|
-
|
|
3274
|
-
|
|
3275
|
-
|
|
3276
|
-
|
|
3277
|
-
}
|
|
3278
|
-
return { status: status.value, value: result };
|
|
3279
|
-
}
|
|
3280
|
-
else {
|
|
3281
|
-
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((base) => {
|
|
3282
|
-
if (!isValid(base))
|
|
3283
|
-
return INVALID;
|
|
3284
|
-
return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({
|
|
3285
|
-
status: status.value,
|
|
3286
|
-
value: result,
|
|
3287
|
-
}));
|
|
74
|
+
} else {
|
|
75
|
+
this.next({
|
|
76
|
+
type: changeType,
|
|
77
|
+
index,
|
|
78
|
+
items: args
|
|
3288
79
|
});
|
|
3289
|
-
|
|
3290
|
-
|
|
3291
|
-
|
|
3292
|
-
|
|
3293
|
-
}
|
|
3294
|
-
|
|
3295
|
-
|
|
3296
|
-
|
|
3297
|
-
|
|
3298
|
-
|
|
3299
|
-
|
|
3300
|
-
|
|
3301
|
-
|
|
3302
|
-
|
|
3303
|
-
|
|
3304
|
-
|
|
3305
|
-
effect: { type: "preprocess", transform: preprocess },
|
|
3306
|
-
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
3307
|
-
...processCreateParams(params),
|
|
3308
|
-
});
|
|
3309
|
-
};
|
|
3310
|
-
class ZodOptional extends ZodType {
|
|
3311
|
-
_parse(input) {
|
|
3312
|
-
const parsedType = this._getType(input);
|
|
3313
|
-
if (parsedType === ZodParsedType.undefined) {
|
|
3314
|
-
return OK(undefined);
|
|
3315
|
-
}
|
|
3316
|
-
return this._def.innerType._parse(input);
|
|
3317
|
-
}
|
|
3318
|
-
unwrap() {
|
|
3319
|
-
return this._def.innerType;
|
|
3320
|
-
}
|
|
3321
|
-
}
|
|
3322
|
-
ZodOptional.create = (type, params) => {
|
|
3323
|
-
return new ZodOptional({
|
|
3324
|
-
innerType: type,
|
|
3325
|
-
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
3326
|
-
...processCreateParams(params),
|
|
3327
|
-
});
|
|
3328
|
-
};
|
|
3329
|
-
class ZodNullable extends ZodType {
|
|
3330
|
-
_parse(input) {
|
|
3331
|
-
const parsedType = this._getType(input);
|
|
3332
|
-
if (parsedType === ZodParsedType.null) {
|
|
3333
|
-
return OK(null);
|
|
3334
|
-
}
|
|
3335
|
-
return this._def.innerType._parse(input);
|
|
3336
|
-
}
|
|
3337
|
-
unwrap() {
|
|
3338
|
-
return this._def.innerType;
|
|
3339
|
-
}
|
|
3340
|
-
}
|
|
3341
|
-
ZodNullable.create = (type, params) => {
|
|
3342
|
-
return new ZodNullable({
|
|
3343
|
-
innerType: type,
|
|
3344
|
-
typeName: ZodFirstPartyTypeKind.ZodNullable,
|
|
3345
|
-
...processCreateParams(params),
|
|
3346
|
-
});
|
|
3347
|
-
};
|
|
3348
|
-
class ZodDefault extends ZodType {
|
|
3349
|
-
_parse(input) {
|
|
3350
|
-
const { ctx } = this._processInputParams(input);
|
|
3351
|
-
let data = ctx.data;
|
|
3352
|
-
if (ctx.parsedType === ZodParsedType.undefined) {
|
|
3353
|
-
data = this._def.defaultValue();
|
|
3354
|
-
}
|
|
3355
|
-
return this._def.innerType._parse({
|
|
3356
|
-
data,
|
|
3357
|
-
path: ctx.path,
|
|
3358
|
-
parent: ctx,
|
|
3359
|
-
});
|
|
3360
|
-
}
|
|
3361
|
-
removeDefault() {
|
|
3362
|
-
return this._def.innerType;
|
|
3363
|
-
}
|
|
3364
|
-
}
|
|
3365
|
-
ZodDefault.create = (type, params) => {
|
|
3366
|
-
return new ZodDefault({
|
|
3367
|
-
innerType: type,
|
|
3368
|
-
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
3369
|
-
defaultValue: typeof params.default === "function" ? params.default : () => params.default,
|
|
3370
|
-
...processCreateParams(params),
|
|
3371
|
-
});
|
|
3372
|
-
};
|
|
3373
|
-
class ZodCatch extends ZodType {
|
|
3374
|
-
_parse(input) {
|
|
3375
|
-
const { ctx } = this._processInputParams(input);
|
|
3376
|
-
// newCtx is used to not collect issues from inner types in ctx
|
|
3377
|
-
const newCtx = {
|
|
3378
|
-
...ctx,
|
|
3379
|
-
common: {
|
|
3380
|
-
...ctx.common,
|
|
3381
|
-
issues: [],
|
|
3382
|
-
},
|
|
3383
|
-
};
|
|
3384
|
-
const result = this._def.innerType._parse({
|
|
3385
|
-
data: newCtx.data,
|
|
3386
|
-
path: newCtx.path,
|
|
3387
|
-
parent: {
|
|
3388
|
-
...newCtx,
|
|
3389
|
-
},
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
return Reflect.get(target, prop, receiver);
|
|
86
|
+
}, "get"),
|
|
87
|
+
set: /* @__PURE__ */ __name((target, prop, value) => {
|
|
88
|
+
const index = !isNaN(Number(prop)) ? Number(prop) : void 0;
|
|
89
|
+
target[prop] = value;
|
|
90
|
+
this.next({
|
|
91
|
+
type: "update",
|
|
92
|
+
index,
|
|
93
|
+
items: [
|
|
94
|
+
value
|
|
95
|
+
]
|
|
3390
96
|
});
|
|
3391
|
-
|
|
3392
|
-
|
|
3393
|
-
|
|
3394
|
-
|
|
3395
|
-
|
|
3396
|
-
|
|
3397
|
-
|
|
3398
|
-
|
|
3399
|
-
|
|
3400
|
-
|
|
3401
|
-
|
|
3402
|
-
|
|
3403
|
-
|
|
3404
|
-
|
|
3405
|
-
|
|
3406
|
-
|
|
3407
|
-
|
|
3408
|
-
|
|
3409
|
-
|
|
3410
|
-
|
|
3411
|
-
|
|
3412
|
-
|
|
3413
|
-
|
|
3414
|
-
|
|
3415
|
-
|
|
3416
|
-
|
|
3417
|
-
|
|
3418
|
-
|
|
3419
|
-
|
|
3420
|
-
|
|
3421
|
-
return
|
|
3422
|
-
|
|
3423
|
-
|
|
3424
|
-
|
|
3425
|
-
|
|
3426
|
-
|
|
3427
|
-
|
|
3428
|
-
|
|
3429
|
-
|
|
3430
|
-
|
|
3431
|
-
};
|
|
3432
|
-
class ZodNaN extends ZodType {
|
|
3433
|
-
_parse(input) {
|
|
3434
|
-
const parsedType = this._getType(input);
|
|
3435
|
-
if (parsedType !== ZodParsedType.nan) {
|
|
3436
|
-
const ctx = this._getOrReturnCtx(input);
|
|
3437
|
-
addIssueToContext(ctx, {
|
|
3438
|
-
code: ZodIssueCode.invalid_type,
|
|
3439
|
-
expected: ZodParsedType.nan,
|
|
3440
|
-
received: ctx.parsedType,
|
|
3441
|
-
});
|
|
3442
|
-
return INVALID;
|
|
3443
|
-
}
|
|
3444
|
-
return { status: "valid", value: input.data };
|
|
3445
|
-
}
|
|
3446
|
-
}
|
|
3447
|
-
ZodNaN.create = (params) => {
|
|
3448
|
-
return new ZodNaN({
|
|
3449
|
-
typeName: ZodFirstPartyTypeKind.ZodNaN,
|
|
3450
|
-
...processCreateParams(params),
|
|
3451
|
-
});
|
|
3452
|
-
};
|
|
3453
|
-
const BRAND = Symbol("zod_brand");
|
|
3454
|
-
class ZodBranded extends ZodType {
|
|
3455
|
-
_parse(input) {
|
|
3456
|
-
const { ctx } = this._processInputParams(input);
|
|
3457
|
-
const data = ctx.data;
|
|
3458
|
-
return this._def.type._parse({
|
|
3459
|
-
data,
|
|
3460
|
-
path: ctx.path,
|
|
3461
|
-
parent: ctx,
|
|
97
|
+
return true;
|
|
98
|
+
}, "set")
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
get items() {
|
|
102
|
+
return this._items;
|
|
103
|
+
}
|
|
104
|
+
set items(newItems) {
|
|
105
|
+
this.createProxy(newItems);
|
|
106
|
+
this.next({
|
|
107
|
+
type: "reset",
|
|
108
|
+
items: newItems
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
var ObjectSubject = class extends BehaviorSubject {
|
|
113
|
+
static {
|
|
114
|
+
__name(this, "ObjectSubject");
|
|
115
|
+
}
|
|
116
|
+
_obj;
|
|
117
|
+
constructor(obj = {}) {
|
|
118
|
+
super({
|
|
119
|
+
type: "init",
|
|
120
|
+
value: obj
|
|
121
|
+
});
|
|
122
|
+
this.createProxy(obj);
|
|
123
|
+
}
|
|
124
|
+
createProxy(obj) {
|
|
125
|
+
this._obj = new Proxy(obj, {
|
|
126
|
+
get: /* @__PURE__ */ __name((target, prop, receiver) => {
|
|
127
|
+
return Reflect.get(target, prop, receiver);
|
|
128
|
+
}, "get"),
|
|
129
|
+
set: /* @__PURE__ */ __name((target, prop, value, receiver) => {
|
|
130
|
+
const key = prop;
|
|
131
|
+
const changeType = key in target ? "update" : "add";
|
|
132
|
+
target[key] = value;
|
|
133
|
+
this.next({
|
|
134
|
+
type: changeType,
|
|
135
|
+
key,
|
|
136
|
+
value
|
|
3462
137
|
});
|
|
3463
|
-
|
|
3464
|
-
|
|
3465
|
-
|
|
3466
|
-
|
|
3467
|
-
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
parent: ctx,
|
|
3477
|
-
});
|
|
3478
|
-
if (inResult.status === "aborted")
|
|
3479
|
-
return INVALID;
|
|
3480
|
-
if (inResult.status === "dirty") {
|
|
3481
|
-
status.dirty();
|
|
3482
|
-
return DIRTY(inResult.value);
|
|
3483
|
-
}
|
|
3484
|
-
else {
|
|
3485
|
-
return this._def.out._parseAsync({
|
|
3486
|
-
data: inResult.value,
|
|
3487
|
-
path: ctx.path,
|
|
3488
|
-
parent: ctx,
|
|
3489
|
-
});
|
|
3490
|
-
}
|
|
3491
|
-
};
|
|
3492
|
-
return handleAsync();
|
|
3493
|
-
}
|
|
3494
|
-
else {
|
|
3495
|
-
const inResult = this._def.in._parseSync({
|
|
3496
|
-
data: ctx.data,
|
|
3497
|
-
path: ctx.path,
|
|
3498
|
-
parent: ctx,
|
|
3499
|
-
});
|
|
3500
|
-
if (inResult.status === "aborted")
|
|
3501
|
-
return INVALID;
|
|
3502
|
-
if (inResult.status === "dirty") {
|
|
3503
|
-
status.dirty();
|
|
3504
|
-
return {
|
|
3505
|
-
status: "dirty",
|
|
3506
|
-
value: inResult.value,
|
|
3507
|
-
};
|
|
3508
|
-
}
|
|
3509
|
-
else {
|
|
3510
|
-
return this._def.out._parseSync({
|
|
3511
|
-
data: inResult.value,
|
|
3512
|
-
path: ctx.path,
|
|
3513
|
-
parent: ctx,
|
|
3514
|
-
});
|
|
3515
|
-
}
|
|
138
|
+
return true;
|
|
139
|
+
}, "set"),
|
|
140
|
+
deleteProperty: /* @__PURE__ */ __name((target, prop) => {
|
|
141
|
+
const key = prop;
|
|
142
|
+
if (key in target) {
|
|
143
|
+
const value = target[key];
|
|
144
|
+
delete target[key];
|
|
145
|
+
this.next({
|
|
146
|
+
type: "remove",
|
|
147
|
+
key,
|
|
148
|
+
value
|
|
149
|
+
});
|
|
150
|
+
return true;
|
|
3516
151
|
}
|
|
3517
|
-
|
|
3518
|
-
|
|
3519
|
-
|
|
3520
|
-
|
|
3521
|
-
|
|
3522
|
-
|
|
3523
|
-
|
|
3524
|
-
|
|
3525
|
-
|
|
3526
|
-
|
|
3527
|
-
|
|
3528
|
-
|
|
3529
|
-
|
|
3530
|
-
|
|
3531
|
-
|
|
3532
|
-
|
|
3533
|
-
|
|
3534
|
-
|
|
3535
|
-
|
|
3536
|
-
|
|
3537
|
-
|
|
3538
|
-
|
|
3539
|
-
|
|
3540
|
-
}
|
|
3541
|
-
|
|
3542
|
-
|
|
3543
|
-
|
|
3544
|
-
|
|
3545
|
-
|
|
3546
|
-
|
|
3547
|
-
|
|
3548
|
-
|
|
3549
|
-
|
|
3550
|
-
|
|
3551
|
-
|
|
3552
|
-
|
|
3553
|
-
|
|
3554
|
-
|
|
3555
|
-
|
|
3556
|
-
|
|
3557
|
-
|
|
3558
|
-
|
|
3559
|
-
|
|
3560
|
-
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
|
|
3565
|
-
|
|
3566
|
-
|
|
3567
|
-
|
|
3568
|
-
|
|
3569
|
-
|
|
3570
|
-
|
|
3571
|
-
|
|
3572
|
-
|
|
3573
|
-
|
|
3574
|
-
|
|
3575
|
-
|
|
3576
|
-
|
|
3577
|
-
|
|
3578
|
-
|
|
3579
|
-
|
|
3580
|
-
|
|
3581
|
-
|
|
3582
|
-
|
|
3583
|
-
|
|
3584
|
-
|
|
3585
|
-
|
|
3586
|
-
|
|
3587
|
-
|
|
3588
|
-
|
|
3589
|
-
|
|
3590
|
-
|
|
3591
|
-
|
|
152
|
+
return false;
|
|
153
|
+
}, "deleteProperty")
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
get obj() {
|
|
157
|
+
return this._obj;
|
|
158
|
+
}
|
|
159
|
+
set obj(newObj) {
|
|
160
|
+
this.createProxy(newObj);
|
|
161
|
+
this.next({
|
|
162
|
+
type: "reset",
|
|
163
|
+
value: newObj
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
var getGlobalReactiveStore = /* @__PURE__ */ __name(() => {
|
|
168
|
+
const globalKey = "__REACTIVE_STORE__";
|
|
169
|
+
if (typeof globalThis !== "undefined") {
|
|
170
|
+
if (!globalThis[globalKey]) {
|
|
171
|
+
globalThis[globalKey] = {
|
|
172
|
+
currentDependencyTracker: null,
|
|
173
|
+
currentSubscriptionsTracker: null
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
return globalThis[globalKey];
|
|
177
|
+
}
|
|
178
|
+
let globalObj;
|
|
179
|
+
if (typeof window !== "undefined") {
|
|
180
|
+
globalObj = window;
|
|
181
|
+
} else if (typeof process !== "undefined" && process.versions && process.versions.node) {
|
|
182
|
+
globalObj = Function("return this")();
|
|
183
|
+
} else if (typeof self !== "undefined") {
|
|
184
|
+
globalObj = self;
|
|
185
|
+
} else {
|
|
186
|
+
console.warn("Unable to find global object, using local instance");
|
|
187
|
+
return {
|
|
188
|
+
currentDependencyTracker: null,
|
|
189
|
+
currentSubscriptionsTracker: null
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
if (!globalObj[globalKey]) {
|
|
193
|
+
globalObj[globalKey] = {
|
|
194
|
+
currentDependencyTracker: null,
|
|
195
|
+
currentSubscriptionsTracker: null
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
return globalObj[globalKey];
|
|
199
|
+
}, "getGlobalReactiveStore");
|
|
200
|
+
var reactiveStore = getGlobalReactiveStore();
|
|
201
|
+
var trackDependency = /* @__PURE__ */ __name((signal2) => {
|
|
202
|
+
if (reactiveStore.currentDependencyTracker) {
|
|
203
|
+
reactiveStore.currentDependencyTracker(signal2);
|
|
204
|
+
}
|
|
205
|
+
}, "trackDependency");
|
|
206
|
+
function signal(defaultValue) {
|
|
207
|
+
let subject;
|
|
208
|
+
if (Array.isArray(defaultValue)) {
|
|
209
|
+
subject = new ArraySubject(defaultValue);
|
|
210
|
+
} else if (typeof defaultValue === "object" && defaultValue !== null) {
|
|
211
|
+
subject = new ObjectSubject(defaultValue);
|
|
212
|
+
} else {
|
|
213
|
+
subject = new BehaviorSubject(defaultValue);
|
|
214
|
+
}
|
|
215
|
+
const getValue = /* @__PURE__ */ __name(() => {
|
|
216
|
+
if (subject instanceof ArraySubject) {
|
|
217
|
+
return subject.items;
|
|
218
|
+
} else if (subject instanceof ObjectSubject) {
|
|
219
|
+
return subject.obj;
|
|
220
|
+
}
|
|
221
|
+
return subject.value;
|
|
222
|
+
}, "getValue");
|
|
223
|
+
const fn = /* @__PURE__ */ __name(function() {
|
|
224
|
+
trackDependency(fn);
|
|
225
|
+
return getValue();
|
|
226
|
+
}, "fn");
|
|
227
|
+
fn.set = (value) => {
|
|
228
|
+
if (subject instanceof ArraySubject) {
|
|
229
|
+
subject.items = value;
|
|
230
|
+
} else if (subject instanceof ObjectSubject) {
|
|
231
|
+
subject.obj = value;
|
|
232
|
+
} else {
|
|
233
|
+
subject.next(value);
|
|
234
|
+
}
|
|
235
|
+
};
|
|
236
|
+
fn._isFrozen = false;
|
|
237
|
+
fn.freeze = () => {
|
|
238
|
+
fn._isFrozen = true;
|
|
239
|
+
};
|
|
240
|
+
fn.unfreeze = () => {
|
|
241
|
+
fn._isFrozen = false;
|
|
242
|
+
if (subject instanceof ArraySubject) {
|
|
243
|
+
subject.next({
|
|
244
|
+
type: "init",
|
|
245
|
+
items: subject.items
|
|
246
|
+
});
|
|
247
|
+
} else if (subject instanceof ObjectSubject) {
|
|
248
|
+
subject.next({
|
|
249
|
+
type: "init",
|
|
250
|
+
value: subject.obj
|
|
251
|
+
});
|
|
252
|
+
} else {
|
|
253
|
+
subject.next(subject.value);
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
fn.mutate = (mutateFn) => {
|
|
257
|
+
const value = getValue();
|
|
258
|
+
mutateFn(value);
|
|
259
|
+
};
|
|
260
|
+
fn.update = (updateFn) => {
|
|
261
|
+
const updatedValue = updateFn(getValue());
|
|
262
|
+
fn.set(updatedValue);
|
|
263
|
+
};
|
|
264
|
+
fn.observable = subject.asObservable().pipe(filter(() => !fn._isFrozen));
|
|
265
|
+
fn._subject = subject;
|
|
266
|
+
return fn;
|
|
267
|
+
}
|
|
268
|
+
__name(signal, "signal");
|
|
269
|
+
function isSignal(value) {
|
|
270
|
+
return !!(value && value.observable);
|
|
271
|
+
}
|
|
272
|
+
__name(isSignal, "isSignal");
|
|
273
|
+
function isComputed(value) {
|
|
274
|
+
return isSignal(value) && !!value.dependencies;
|
|
275
|
+
}
|
|
276
|
+
__name(isComputed, "isComputed");
|
|
277
|
+
function computed(computeFunction, disposableFn) {
|
|
278
|
+
const dependencies = /* @__PURE__ */ new Set();
|
|
279
|
+
let init = true;
|
|
280
|
+
let lastComputedValue;
|
|
281
|
+
const previousTracker = reactiveStore.currentDependencyTracker;
|
|
282
|
+
reactiveStore.currentDependencyTracker = (signal2) => {
|
|
283
|
+
dependencies.add(signal2);
|
|
284
|
+
};
|
|
285
|
+
lastComputedValue = computeFunction();
|
|
286
|
+
if (computeFunction["isEffect"]) {
|
|
287
|
+
disposableFn = lastComputedValue;
|
|
288
|
+
}
|
|
289
|
+
reactiveStore.currentDependencyTracker = previousTracker;
|
|
290
|
+
const computedObservable = combineLatest([
|
|
291
|
+
...dependencies
|
|
292
|
+
].map((signal2) => signal2.observable)).pipe(filter(() => !init), map(() => computeFunction()), finalize(() => disposableFn?.()));
|
|
293
|
+
const fn = /* @__PURE__ */ __name(function() {
|
|
294
|
+
trackDependency(fn);
|
|
295
|
+
return lastComputedValue;
|
|
296
|
+
}, "fn");
|
|
297
|
+
fn.observable = computedObservable;
|
|
298
|
+
fn.subscription = computedObservable.subscribe((value) => {
|
|
299
|
+
lastComputedValue = value;
|
|
300
|
+
});
|
|
301
|
+
fn.dependencies = dependencies;
|
|
302
|
+
reactiveStore.currentSubscriptionsTracker?.(fn.subscription);
|
|
303
|
+
init = false;
|
|
304
|
+
return fn;
|
|
305
|
+
}
|
|
306
|
+
__name(computed, "computed");
|
|
307
|
+
function untracked(fn) {
|
|
308
|
+
const prevDepTracker = reactiveStore.currentDependencyTracker;
|
|
309
|
+
const prevSubTracker = reactiveStore.currentSubscriptionsTracker;
|
|
310
|
+
reactiveStore.currentDependencyTracker = null;
|
|
311
|
+
reactiveStore.currentSubscriptionsTracker = null;
|
|
312
|
+
try {
|
|
313
|
+
return fn();
|
|
314
|
+
} finally {
|
|
315
|
+
reactiveStore.currentDependencyTracker = prevDepTracker;
|
|
316
|
+
reactiveStore.currentSubscriptionsTracker = prevSubTracker;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
__name(untracked, "untracked");
|
|
320
|
+
|
|
321
|
+
// src/effect.ts
|
|
322
|
+
function effect(fn) {
|
|
323
|
+
fn["isEffect"] = true;
|
|
324
|
+
return computed(fn);
|
|
3592
325
|
}
|
|
3593
|
-
|
|
3594
|
-
object: ZodObject.lazycreate,
|
|
3595
|
-
};
|
|
3596
|
-
var ZodFirstPartyTypeKind;
|
|
3597
|
-
(function (ZodFirstPartyTypeKind) {
|
|
3598
|
-
ZodFirstPartyTypeKind["ZodString"] = "ZodString";
|
|
3599
|
-
ZodFirstPartyTypeKind["ZodNumber"] = "ZodNumber";
|
|
3600
|
-
ZodFirstPartyTypeKind["ZodNaN"] = "ZodNaN";
|
|
3601
|
-
ZodFirstPartyTypeKind["ZodBigInt"] = "ZodBigInt";
|
|
3602
|
-
ZodFirstPartyTypeKind["ZodBoolean"] = "ZodBoolean";
|
|
3603
|
-
ZodFirstPartyTypeKind["ZodDate"] = "ZodDate";
|
|
3604
|
-
ZodFirstPartyTypeKind["ZodSymbol"] = "ZodSymbol";
|
|
3605
|
-
ZodFirstPartyTypeKind["ZodUndefined"] = "ZodUndefined";
|
|
3606
|
-
ZodFirstPartyTypeKind["ZodNull"] = "ZodNull";
|
|
3607
|
-
ZodFirstPartyTypeKind["ZodAny"] = "ZodAny";
|
|
3608
|
-
ZodFirstPartyTypeKind["ZodUnknown"] = "ZodUnknown";
|
|
3609
|
-
ZodFirstPartyTypeKind["ZodNever"] = "ZodNever";
|
|
3610
|
-
ZodFirstPartyTypeKind["ZodVoid"] = "ZodVoid";
|
|
3611
|
-
ZodFirstPartyTypeKind["ZodArray"] = "ZodArray";
|
|
3612
|
-
ZodFirstPartyTypeKind["ZodObject"] = "ZodObject";
|
|
3613
|
-
ZodFirstPartyTypeKind["ZodUnion"] = "ZodUnion";
|
|
3614
|
-
ZodFirstPartyTypeKind["ZodDiscriminatedUnion"] = "ZodDiscriminatedUnion";
|
|
3615
|
-
ZodFirstPartyTypeKind["ZodIntersection"] = "ZodIntersection";
|
|
3616
|
-
ZodFirstPartyTypeKind["ZodTuple"] = "ZodTuple";
|
|
3617
|
-
ZodFirstPartyTypeKind["ZodRecord"] = "ZodRecord";
|
|
3618
|
-
ZodFirstPartyTypeKind["ZodMap"] = "ZodMap";
|
|
3619
|
-
ZodFirstPartyTypeKind["ZodSet"] = "ZodSet";
|
|
3620
|
-
ZodFirstPartyTypeKind["ZodFunction"] = "ZodFunction";
|
|
3621
|
-
ZodFirstPartyTypeKind["ZodLazy"] = "ZodLazy";
|
|
3622
|
-
ZodFirstPartyTypeKind["ZodLiteral"] = "ZodLiteral";
|
|
3623
|
-
ZodFirstPartyTypeKind["ZodEnum"] = "ZodEnum";
|
|
3624
|
-
ZodFirstPartyTypeKind["ZodEffects"] = "ZodEffects";
|
|
3625
|
-
ZodFirstPartyTypeKind["ZodNativeEnum"] = "ZodNativeEnum";
|
|
3626
|
-
ZodFirstPartyTypeKind["ZodOptional"] = "ZodOptional";
|
|
3627
|
-
ZodFirstPartyTypeKind["ZodNullable"] = "ZodNullable";
|
|
3628
|
-
ZodFirstPartyTypeKind["ZodDefault"] = "ZodDefault";
|
|
3629
|
-
ZodFirstPartyTypeKind["ZodCatch"] = "ZodCatch";
|
|
3630
|
-
ZodFirstPartyTypeKind["ZodPromise"] = "ZodPromise";
|
|
3631
|
-
ZodFirstPartyTypeKind["ZodBranded"] = "ZodBranded";
|
|
3632
|
-
ZodFirstPartyTypeKind["ZodPipeline"] = "ZodPipeline";
|
|
3633
|
-
ZodFirstPartyTypeKind["ZodReadonly"] = "ZodReadonly";
|
|
3634
|
-
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
|
|
3635
|
-
const instanceOfType = (
|
|
3636
|
-
// const instanceOfType = <T extends new (...args: any[]) => any>(
|
|
3637
|
-
cls, params = {
|
|
3638
|
-
message: `Input not instance of ${cls.name}`,
|
|
3639
|
-
}) => custom((data) => data instanceof cls, params);
|
|
3640
|
-
const stringType = ZodString.create;
|
|
3641
|
-
const numberType = ZodNumber.create;
|
|
3642
|
-
const nanType = ZodNaN.create;
|
|
3643
|
-
const bigIntType = ZodBigInt.create;
|
|
3644
|
-
const booleanType = ZodBoolean.create;
|
|
3645
|
-
const dateType = ZodDate.create;
|
|
3646
|
-
const symbolType = ZodSymbol.create;
|
|
3647
|
-
const undefinedType = ZodUndefined.create;
|
|
3648
|
-
const nullType = ZodNull.create;
|
|
3649
|
-
const anyType = ZodAny.create;
|
|
3650
|
-
const unknownType = ZodUnknown.create;
|
|
3651
|
-
const neverType = ZodNever.create;
|
|
3652
|
-
const voidType = ZodVoid.create;
|
|
3653
|
-
const arrayType = ZodArray.create;
|
|
3654
|
-
const objectType = ZodObject.create;
|
|
3655
|
-
const strictObjectType = ZodObject.strictCreate;
|
|
3656
|
-
const unionType = ZodUnion.create;
|
|
3657
|
-
const discriminatedUnionType = ZodDiscriminatedUnion.create;
|
|
3658
|
-
const intersectionType = ZodIntersection.create;
|
|
3659
|
-
const tupleType = ZodTuple.create;
|
|
3660
|
-
const recordType = ZodRecord.create;
|
|
3661
|
-
const mapType = ZodMap.create;
|
|
3662
|
-
const setType = ZodSet.create;
|
|
3663
|
-
const functionType = ZodFunction.create;
|
|
3664
|
-
const lazyType = ZodLazy.create;
|
|
3665
|
-
const literalType = ZodLiteral.create;
|
|
3666
|
-
const enumType = ZodEnum.create;
|
|
3667
|
-
const nativeEnumType = ZodNativeEnum.create;
|
|
3668
|
-
const promiseType = ZodPromise.create;
|
|
3669
|
-
const effectsType = ZodEffects.create;
|
|
3670
|
-
const optionalType = ZodOptional.create;
|
|
3671
|
-
const nullableType = ZodNullable.create;
|
|
3672
|
-
const preprocessType = ZodEffects.createWithPreprocess;
|
|
3673
|
-
const pipelineType = ZodPipeline.create;
|
|
3674
|
-
const ostring = () => stringType().optional();
|
|
3675
|
-
const onumber = () => numberType().optional();
|
|
3676
|
-
const oboolean = () => booleanType().optional();
|
|
3677
|
-
const coerce = {
|
|
3678
|
-
string: ((arg) => ZodString.create({ ...arg, coerce: true })),
|
|
3679
|
-
number: ((arg) => ZodNumber.create({ ...arg, coerce: true })),
|
|
3680
|
-
boolean: ((arg) => ZodBoolean.create({
|
|
3681
|
-
...arg,
|
|
3682
|
-
coerce: true,
|
|
3683
|
-
})),
|
|
3684
|
-
bigint: ((arg) => ZodBigInt.create({ ...arg, coerce: true })),
|
|
3685
|
-
date: ((arg) => ZodDate.create({ ...arg, coerce: true })),
|
|
3686
|
-
};
|
|
3687
|
-
const NEVER = INVALID;
|
|
326
|
+
__name(effect, "effect");
|
|
3688
327
|
|
|
3689
|
-
export {
|
|
328
|
+
export { ArraySubject, ObjectSubject, computed, effect, isComputed, isSignal, signal, untracked };
|
|
3690
329
|
//# sourceMappingURL=index41.js.map
|