langgraph-ui-components 0.0.7 → 0.0.8
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/index.es100.js +25 -157
- package/dist/index.es100.js.map +1 -1
- package/dist/index.es101.js +3130 -176
- package/dist/index.es101.js.map +1 -1
- package/dist/index.es102.js +36 -13
- package/dist/index.es102.js.map +1 -1
- package/dist/index.es103.js +5 -30
- package/dist/index.es103.js.map +1 -1
- package/dist/index.es104.js +69 -30
- package/dist/index.es104.js.map +1 -1
- package/dist/index.es105.js +28 -3148
- package/dist/index.es105.js.map +1 -1
- package/dist/index.es106.js +5 -16
- package/dist/index.es106.js.map +1 -1
- package/dist/index.es107.js +2 -7
- package/dist/index.es107.js.map +1 -1
- package/dist/index.es108.js +13 -2
- package/dist/index.es108.js.map +1 -1
- package/dist/index.es109.js +499 -9
- package/dist/index.es109.js.map +1 -1
- package/dist/index.es11.js +2 -2
- package/dist/index.es11.js.map +1 -1
- package/dist/index.es110.js +5 -501
- package/dist/index.es110.js.map +1 -1
- package/dist/index.es111.js +2 -7
- package/dist/index.es111.js.map +1 -1
- package/dist/index.es112.js +148 -2
- package/dist/index.es112.js.map +1 -1
- package/dist/index.es113.js +878 -118
- package/dist/index.es113.js.map +1 -1
- package/dist/index.es114.js +251 -877
- package/dist/index.es114.js.map +1 -1
- package/dist/index.es115.js +3 -281
- package/dist/index.es115.js.map +1 -1
- package/dist/index.es116.js +445 -3
- package/dist/index.es116.js.map +1 -1
- package/dist/index.es117.js +39 -437
- package/dist/index.es117.js.map +1 -1
- package/dist/index.es118.js +22 -46
- package/dist/index.es118.js.map +1 -1
- package/dist/index.es119.js +78 -21
- package/dist/index.es119.js.map +1 -1
- package/dist/index.es120.js +126 -76
- package/dist/index.es120.js.map +1 -1
- package/dist/index.es121.js +45 -125
- package/dist/index.es121.js.map +1 -1
- package/dist/index.es122.js +9 -48
- package/dist/index.es122.js.map +1 -1
- package/dist/index.es123.js +45 -10
- package/dist/index.es123.js.map +1 -1
- package/dist/index.es124.js +97 -43
- package/dist/index.es124.js.map +1 -1
- package/dist/index.es125.js +48 -94
- package/dist/index.es125.js.map +1 -1
- package/dist/index.es126.js +14 -51
- package/dist/index.es126.js.map +1 -1
- package/dist/index.es127.js +13 -162
- package/dist/index.es127.js.map +1 -1
- package/dist/index.es128.js +89 -2
- package/dist/index.es128.js.map +1 -1
- package/dist/index.es129.js +2 -15
- package/dist/index.es129.js.map +1 -1
- package/dist/index.es130.js +54 -87
- package/dist/index.es130.js.map +1 -1
- package/dist/index.es131.js +100 -2
- package/dist/index.es131.js.map +1 -1
- package/dist/index.es132.js +186 -51
- package/dist/index.es132.js.map +1 -1
- package/dist/index.es133.js +97 -87
- package/dist/index.es133.js.map +1 -1
- package/dist/index.es134.js +31 -187
- package/dist/index.es134.js.map +1 -1
- package/dist/index.es135.js +10 -110
- package/dist/index.es135.js.map +1 -1
- package/dist/index.es136.js +11 -35
- package/dist/index.es136.js.map +1 -1
- package/dist/index.es137.js +12 -10
- package/dist/index.es137.js.map +1 -1
- package/dist/index.es138.js +90 -10
- package/dist/index.es138.js.map +1 -1
- package/dist/index.es139.js +274 -11
- package/dist/index.es139.js.map +1 -1
- package/dist/index.es140.js +2 -91
- package/dist/index.es140.js.map +1 -1
- package/dist/index.es141.js +150 -261
- package/dist/index.es141.js.map +1 -1
- package/dist/index.es142.js +51 -2
- package/dist/index.es142.js.map +1 -1
- package/dist/index.es143.js +34 -158
- package/dist/index.es143.js.map +1 -1
- package/dist/index.es144.js +34 -50
- package/dist/index.es144.js.map +1 -1
- package/dist/index.es145.js +32 -32
- package/dist/index.es145.js.map +1 -1
- package/dist/index.es146.js +23 -34
- package/dist/index.es146.js.map +1 -1
- package/dist/index.es147.js +583 -32
- package/dist/index.es147.js.map +1 -1
- package/dist/index.es148.js +121 -20
- package/dist/index.es148.js.map +1 -1
- package/dist/index.es149.js +158 -585
- package/dist/index.es149.js.map +1 -1
- package/dist/index.es15.js +15 -15
- package/dist/index.es150.js +2 -125
- package/dist/index.es150.js.map +1 -1
- package/dist/index.es157.js +2 -2
- package/dist/index.es159.js +2 -2
- package/dist/index.es161.js +64 -25
- package/dist/index.es161.js.map +1 -1
- package/dist/index.es162.js +22 -12
- package/dist/index.es162.js.map +1 -1
- package/dist/index.es163.js +5 -53
- package/dist/index.es163.js.map +1 -1
- package/dist/index.es164.js +337 -52
- package/dist/index.es164.js.map +1 -1
- package/dist/index.es165.js +24 -21
- package/dist/index.es165.js.map +1 -1
- package/dist/index.es166.js +12 -6
- package/dist/index.es166.js.map +1 -1
- package/dist/index.es167.js +49 -344
- package/dist/index.es167.js.map +1 -1
- package/dist/index.es175.js +1 -1
- package/dist/index.es178.js +1 -1
- package/dist/index.es183.js +1 -1
- package/dist/index.es195.js +2 -2
- package/dist/index.es197.js +2 -2
- package/dist/index.es200.js +1 -1
- package/dist/index.es201.js +1 -1
- package/dist/index.es203.js +1 -1
- package/dist/index.es204.js +2 -2
- package/dist/index.es206.js +1 -1
- package/dist/index.es209.js +3 -3
- package/dist/index.es21.js +1 -1
- package/dist/index.es210.js +2 -2
- package/dist/index.es212.js +1 -1
- package/dist/index.es214.js +1 -1
- package/dist/index.es215.js +1 -1
- package/dist/index.es217.js +1 -1
- package/dist/index.es218.js +2 -2
- package/dist/index.es219.js +1 -1
- package/dist/index.es220.js +1 -1
- package/dist/index.es221.js +1 -1
- package/dist/index.es222.js +1 -1
- package/dist/index.es223.js +1 -1
- package/dist/index.es224.js +1 -1
- package/dist/index.es225.js +1 -1
- package/dist/index.es229.js +2 -2
- package/dist/index.es28.js +7 -7
- package/dist/index.es29.js +1 -1
- package/dist/index.es32.js +4 -4
- package/dist/index.es33.js +15 -1191
- package/dist/index.es33.js.map +1 -1
- package/dist/index.es34.js +117 -6
- package/dist/index.es34.js.map +1 -1
- package/dist/index.es35.js +70 -8
- package/dist/index.es35.js.map +1 -1
- package/dist/index.es36.js +19 -148
- package/dist/index.es36.js.map +1 -1
- package/dist/index.es37.js +255 -112
- package/dist/index.es37.js.map +1 -1
- package/dist/index.es38.js +40 -68
- package/dist/index.es38.js.map +1 -1
- package/dist/index.es39.js +214 -17
- package/dist/index.es39.js.map +1 -1
- package/dist/index.es40.js +53 -247
- package/dist/index.es40.js.map +1 -1
- package/dist/index.es41.js +123 -24
- package/dist/index.es41.js.map +1 -1
- package/dist/index.es42.js +14 -205
- package/dist/index.es42.js.map +1 -1
- package/dist/index.es43.js +221 -58
- package/dist/index.es43.js.map +1 -1
- package/dist/index.es44.js +7 -140
- package/dist/index.es44.js.map +1 -1
- package/dist/index.es45.js +1185 -17
- package/dist/index.es45.js.map +1 -1
- package/dist/index.es46.js +6 -229
- package/dist/index.es46.js.map +1 -1
- package/dist/index.es47.js +146 -15
- package/dist/index.es47.js.map +1 -1
- package/dist/index.es49.js +7 -90
- package/dist/index.es49.js.map +1 -1
- package/dist/index.es50.js +92 -6
- package/dist/index.es50.js.map +1 -1
- package/dist/index.es51.js +3 -14
- package/dist/index.es51.js.map +1 -1
- package/dist/index.es52.js +36 -8
- package/dist/index.es52.js.map +1 -1
- package/dist/index.es53.js +61 -83
- package/dist/index.es53.js.map +1 -1
- package/dist/index.es54.js +37 -3
- package/dist/index.es54.js.map +1 -1
- package/dist/index.es55.js +304 -34
- package/dist/index.es55.js.map +1 -1
- package/dist/index.es56.js +9 -72
- package/dist/index.es56.js.map +1 -1
- package/dist/index.es57.js +85 -32
- package/dist/index.es57.js.map +1 -1
- package/dist/index.es58.js +6 -304
- package/dist/index.es58.js.map +1 -1
- package/dist/index.es59.js +15 -9
- package/dist/index.es59.js.map +1 -1
- package/dist/index.es60.js +142 -5
- package/dist/index.es60.js.map +1 -1
- package/dist/index.es61.js +285 -148
- package/dist/index.es61.js.map +1 -1
- package/dist/index.es62.js +62 -668
- package/dist/index.es62.js.map +1 -1
- package/dist/index.es63.js +201 -131
- package/dist/index.es63.js.map +1 -1
- package/dist/index.es64.js +29 -295
- package/dist/index.es64.js.map +1 -1
- package/dist/index.es65.js +8 -65
- package/dist/index.es65.js.map +1 -1
- package/dist/index.es66.js +9 -210
- package/dist/index.es66.js.map +1 -1
- package/dist/index.es67.js +181 -28
- package/dist/index.es67.js.map +1 -1
- package/dist/index.es68.js +23 -8
- package/dist/index.es68.js.map +1 -1
- package/dist/index.es69.js +1397 -9
- package/dist/index.es69.js.map +1 -1
- package/dist/index.es70.js +9 -187
- package/dist/index.es70.js.map +1 -1
- package/dist/index.es71.js +8 -24
- package/dist/index.es71.js.map +1 -1
- package/dist/index.es72.js +5 -1399
- package/dist/index.es72.js.map +1 -1
- package/dist/index.es73.js +163 -9
- package/dist/index.es73.js.map +1 -1
- package/dist/index.es74.js +672 -8
- package/dist/index.es74.js.map +1 -1
- package/dist/index.es75.js +11 -36
- package/dist/index.es75.js.map +1 -1
- package/dist/index.es76.js +40 -5
- package/dist/index.es76.js.map +1 -1
- package/dist/index.es77.js +32 -71
- package/dist/index.es77.js.map +1 -1
- package/dist/index.es78.js +225 -30
- package/dist/index.es78.js.map +1 -1
- package/dist/index.es79.js +29 -11
- package/dist/index.es79.js.map +1 -1
- package/dist/index.es80.js +72 -37
- package/dist/index.es80.js.map +1 -1
- package/dist/index.es81.js +74 -32
- package/dist/index.es81.js.map +1 -1
- package/dist/index.es82.js +46 -226
- package/dist/index.es82.js.map +1 -1
- package/dist/index.es83.js +6 -6
- package/dist/index.es83.js.map +1 -1
- package/dist/index.es84.js +39 -71
- package/dist/index.es84.js.map +1 -1
- package/dist/index.es85.js +54 -73
- package/dist/index.es85.js.map +1 -1
- package/dist/index.es86.js +13 -52
- package/dist/index.es86.js.map +1 -1
- package/dist/index.es87.js +135 -28
- package/dist/index.es87.js.map +1 -1
- package/dist/index.es88.js +20 -42
- package/dist/index.es88.js.map +1 -1
- package/dist/index.es89.js +175 -50
- package/dist/index.es89.js.map +1 -1
- package/dist/index.es90.js +240 -12
- package/dist/index.es90.js.map +1 -1
- package/dist/index.es91.js +348 -130
- package/dist/index.es91.js.map +1 -1
- package/dist/index.es92.js +134 -18
- package/dist/index.es92.js.map +1 -1
- package/dist/index.es93.js +62 -174
- package/dist/index.es93.js.map +1 -1
- package/dist/index.es94.js +30 -234
- package/dist/index.es94.js.map +1 -1
- package/dist/index.es95.js +26 -344
- package/dist/index.es95.js.map +1 -1
- package/dist/index.es96.js +156 -127
- package/dist/index.es96.js.map +1 -1
- package/dist/index.es97.js +196 -63
- package/dist/index.es97.js.map +1 -1
- package/dist/index.es98.js +14 -37
- package/dist/index.es98.js.map +1 -1
- package/dist/index.es99.js +27 -34
- package/dist/index.es99.js.map +1 -1
- package/package.json +1 -1
package/dist/index.es101.js
CHANGED
|
@@ -1,206 +1,3160 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
1
|
+
import { ZodIssueCode, ZodError } from "./index.es172.js";
|
|
2
|
+
import { getParsedType, util, ZodParsedType } from "./index.es171.js";
|
|
3
|
+
import { errorUtil } from "./index.es173.js";
|
|
4
|
+
import { OK, ParseStatus, isAsync, isValid, addIssueToContext, INVALID, DIRTY, isAborted, isDirty } from "./index.es170.js";
|
|
5
|
+
class ParseInputLazyPath {
|
|
6
|
+
constructor(parent, value, path, key) {
|
|
7
|
+
this._cachedPath = [];
|
|
8
|
+
this.parent = parent;
|
|
9
|
+
this.data = value;
|
|
10
|
+
this._path = path;
|
|
11
|
+
this._key = key;
|
|
12
|
+
}
|
|
13
|
+
get path() {
|
|
14
|
+
if (!this._cachedPath.length) {
|
|
15
|
+
if (Array.isArray(this._key)) {
|
|
16
|
+
this._cachedPath.push(...this._path, ...this._key);
|
|
17
|
+
} else {
|
|
18
|
+
this._cachedPath.push(...this._path, this._key);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return this._cachedPath;
|
|
16
22
|
}
|
|
17
|
-
else return data.name ?? "UnknownSchema";
|
|
18
23
|
}
|
|
19
|
-
|
|
20
|
-
if (
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
24
|
+
const handleResult = (ctx, result) => {
|
|
25
|
+
if (isValid(result)) {
|
|
26
|
+
return { success: true, data: result.value };
|
|
27
|
+
} else {
|
|
28
|
+
if (!ctx.common.issues.length) {
|
|
29
|
+
throw new Error("Validation failed but no issues detected.");
|
|
25
30
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
31
|
+
return {
|
|
32
|
+
success: false,
|
|
33
|
+
get error() {
|
|
34
|
+
if (this._error)
|
|
35
|
+
return this._error;
|
|
36
|
+
const error = new ZodError(ctx.common.issues);
|
|
37
|
+
this._error = error;
|
|
38
|
+
return this._error;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
};
|
|
43
|
+
function processCreateParams(params) {
|
|
44
|
+
if (!params)
|
|
45
|
+
return {};
|
|
46
|
+
const { errorMap, invalid_type_error, required_error, description } = params;
|
|
47
|
+
if (errorMap && (invalid_type_error || required_error)) {
|
|
48
|
+
throw new Error(`Can't use "invalid_type_error" or "required_error" in conjunction with custom error map.`);
|
|
49
|
+
}
|
|
50
|
+
if (errorMap)
|
|
51
|
+
return { errorMap, description };
|
|
52
|
+
const customMap = (iss, ctx) => {
|
|
53
|
+
const { message } = params;
|
|
54
|
+
if (iss.code === "invalid_enum_value") {
|
|
55
|
+
return { message: message ?? ctx.defaultError };
|
|
56
|
+
}
|
|
57
|
+
if (typeof ctx.data === "undefined") {
|
|
58
|
+
return { message: message ?? required_error ?? ctx.defaultError };
|
|
32
59
|
}
|
|
60
|
+
if (iss.code !== "invalid_type")
|
|
61
|
+
return { message: ctx.defaultError };
|
|
62
|
+
return { message: message ?? invalid_type_error ?? ctx.defaultError };
|
|
33
63
|
};
|
|
64
|
+
return { errorMap: customMap, description };
|
|
34
65
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
constructor(params) {
|
|
39
|
-
this.nodes = params?.nodes ?? this.nodes;
|
|
40
|
-
this.edges = params?.edges ?? this.edges;
|
|
66
|
+
class ZodType {
|
|
67
|
+
get description() {
|
|
68
|
+
return this._def.description;
|
|
41
69
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
70
|
+
_getType(input) {
|
|
71
|
+
return getParsedType(input.data);
|
|
72
|
+
}
|
|
73
|
+
_getOrReturnCtx(input, ctx) {
|
|
74
|
+
return ctx || {
|
|
75
|
+
common: input.parent.common,
|
|
76
|
+
data: input.data,
|
|
77
|
+
parsedType: getParsedType(input.data),
|
|
78
|
+
schemaErrorMap: this._def.errorMap,
|
|
79
|
+
path: input.path,
|
|
80
|
+
parent: input.parent
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
_processInputParams(input) {
|
|
47
84
|
return {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
if (typeof edge.data !== "undefined") item.data = edge.data;
|
|
58
|
-
if (typeof edge.conditional !== "undefined") item.conditional = edge.conditional;
|
|
59
|
-
return item;
|
|
60
|
-
})
|
|
85
|
+
status: new ParseStatus(),
|
|
86
|
+
ctx: {
|
|
87
|
+
common: input.parent.common,
|
|
88
|
+
data: input.data,
|
|
89
|
+
parsedType: getParsedType(input.data),
|
|
90
|
+
schemaErrorMap: this._def.errorMap,
|
|
91
|
+
path: input.path,
|
|
92
|
+
parent: input.parent
|
|
93
|
+
}
|
|
61
94
|
};
|
|
62
95
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
96
|
+
_parseSync(input) {
|
|
97
|
+
const result = this._parse(input);
|
|
98
|
+
if (isAsync(result)) {
|
|
99
|
+
throw new Error("Synchronous parse encountered promise.");
|
|
100
|
+
}
|
|
101
|
+
return result;
|
|
102
|
+
}
|
|
103
|
+
_parseAsync(input) {
|
|
104
|
+
const result = this._parse(input);
|
|
105
|
+
return Promise.resolve(result);
|
|
106
|
+
}
|
|
107
|
+
parse(data, params) {
|
|
108
|
+
const result = this.safeParse(data, params);
|
|
109
|
+
if (result.success)
|
|
110
|
+
return result.data;
|
|
111
|
+
throw result.error;
|
|
112
|
+
}
|
|
113
|
+
safeParse(data, params) {
|
|
114
|
+
const ctx = {
|
|
115
|
+
common: {
|
|
116
|
+
issues: [],
|
|
117
|
+
async: params?.async ?? false,
|
|
118
|
+
contextualErrorMap: params?.errorMap
|
|
119
|
+
},
|
|
120
|
+
path: params?.path || [],
|
|
121
|
+
schemaErrorMap: this._def.errorMap,
|
|
122
|
+
parent: null,
|
|
68
123
|
data,
|
|
69
|
-
|
|
70
|
-
metadata
|
|
124
|
+
parsedType: getParsedType(data)
|
|
71
125
|
};
|
|
72
|
-
this.
|
|
73
|
-
return
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
target: target.id,
|
|
126
|
+
const result = this._parseSync({ data, path: ctx.path, parent: ctx });
|
|
127
|
+
return handleResult(ctx, result);
|
|
128
|
+
}
|
|
129
|
+
"~validate"(data) {
|
|
130
|
+
const ctx = {
|
|
131
|
+
common: {
|
|
132
|
+
issues: [],
|
|
133
|
+
async: !!this["~standard"].async
|
|
134
|
+
},
|
|
135
|
+
path: [],
|
|
136
|
+
schemaErrorMap: this._def.errorMap,
|
|
137
|
+
parent: null,
|
|
85
138
|
data,
|
|
86
|
-
|
|
139
|
+
parsedType: getParsedType(data)
|
|
140
|
+
};
|
|
141
|
+
if (!this["~standard"].async) {
|
|
142
|
+
try {
|
|
143
|
+
const result = this._parseSync({ data, path: [], parent: ctx });
|
|
144
|
+
return isValid(result) ? {
|
|
145
|
+
value: result.value
|
|
146
|
+
} : {
|
|
147
|
+
issues: ctx.common.issues
|
|
148
|
+
};
|
|
149
|
+
} catch (err) {
|
|
150
|
+
if (err?.message?.toLowerCase()?.includes("encountered")) {
|
|
151
|
+
this["~standard"].async = true;
|
|
152
|
+
}
|
|
153
|
+
ctx.common = {
|
|
154
|
+
issues: [],
|
|
155
|
+
async: true
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return this._parseAsync({ data, path: [], parent: ctx }).then((result) => isValid(result) ? {
|
|
160
|
+
value: result.value
|
|
161
|
+
} : {
|
|
162
|
+
issues: ctx.common.issues
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
async parseAsync(data, params) {
|
|
166
|
+
const result = await this.safeParseAsync(data, params);
|
|
167
|
+
if (result.success)
|
|
168
|
+
return result.data;
|
|
169
|
+
throw result.error;
|
|
170
|
+
}
|
|
171
|
+
async safeParseAsync(data, params) {
|
|
172
|
+
const ctx = {
|
|
173
|
+
common: {
|
|
174
|
+
issues: [],
|
|
175
|
+
contextualErrorMap: params?.errorMap,
|
|
176
|
+
async: true
|
|
177
|
+
},
|
|
178
|
+
path: params?.path || [],
|
|
179
|
+
schemaErrorMap: this._def.errorMap,
|
|
180
|
+
parent: null,
|
|
181
|
+
data,
|
|
182
|
+
parsedType: getParsedType(data)
|
|
183
|
+
};
|
|
184
|
+
const maybeAsyncResult = this._parse({ data, path: ctx.path, parent: ctx });
|
|
185
|
+
const result = await (isAsync(maybeAsyncResult) ? maybeAsyncResult : Promise.resolve(maybeAsyncResult));
|
|
186
|
+
return handleResult(ctx, result);
|
|
187
|
+
}
|
|
188
|
+
refine(check, message) {
|
|
189
|
+
const getIssueProperties = (val) => {
|
|
190
|
+
if (typeof message === "string" || typeof message === "undefined") {
|
|
191
|
+
return { message };
|
|
192
|
+
} else if (typeof message === "function") {
|
|
193
|
+
return message(val);
|
|
194
|
+
} else {
|
|
195
|
+
return message;
|
|
196
|
+
}
|
|
87
197
|
};
|
|
88
|
-
this.
|
|
89
|
-
|
|
198
|
+
return this._refinement((val, ctx) => {
|
|
199
|
+
const result = check(val);
|
|
200
|
+
const setError = () => ctx.addIssue({
|
|
201
|
+
code: ZodIssueCode.custom,
|
|
202
|
+
...getIssueProperties(val)
|
|
203
|
+
});
|
|
204
|
+
if (typeof Promise !== "undefined" && result instanceof Promise) {
|
|
205
|
+
return result.then((data) => {
|
|
206
|
+
if (!data) {
|
|
207
|
+
setError();
|
|
208
|
+
return false;
|
|
209
|
+
} else {
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
if (!result) {
|
|
215
|
+
setError();
|
|
216
|
+
return false;
|
|
217
|
+
} else {
|
|
218
|
+
return true;
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
refinement(check, refinementData) {
|
|
223
|
+
return this._refinement((val, ctx) => {
|
|
224
|
+
if (!check(val)) {
|
|
225
|
+
ctx.addIssue(typeof refinementData === "function" ? refinementData(val, ctx) : refinementData);
|
|
226
|
+
return false;
|
|
227
|
+
} else {
|
|
228
|
+
return true;
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
_refinement(refinement) {
|
|
233
|
+
return new ZodEffects({
|
|
234
|
+
schema: this,
|
|
235
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
236
|
+
effect: { type: "refinement", refinement }
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
superRefine(refinement) {
|
|
240
|
+
return this._refinement(refinement);
|
|
241
|
+
}
|
|
242
|
+
constructor(def) {
|
|
243
|
+
this.spa = this.safeParseAsync;
|
|
244
|
+
this._def = def;
|
|
245
|
+
this.parse = this.parse.bind(this);
|
|
246
|
+
this.safeParse = this.safeParse.bind(this);
|
|
247
|
+
this.parseAsync = this.parseAsync.bind(this);
|
|
248
|
+
this.safeParseAsync = this.safeParseAsync.bind(this);
|
|
249
|
+
this.spa = this.spa.bind(this);
|
|
250
|
+
this.refine = this.refine.bind(this);
|
|
251
|
+
this.refinement = this.refinement.bind(this);
|
|
252
|
+
this.superRefine = this.superRefine.bind(this);
|
|
253
|
+
this.optional = this.optional.bind(this);
|
|
254
|
+
this.nullable = this.nullable.bind(this);
|
|
255
|
+
this.nullish = this.nullish.bind(this);
|
|
256
|
+
this.array = this.array.bind(this);
|
|
257
|
+
this.promise = this.promise.bind(this);
|
|
258
|
+
this.or = this.or.bind(this);
|
|
259
|
+
this.and = this.and.bind(this);
|
|
260
|
+
this.transform = this.transform.bind(this);
|
|
261
|
+
this.brand = this.brand.bind(this);
|
|
262
|
+
this.default = this.default.bind(this);
|
|
263
|
+
this.catch = this.catch.bind(this);
|
|
264
|
+
this.describe = this.describe.bind(this);
|
|
265
|
+
this.pipe = this.pipe.bind(this);
|
|
266
|
+
this.readonly = this.readonly.bind(this);
|
|
267
|
+
this.isNullable = this.isNullable.bind(this);
|
|
268
|
+
this.isOptional = this.isOptional.bind(this);
|
|
269
|
+
this["~standard"] = {
|
|
270
|
+
version: 1,
|
|
271
|
+
vendor: "zod",
|
|
272
|
+
validate: (data) => this["~validate"](data)
|
|
273
|
+
};
|
|
274
|
+
}
|
|
275
|
+
optional() {
|
|
276
|
+
return ZodOptional.create(this, this._def);
|
|
277
|
+
}
|
|
278
|
+
nullable() {
|
|
279
|
+
return ZodNullable.create(this, this._def);
|
|
280
|
+
}
|
|
281
|
+
nullish() {
|
|
282
|
+
return this.nullable().optional();
|
|
283
|
+
}
|
|
284
|
+
array() {
|
|
285
|
+
return ZodArray.create(this);
|
|
286
|
+
}
|
|
287
|
+
promise() {
|
|
288
|
+
return ZodPromise.create(this, this._def);
|
|
289
|
+
}
|
|
290
|
+
or(option) {
|
|
291
|
+
return ZodUnion.create([this, option], this._def);
|
|
90
292
|
}
|
|
91
|
-
|
|
92
|
-
return
|
|
293
|
+
and(incoming) {
|
|
294
|
+
return ZodIntersection.create(this, incoming, this._def);
|
|
93
295
|
}
|
|
94
|
-
|
|
95
|
-
return
|
|
296
|
+
transform(transform) {
|
|
297
|
+
return new ZodEffects({
|
|
298
|
+
...processCreateParams(this._def),
|
|
299
|
+
schema: this,
|
|
300
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
301
|
+
effect: { type: "transform", transform }
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
default(def) {
|
|
305
|
+
const defaultValueFunc = typeof def === "function" ? def : () => def;
|
|
306
|
+
return new ZodDefault({
|
|
307
|
+
...processCreateParams(this._def),
|
|
308
|
+
innerType: this,
|
|
309
|
+
defaultValue: defaultValueFunc,
|
|
310
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
brand() {
|
|
314
|
+
return new ZodBranded({
|
|
315
|
+
typeName: ZodFirstPartyTypeKind.ZodBranded,
|
|
316
|
+
type: this,
|
|
317
|
+
...processCreateParams(this._def)
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
catch(def) {
|
|
321
|
+
const catchValueFunc = typeof def === "function" ? def : () => def;
|
|
322
|
+
return new ZodCatch({
|
|
323
|
+
...processCreateParams(this._def),
|
|
324
|
+
innerType: this,
|
|
325
|
+
catchValue: catchValueFunc,
|
|
326
|
+
typeName: ZodFirstPartyTypeKind.ZodCatch
|
|
327
|
+
});
|
|
328
|
+
}
|
|
329
|
+
describe(description) {
|
|
330
|
+
const This = this.constructor;
|
|
331
|
+
return new This({
|
|
332
|
+
...this._def,
|
|
333
|
+
description
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
pipe(target) {
|
|
337
|
+
return ZodPipeline.create(this, target);
|
|
338
|
+
}
|
|
339
|
+
readonly() {
|
|
340
|
+
return ZodReadonly.create(this);
|
|
341
|
+
}
|
|
342
|
+
isOptional() {
|
|
343
|
+
return this.safeParse(void 0).success;
|
|
344
|
+
}
|
|
345
|
+
isNullable() {
|
|
346
|
+
return this.safeParse(null).success;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
const cuidRegex = /^c[^\s-]{8,}$/i;
|
|
350
|
+
const cuid2Regex = /^[0-9a-z]+$/;
|
|
351
|
+
const ulidRegex = /^[0-9A-HJKMNP-TV-Z]{26}$/i;
|
|
352
|
+
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;
|
|
353
|
+
const nanoidRegex = /^[a-z0-9_-]{21}$/i;
|
|
354
|
+
const jwtRegex = /^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]*$/;
|
|
355
|
+
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)?)??$/;
|
|
356
|
+
const emailRegex = /^(?!\.)(?!.*\.\.)([A-Z0-9_'+\-\.]*)[A-Z0-9_+-]@([A-Z0-9][A-Z0-9\-]*\.)+[A-Z]{2,}$/i;
|
|
357
|
+
const _emojiRegex = `^(\\p{Extended_Pictographic}|\\p{Emoji_Component})+$`;
|
|
358
|
+
let emojiRegex;
|
|
359
|
+
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])$/;
|
|
360
|
+
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])$/;
|
|
361
|
+
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]))$/;
|
|
362
|
+
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])$/;
|
|
363
|
+
const base64Regex = /^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;
|
|
364
|
+
const base64urlRegex = /^([0-9a-zA-Z-_]{4})*(([0-9a-zA-Z-_]{2}(==)?)|([0-9a-zA-Z-_]{3}(=)?))?$/;
|
|
365
|
+
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])))`;
|
|
366
|
+
const dateRegex = new RegExp(`^${dateRegexSource}$`);
|
|
367
|
+
function timeRegexSource(args) {
|
|
368
|
+
let secondsRegexSource = `[0-5]\\d`;
|
|
369
|
+
if (args.precision) {
|
|
370
|
+
secondsRegexSource = `${secondsRegexSource}\\.\\d{${args.precision}}`;
|
|
371
|
+
} else if (args.precision == null) {
|
|
372
|
+
secondsRegexSource = `${secondsRegexSource}(\\.\\d+)?`;
|
|
373
|
+
}
|
|
374
|
+
const secondsQuantifier = args.precision ? "+" : "?";
|
|
375
|
+
return `([01]\\d|2[0-3]):[0-5]\\d(:${secondsRegexSource})${secondsQuantifier}`;
|
|
376
|
+
}
|
|
377
|
+
function timeRegex(args) {
|
|
378
|
+
return new RegExp(`^${timeRegexSource(args)}$`);
|
|
379
|
+
}
|
|
380
|
+
function datetimeRegex(args) {
|
|
381
|
+
let regex = `${dateRegexSource}T${timeRegexSource(args)}`;
|
|
382
|
+
const opts = [];
|
|
383
|
+
opts.push(args.local ? `Z?` : `Z`);
|
|
384
|
+
if (args.offset)
|
|
385
|
+
opts.push(`([+-]\\d{2}:?\\d{2})`);
|
|
386
|
+
regex = `${regex}(${opts.join("|")})`;
|
|
387
|
+
return new RegExp(`^${regex}$`);
|
|
388
|
+
}
|
|
389
|
+
function isValidIP(ip, version) {
|
|
390
|
+
if ((version === "v4" || !version) && ipv4Regex.test(ip)) {
|
|
391
|
+
return true;
|
|
392
|
+
}
|
|
393
|
+
if ((version === "v6" || !version) && ipv6Regex.test(ip)) {
|
|
394
|
+
return true;
|
|
395
|
+
}
|
|
396
|
+
return false;
|
|
397
|
+
}
|
|
398
|
+
function isValidJWT(jwt, alg) {
|
|
399
|
+
if (!jwtRegex.test(jwt))
|
|
400
|
+
return false;
|
|
401
|
+
try {
|
|
402
|
+
const [header] = jwt.split(".");
|
|
403
|
+
if (!header)
|
|
404
|
+
return false;
|
|
405
|
+
const base64 = header.replace(/-/g, "+").replace(/_/g, "/").padEnd(header.length + (4 - header.length % 4) % 4, "=");
|
|
406
|
+
const decoded = JSON.parse(atob(base64));
|
|
407
|
+
if (typeof decoded !== "object" || decoded === null)
|
|
408
|
+
return false;
|
|
409
|
+
if ("typ" in decoded && decoded?.typ !== "JWT")
|
|
410
|
+
return false;
|
|
411
|
+
if (!decoded.alg)
|
|
412
|
+
return false;
|
|
413
|
+
if (alg && decoded.alg !== alg)
|
|
414
|
+
return false;
|
|
415
|
+
return true;
|
|
416
|
+
} catch {
|
|
417
|
+
return false;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
function isValidCidr(ip, version) {
|
|
421
|
+
if ((version === "v4" || !version) && ipv4CidrRegex.test(ip)) {
|
|
422
|
+
return true;
|
|
423
|
+
}
|
|
424
|
+
if ((version === "v6" || !version) && ipv6CidrRegex.test(ip)) {
|
|
425
|
+
return true;
|
|
426
|
+
}
|
|
427
|
+
return false;
|
|
428
|
+
}
|
|
429
|
+
class ZodString extends ZodType {
|
|
430
|
+
_parse(input) {
|
|
431
|
+
if (this._def.coerce) {
|
|
432
|
+
input.data = String(input.data);
|
|
433
|
+
}
|
|
434
|
+
const parsedType = this._getType(input);
|
|
435
|
+
if (parsedType !== ZodParsedType.string) {
|
|
436
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
437
|
+
addIssueToContext(ctx2, {
|
|
438
|
+
code: ZodIssueCode.invalid_type,
|
|
439
|
+
expected: ZodParsedType.string,
|
|
440
|
+
received: ctx2.parsedType
|
|
441
|
+
});
|
|
442
|
+
return INVALID;
|
|
443
|
+
}
|
|
444
|
+
const status = new ParseStatus();
|
|
445
|
+
let ctx = void 0;
|
|
446
|
+
for (const check of this._def.checks) {
|
|
447
|
+
if (check.kind === "min") {
|
|
448
|
+
if (input.data.length < check.value) {
|
|
449
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
450
|
+
addIssueToContext(ctx, {
|
|
451
|
+
code: ZodIssueCode.too_small,
|
|
452
|
+
minimum: check.value,
|
|
453
|
+
type: "string",
|
|
454
|
+
inclusive: true,
|
|
455
|
+
exact: false,
|
|
456
|
+
message: check.message
|
|
457
|
+
});
|
|
458
|
+
status.dirty();
|
|
459
|
+
}
|
|
460
|
+
} else if (check.kind === "max") {
|
|
461
|
+
if (input.data.length > check.value) {
|
|
462
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
463
|
+
addIssueToContext(ctx, {
|
|
464
|
+
code: ZodIssueCode.too_big,
|
|
465
|
+
maximum: check.value,
|
|
466
|
+
type: "string",
|
|
467
|
+
inclusive: true,
|
|
468
|
+
exact: false,
|
|
469
|
+
message: check.message
|
|
470
|
+
});
|
|
471
|
+
status.dirty();
|
|
472
|
+
}
|
|
473
|
+
} else if (check.kind === "length") {
|
|
474
|
+
const tooBig = input.data.length > check.value;
|
|
475
|
+
const tooSmall = input.data.length < check.value;
|
|
476
|
+
if (tooBig || tooSmall) {
|
|
477
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
478
|
+
if (tooBig) {
|
|
479
|
+
addIssueToContext(ctx, {
|
|
480
|
+
code: ZodIssueCode.too_big,
|
|
481
|
+
maximum: check.value,
|
|
482
|
+
type: "string",
|
|
483
|
+
inclusive: true,
|
|
484
|
+
exact: true,
|
|
485
|
+
message: check.message
|
|
486
|
+
});
|
|
487
|
+
} else if (tooSmall) {
|
|
488
|
+
addIssueToContext(ctx, {
|
|
489
|
+
code: ZodIssueCode.too_small,
|
|
490
|
+
minimum: check.value,
|
|
491
|
+
type: "string",
|
|
492
|
+
inclusive: true,
|
|
493
|
+
exact: true,
|
|
494
|
+
message: check.message
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
status.dirty();
|
|
498
|
+
}
|
|
499
|
+
} else if (check.kind === "email") {
|
|
500
|
+
if (!emailRegex.test(input.data)) {
|
|
501
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
502
|
+
addIssueToContext(ctx, {
|
|
503
|
+
validation: "email",
|
|
504
|
+
code: ZodIssueCode.invalid_string,
|
|
505
|
+
message: check.message
|
|
506
|
+
});
|
|
507
|
+
status.dirty();
|
|
508
|
+
}
|
|
509
|
+
} else if (check.kind === "emoji") {
|
|
510
|
+
if (!emojiRegex) {
|
|
511
|
+
emojiRegex = new RegExp(_emojiRegex, "u");
|
|
512
|
+
}
|
|
513
|
+
if (!emojiRegex.test(input.data)) {
|
|
514
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
515
|
+
addIssueToContext(ctx, {
|
|
516
|
+
validation: "emoji",
|
|
517
|
+
code: ZodIssueCode.invalid_string,
|
|
518
|
+
message: check.message
|
|
519
|
+
});
|
|
520
|
+
status.dirty();
|
|
521
|
+
}
|
|
522
|
+
} else if (check.kind === "uuid") {
|
|
523
|
+
if (!uuidRegex.test(input.data)) {
|
|
524
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
525
|
+
addIssueToContext(ctx, {
|
|
526
|
+
validation: "uuid",
|
|
527
|
+
code: ZodIssueCode.invalid_string,
|
|
528
|
+
message: check.message
|
|
529
|
+
});
|
|
530
|
+
status.dirty();
|
|
531
|
+
}
|
|
532
|
+
} else if (check.kind === "nanoid") {
|
|
533
|
+
if (!nanoidRegex.test(input.data)) {
|
|
534
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
535
|
+
addIssueToContext(ctx, {
|
|
536
|
+
validation: "nanoid",
|
|
537
|
+
code: ZodIssueCode.invalid_string,
|
|
538
|
+
message: check.message
|
|
539
|
+
});
|
|
540
|
+
status.dirty();
|
|
541
|
+
}
|
|
542
|
+
} else if (check.kind === "cuid") {
|
|
543
|
+
if (!cuidRegex.test(input.data)) {
|
|
544
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
545
|
+
addIssueToContext(ctx, {
|
|
546
|
+
validation: "cuid",
|
|
547
|
+
code: ZodIssueCode.invalid_string,
|
|
548
|
+
message: check.message
|
|
549
|
+
});
|
|
550
|
+
status.dirty();
|
|
551
|
+
}
|
|
552
|
+
} else if (check.kind === "cuid2") {
|
|
553
|
+
if (!cuid2Regex.test(input.data)) {
|
|
554
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
555
|
+
addIssueToContext(ctx, {
|
|
556
|
+
validation: "cuid2",
|
|
557
|
+
code: ZodIssueCode.invalid_string,
|
|
558
|
+
message: check.message
|
|
559
|
+
});
|
|
560
|
+
status.dirty();
|
|
561
|
+
}
|
|
562
|
+
} else if (check.kind === "ulid") {
|
|
563
|
+
if (!ulidRegex.test(input.data)) {
|
|
564
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
565
|
+
addIssueToContext(ctx, {
|
|
566
|
+
validation: "ulid",
|
|
567
|
+
code: ZodIssueCode.invalid_string,
|
|
568
|
+
message: check.message
|
|
569
|
+
});
|
|
570
|
+
status.dirty();
|
|
571
|
+
}
|
|
572
|
+
} else if (check.kind === "url") {
|
|
573
|
+
try {
|
|
574
|
+
new URL(input.data);
|
|
575
|
+
} catch {
|
|
576
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
577
|
+
addIssueToContext(ctx, {
|
|
578
|
+
validation: "url",
|
|
579
|
+
code: ZodIssueCode.invalid_string,
|
|
580
|
+
message: check.message
|
|
581
|
+
});
|
|
582
|
+
status.dirty();
|
|
583
|
+
}
|
|
584
|
+
} else if (check.kind === "regex") {
|
|
585
|
+
check.regex.lastIndex = 0;
|
|
586
|
+
const testResult = check.regex.test(input.data);
|
|
587
|
+
if (!testResult) {
|
|
588
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
589
|
+
addIssueToContext(ctx, {
|
|
590
|
+
validation: "regex",
|
|
591
|
+
code: ZodIssueCode.invalid_string,
|
|
592
|
+
message: check.message
|
|
593
|
+
});
|
|
594
|
+
status.dirty();
|
|
595
|
+
}
|
|
596
|
+
} else if (check.kind === "trim") {
|
|
597
|
+
input.data = input.data.trim();
|
|
598
|
+
} else if (check.kind === "includes") {
|
|
599
|
+
if (!input.data.includes(check.value, check.position)) {
|
|
600
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
601
|
+
addIssueToContext(ctx, {
|
|
602
|
+
code: ZodIssueCode.invalid_string,
|
|
603
|
+
validation: { includes: check.value, position: check.position },
|
|
604
|
+
message: check.message
|
|
605
|
+
});
|
|
606
|
+
status.dirty();
|
|
607
|
+
}
|
|
608
|
+
} else if (check.kind === "toLowerCase") {
|
|
609
|
+
input.data = input.data.toLowerCase();
|
|
610
|
+
} else if (check.kind === "toUpperCase") {
|
|
611
|
+
input.data = input.data.toUpperCase();
|
|
612
|
+
} else if (check.kind === "startsWith") {
|
|
613
|
+
if (!input.data.startsWith(check.value)) {
|
|
614
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
615
|
+
addIssueToContext(ctx, {
|
|
616
|
+
code: ZodIssueCode.invalid_string,
|
|
617
|
+
validation: { startsWith: check.value },
|
|
618
|
+
message: check.message
|
|
619
|
+
});
|
|
620
|
+
status.dirty();
|
|
621
|
+
}
|
|
622
|
+
} else if (check.kind === "endsWith") {
|
|
623
|
+
if (!input.data.endsWith(check.value)) {
|
|
624
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
625
|
+
addIssueToContext(ctx, {
|
|
626
|
+
code: ZodIssueCode.invalid_string,
|
|
627
|
+
validation: { endsWith: check.value },
|
|
628
|
+
message: check.message
|
|
629
|
+
});
|
|
630
|
+
status.dirty();
|
|
631
|
+
}
|
|
632
|
+
} else if (check.kind === "datetime") {
|
|
633
|
+
const regex = datetimeRegex(check);
|
|
634
|
+
if (!regex.test(input.data)) {
|
|
635
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
636
|
+
addIssueToContext(ctx, {
|
|
637
|
+
code: ZodIssueCode.invalid_string,
|
|
638
|
+
validation: "datetime",
|
|
639
|
+
message: check.message
|
|
640
|
+
});
|
|
641
|
+
status.dirty();
|
|
642
|
+
}
|
|
643
|
+
} else if (check.kind === "date") {
|
|
644
|
+
const regex = dateRegex;
|
|
645
|
+
if (!regex.test(input.data)) {
|
|
646
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
647
|
+
addIssueToContext(ctx, {
|
|
648
|
+
code: ZodIssueCode.invalid_string,
|
|
649
|
+
validation: "date",
|
|
650
|
+
message: check.message
|
|
651
|
+
});
|
|
652
|
+
status.dirty();
|
|
653
|
+
}
|
|
654
|
+
} else if (check.kind === "time") {
|
|
655
|
+
const regex = timeRegex(check);
|
|
656
|
+
if (!regex.test(input.data)) {
|
|
657
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
658
|
+
addIssueToContext(ctx, {
|
|
659
|
+
code: ZodIssueCode.invalid_string,
|
|
660
|
+
validation: "time",
|
|
661
|
+
message: check.message
|
|
662
|
+
});
|
|
663
|
+
status.dirty();
|
|
664
|
+
}
|
|
665
|
+
} else if (check.kind === "duration") {
|
|
666
|
+
if (!durationRegex.test(input.data)) {
|
|
667
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
668
|
+
addIssueToContext(ctx, {
|
|
669
|
+
validation: "duration",
|
|
670
|
+
code: ZodIssueCode.invalid_string,
|
|
671
|
+
message: check.message
|
|
672
|
+
});
|
|
673
|
+
status.dirty();
|
|
674
|
+
}
|
|
675
|
+
} else if (check.kind === "ip") {
|
|
676
|
+
if (!isValidIP(input.data, check.version)) {
|
|
677
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
678
|
+
addIssueToContext(ctx, {
|
|
679
|
+
validation: "ip",
|
|
680
|
+
code: ZodIssueCode.invalid_string,
|
|
681
|
+
message: check.message
|
|
682
|
+
});
|
|
683
|
+
status.dirty();
|
|
684
|
+
}
|
|
685
|
+
} else if (check.kind === "jwt") {
|
|
686
|
+
if (!isValidJWT(input.data, check.alg)) {
|
|
687
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
688
|
+
addIssueToContext(ctx, {
|
|
689
|
+
validation: "jwt",
|
|
690
|
+
code: ZodIssueCode.invalid_string,
|
|
691
|
+
message: check.message
|
|
692
|
+
});
|
|
693
|
+
status.dirty();
|
|
694
|
+
}
|
|
695
|
+
} else if (check.kind === "cidr") {
|
|
696
|
+
if (!isValidCidr(input.data, check.version)) {
|
|
697
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
698
|
+
addIssueToContext(ctx, {
|
|
699
|
+
validation: "cidr",
|
|
700
|
+
code: ZodIssueCode.invalid_string,
|
|
701
|
+
message: check.message
|
|
702
|
+
});
|
|
703
|
+
status.dirty();
|
|
704
|
+
}
|
|
705
|
+
} else if (check.kind === "base64") {
|
|
706
|
+
if (!base64Regex.test(input.data)) {
|
|
707
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
708
|
+
addIssueToContext(ctx, {
|
|
709
|
+
validation: "base64",
|
|
710
|
+
code: ZodIssueCode.invalid_string,
|
|
711
|
+
message: check.message
|
|
712
|
+
});
|
|
713
|
+
status.dirty();
|
|
714
|
+
}
|
|
715
|
+
} else if (check.kind === "base64url") {
|
|
716
|
+
if (!base64urlRegex.test(input.data)) {
|
|
717
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
718
|
+
addIssueToContext(ctx, {
|
|
719
|
+
validation: "base64url",
|
|
720
|
+
code: ZodIssueCode.invalid_string,
|
|
721
|
+
message: check.message
|
|
722
|
+
});
|
|
723
|
+
status.dirty();
|
|
724
|
+
}
|
|
725
|
+
} else {
|
|
726
|
+
util.assertNever(check);
|
|
727
|
+
}
|
|
728
|
+
}
|
|
729
|
+
return { status: status.value, value: input.data };
|
|
730
|
+
}
|
|
731
|
+
_regex(regex, validation, message) {
|
|
732
|
+
return this.refinement((data) => regex.test(data), {
|
|
733
|
+
validation,
|
|
734
|
+
code: ZodIssueCode.invalid_string,
|
|
735
|
+
...errorUtil.errToObj(message)
|
|
736
|
+
});
|
|
737
|
+
}
|
|
738
|
+
_addCheck(check) {
|
|
739
|
+
return new ZodString({
|
|
740
|
+
...this._def,
|
|
741
|
+
checks: [...this._def.checks, check]
|
|
742
|
+
});
|
|
743
|
+
}
|
|
744
|
+
email(message) {
|
|
745
|
+
return this._addCheck({ kind: "email", ...errorUtil.errToObj(message) });
|
|
746
|
+
}
|
|
747
|
+
url(message) {
|
|
748
|
+
return this._addCheck({ kind: "url", ...errorUtil.errToObj(message) });
|
|
749
|
+
}
|
|
750
|
+
emoji(message) {
|
|
751
|
+
return this._addCheck({ kind: "emoji", ...errorUtil.errToObj(message) });
|
|
752
|
+
}
|
|
753
|
+
uuid(message) {
|
|
754
|
+
return this._addCheck({ kind: "uuid", ...errorUtil.errToObj(message) });
|
|
755
|
+
}
|
|
756
|
+
nanoid(message) {
|
|
757
|
+
return this._addCheck({ kind: "nanoid", ...errorUtil.errToObj(message) });
|
|
758
|
+
}
|
|
759
|
+
cuid(message) {
|
|
760
|
+
return this._addCheck({ kind: "cuid", ...errorUtil.errToObj(message) });
|
|
761
|
+
}
|
|
762
|
+
cuid2(message) {
|
|
763
|
+
return this._addCheck({ kind: "cuid2", ...errorUtil.errToObj(message) });
|
|
764
|
+
}
|
|
765
|
+
ulid(message) {
|
|
766
|
+
return this._addCheck({ kind: "ulid", ...errorUtil.errToObj(message) });
|
|
767
|
+
}
|
|
768
|
+
base64(message) {
|
|
769
|
+
return this._addCheck({ kind: "base64", ...errorUtil.errToObj(message) });
|
|
770
|
+
}
|
|
771
|
+
base64url(message) {
|
|
772
|
+
return this._addCheck({
|
|
773
|
+
kind: "base64url",
|
|
774
|
+
...errorUtil.errToObj(message)
|
|
775
|
+
});
|
|
776
|
+
}
|
|
777
|
+
jwt(options) {
|
|
778
|
+
return this._addCheck({ kind: "jwt", ...errorUtil.errToObj(options) });
|
|
779
|
+
}
|
|
780
|
+
ip(options) {
|
|
781
|
+
return this._addCheck({ kind: "ip", ...errorUtil.errToObj(options) });
|
|
782
|
+
}
|
|
783
|
+
cidr(options) {
|
|
784
|
+
return this._addCheck({ kind: "cidr", ...errorUtil.errToObj(options) });
|
|
785
|
+
}
|
|
786
|
+
datetime(options) {
|
|
787
|
+
if (typeof options === "string") {
|
|
788
|
+
return this._addCheck({
|
|
789
|
+
kind: "datetime",
|
|
790
|
+
precision: null,
|
|
791
|
+
offset: false,
|
|
792
|
+
local: false,
|
|
793
|
+
message: options
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
return this._addCheck({
|
|
797
|
+
kind: "datetime",
|
|
798
|
+
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
799
|
+
offset: options?.offset ?? false,
|
|
800
|
+
local: options?.local ?? false,
|
|
801
|
+
...errorUtil.errToObj(options?.message)
|
|
802
|
+
});
|
|
803
|
+
}
|
|
804
|
+
date(message) {
|
|
805
|
+
return this._addCheck({ kind: "date", message });
|
|
806
|
+
}
|
|
807
|
+
time(options) {
|
|
808
|
+
if (typeof options === "string") {
|
|
809
|
+
return this._addCheck({
|
|
810
|
+
kind: "time",
|
|
811
|
+
precision: null,
|
|
812
|
+
message: options
|
|
813
|
+
});
|
|
814
|
+
}
|
|
815
|
+
return this._addCheck({
|
|
816
|
+
kind: "time",
|
|
817
|
+
precision: typeof options?.precision === "undefined" ? null : options?.precision,
|
|
818
|
+
...errorUtil.errToObj(options?.message)
|
|
819
|
+
});
|
|
820
|
+
}
|
|
821
|
+
duration(message) {
|
|
822
|
+
return this._addCheck({ kind: "duration", ...errorUtil.errToObj(message) });
|
|
823
|
+
}
|
|
824
|
+
regex(regex, message) {
|
|
825
|
+
return this._addCheck({
|
|
826
|
+
kind: "regex",
|
|
827
|
+
regex,
|
|
828
|
+
...errorUtil.errToObj(message)
|
|
829
|
+
});
|
|
830
|
+
}
|
|
831
|
+
includes(value, options) {
|
|
832
|
+
return this._addCheck({
|
|
833
|
+
kind: "includes",
|
|
834
|
+
value,
|
|
835
|
+
position: options?.position,
|
|
836
|
+
...errorUtil.errToObj(options?.message)
|
|
837
|
+
});
|
|
838
|
+
}
|
|
839
|
+
startsWith(value, message) {
|
|
840
|
+
return this._addCheck({
|
|
841
|
+
kind: "startsWith",
|
|
842
|
+
value,
|
|
843
|
+
...errorUtil.errToObj(message)
|
|
844
|
+
});
|
|
845
|
+
}
|
|
846
|
+
endsWith(value, message) {
|
|
847
|
+
return this._addCheck({
|
|
848
|
+
kind: "endsWith",
|
|
849
|
+
value,
|
|
850
|
+
...errorUtil.errToObj(message)
|
|
851
|
+
});
|
|
852
|
+
}
|
|
853
|
+
min(minLength, message) {
|
|
854
|
+
return this._addCheck({
|
|
855
|
+
kind: "min",
|
|
856
|
+
value: minLength,
|
|
857
|
+
...errorUtil.errToObj(message)
|
|
858
|
+
});
|
|
859
|
+
}
|
|
860
|
+
max(maxLength, message) {
|
|
861
|
+
return this._addCheck({
|
|
862
|
+
kind: "max",
|
|
863
|
+
value: maxLength,
|
|
864
|
+
...errorUtil.errToObj(message)
|
|
865
|
+
});
|
|
866
|
+
}
|
|
867
|
+
length(len, message) {
|
|
868
|
+
return this._addCheck({
|
|
869
|
+
kind: "length",
|
|
870
|
+
value: len,
|
|
871
|
+
...errorUtil.errToObj(message)
|
|
872
|
+
});
|
|
96
873
|
}
|
|
97
874
|
/**
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
875
|
+
* Equivalent to `.min(1)`
|
|
876
|
+
*/
|
|
877
|
+
nonempty(message) {
|
|
878
|
+
return this.min(1, errorUtil.errToObj(message));
|
|
879
|
+
}
|
|
880
|
+
trim() {
|
|
881
|
+
return new ZodString({
|
|
882
|
+
...this._def,
|
|
883
|
+
checks: [...this._def.checks, { kind: "trim" }]
|
|
884
|
+
});
|
|
885
|
+
}
|
|
886
|
+
toLowerCase() {
|
|
887
|
+
return new ZodString({
|
|
888
|
+
...this._def,
|
|
889
|
+
checks: [...this._def.checks, { kind: "toLowerCase" }]
|
|
890
|
+
});
|
|
891
|
+
}
|
|
892
|
+
toUpperCase() {
|
|
893
|
+
return new ZodString({
|
|
894
|
+
...this._def,
|
|
895
|
+
checks: [...this._def.checks, { kind: "toUpperCase" }]
|
|
896
|
+
});
|
|
897
|
+
}
|
|
898
|
+
get isDatetime() {
|
|
899
|
+
return !!this._def.checks.find((ch) => ch.kind === "datetime");
|
|
900
|
+
}
|
|
901
|
+
get isDate() {
|
|
902
|
+
return !!this._def.checks.find((ch) => ch.kind === "date");
|
|
903
|
+
}
|
|
904
|
+
get isTime() {
|
|
905
|
+
return !!this._def.checks.find((ch) => ch.kind === "time");
|
|
906
|
+
}
|
|
907
|
+
get isDuration() {
|
|
908
|
+
return !!this._def.checks.find((ch) => ch.kind === "duration");
|
|
909
|
+
}
|
|
910
|
+
get isEmail() {
|
|
911
|
+
return !!this._def.checks.find((ch) => ch.kind === "email");
|
|
912
|
+
}
|
|
913
|
+
get isURL() {
|
|
914
|
+
return !!this._def.checks.find((ch) => ch.kind === "url");
|
|
915
|
+
}
|
|
916
|
+
get isEmoji() {
|
|
917
|
+
return !!this._def.checks.find((ch) => ch.kind === "emoji");
|
|
918
|
+
}
|
|
919
|
+
get isUUID() {
|
|
920
|
+
return !!this._def.checks.find((ch) => ch.kind === "uuid");
|
|
921
|
+
}
|
|
922
|
+
get isNANOID() {
|
|
923
|
+
return !!this._def.checks.find((ch) => ch.kind === "nanoid");
|
|
924
|
+
}
|
|
925
|
+
get isCUID() {
|
|
926
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid");
|
|
927
|
+
}
|
|
928
|
+
get isCUID2() {
|
|
929
|
+
return !!this._def.checks.find((ch) => ch.kind === "cuid2");
|
|
930
|
+
}
|
|
931
|
+
get isULID() {
|
|
932
|
+
return !!this._def.checks.find((ch) => ch.kind === "ulid");
|
|
933
|
+
}
|
|
934
|
+
get isIP() {
|
|
935
|
+
return !!this._def.checks.find((ch) => ch.kind === "ip");
|
|
936
|
+
}
|
|
937
|
+
get isCIDR() {
|
|
938
|
+
return !!this._def.checks.find((ch) => ch.kind === "cidr");
|
|
939
|
+
}
|
|
940
|
+
get isBase64() {
|
|
941
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64");
|
|
942
|
+
}
|
|
943
|
+
get isBase64url() {
|
|
944
|
+
return !!this._def.checks.find((ch) => ch.kind === "base64url");
|
|
945
|
+
}
|
|
946
|
+
get minLength() {
|
|
947
|
+
let min = null;
|
|
948
|
+
for (const ch of this._def.checks) {
|
|
949
|
+
if (ch.kind === "min") {
|
|
950
|
+
if (min === null || ch.value > min)
|
|
951
|
+
min = ch.value;
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
return min;
|
|
955
|
+
}
|
|
956
|
+
get maxLength() {
|
|
957
|
+
let max = null;
|
|
958
|
+
for (const ch of this._def.checks) {
|
|
959
|
+
if (ch.kind === "max") {
|
|
960
|
+
if (max === null || ch.value < max)
|
|
961
|
+
max = ch.value;
|
|
962
|
+
}
|
|
963
|
+
}
|
|
964
|
+
return max;
|
|
965
|
+
}
|
|
966
|
+
}
|
|
967
|
+
ZodString.create = (params) => {
|
|
968
|
+
return new ZodString({
|
|
969
|
+
checks: [],
|
|
970
|
+
typeName: ZodFirstPartyTypeKind.ZodString,
|
|
971
|
+
coerce: params?.coerce ?? false,
|
|
972
|
+
...processCreateParams(params)
|
|
973
|
+
});
|
|
974
|
+
};
|
|
975
|
+
function floatSafeRemainder(val, step) {
|
|
976
|
+
const valDecCount = (val.toString().split(".")[1] || "").length;
|
|
977
|
+
const stepDecCount = (step.toString().split(".")[1] || "").length;
|
|
978
|
+
const decCount = valDecCount > stepDecCount ? valDecCount : stepDecCount;
|
|
979
|
+
const valInt = Number.parseInt(val.toFixed(decCount).replace(".", ""));
|
|
980
|
+
const stepInt = Number.parseInt(step.toFixed(decCount).replace(".", ""));
|
|
981
|
+
return valInt % stepInt / 10 ** decCount;
|
|
982
|
+
}
|
|
983
|
+
class ZodNumber extends ZodType {
|
|
984
|
+
constructor() {
|
|
985
|
+
super(...arguments);
|
|
986
|
+
this.min = this.gte;
|
|
987
|
+
this.max = this.lte;
|
|
988
|
+
this.step = this.multipleOf;
|
|
989
|
+
}
|
|
990
|
+
_parse(input) {
|
|
991
|
+
if (this._def.coerce) {
|
|
992
|
+
input.data = Number(input.data);
|
|
993
|
+
}
|
|
994
|
+
const parsedType = this._getType(input);
|
|
995
|
+
if (parsedType !== ZodParsedType.number) {
|
|
996
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
997
|
+
addIssueToContext(ctx2, {
|
|
998
|
+
code: ZodIssueCode.invalid_type,
|
|
999
|
+
expected: ZodParsedType.number,
|
|
1000
|
+
received: ctx2.parsedType
|
|
1001
|
+
});
|
|
1002
|
+
return INVALID;
|
|
1003
|
+
}
|
|
1004
|
+
let ctx = void 0;
|
|
1005
|
+
const status = new ParseStatus();
|
|
1006
|
+
for (const check of this._def.checks) {
|
|
1007
|
+
if (check.kind === "int") {
|
|
1008
|
+
if (!util.isInteger(input.data)) {
|
|
1009
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1010
|
+
addIssueToContext(ctx, {
|
|
1011
|
+
code: ZodIssueCode.invalid_type,
|
|
1012
|
+
expected: "integer",
|
|
1013
|
+
received: "float",
|
|
1014
|
+
message: check.message
|
|
1015
|
+
});
|
|
1016
|
+
status.dirty();
|
|
1017
|
+
}
|
|
1018
|
+
} else if (check.kind === "min") {
|
|
1019
|
+
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
1020
|
+
if (tooSmall) {
|
|
1021
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1022
|
+
addIssueToContext(ctx, {
|
|
1023
|
+
code: ZodIssueCode.too_small,
|
|
1024
|
+
minimum: check.value,
|
|
1025
|
+
type: "number",
|
|
1026
|
+
inclusive: check.inclusive,
|
|
1027
|
+
exact: false,
|
|
1028
|
+
message: check.message
|
|
1029
|
+
});
|
|
1030
|
+
status.dirty();
|
|
1031
|
+
}
|
|
1032
|
+
} else if (check.kind === "max") {
|
|
1033
|
+
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
1034
|
+
if (tooBig) {
|
|
1035
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1036
|
+
addIssueToContext(ctx, {
|
|
1037
|
+
code: ZodIssueCode.too_big,
|
|
1038
|
+
maximum: check.value,
|
|
1039
|
+
type: "number",
|
|
1040
|
+
inclusive: check.inclusive,
|
|
1041
|
+
exact: false,
|
|
1042
|
+
message: check.message
|
|
1043
|
+
});
|
|
1044
|
+
status.dirty();
|
|
1045
|
+
}
|
|
1046
|
+
} else if (check.kind === "multipleOf") {
|
|
1047
|
+
if (floatSafeRemainder(input.data, check.value) !== 0) {
|
|
1048
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1049
|
+
addIssueToContext(ctx, {
|
|
1050
|
+
code: ZodIssueCode.not_multiple_of,
|
|
1051
|
+
multipleOf: check.value,
|
|
1052
|
+
message: check.message
|
|
1053
|
+
});
|
|
1054
|
+
status.dirty();
|
|
1055
|
+
}
|
|
1056
|
+
} else if (check.kind === "finite") {
|
|
1057
|
+
if (!Number.isFinite(input.data)) {
|
|
1058
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1059
|
+
addIssueToContext(ctx, {
|
|
1060
|
+
code: ZodIssueCode.not_finite,
|
|
1061
|
+
message: check.message
|
|
1062
|
+
});
|
|
1063
|
+
status.dirty();
|
|
1064
|
+
}
|
|
1065
|
+
} else {
|
|
1066
|
+
util.assertNever(check);
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
return { status: status.value, value: input.data };
|
|
1070
|
+
}
|
|
1071
|
+
gte(value, message) {
|
|
1072
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
1073
|
+
}
|
|
1074
|
+
gt(value, message) {
|
|
1075
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
1076
|
+
}
|
|
1077
|
+
lte(value, message) {
|
|
1078
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
1079
|
+
}
|
|
1080
|
+
lt(value, message) {
|
|
1081
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
1082
|
+
}
|
|
1083
|
+
setLimit(kind, value, inclusive, message) {
|
|
1084
|
+
return new ZodNumber({
|
|
1085
|
+
...this._def,
|
|
1086
|
+
checks: [
|
|
1087
|
+
...this._def.checks,
|
|
1088
|
+
{
|
|
1089
|
+
kind,
|
|
1090
|
+
value,
|
|
1091
|
+
inclusive,
|
|
1092
|
+
message: errorUtil.toString(message)
|
|
1093
|
+
}
|
|
1094
|
+
]
|
|
1095
|
+
});
|
|
1096
|
+
}
|
|
1097
|
+
_addCheck(check) {
|
|
1098
|
+
return new ZodNumber({
|
|
1099
|
+
...this._def,
|
|
1100
|
+
checks: [...this._def.checks, check]
|
|
1101
|
+
});
|
|
1102
|
+
}
|
|
1103
|
+
int(message) {
|
|
1104
|
+
return this._addCheck({
|
|
1105
|
+
kind: "int",
|
|
1106
|
+
message: errorUtil.toString(message)
|
|
1107
|
+
});
|
|
1108
|
+
}
|
|
1109
|
+
positive(message) {
|
|
1110
|
+
return this._addCheck({
|
|
1111
|
+
kind: "min",
|
|
1112
|
+
value: 0,
|
|
1113
|
+
inclusive: false,
|
|
1114
|
+
message: errorUtil.toString(message)
|
|
1115
|
+
});
|
|
1116
|
+
}
|
|
1117
|
+
negative(message) {
|
|
1118
|
+
return this._addCheck({
|
|
1119
|
+
kind: "max",
|
|
1120
|
+
value: 0,
|
|
1121
|
+
inclusive: false,
|
|
1122
|
+
message: errorUtil.toString(message)
|
|
1123
|
+
});
|
|
1124
|
+
}
|
|
1125
|
+
nonpositive(message) {
|
|
1126
|
+
return this._addCheck({
|
|
1127
|
+
kind: "max",
|
|
1128
|
+
value: 0,
|
|
1129
|
+
inclusive: true,
|
|
1130
|
+
message: errorUtil.toString(message)
|
|
1131
|
+
});
|
|
1132
|
+
}
|
|
1133
|
+
nonnegative(message) {
|
|
1134
|
+
return this._addCheck({
|
|
1135
|
+
kind: "min",
|
|
1136
|
+
value: 0,
|
|
1137
|
+
inclusive: true,
|
|
1138
|
+
message: errorUtil.toString(message)
|
|
1139
|
+
});
|
|
1140
|
+
}
|
|
1141
|
+
multipleOf(value, message) {
|
|
1142
|
+
return this._addCheck({
|
|
1143
|
+
kind: "multipleOf",
|
|
1144
|
+
value,
|
|
1145
|
+
message: errorUtil.toString(message)
|
|
1146
|
+
});
|
|
1147
|
+
}
|
|
1148
|
+
finite(message) {
|
|
1149
|
+
return this._addCheck({
|
|
1150
|
+
kind: "finite",
|
|
1151
|
+
message: errorUtil.toString(message)
|
|
1152
|
+
});
|
|
1153
|
+
}
|
|
1154
|
+
safe(message) {
|
|
1155
|
+
return this._addCheck({
|
|
1156
|
+
kind: "min",
|
|
1157
|
+
inclusive: true,
|
|
1158
|
+
value: Number.MIN_SAFE_INTEGER,
|
|
1159
|
+
message: errorUtil.toString(message)
|
|
1160
|
+
})._addCheck({
|
|
1161
|
+
kind: "max",
|
|
1162
|
+
inclusive: true,
|
|
1163
|
+
value: Number.MAX_SAFE_INTEGER,
|
|
1164
|
+
message: errorUtil.toString(message)
|
|
1165
|
+
});
|
|
1166
|
+
}
|
|
1167
|
+
get minValue() {
|
|
1168
|
+
let min = null;
|
|
1169
|
+
for (const ch of this._def.checks) {
|
|
1170
|
+
if (ch.kind === "min") {
|
|
1171
|
+
if (min === null || ch.value > min)
|
|
1172
|
+
min = ch.value;
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
return min;
|
|
1176
|
+
}
|
|
1177
|
+
get maxValue() {
|
|
1178
|
+
let max = null;
|
|
1179
|
+
for (const ch of this._def.checks) {
|
|
1180
|
+
if (ch.kind === "max") {
|
|
1181
|
+
if (max === null || ch.value < max)
|
|
1182
|
+
max = ch.value;
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
return max;
|
|
1186
|
+
}
|
|
1187
|
+
get isInt() {
|
|
1188
|
+
return !!this._def.checks.find((ch) => ch.kind === "int" || ch.kind === "multipleOf" && util.isInteger(ch.value));
|
|
1189
|
+
}
|
|
1190
|
+
get isFinite() {
|
|
1191
|
+
let max = null;
|
|
1192
|
+
let min = null;
|
|
1193
|
+
for (const ch of this._def.checks) {
|
|
1194
|
+
if (ch.kind === "finite" || ch.kind === "int" || ch.kind === "multipleOf") {
|
|
1195
|
+
return true;
|
|
1196
|
+
} else if (ch.kind === "min") {
|
|
1197
|
+
if (min === null || ch.value > min)
|
|
1198
|
+
min = ch.value;
|
|
1199
|
+
} else if (ch.kind === "max") {
|
|
1200
|
+
if (max === null || ch.value < max)
|
|
1201
|
+
max = ch.value;
|
|
1202
|
+
}
|
|
1203
|
+
}
|
|
1204
|
+
return Number.isFinite(min) && Number.isFinite(max);
|
|
1205
|
+
}
|
|
1206
|
+
}
|
|
1207
|
+
ZodNumber.create = (params) => {
|
|
1208
|
+
return new ZodNumber({
|
|
1209
|
+
checks: [],
|
|
1210
|
+
typeName: ZodFirstPartyTypeKind.ZodNumber,
|
|
1211
|
+
coerce: params?.coerce || false,
|
|
1212
|
+
...processCreateParams(params)
|
|
1213
|
+
});
|
|
1214
|
+
};
|
|
1215
|
+
class ZodBigInt extends ZodType {
|
|
1216
|
+
constructor() {
|
|
1217
|
+
super(...arguments);
|
|
1218
|
+
this.min = this.gte;
|
|
1219
|
+
this.max = this.lte;
|
|
1220
|
+
}
|
|
1221
|
+
_parse(input) {
|
|
1222
|
+
if (this._def.coerce) {
|
|
1223
|
+
try {
|
|
1224
|
+
input.data = BigInt(input.data);
|
|
1225
|
+
} catch {
|
|
1226
|
+
return this._getInvalidInput(input);
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
const parsedType = this._getType(input);
|
|
1230
|
+
if (parsedType !== ZodParsedType.bigint) {
|
|
1231
|
+
return this._getInvalidInput(input);
|
|
1232
|
+
}
|
|
1233
|
+
let ctx = void 0;
|
|
1234
|
+
const status = new ParseStatus();
|
|
1235
|
+
for (const check of this._def.checks) {
|
|
1236
|
+
if (check.kind === "min") {
|
|
1237
|
+
const tooSmall = check.inclusive ? input.data < check.value : input.data <= check.value;
|
|
1238
|
+
if (tooSmall) {
|
|
1239
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1240
|
+
addIssueToContext(ctx, {
|
|
1241
|
+
code: ZodIssueCode.too_small,
|
|
1242
|
+
type: "bigint",
|
|
1243
|
+
minimum: check.value,
|
|
1244
|
+
inclusive: check.inclusive,
|
|
1245
|
+
message: check.message
|
|
1246
|
+
});
|
|
1247
|
+
status.dirty();
|
|
1248
|
+
}
|
|
1249
|
+
} else if (check.kind === "max") {
|
|
1250
|
+
const tooBig = check.inclusive ? input.data > check.value : input.data >= check.value;
|
|
1251
|
+
if (tooBig) {
|
|
1252
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1253
|
+
addIssueToContext(ctx, {
|
|
1254
|
+
code: ZodIssueCode.too_big,
|
|
1255
|
+
type: "bigint",
|
|
1256
|
+
maximum: check.value,
|
|
1257
|
+
inclusive: check.inclusive,
|
|
1258
|
+
message: check.message
|
|
1259
|
+
});
|
|
1260
|
+
status.dirty();
|
|
1261
|
+
}
|
|
1262
|
+
} else if (check.kind === "multipleOf") {
|
|
1263
|
+
if (input.data % check.value !== BigInt(0)) {
|
|
1264
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1265
|
+
addIssueToContext(ctx, {
|
|
1266
|
+
code: ZodIssueCode.not_multiple_of,
|
|
1267
|
+
multipleOf: check.value,
|
|
1268
|
+
message: check.message
|
|
1269
|
+
});
|
|
1270
|
+
status.dirty();
|
|
1271
|
+
}
|
|
1272
|
+
} else {
|
|
1273
|
+
util.assertNever(check);
|
|
1274
|
+
}
|
|
1275
|
+
}
|
|
1276
|
+
return { status: status.value, value: input.data };
|
|
1277
|
+
}
|
|
1278
|
+
_getInvalidInput(input) {
|
|
1279
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1280
|
+
addIssueToContext(ctx, {
|
|
1281
|
+
code: ZodIssueCode.invalid_type,
|
|
1282
|
+
expected: ZodParsedType.bigint,
|
|
1283
|
+
received: ctx.parsedType
|
|
1284
|
+
});
|
|
1285
|
+
return INVALID;
|
|
1286
|
+
}
|
|
1287
|
+
gte(value, message) {
|
|
1288
|
+
return this.setLimit("min", value, true, errorUtil.toString(message));
|
|
1289
|
+
}
|
|
1290
|
+
gt(value, message) {
|
|
1291
|
+
return this.setLimit("min", value, false, errorUtil.toString(message));
|
|
1292
|
+
}
|
|
1293
|
+
lte(value, message) {
|
|
1294
|
+
return this.setLimit("max", value, true, errorUtil.toString(message));
|
|
1295
|
+
}
|
|
1296
|
+
lt(value, message) {
|
|
1297
|
+
return this.setLimit("max", value, false, errorUtil.toString(message));
|
|
1298
|
+
}
|
|
1299
|
+
setLimit(kind, value, inclusive, message) {
|
|
1300
|
+
return new ZodBigInt({
|
|
1301
|
+
...this._def,
|
|
1302
|
+
checks: [
|
|
1303
|
+
...this._def.checks,
|
|
1304
|
+
{
|
|
1305
|
+
kind,
|
|
1306
|
+
value,
|
|
1307
|
+
inclusive,
|
|
1308
|
+
message: errorUtil.toString(message)
|
|
1309
|
+
}
|
|
1310
|
+
]
|
|
1311
|
+
});
|
|
1312
|
+
}
|
|
1313
|
+
_addCheck(check) {
|
|
1314
|
+
return new ZodBigInt({
|
|
1315
|
+
...this._def,
|
|
1316
|
+
checks: [...this._def.checks, check]
|
|
1317
|
+
});
|
|
1318
|
+
}
|
|
1319
|
+
positive(message) {
|
|
1320
|
+
return this._addCheck({
|
|
1321
|
+
kind: "min",
|
|
1322
|
+
value: BigInt(0),
|
|
1323
|
+
inclusive: false,
|
|
1324
|
+
message: errorUtil.toString(message)
|
|
1325
|
+
});
|
|
1326
|
+
}
|
|
1327
|
+
negative(message) {
|
|
1328
|
+
return this._addCheck({
|
|
1329
|
+
kind: "max",
|
|
1330
|
+
value: BigInt(0),
|
|
1331
|
+
inclusive: false,
|
|
1332
|
+
message: errorUtil.toString(message)
|
|
1333
|
+
});
|
|
1334
|
+
}
|
|
1335
|
+
nonpositive(message) {
|
|
1336
|
+
return this._addCheck({
|
|
1337
|
+
kind: "max",
|
|
1338
|
+
value: BigInt(0),
|
|
1339
|
+
inclusive: true,
|
|
1340
|
+
message: errorUtil.toString(message)
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1343
|
+
nonnegative(message) {
|
|
1344
|
+
return this._addCheck({
|
|
1345
|
+
kind: "min",
|
|
1346
|
+
value: BigInt(0),
|
|
1347
|
+
inclusive: true,
|
|
1348
|
+
message: errorUtil.toString(message)
|
|
1349
|
+
});
|
|
1350
|
+
}
|
|
1351
|
+
multipleOf(value, message) {
|
|
1352
|
+
return this._addCheck({
|
|
1353
|
+
kind: "multipleOf",
|
|
1354
|
+
value,
|
|
1355
|
+
message: errorUtil.toString(message)
|
|
1356
|
+
});
|
|
1357
|
+
}
|
|
1358
|
+
get minValue() {
|
|
1359
|
+
let min = null;
|
|
1360
|
+
for (const ch of this._def.checks) {
|
|
1361
|
+
if (ch.kind === "min") {
|
|
1362
|
+
if (min === null || ch.value > min)
|
|
1363
|
+
min = ch.value;
|
|
1364
|
+
}
|
|
1365
|
+
}
|
|
1366
|
+
return min;
|
|
1367
|
+
}
|
|
1368
|
+
get maxValue() {
|
|
1369
|
+
let max = null;
|
|
1370
|
+
for (const ch of this._def.checks) {
|
|
1371
|
+
if (ch.kind === "max") {
|
|
1372
|
+
if (max === null || ch.value < max)
|
|
1373
|
+
max = ch.value;
|
|
1374
|
+
}
|
|
1375
|
+
}
|
|
1376
|
+
return max;
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
ZodBigInt.create = (params) => {
|
|
1380
|
+
return new ZodBigInt({
|
|
1381
|
+
checks: [],
|
|
1382
|
+
typeName: ZodFirstPartyTypeKind.ZodBigInt,
|
|
1383
|
+
coerce: params?.coerce ?? false,
|
|
1384
|
+
...processCreateParams(params)
|
|
1385
|
+
});
|
|
1386
|
+
};
|
|
1387
|
+
class ZodBoolean extends ZodType {
|
|
1388
|
+
_parse(input) {
|
|
1389
|
+
if (this._def.coerce) {
|
|
1390
|
+
input.data = Boolean(input.data);
|
|
1391
|
+
}
|
|
1392
|
+
const parsedType = this._getType(input);
|
|
1393
|
+
if (parsedType !== ZodParsedType.boolean) {
|
|
1394
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1395
|
+
addIssueToContext(ctx, {
|
|
1396
|
+
code: ZodIssueCode.invalid_type,
|
|
1397
|
+
expected: ZodParsedType.boolean,
|
|
1398
|
+
received: ctx.parsedType
|
|
1399
|
+
});
|
|
1400
|
+
return INVALID;
|
|
1401
|
+
}
|
|
1402
|
+
return OK(input.data);
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
ZodBoolean.create = (params) => {
|
|
1406
|
+
return new ZodBoolean({
|
|
1407
|
+
typeName: ZodFirstPartyTypeKind.ZodBoolean,
|
|
1408
|
+
coerce: params?.coerce || false,
|
|
1409
|
+
...processCreateParams(params)
|
|
1410
|
+
});
|
|
1411
|
+
};
|
|
1412
|
+
class ZodDate extends ZodType {
|
|
1413
|
+
_parse(input) {
|
|
1414
|
+
if (this._def.coerce) {
|
|
1415
|
+
input.data = new Date(input.data);
|
|
1416
|
+
}
|
|
1417
|
+
const parsedType = this._getType(input);
|
|
1418
|
+
if (parsedType !== ZodParsedType.date) {
|
|
1419
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
1420
|
+
addIssueToContext(ctx2, {
|
|
1421
|
+
code: ZodIssueCode.invalid_type,
|
|
1422
|
+
expected: ZodParsedType.date,
|
|
1423
|
+
received: ctx2.parsedType
|
|
1424
|
+
});
|
|
1425
|
+
return INVALID;
|
|
1426
|
+
}
|
|
1427
|
+
if (Number.isNaN(input.data.getTime())) {
|
|
1428
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
1429
|
+
addIssueToContext(ctx2, {
|
|
1430
|
+
code: ZodIssueCode.invalid_date
|
|
1431
|
+
});
|
|
1432
|
+
return INVALID;
|
|
1433
|
+
}
|
|
1434
|
+
const status = new ParseStatus();
|
|
1435
|
+
let ctx = void 0;
|
|
1436
|
+
for (const check of this._def.checks) {
|
|
1437
|
+
if (check.kind === "min") {
|
|
1438
|
+
if (input.data.getTime() < check.value) {
|
|
1439
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1440
|
+
addIssueToContext(ctx, {
|
|
1441
|
+
code: ZodIssueCode.too_small,
|
|
1442
|
+
message: check.message,
|
|
1443
|
+
inclusive: true,
|
|
1444
|
+
exact: false,
|
|
1445
|
+
minimum: check.value,
|
|
1446
|
+
type: "date"
|
|
1447
|
+
});
|
|
1448
|
+
status.dirty();
|
|
1449
|
+
}
|
|
1450
|
+
} else if (check.kind === "max") {
|
|
1451
|
+
if (input.data.getTime() > check.value) {
|
|
1452
|
+
ctx = this._getOrReturnCtx(input, ctx);
|
|
1453
|
+
addIssueToContext(ctx, {
|
|
1454
|
+
code: ZodIssueCode.too_big,
|
|
1455
|
+
message: check.message,
|
|
1456
|
+
inclusive: true,
|
|
1457
|
+
exact: false,
|
|
1458
|
+
maximum: check.value,
|
|
1459
|
+
type: "date"
|
|
1460
|
+
});
|
|
1461
|
+
status.dirty();
|
|
1462
|
+
}
|
|
1463
|
+
} else {
|
|
1464
|
+
util.assertNever(check);
|
|
1465
|
+
}
|
|
1466
|
+
}
|
|
1467
|
+
return {
|
|
1468
|
+
status: status.value,
|
|
1469
|
+
value: new Date(input.data.getTime())
|
|
107
1470
|
};
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
1471
|
+
}
|
|
1472
|
+
_addCheck(check) {
|
|
1473
|
+
return new ZodDate({
|
|
1474
|
+
...this._def,
|
|
1475
|
+
checks: [...this._def.checks, check]
|
|
1476
|
+
});
|
|
1477
|
+
}
|
|
1478
|
+
min(minDate, message) {
|
|
1479
|
+
return this._addCheck({
|
|
1480
|
+
kind: "min",
|
|
1481
|
+
value: minDate.getTime(),
|
|
1482
|
+
message: errorUtil.toString(message)
|
|
1483
|
+
});
|
|
1484
|
+
}
|
|
1485
|
+
max(maxDate, message) {
|
|
1486
|
+
return this._addCheck({
|
|
1487
|
+
kind: "max",
|
|
1488
|
+
value: maxDate.getTime(),
|
|
1489
|
+
message: errorUtil.toString(message)
|
|
1490
|
+
});
|
|
1491
|
+
}
|
|
1492
|
+
get minDate() {
|
|
1493
|
+
let min = null;
|
|
1494
|
+
for (const ch of this._def.checks) {
|
|
1495
|
+
if (ch.kind === "min") {
|
|
1496
|
+
if (min === null || ch.value > min)
|
|
1497
|
+
min = ch.value;
|
|
1498
|
+
}
|
|
1499
|
+
}
|
|
1500
|
+
return min != null ? new Date(min) : null;
|
|
1501
|
+
}
|
|
1502
|
+
get maxDate() {
|
|
1503
|
+
let max = null;
|
|
1504
|
+
for (const ch of this._def.checks) {
|
|
1505
|
+
if (ch.kind === "max") {
|
|
1506
|
+
if (max === null || ch.value < max)
|
|
1507
|
+
max = ch.value;
|
|
1508
|
+
}
|
|
1509
|
+
}
|
|
1510
|
+
return max != null ? new Date(max) : null;
|
|
1511
|
+
}
|
|
1512
|
+
}
|
|
1513
|
+
ZodDate.create = (params) => {
|
|
1514
|
+
return new ZodDate({
|
|
1515
|
+
checks: [],
|
|
1516
|
+
coerce: params?.coerce || false,
|
|
1517
|
+
typeName: ZodFirstPartyTypeKind.ZodDate,
|
|
1518
|
+
...processCreateParams(params)
|
|
1519
|
+
});
|
|
1520
|
+
};
|
|
1521
|
+
class ZodSymbol extends ZodType {
|
|
1522
|
+
_parse(input) {
|
|
1523
|
+
const parsedType = this._getType(input);
|
|
1524
|
+
if (parsedType !== ZodParsedType.symbol) {
|
|
1525
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1526
|
+
addIssueToContext(ctx, {
|
|
1527
|
+
code: ZodIssueCode.invalid_type,
|
|
1528
|
+
expected: ZodParsedType.symbol,
|
|
1529
|
+
received: ctx.parsedType
|
|
1530
|
+
});
|
|
1531
|
+
return INVALID;
|
|
1532
|
+
}
|
|
1533
|
+
return OK(input.data);
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
ZodSymbol.create = (params) => {
|
|
1537
|
+
return new ZodSymbol({
|
|
1538
|
+
typeName: ZodFirstPartyTypeKind.ZodSymbol,
|
|
1539
|
+
...processCreateParams(params)
|
|
1540
|
+
});
|
|
1541
|
+
};
|
|
1542
|
+
class ZodUndefined extends ZodType {
|
|
1543
|
+
_parse(input) {
|
|
1544
|
+
const parsedType = this._getType(input);
|
|
1545
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
1546
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1547
|
+
addIssueToContext(ctx, {
|
|
1548
|
+
code: ZodIssueCode.invalid_type,
|
|
1549
|
+
expected: ZodParsedType.undefined,
|
|
1550
|
+
received: ctx.parsedType
|
|
1551
|
+
});
|
|
1552
|
+
return INVALID;
|
|
1553
|
+
}
|
|
1554
|
+
return OK(input.data);
|
|
1555
|
+
}
|
|
1556
|
+
}
|
|
1557
|
+
ZodUndefined.create = (params) => {
|
|
1558
|
+
return new ZodUndefined({
|
|
1559
|
+
typeName: ZodFirstPartyTypeKind.ZodUndefined,
|
|
1560
|
+
...processCreateParams(params)
|
|
1561
|
+
});
|
|
1562
|
+
};
|
|
1563
|
+
class ZodNull extends ZodType {
|
|
1564
|
+
_parse(input) {
|
|
1565
|
+
const parsedType = this._getType(input);
|
|
1566
|
+
if (parsedType !== ZodParsedType.null) {
|
|
1567
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1568
|
+
addIssueToContext(ctx, {
|
|
1569
|
+
code: ZodIssueCode.invalid_type,
|
|
1570
|
+
expected: ZodParsedType.null,
|
|
1571
|
+
received: ctx.parsedType
|
|
1572
|
+
});
|
|
1573
|
+
return INVALID;
|
|
1574
|
+
}
|
|
1575
|
+
return OK(input.data);
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
ZodNull.create = (params) => {
|
|
1579
|
+
return new ZodNull({
|
|
1580
|
+
typeName: ZodFirstPartyTypeKind.ZodNull,
|
|
1581
|
+
...processCreateParams(params)
|
|
1582
|
+
});
|
|
1583
|
+
};
|
|
1584
|
+
class ZodAny extends ZodType {
|
|
1585
|
+
constructor() {
|
|
1586
|
+
super(...arguments);
|
|
1587
|
+
this._any = true;
|
|
1588
|
+
}
|
|
1589
|
+
_parse(input) {
|
|
1590
|
+
return OK(input.data);
|
|
1591
|
+
}
|
|
1592
|
+
}
|
|
1593
|
+
ZodAny.create = (params) => {
|
|
1594
|
+
return new ZodAny({
|
|
1595
|
+
typeName: ZodFirstPartyTypeKind.ZodAny,
|
|
1596
|
+
...processCreateParams(params)
|
|
1597
|
+
});
|
|
1598
|
+
};
|
|
1599
|
+
class ZodUnknown extends ZodType {
|
|
1600
|
+
constructor() {
|
|
1601
|
+
super(...arguments);
|
|
1602
|
+
this._unknown = true;
|
|
1603
|
+
}
|
|
1604
|
+
_parse(input) {
|
|
1605
|
+
return OK(input.data);
|
|
1606
|
+
}
|
|
1607
|
+
}
|
|
1608
|
+
ZodUnknown.create = (params) => {
|
|
1609
|
+
return new ZodUnknown({
|
|
1610
|
+
typeName: ZodFirstPartyTypeKind.ZodUnknown,
|
|
1611
|
+
...processCreateParams(params)
|
|
1612
|
+
});
|
|
1613
|
+
};
|
|
1614
|
+
class ZodNever extends ZodType {
|
|
1615
|
+
_parse(input) {
|
|
1616
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1617
|
+
addIssueToContext(ctx, {
|
|
1618
|
+
code: ZodIssueCode.invalid_type,
|
|
1619
|
+
expected: ZodParsedType.never,
|
|
1620
|
+
received: ctx.parsedType
|
|
1621
|
+
});
|
|
1622
|
+
return INVALID;
|
|
1623
|
+
}
|
|
1624
|
+
}
|
|
1625
|
+
ZodNever.create = (params) => {
|
|
1626
|
+
return new ZodNever({
|
|
1627
|
+
typeName: ZodFirstPartyTypeKind.ZodNever,
|
|
1628
|
+
...processCreateParams(params)
|
|
1629
|
+
});
|
|
1630
|
+
};
|
|
1631
|
+
class ZodVoid extends ZodType {
|
|
1632
|
+
_parse(input) {
|
|
1633
|
+
const parsedType = this._getType(input);
|
|
1634
|
+
if (parsedType !== ZodParsedType.undefined) {
|
|
1635
|
+
const ctx = this._getOrReturnCtx(input);
|
|
1636
|
+
addIssueToContext(ctx, {
|
|
1637
|
+
code: ZodIssueCode.invalid_type,
|
|
1638
|
+
expected: ZodParsedType.void,
|
|
1639
|
+
received: ctx.parsedType
|
|
1640
|
+
});
|
|
1641
|
+
return INVALID;
|
|
1642
|
+
}
|
|
1643
|
+
return OK(input.data);
|
|
1644
|
+
}
|
|
1645
|
+
}
|
|
1646
|
+
ZodVoid.create = (params) => {
|
|
1647
|
+
return new ZodVoid({
|
|
1648
|
+
typeName: ZodFirstPartyTypeKind.ZodVoid,
|
|
1649
|
+
...processCreateParams(params)
|
|
1650
|
+
});
|
|
1651
|
+
};
|
|
1652
|
+
class ZodArray extends ZodType {
|
|
1653
|
+
_parse(input) {
|
|
1654
|
+
const { ctx, status } = this._processInputParams(input);
|
|
1655
|
+
const def = this._def;
|
|
1656
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
1657
|
+
addIssueToContext(ctx, {
|
|
1658
|
+
code: ZodIssueCode.invalid_type,
|
|
1659
|
+
expected: ZodParsedType.array,
|
|
1660
|
+
received: ctx.parsedType
|
|
1661
|
+
});
|
|
1662
|
+
return INVALID;
|
|
1663
|
+
}
|
|
1664
|
+
if (def.exactLength !== null) {
|
|
1665
|
+
const tooBig = ctx.data.length > def.exactLength.value;
|
|
1666
|
+
const tooSmall = ctx.data.length < def.exactLength.value;
|
|
1667
|
+
if (tooBig || tooSmall) {
|
|
1668
|
+
addIssueToContext(ctx, {
|
|
1669
|
+
code: tooBig ? ZodIssueCode.too_big : ZodIssueCode.too_small,
|
|
1670
|
+
minimum: tooSmall ? def.exactLength.value : void 0,
|
|
1671
|
+
maximum: tooBig ? def.exactLength.value : void 0,
|
|
1672
|
+
type: "array",
|
|
1673
|
+
inclusive: true,
|
|
1674
|
+
exact: true,
|
|
1675
|
+
message: def.exactLength.message
|
|
1676
|
+
});
|
|
1677
|
+
status.dirty();
|
|
1678
|
+
}
|
|
1679
|
+
}
|
|
1680
|
+
if (def.minLength !== null) {
|
|
1681
|
+
if (ctx.data.length < def.minLength.value) {
|
|
1682
|
+
addIssueToContext(ctx, {
|
|
1683
|
+
code: ZodIssueCode.too_small,
|
|
1684
|
+
minimum: def.minLength.value,
|
|
1685
|
+
type: "array",
|
|
1686
|
+
inclusive: true,
|
|
1687
|
+
exact: false,
|
|
1688
|
+
message: def.minLength.message
|
|
1689
|
+
});
|
|
1690
|
+
status.dirty();
|
|
1691
|
+
}
|
|
1692
|
+
}
|
|
1693
|
+
if (def.maxLength !== null) {
|
|
1694
|
+
if (ctx.data.length > def.maxLength.value) {
|
|
1695
|
+
addIssueToContext(ctx, {
|
|
1696
|
+
code: ZodIssueCode.too_big,
|
|
1697
|
+
maximum: def.maxLength.value,
|
|
1698
|
+
type: "array",
|
|
1699
|
+
inclusive: true,
|
|
1700
|
+
exact: false,
|
|
1701
|
+
message: def.maxLength.message
|
|
1702
|
+
});
|
|
1703
|
+
status.dirty();
|
|
1704
|
+
}
|
|
1705
|
+
}
|
|
1706
|
+
if (ctx.common.async) {
|
|
1707
|
+
return Promise.all([...ctx.data].map((item, i) => {
|
|
1708
|
+
return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
1709
|
+
})).then((result2) => {
|
|
1710
|
+
return ParseStatus.mergeArray(status, result2);
|
|
1711
|
+
});
|
|
1712
|
+
}
|
|
1713
|
+
const result = [...ctx.data].map((item, i) => {
|
|
1714
|
+
return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
|
|
1715
|
+
});
|
|
1716
|
+
return ParseStatus.mergeArray(status, result);
|
|
1717
|
+
}
|
|
1718
|
+
get element() {
|
|
1719
|
+
return this._def.type;
|
|
1720
|
+
}
|
|
1721
|
+
min(minLength, message) {
|
|
1722
|
+
return new ZodArray({
|
|
1723
|
+
...this._def,
|
|
1724
|
+
minLength: { value: minLength, message: errorUtil.toString(message) }
|
|
1725
|
+
});
|
|
1726
|
+
}
|
|
1727
|
+
max(maxLength, message) {
|
|
1728
|
+
return new ZodArray({
|
|
1729
|
+
...this._def,
|
|
1730
|
+
maxLength: { value: maxLength, message: errorUtil.toString(message) }
|
|
1731
|
+
});
|
|
1732
|
+
}
|
|
1733
|
+
length(len, message) {
|
|
1734
|
+
return new ZodArray({
|
|
1735
|
+
...this._def,
|
|
1736
|
+
exactLength: { value: len, message: errorUtil.toString(message) }
|
|
1737
|
+
});
|
|
1738
|
+
}
|
|
1739
|
+
nonempty(message) {
|
|
1740
|
+
return this.min(1, message);
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
ZodArray.create = (schema, params) => {
|
|
1744
|
+
return new ZodArray({
|
|
1745
|
+
type: schema,
|
|
1746
|
+
minLength: null,
|
|
1747
|
+
maxLength: null,
|
|
1748
|
+
exactLength: null,
|
|
1749
|
+
typeName: ZodFirstPartyTypeKind.ZodArray,
|
|
1750
|
+
...processCreateParams(params)
|
|
1751
|
+
});
|
|
1752
|
+
};
|
|
1753
|
+
function deepPartialify(schema) {
|
|
1754
|
+
if (schema instanceof ZodObject) {
|
|
1755
|
+
const newShape = {};
|
|
1756
|
+
for (const key in schema.shape) {
|
|
1757
|
+
const fieldSchema = schema.shape[key];
|
|
1758
|
+
newShape[key] = ZodOptional.create(deepPartialify(fieldSchema));
|
|
1759
|
+
}
|
|
1760
|
+
return new ZodObject({
|
|
1761
|
+
...schema._def,
|
|
1762
|
+
shape: () => newShape
|
|
1763
|
+
});
|
|
1764
|
+
} else if (schema instanceof ZodArray) {
|
|
1765
|
+
return new ZodArray({
|
|
1766
|
+
...schema._def,
|
|
1767
|
+
type: deepPartialify(schema.element)
|
|
1768
|
+
});
|
|
1769
|
+
} else if (schema instanceof ZodOptional) {
|
|
1770
|
+
return ZodOptional.create(deepPartialify(schema.unwrap()));
|
|
1771
|
+
} else if (schema instanceof ZodNullable) {
|
|
1772
|
+
return ZodNullable.create(deepPartialify(schema.unwrap()));
|
|
1773
|
+
} else if (schema instanceof ZodTuple) {
|
|
1774
|
+
return ZodTuple.create(schema.items.map((item) => deepPartialify(item)));
|
|
1775
|
+
} else {
|
|
1776
|
+
return schema;
|
|
1777
|
+
}
|
|
1778
|
+
}
|
|
1779
|
+
class ZodObject extends ZodType {
|
|
1780
|
+
constructor() {
|
|
1781
|
+
super(...arguments);
|
|
1782
|
+
this._cached = null;
|
|
1783
|
+
this.nonstrict = this.passthrough;
|
|
1784
|
+
this.augment = this.extend;
|
|
1785
|
+
}
|
|
1786
|
+
_getCached() {
|
|
1787
|
+
if (this._cached !== null)
|
|
1788
|
+
return this._cached;
|
|
1789
|
+
const shape = this._def.shape();
|
|
1790
|
+
const keys = util.objectKeys(shape);
|
|
1791
|
+
this._cached = { shape, keys };
|
|
1792
|
+
return this._cached;
|
|
1793
|
+
}
|
|
1794
|
+
_parse(input) {
|
|
1795
|
+
const parsedType = this._getType(input);
|
|
1796
|
+
if (parsedType !== ZodParsedType.object) {
|
|
1797
|
+
const ctx2 = this._getOrReturnCtx(input);
|
|
1798
|
+
addIssueToContext(ctx2, {
|
|
1799
|
+
code: ZodIssueCode.invalid_type,
|
|
1800
|
+
expected: ZodParsedType.object,
|
|
1801
|
+
received: ctx2.parsedType
|
|
1802
|
+
});
|
|
1803
|
+
return INVALID;
|
|
1804
|
+
}
|
|
1805
|
+
const { status, ctx } = this._processInputParams(input);
|
|
1806
|
+
const { shape, keys: shapeKeys } = this._getCached();
|
|
1807
|
+
const extraKeys = [];
|
|
1808
|
+
if (!(this._def.catchall instanceof ZodNever && this._def.unknownKeys === "strip")) {
|
|
1809
|
+
for (const key in ctx.data) {
|
|
1810
|
+
if (!shapeKeys.includes(key)) {
|
|
1811
|
+
extraKeys.push(key);
|
|
1812
|
+
}
|
|
1813
|
+
}
|
|
1814
|
+
}
|
|
1815
|
+
const pairs = [];
|
|
1816
|
+
for (const key of shapeKeys) {
|
|
1817
|
+
const keyValidator = shape[key];
|
|
1818
|
+
const value = ctx.data[key];
|
|
1819
|
+
pairs.push({
|
|
1820
|
+
key: { status: "valid", value: key },
|
|
1821
|
+
value: keyValidator._parse(new ParseInputLazyPath(ctx, value, ctx.path, key)),
|
|
1822
|
+
alwaysSet: key in ctx.data
|
|
1823
|
+
});
|
|
1824
|
+
}
|
|
1825
|
+
if (this._def.catchall instanceof ZodNever) {
|
|
1826
|
+
const unknownKeys = this._def.unknownKeys;
|
|
1827
|
+
if (unknownKeys === "passthrough") {
|
|
1828
|
+
for (const key of extraKeys) {
|
|
1829
|
+
pairs.push({
|
|
1830
|
+
key: { status: "valid", value: key },
|
|
1831
|
+
value: { status: "valid", value: ctx.data[key] }
|
|
1832
|
+
});
|
|
1833
|
+
}
|
|
1834
|
+
} else if (unknownKeys === "strict") {
|
|
1835
|
+
if (extraKeys.length > 0) {
|
|
1836
|
+
addIssueToContext(ctx, {
|
|
1837
|
+
code: ZodIssueCode.unrecognized_keys,
|
|
1838
|
+
keys: extraKeys
|
|
1839
|
+
});
|
|
1840
|
+
status.dirty();
|
|
1841
|
+
}
|
|
1842
|
+
} else if (unknownKeys === "strip") ;
|
|
1843
|
+
else {
|
|
1844
|
+
throw new Error(`Internal ZodObject error: invalid unknownKeys value.`);
|
|
1845
|
+
}
|
|
1846
|
+
} else {
|
|
1847
|
+
const catchall = this._def.catchall;
|
|
1848
|
+
for (const key of extraKeys) {
|
|
1849
|
+
const value = ctx.data[key];
|
|
1850
|
+
pairs.push({
|
|
1851
|
+
key: { status: "valid", value: key },
|
|
1852
|
+
value: catchall._parse(
|
|
1853
|
+
new ParseInputLazyPath(ctx, value, ctx.path, key)
|
|
1854
|
+
//, ctx.child(key), value, getParsedType(value)
|
|
1855
|
+
),
|
|
1856
|
+
alwaysSet: key in ctx.data
|
|
1857
|
+
});
|
|
1858
|
+
}
|
|
1859
|
+
}
|
|
1860
|
+
if (ctx.common.async) {
|
|
1861
|
+
return Promise.resolve().then(async () => {
|
|
1862
|
+
const syncPairs = [];
|
|
1863
|
+
for (const pair of pairs) {
|
|
1864
|
+
const key = await pair.key;
|
|
1865
|
+
const value = await pair.value;
|
|
1866
|
+
syncPairs.push({
|
|
1867
|
+
key,
|
|
1868
|
+
value,
|
|
1869
|
+
alwaysSet: pair.alwaysSet
|
|
1870
|
+
});
|
|
1871
|
+
}
|
|
1872
|
+
return syncPairs;
|
|
1873
|
+
}).then((syncPairs) => {
|
|
1874
|
+
return ParseStatus.mergeObjectSync(status, syncPairs);
|
|
1875
|
+
});
|
|
1876
|
+
} else {
|
|
1877
|
+
return ParseStatus.mergeObjectSync(status, pairs);
|
|
1878
|
+
}
|
|
1879
|
+
}
|
|
1880
|
+
get shape() {
|
|
1881
|
+
return this._def.shape();
|
|
1882
|
+
}
|
|
1883
|
+
strict(message) {
|
|
1884
|
+
errorUtil.errToObj;
|
|
1885
|
+
return new ZodObject({
|
|
1886
|
+
...this._def,
|
|
1887
|
+
unknownKeys: "strict",
|
|
1888
|
+
...message !== void 0 ? {
|
|
1889
|
+
errorMap: (issue, ctx) => {
|
|
1890
|
+
const defaultError = this._def.errorMap?.(issue, ctx).message ?? ctx.defaultError;
|
|
1891
|
+
if (issue.code === "unrecognized_keys")
|
|
1892
|
+
return {
|
|
1893
|
+
message: errorUtil.errToObj(message).message ?? defaultError
|
|
1894
|
+
};
|
|
1895
|
+
return {
|
|
1896
|
+
message: defaultError
|
|
1897
|
+
};
|
|
1898
|
+
}
|
|
1899
|
+
} : {}
|
|
1900
|
+
});
|
|
1901
|
+
}
|
|
1902
|
+
strip() {
|
|
1903
|
+
return new ZodObject({
|
|
1904
|
+
...this._def,
|
|
1905
|
+
unknownKeys: "strip"
|
|
1906
|
+
});
|
|
1907
|
+
}
|
|
1908
|
+
passthrough() {
|
|
1909
|
+
return new ZodObject({
|
|
1910
|
+
...this._def,
|
|
1911
|
+
unknownKeys: "passthrough"
|
|
1912
|
+
});
|
|
1913
|
+
}
|
|
1914
|
+
// const AugmentFactory =
|
|
1915
|
+
// <Def extends ZodObjectDef>(def: Def) =>
|
|
1916
|
+
// <Augmentation extends ZodRawShape>(
|
|
1917
|
+
// augmentation: Augmentation
|
|
1918
|
+
// ): ZodObject<
|
|
1919
|
+
// extendShape<ReturnType<Def["shape"]>, Augmentation>,
|
|
1920
|
+
// Def["unknownKeys"],
|
|
1921
|
+
// Def["catchall"]
|
|
1922
|
+
// > => {
|
|
1923
|
+
// return new ZodObject({
|
|
1924
|
+
// ...def,
|
|
1925
|
+
// shape: () => ({
|
|
1926
|
+
// ...def.shape(),
|
|
1927
|
+
// ...augmentation,
|
|
1928
|
+
// }),
|
|
1929
|
+
// }) as any;
|
|
1930
|
+
// };
|
|
1931
|
+
extend(augmentation) {
|
|
1932
|
+
return new ZodObject({
|
|
1933
|
+
...this._def,
|
|
1934
|
+
shape: () => ({
|
|
1935
|
+
...this._def.shape(),
|
|
1936
|
+
...augmentation
|
|
1937
|
+
})
|
|
1938
|
+
});
|
|
1939
|
+
}
|
|
1940
|
+
/**
|
|
1941
|
+
* Prior to zod@1.0.12 there was a bug in the
|
|
1942
|
+
* inferred type of merged objects. Please
|
|
1943
|
+
* upgrade if you are experiencing issues.
|
|
1944
|
+
*/
|
|
1945
|
+
merge(merging) {
|
|
1946
|
+
const merged = new ZodObject({
|
|
1947
|
+
unknownKeys: merging._def.unknownKeys,
|
|
1948
|
+
catchall: merging._def.catchall,
|
|
1949
|
+
shape: () => ({
|
|
1950
|
+
...this._def.shape(),
|
|
1951
|
+
...merging._def.shape()
|
|
1952
|
+
}),
|
|
1953
|
+
typeName: ZodFirstPartyTypeKind.ZodObject
|
|
1954
|
+
});
|
|
1955
|
+
return merged;
|
|
1956
|
+
}
|
|
1957
|
+
// merge<
|
|
1958
|
+
// Incoming extends AnyZodObject,
|
|
1959
|
+
// Augmentation extends Incoming["shape"],
|
|
1960
|
+
// NewOutput extends {
|
|
1961
|
+
// [k in keyof Augmentation | keyof Output]: k extends keyof Augmentation
|
|
1962
|
+
// ? Augmentation[k]["_output"]
|
|
1963
|
+
// : k extends keyof Output
|
|
1964
|
+
// ? Output[k]
|
|
1965
|
+
// : never;
|
|
1966
|
+
// },
|
|
1967
|
+
// NewInput extends {
|
|
1968
|
+
// [k in keyof Augmentation | keyof Input]: k extends keyof Augmentation
|
|
1969
|
+
// ? Augmentation[k]["_input"]
|
|
1970
|
+
// : k extends keyof Input
|
|
1971
|
+
// ? Input[k]
|
|
1972
|
+
// : never;
|
|
1973
|
+
// }
|
|
1974
|
+
// >(
|
|
1975
|
+
// merging: Incoming
|
|
1976
|
+
// ): ZodObject<
|
|
1977
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
1978
|
+
// Incoming["_def"]["unknownKeys"],
|
|
1979
|
+
// Incoming["_def"]["catchall"],
|
|
1980
|
+
// NewOutput,
|
|
1981
|
+
// NewInput
|
|
1982
|
+
// > {
|
|
1983
|
+
// const merged: any = new ZodObject({
|
|
1984
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
1985
|
+
// catchall: merging._def.catchall,
|
|
1986
|
+
// shape: () =>
|
|
1987
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
1988
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
1989
|
+
// }) as any;
|
|
1990
|
+
// return merged;
|
|
1991
|
+
// }
|
|
1992
|
+
setKey(key, schema) {
|
|
1993
|
+
return this.augment({ [key]: schema });
|
|
1994
|
+
}
|
|
1995
|
+
// merge<Incoming extends AnyZodObject>(
|
|
1996
|
+
// merging: Incoming
|
|
1997
|
+
// ): //ZodObject<T & Incoming["_shape"], UnknownKeys, Catchall> = (merging) => {
|
|
1998
|
+
// ZodObject<
|
|
1999
|
+
// extendShape<T, ReturnType<Incoming["_def"]["shape"]>>,
|
|
2000
|
+
// Incoming["_def"]["unknownKeys"],
|
|
2001
|
+
// Incoming["_def"]["catchall"]
|
|
2002
|
+
// > {
|
|
2003
|
+
// // const mergedShape = objectUtil.mergeShapes(
|
|
2004
|
+
// // this._def.shape(),
|
|
2005
|
+
// // merging._def.shape()
|
|
2006
|
+
// // );
|
|
2007
|
+
// const merged: any = new ZodObject({
|
|
2008
|
+
// unknownKeys: merging._def.unknownKeys,
|
|
2009
|
+
// catchall: merging._def.catchall,
|
|
2010
|
+
// shape: () =>
|
|
2011
|
+
// objectUtil.mergeShapes(this._def.shape(), merging._def.shape()),
|
|
2012
|
+
// typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2013
|
+
// }) as any;
|
|
2014
|
+
// return merged;
|
|
2015
|
+
// }
|
|
2016
|
+
catchall(index) {
|
|
2017
|
+
return new ZodObject({
|
|
2018
|
+
...this._def,
|
|
2019
|
+
catchall: index
|
|
2020
|
+
});
|
|
2021
|
+
}
|
|
2022
|
+
pick(mask) {
|
|
2023
|
+
const shape = {};
|
|
2024
|
+
for (const key of util.objectKeys(mask)) {
|
|
2025
|
+
if (mask[key] && this.shape[key]) {
|
|
2026
|
+
shape[key] = this.shape[key];
|
|
2027
|
+
}
|
|
2028
|
+
}
|
|
2029
|
+
return new ZodObject({
|
|
2030
|
+
...this._def,
|
|
2031
|
+
shape: () => shape
|
|
2032
|
+
});
|
|
2033
|
+
}
|
|
2034
|
+
omit(mask) {
|
|
2035
|
+
const shape = {};
|
|
2036
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
2037
|
+
if (!mask[key]) {
|
|
2038
|
+
shape[key] = this.shape[key];
|
|
2039
|
+
}
|
|
2040
|
+
}
|
|
2041
|
+
return new ZodObject({
|
|
2042
|
+
...this._def,
|
|
2043
|
+
shape: () => shape
|
|
2044
|
+
});
|
|
2045
|
+
}
|
|
2046
|
+
/**
|
|
2047
|
+
* @deprecated
|
|
2048
|
+
*/
|
|
2049
|
+
deepPartial() {
|
|
2050
|
+
return deepPartialify(this);
|
|
2051
|
+
}
|
|
2052
|
+
partial(mask) {
|
|
2053
|
+
const newShape = {};
|
|
2054
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
2055
|
+
const fieldSchema = this.shape[key];
|
|
2056
|
+
if (mask && !mask[key]) {
|
|
2057
|
+
newShape[key] = fieldSchema;
|
|
2058
|
+
} else {
|
|
2059
|
+
newShape[key] = fieldSchema.optional();
|
|
2060
|
+
}
|
|
2061
|
+
}
|
|
2062
|
+
return new ZodObject({
|
|
2063
|
+
...this._def,
|
|
2064
|
+
shape: () => newShape
|
|
2065
|
+
});
|
|
2066
|
+
}
|
|
2067
|
+
required(mask) {
|
|
2068
|
+
const newShape = {};
|
|
2069
|
+
for (const key of util.objectKeys(this.shape)) {
|
|
2070
|
+
if (mask && !mask[key]) {
|
|
2071
|
+
newShape[key] = this.shape[key];
|
|
2072
|
+
} else {
|
|
2073
|
+
const fieldSchema = this.shape[key];
|
|
2074
|
+
let newField = fieldSchema;
|
|
2075
|
+
while (newField instanceof ZodOptional) {
|
|
2076
|
+
newField = newField._def.innerType;
|
|
2077
|
+
}
|
|
2078
|
+
newShape[key] = newField;
|
|
2079
|
+
}
|
|
2080
|
+
}
|
|
2081
|
+
return new ZodObject({
|
|
2082
|
+
...this._def,
|
|
2083
|
+
shape: () => newShape
|
|
2084
|
+
});
|
|
2085
|
+
}
|
|
2086
|
+
keyof() {
|
|
2087
|
+
return createZodEnum(util.objectKeys(this.shape));
|
|
2088
|
+
}
|
|
2089
|
+
}
|
|
2090
|
+
ZodObject.create = (shape, params) => {
|
|
2091
|
+
return new ZodObject({
|
|
2092
|
+
shape: () => shape,
|
|
2093
|
+
unknownKeys: "strip",
|
|
2094
|
+
catchall: ZodNever.create(),
|
|
2095
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2096
|
+
...processCreateParams(params)
|
|
2097
|
+
});
|
|
2098
|
+
};
|
|
2099
|
+
ZodObject.strictCreate = (shape, params) => {
|
|
2100
|
+
return new ZodObject({
|
|
2101
|
+
shape: () => shape,
|
|
2102
|
+
unknownKeys: "strict",
|
|
2103
|
+
catchall: ZodNever.create(),
|
|
2104
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2105
|
+
...processCreateParams(params)
|
|
2106
|
+
});
|
|
2107
|
+
};
|
|
2108
|
+
ZodObject.lazycreate = (shape, params) => {
|
|
2109
|
+
return new ZodObject({
|
|
2110
|
+
shape,
|
|
2111
|
+
unknownKeys: "strip",
|
|
2112
|
+
catchall: ZodNever.create(),
|
|
2113
|
+
typeName: ZodFirstPartyTypeKind.ZodObject,
|
|
2114
|
+
...processCreateParams(params)
|
|
2115
|
+
});
|
|
2116
|
+
};
|
|
2117
|
+
class ZodUnion extends ZodType {
|
|
2118
|
+
_parse(input) {
|
|
2119
|
+
const { ctx } = this._processInputParams(input);
|
|
2120
|
+
const options = this._def.options;
|
|
2121
|
+
function handleResults(results) {
|
|
2122
|
+
for (const result of results) {
|
|
2123
|
+
if (result.result.status === "valid") {
|
|
2124
|
+
return result.result;
|
|
2125
|
+
}
|
|
2126
|
+
}
|
|
2127
|
+
for (const result of results) {
|
|
2128
|
+
if (result.result.status === "dirty") {
|
|
2129
|
+
ctx.common.issues.push(...result.ctx.common.issues);
|
|
2130
|
+
return result.result;
|
|
2131
|
+
}
|
|
2132
|
+
}
|
|
2133
|
+
const unionErrors = results.map((result) => new ZodError(result.ctx.common.issues));
|
|
2134
|
+
addIssueToContext(ctx, {
|
|
2135
|
+
code: ZodIssueCode.invalid_union,
|
|
2136
|
+
unionErrors
|
|
2137
|
+
});
|
|
2138
|
+
return INVALID;
|
|
2139
|
+
}
|
|
2140
|
+
if (ctx.common.async) {
|
|
2141
|
+
return Promise.all(options.map(async (option) => {
|
|
2142
|
+
const childCtx = {
|
|
2143
|
+
...ctx,
|
|
2144
|
+
common: {
|
|
2145
|
+
...ctx.common,
|
|
2146
|
+
issues: []
|
|
2147
|
+
},
|
|
2148
|
+
parent: null
|
|
2149
|
+
};
|
|
2150
|
+
return {
|
|
2151
|
+
result: await option._parseAsync({
|
|
2152
|
+
data: ctx.data,
|
|
2153
|
+
path: ctx.path,
|
|
2154
|
+
parent: childCtx
|
|
2155
|
+
}),
|
|
2156
|
+
ctx: childCtx
|
|
2157
|
+
};
|
|
2158
|
+
})).then(handleResults);
|
|
2159
|
+
} else {
|
|
2160
|
+
let dirty = void 0;
|
|
2161
|
+
const issues = [];
|
|
2162
|
+
for (const option of options) {
|
|
2163
|
+
const childCtx = {
|
|
2164
|
+
...ctx,
|
|
2165
|
+
common: {
|
|
2166
|
+
...ctx.common,
|
|
2167
|
+
issues: []
|
|
2168
|
+
},
|
|
2169
|
+
parent: null
|
|
2170
|
+
};
|
|
2171
|
+
const result = option._parseSync({
|
|
2172
|
+
data: ctx.data,
|
|
2173
|
+
path: ctx.path,
|
|
2174
|
+
parent: childCtx
|
|
2175
|
+
});
|
|
2176
|
+
if (result.status === "valid") {
|
|
2177
|
+
return result;
|
|
2178
|
+
} else if (result.status === "dirty" && !dirty) {
|
|
2179
|
+
dirty = { result, ctx: childCtx };
|
|
2180
|
+
}
|
|
2181
|
+
if (childCtx.common.issues.length) {
|
|
2182
|
+
issues.push(childCtx.common.issues);
|
|
2183
|
+
}
|
|
2184
|
+
}
|
|
2185
|
+
if (dirty) {
|
|
2186
|
+
ctx.common.issues.push(...dirty.ctx.common.issues);
|
|
2187
|
+
return dirty.result;
|
|
2188
|
+
}
|
|
2189
|
+
const unionErrors = issues.map((issues2) => new ZodError(issues2));
|
|
2190
|
+
addIssueToContext(ctx, {
|
|
2191
|
+
code: ZodIssueCode.invalid_union,
|
|
2192
|
+
unionErrors
|
|
2193
|
+
});
|
|
2194
|
+
return INVALID;
|
|
2195
|
+
}
|
|
2196
|
+
}
|
|
2197
|
+
get options() {
|
|
2198
|
+
return this._def.options;
|
|
2199
|
+
}
|
|
2200
|
+
}
|
|
2201
|
+
ZodUnion.create = (types, params) => {
|
|
2202
|
+
return new ZodUnion({
|
|
2203
|
+
options: types,
|
|
2204
|
+
typeName: ZodFirstPartyTypeKind.ZodUnion,
|
|
2205
|
+
...processCreateParams(params)
|
|
2206
|
+
});
|
|
2207
|
+
};
|
|
2208
|
+
function mergeValues(a, b) {
|
|
2209
|
+
const aType = getParsedType(a);
|
|
2210
|
+
const bType = getParsedType(b);
|
|
2211
|
+
if (a === b) {
|
|
2212
|
+
return { valid: true, data: a };
|
|
2213
|
+
} else if (aType === ZodParsedType.object && bType === ZodParsedType.object) {
|
|
2214
|
+
const bKeys = util.objectKeys(b);
|
|
2215
|
+
const sharedKeys = util.objectKeys(a).filter((key) => bKeys.indexOf(key) !== -1);
|
|
2216
|
+
const newObj = { ...a, ...b };
|
|
2217
|
+
for (const key of sharedKeys) {
|
|
2218
|
+
const sharedValue = mergeValues(a[key], b[key]);
|
|
2219
|
+
if (!sharedValue.valid) {
|
|
2220
|
+
return { valid: false };
|
|
2221
|
+
}
|
|
2222
|
+
newObj[key] = sharedValue.data;
|
|
2223
|
+
}
|
|
2224
|
+
return { valid: true, data: newObj };
|
|
2225
|
+
} else if (aType === ZodParsedType.array && bType === ZodParsedType.array) {
|
|
2226
|
+
if (a.length !== b.length) {
|
|
2227
|
+
return { valid: false };
|
|
2228
|
+
}
|
|
2229
|
+
const newArray = [];
|
|
2230
|
+
for (let index = 0; index < a.length; index++) {
|
|
2231
|
+
const itemA = a[index];
|
|
2232
|
+
const itemB = b[index];
|
|
2233
|
+
const sharedValue = mergeValues(itemA, itemB);
|
|
2234
|
+
if (!sharedValue.valid) {
|
|
2235
|
+
return { valid: false };
|
|
2236
|
+
}
|
|
2237
|
+
newArray.push(sharedValue.data);
|
|
2238
|
+
}
|
|
2239
|
+
return { valid: true, data: newArray };
|
|
2240
|
+
} else if (aType === ZodParsedType.date && bType === ZodParsedType.date && +a === +b) {
|
|
2241
|
+
return { valid: true, data: a };
|
|
2242
|
+
} else {
|
|
2243
|
+
return { valid: false };
|
|
2244
|
+
}
|
|
2245
|
+
}
|
|
2246
|
+
class ZodIntersection extends ZodType {
|
|
2247
|
+
_parse(input) {
|
|
2248
|
+
const { status, ctx } = this._processInputParams(input);
|
|
2249
|
+
const handleParsed = (parsedLeft, parsedRight) => {
|
|
2250
|
+
if (isAborted(parsedLeft) || isAborted(parsedRight)) {
|
|
2251
|
+
return INVALID;
|
|
2252
|
+
}
|
|
2253
|
+
const merged = mergeValues(parsedLeft.value, parsedRight.value);
|
|
2254
|
+
if (!merged.valid) {
|
|
2255
|
+
addIssueToContext(ctx, {
|
|
2256
|
+
code: ZodIssueCode.invalid_intersection_types
|
|
2257
|
+
});
|
|
2258
|
+
return INVALID;
|
|
2259
|
+
}
|
|
2260
|
+
if (isDirty(parsedLeft) || isDirty(parsedRight)) {
|
|
2261
|
+
status.dirty();
|
|
2262
|
+
}
|
|
2263
|
+
return { status: status.value, value: merged.data };
|
|
2264
|
+
};
|
|
2265
|
+
if (ctx.common.async) {
|
|
2266
|
+
return Promise.all([
|
|
2267
|
+
this._def.left._parseAsync({
|
|
2268
|
+
data: ctx.data,
|
|
2269
|
+
path: ctx.path,
|
|
2270
|
+
parent: ctx
|
|
2271
|
+
}),
|
|
2272
|
+
this._def.right._parseAsync({
|
|
2273
|
+
data: ctx.data,
|
|
2274
|
+
path: ctx.path,
|
|
2275
|
+
parent: ctx
|
|
2276
|
+
})
|
|
2277
|
+
]).then(([left, right]) => handleParsed(left, right));
|
|
2278
|
+
} else {
|
|
2279
|
+
return handleParsed(this._def.left._parseSync({
|
|
2280
|
+
data: ctx.data,
|
|
2281
|
+
path: ctx.path,
|
|
2282
|
+
parent: ctx
|
|
2283
|
+
}), this._def.right._parseSync({
|
|
2284
|
+
data: ctx.data,
|
|
2285
|
+
path: ctx.path,
|
|
2286
|
+
parent: ctx
|
|
2287
|
+
}));
|
|
2288
|
+
}
|
|
2289
|
+
}
|
|
2290
|
+
}
|
|
2291
|
+
ZodIntersection.create = (left, right, params) => {
|
|
2292
|
+
return new ZodIntersection({
|
|
2293
|
+
left,
|
|
2294
|
+
right,
|
|
2295
|
+
typeName: ZodFirstPartyTypeKind.ZodIntersection,
|
|
2296
|
+
...processCreateParams(params)
|
|
2297
|
+
});
|
|
2298
|
+
};
|
|
2299
|
+
class ZodTuple extends ZodType {
|
|
2300
|
+
_parse(input) {
|
|
2301
|
+
const { status, ctx } = this._processInputParams(input);
|
|
2302
|
+
if (ctx.parsedType !== ZodParsedType.array) {
|
|
2303
|
+
addIssueToContext(ctx, {
|
|
2304
|
+
code: ZodIssueCode.invalid_type,
|
|
2305
|
+
expected: ZodParsedType.array,
|
|
2306
|
+
received: ctx.parsedType
|
|
2307
|
+
});
|
|
2308
|
+
return INVALID;
|
|
2309
|
+
}
|
|
2310
|
+
if (ctx.data.length < this._def.items.length) {
|
|
2311
|
+
addIssueToContext(ctx, {
|
|
2312
|
+
code: ZodIssueCode.too_small,
|
|
2313
|
+
minimum: this._def.items.length,
|
|
2314
|
+
inclusive: true,
|
|
2315
|
+
exact: false,
|
|
2316
|
+
type: "array"
|
|
2317
|
+
});
|
|
2318
|
+
return INVALID;
|
|
2319
|
+
}
|
|
2320
|
+
const rest = this._def.rest;
|
|
2321
|
+
if (!rest && ctx.data.length > this._def.items.length) {
|
|
2322
|
+
addIssueToContext(ctx, {
|
|
2323
|
+
code: ZodIssueCode.too_big,
|
|
2324
|
+
maximum: this._def.items.length,
|
|
2325
|
+
inclusive: true,
|
|
2326
|
+
exact: false,
|
|
2327
|
+
type: "array"
|
|
2328
|
+
});
|
|
2329
|
+
status.dirty();
|
|
2330
|
+
}
|
|
2331
|
+
const items = [...ctx.data].map((item, itemIndex) => {
|
|
2332
|
+
const schema = this._def.items[itemIndex] || this._def.rest;
|
|
2333
|
+
if (!schema)
|
|
2334
|
+
return null;
|
|
2335
|
+
return schema._parse(new ParseInputLazyPath(ctx, item, ctx.path, itemIndex));
|
|
2336
|
+
}).filter((x) => !!x);
|
|
2337
|
+
if (ctx.common.async) {
|
|
2338
|
+
return Promise.all(items).then((results) => {
|
|
2339
|
+
return ParseStatus.mergeArray(status, results);
|
|
2340
|
+
});
|
|
2341
|
+
} else {
|
|
2342
|
+
return ParseStatus.mergeArray(status, items);
|
|
2343
|
+
}
|
|
2344
|
+
}
|
|
2345
|
+
get items() {
|
|
2346
|
+
return this._def.items;
|
|
2347
|
+
}
|
|
2348
|
+
rest(rest) {
|
|
2349
|
+
return new ZodTuple({
|
|
2350
|
+
...this._def,
|
|
2351
|
+
rest
|
|
2352
|
+
});
|
|
2353
|
+
}
|
|
2354
|
+
}
|
|
2355
|
+
ZodTuple.create = (schemas, params) => {
|
|
2356
|
+
if (!Array.isArray(schemas)) {
|
|
2357
|
+
throw new Error("You must pass an array of schemas to z.tuple([ ... ])");
|
|
2358
|
+
}
|
|
2359
|
+
return new ZodTuple({
|
|
2360
|
+
items: schemas,
|
|
2361
|
+
typeName: ZodFirstPartyTypeKind.ZodTuple,
|
|
2362
|
+
rest: null,
|
|
2363
|
+
...processCreateParams(params)
|
|
2364
|
+
});
|
|
2365
|
+
};
|
|
2366
|
+
class ZodMap extends ZodType {
|
|
2367
|
+
get keySchema() {
|
|
2368
|
+
return this._def.keyType;
|
|
2369
|
+
}
|
|
2370
|
+
get valueSchema() {
|
|
2371
|
+
return this._def.valueType;
|
|
2372
|
+
}
|
|
2373
|
+
_parse(input) {
|
|
2374
|
+
const { status, ctx } = this._processInputParams(input);
|
|
2375
|
+
if (ctx.parsedType !== ZodParsedType.map) {
|
|
2376
|
+
addIssueToContext(ctx, {
|
|
2377
|
+
code: ZodIssueCode.invalid_type,
|
|
2378
|
+
expected: ZodParsedType.map,
|
|
2379
|
+
received: ctx.parsedType
|
|
2380
|
+
});
|
|
2381
|
+
return INVALID;
|
|
2382
|
+
}
|
|
2383
|
+
const keyType = this._def.keyType;
|
|
2384
|
+
const valueType = this._def.valueType;
|
|
2385
|
+
const pairs = [...ctx.data.entries()].map(([key, value], index) => {
|
|
2386
|
+
return {
|
|
2387
|
+
key: keyType._parse(new ParseInputLazyPath(ctx, key, ctx.path, [index, "key"])),
|
|
2388
|
+
value: valueType._parse(new ParseInputLazyPath(ctx, value, ctx.path, [index, "value"]))
|
|
2389
|
+
};
|
|
2390
|
+
});
|
|
2391
|
+
if (ctx.common.async) {
|
|
2392
|
+
const finalMap = /* @__PURE__ */ new Map();
|
|
2393
|
+
return Promise.resolve().then(async () => {
|
|
2394
|
+
for (const pair of pairs) {
|
|
2395
|
+
const key = await pair.key;
|
|
2396
|
+
const value = await pair.value;
|
|
2397
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
2398
|
+
return INVALID;
|
|
2399
|
+
}
|
|
2400
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
2401
|
+
status.dirty();
|
|
2402
|
+
}
|
|
2403
|
+
finalMap.set(key.value, value.value);
|
|
2404
|
+
}
|
|
2405
|
+
return { status: status.value, value: finalMap };
|
|
2406
|
+
});
|
|
2407
|
+
} else {
|
|
2408
|
+
const finalMap = /* @__PURE__ */ new Map();
|
|
2409
|
+
for (const pair of pairs) {
|
|
2410
|
+
const key = pair.key;
|
|
2411
|
+
const value = pair.value;
|
|
2412
|
+
if (key.status === "aborted" || value.status === "aborted") {
|
|
2413
|
+
return INVALID;
|
|
2414
|
+
}
|
|
2415
|
+
if (key.status === "dirty" || value.status === "dirty") {
|
|
2416
|
+
status.dirty();
|
|
2417
|
+
}
|
|
2418
|
+
finalMap.set(key.value, value.value);
|
|
2419
|
+
}
|
|
2420
|
+
return { status: status.value, value: finalMap };
|
|
2421
|
+
}
|
|
2422
|
+
}
|
|
2423
|
+
}
|
|
2424
|
+
ZodMap.create = (keyType, valueType, params) => {
|
|
2425
|
+
return new ZodMap({
|
|
2426
|
+
valueType,
|
|
2427
|
+
keyType,
|
|
2428
|
+
typeName: ZodFirstPartyTypeKind.ZodMap,
|
|
2429
|
+
...processCreateParams(params)
|
|
2430
|
+
});
|
|
2431
|
+
};
|
|
2432
|
+
class ZodSet extends ZodType {
|
|
2433
|
+
_parse(input) {
|
|
2434
|
+
const { status, ctx } = this._processInputParams(input);
|
|
2435
|
+
if (ctx.parsedType !== ZodParsedType.set) {
|
|
2436
|
+
addIssueToContext(ctx, {
|
|
2437
|
+
code: ZodIssueCode.invalid_type,
|
|
2438
|
+
expected: ZodParsedType.set,
|
|
2439
|
+
received: ctx.parsedType
|
|
2440
|
+
});
|
|
2441
|
+
return INVALID;
|
|
2442
|
+
}
|
|
2443
|
+
const def = this._def;
|
|
2444
|
+
if (def.minSize !== null) {
|
|
2445
|
+
if (ctx.data.size < def.minSize.value) {
|
|
2446
|
+
addIssueToContext(ctx, {
|
|
2447
|
+
code: ZodIssueCode.too_small,
|
|
2448
|
+
minimum: def.minSize.value,
|
|
2449
|
+
type: "set",
|
|
2450
|
+
inclusive: true,
|
|
2451
|
+
exact: false,
|
|
2452
|
+
message: def.minSize.message
|
|
2453
|
+
});
|
|
2454
|
+
status.dirty();
|
|
2455
|
+
}
|
|
2456
|
+
}
|
|
2457
|
+
if (def.maxSize !== null) {
|
|
2458
|
+
if (ctx.data.size > def.maxSize.value) {
|
|
2459
|
+
addIssueToContext(ctx, {
|
|
2460
|
+
code: ZodIssueCode.too_big,
|
|
2461
|
+
maximum: def.maxSize.value,
|
|
2462
|
+
type: "set",
|
|
2463
|
+
inclusive: true,
|
|
2464
|
+
exact: false,
|
|
2465
|
+
message: def.maxSize.message
|
|
2466
|
+
});
|
|
2467
|
+
status.dirty();
|
|
2468
|
+
}
|
|
2469
|
+
}
|
|
2470
|
+
const valueType = this._def.valueType;
|
|
2471
|
+
function finalizeSet(elements2) {
|
|
2472
|
+
const parsedSet = /* @__PURE__ */ new Set();
|
|
2473
|
+
for (const element of elements2) {
|
|
2474
|
+
if (element.status === "aborted")
|
|
2475
|
+
return INVALID;
|
|
2476
|
+
if (element.status === "dirty")
|
|
2477
|
+
status.dirty();
|
|
2478
|
+
parsedSet.add(element.value);
|
|
2479
|
+
}
|
|
2480
|
+
return { status: status.value, value: parsedSet };
|
|
2481
|
+
}
|
|
2482
|
+
const elements = [...ctx.data.values()].map((item, i) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i)));
|
|
2483
|
+
if (ctx.common.async) {
|
|
2484
|
+
return Promise.all(elements).then((elements2) => finalizeSet(elements2));
|
|
2485
|
+
} else {
|
|
2486
|
+
return finalizeSet(elements);
|
|
2487
|
+
}
|
|
2488
|
+
}
|
|
2489
|
+
min(minSize, message) {
|
|
2490
|
+
return new ZodSet({
|
|
2491
|
+
...this._def,
|
|
2492
|
+
minSize: { value: minSize, message: errorUtil.toString(message) }
|
|
2493
|
+
});
|
|
2494
|
+
}
|
|
2495
|
+
max(maxSize, message) {
|
|
2496
|
+
return new ZodSet({
|
|
2497
|
+
...this._def,
|
|
2498
|
+
maxSize: { value: maxSize, message: errorUtil.toString(message) }
|
|
2499
|
+
});
|
|
2500
|
+
}
|
|
2501
|
+
size(size, message) {
|
|
2502
|
+
return this.min(size, message).max(size, message);
|
|
2503
|
+
}
|
|
2504
|
+
nonempty(message) {
|
|
2505
|
+
return this.min(1, message);
|
|
2506
|
+
}
|
|
2507
|
+
}
|
|
2508
|
+
ZodSet.create = (valueType, params) => {
|
|
2509
|
+
return new ZodSet({
|
|
2510
|
+
valueType,
|
|
2511
|
+
minSize: null,
|
|
2512
|
+
maxSize: null,
|
|
2513
|
+
typeName: ZodFirstPartyTypeKind.ZodSet,
|
|
2514
|
+
...processCreateParams(params)
|
|
2515
|
+
});
|
|
2516
|
+
};
|
|
2517
|
+
class ZodLazy extends ZodType {
|
|
2518
|
+
get schema() {
|
|
2519
|
+
return this._def.getter();
|
|
2520
|
+
}
|
|
2521
|
+
_parse(input) {
|
|
2522
|
+
const { ctx } = this._processInputParams(input);
|
|
2523
|
+
const lazySchema = this._def.getter();
|
|
2524
|
+
return lazySchema._parse({ data: ctx.data, path: ctx.path, parent: ctx });
|
|
2525
|
+
}
|
|
2526
|
+
}
|
|
2527
|
+
ZodLazy.create = (getter, params) => {
|
|
2528
|
+
return new ZodLazy({
|
|
2529
|
+
getter,
|
|
2530
|
+
typeName: ZodFirstPartyTypeKind.ZodLazy,
|
|
2531
|
+
...processCreateParams(params)
|
|
2532
|
+
});
|
|
2533
|
+
};
|
|
2534
|
+
class ZodLiteral extends ZodType {
|
|
2535
|
+
_parse(input) {
|
|
2536
|
+
if (input.data !== this._def.value) {
|
|
2537
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2538
|
+
addIssueToContext(ctx, {
|
|
2539
|
+
received: ctx.data,
|
|
2540
|
+
code: ZodIssueCode.invalid_literal,
|
|
2541
|
+
expected: this._def.value
|
|
2542
|
+
});
|
|
2543
|
+
return INVALID;
|
|
2544
|
+
}
|
|
2545
|
+
return { status: "valid", value: input.data };
|
|
2546
|
+
}
|
|
2547
|
+
get value() {
|
|
2548
|
+
return this._def.value;
|
|
2549
|
+
}
|
|
2550
|
+
}
|
|
2551
|
+
ZodLiteral.create = (value, params) => {
|
|
2552
|
+
return new ZodLiteral({
|
|
2553
|
+
value,
|
|
2554
|
+
typeName: ZodFirstPartyTypeKind.ZodLiteral,
|
|
2555
|
+
...processCreateParams(params)
|
|
2556
|
+
});
|
|
2557
|
+
};
|
|
2558
|
+
function createZodEnum(values, params) {
|
|
2559
|
+
return new ZodEnum({
|
|
2560
|
+
values,
|
|
2561
|
+
typeName: ZodFirstPartyTypeKind.ZodEnum,
|
|
2562
|
+
...processCreateParams(params)
|
|
2563
|
+
});
|
|
2564
|
+
}
|
|
2565
|
+
class ZodEnum extends ZodType {
|
|
2566
|
+
_parse(input) {
|
|
2567
|
+
if (typeof input.data !== "string") {
|
|
2568
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2569
|
+
const expectedValues = this._def.values;
|
|
2570
|
+
addIssueToContext(ctx, {
|
|
2571
|
+
expected: util.joinValues(expectedValues),
|
|
2572
|
+
received: ctx.parsedType,
|
|
2573
|
+
code: ZodIssueCode.invalid_type
|
|
2574
|
+
});
|
|
2575
|
+
return INVALID;
|
|
2576
|
+
}
|
|
2577
|
+
if (!this._cache) {
|
|
2578
|
+
this._cache = new Set(this._def.values);
|
|
2579
|
+
}
|
|
2580
|
+
if (!this._cache.has(input.data)) {
|
|
2581
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2582
|
+
const expectedValues = this._def.values;
|
|
2583
|
+
addIssueToContext(ctx, {
|
|
2584
|
+
received: ctx.data,
|
|
2585
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
2586
|
+
options: expectedValues
|
|
2587
|
+
});
|
|
2588
|
+
return INVALID;
|
|
2589
|
+
}
|
|
2590
|
+
return OK(input.data);
|
|
2591
|
+
}
|
|
2592
|
+
get options() {
|
|
2593
|
+
return this._def.values;
|
|
2594
|
+
}
|
|
2595
|
+
get enum() {
|
|
2596
|
+
const enumValues = {};
|
|
2597
|
+
for (const val of this._def.values) {
|
|
2598
|
+
enumValues[val] = val;
|
|
2599
|
+
}
|
|
2600
|
+
return enumValues;
|
|
2601
|
+
}
|
|
2602
|
+
get Values() {
|
|
2603
|
+
const enumValues = {};
|
|
2604
|
+
for (const val of this._def.values) {
|
|
2605
|
+
enumValues[val] = val;
|
|
2606
|
+
}
|
|
2607
|
+
return enumValues;
|
|
2608
|
+
}
|
|
2609
|
+
get Enum() {
|
|
2610
|
+
const enumValues = {};
|
|
2611
|
+
for (const val of this._def.values) {
|
|
2612
|
+
enumValues[val] = val;
|
|
2613
|
+
}
|
|
2614
|
+
return enumValues;
|
|
2615
|
+
}
|
|
2616
|
+
extract(values, newDef = this._def) {
|
|
2617
|
+
return ZodEnum.create(values, {
|
|
2618
|
+
...this._def,
|
|
2619
|
+
...newDef
|
|
2620
|
+
});
|
|
2621
|
+
}
|
|
2622
|
+
exclude(values, newDef = this._def) {
|
|
2623
|
+
return ZodEnum.create(this.options.filter((opt) => !values.includes(opt)), {
|
|
2624
|
+
...this._def,
|
|
2625
|
+
...newDef
|
|
2626
|
+
});
|
|
2627
|
+
}
|
|
2628
|
+
}
|
|
2629
|
+
ZodEnum.create = createZodEnum;
|
|
2630
|
+
class ZodNativeEnum extends ZodType {
|
|
2631
|
+
_parse(input) {
|
|
2632
|
+
const nativeEnumValues = util.getValidEnumValues(this._def.values);
|
|
2633
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2634
|
+
if (ctx.parsedType !== ZodParsedType.string && ctx.parsedType !== ZodParsedType.number) {
|
|
2635
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
2636
|
+
addIssueToContext(ctx, {
|
|
2637
|
+
expected: util.joinValues(expectedValues),
|
|
2638
|
+
received: ctx.parsedType,
|
|
2639
|
+
code: ZodIssueCode.invalid_type
|
|
2640
|
+
});
|
|
2641
|
+
return INVALID;
|
|
2642
|
+
}
|
|
2643
|
+
if (!this._cache) {
|
|
2644
|
+
this._cache = new Set(util.getValidEnumValues(this._def.values));
|
|
2645
|
+
}
|
|
2646
|
+
if (!this._cache.has(input.data)) {
|
|
2647
|
+
const expectedValues = util.objectValues(nativeEnumValues);
|
|
2648
|
+
addIssueToContext(ctx, {
|
|
2649
|
+
received: ctx.data,
|
|
2650
|
+
code: ZodIssueCode.invalid_enum_value,
|
|
2651
|
+
options: expectedValues
|
|
2652
|
+
});
|
|
2653
|
+
return INVALID;
|
|
2654
|
+
}
|
|
2655
|
+
return OK(input.data);
|
|
2656
|
+
}
|
|
2657
|
+
get enum() {
|
|
2658
|
+
return this._def.values;
|
|
2659
|
+
}
|
|
2660
|
+
}
|
|
2661
|
+
ZodNativeEnum.create = (values, params) => {
|
|
2662
|
+
return new ZodNativeEnum({
|
|
2663
|
+
values,
|
|
2664
|
+
typeName: ZodFirstPartyTypeKind.ZodNativeEnum,
|
|
2665
|
+
...processCreateParams(params)
|
|
2666
|
+
});
|
|
2667
|
+
};
|
|
2668
|
+
class ZodPromise extends ZodType {
|
|
2669
|
+
unwrap() {
|
|
2670
|
+
return this._def.type;
|
|
2671
|
+
}
|
|
2672
|
+
_parse(input) {
|
|
2673
|
+
const { ctx } = this._processInputParams(input);
|
|
2674
|
+
if (ctx.parsedType !== ZodParsedType.promise && ctx.common.async === false) {
|
|
2675
|
+
addIssueToContext(ctx, {
|
|
2676
|
+
code: ZodIssueCode.invalid_type,
|
|
2677
|
+
expected: ZodParsedType.promise,
|
|
2678
|
+
received: ctx.parsedType
|
|
2679
|
+
});
|
|
2680
|
+
return INVALID;
|
|
2681
|
+
}
|
|
2682
|
+
const promisified = ctx.parsedType === ZodParsedType.promise ? ctx.data : Promise.resolve(ctx.data);
|
|
2683
|
+
return OK(promisified.then((data) => {
|
|
2684
|
+
return this._def.type.parseAsync(data, {
|
|
2685
|
+
path: ctx.path,
|
|
2686
|
+
errorMap: ctx.common.contextualErrorMap
|
|
2687
|
+
});
|
|
2688
|
+
}));
|
|
2689
|
+
}
|
|
2690
|
+
}
|
|
2691
|
+
ZodPromise.create = (schema, params) => {
|
|
2692
|
+
return new ZodPromise({
|
|
2693
|
+
type: schema,
|
|
2694
|
+
typeName: ZodFirstPartyTypeKind.ZodPromise,
|
|
2695
|
+
...processCreateParams(params)
|
|
2696
|
+
});
|
|
2697
|
+
};
|
|
2698
|
+
class ZodEffects extends ZodType {
|
|
2699
|
+
innerType() {
|
|
2700
|
+
return this._def.schema;
|
|
2701
|
+
}
|
|
2702
|
+
sourceType() {
|
|
2703
|
+
return this._def.schema._def.typeName === ZodFirstPartyTypeKind.ZodEffects ? this._def.schema.sourceType() : this._def.schema;
|
|
2704
|
+
}
|
|
2705
|
+
_parse(input) {
|
|
2706
|
+
const { status, ctx } = this._processInputParams(input);
|
|
2707
|
+
const effect = this._def.effect || null;
|
|
2708
|
+
const checkCtx = {
|
|
2709
|
+
addIssue: (arg) => {
|
|
2710
|
+
addIssueToContext(ctx, arg);
|
|
2711
|
+
if (arg.fatal) {
|
|
2712
|
+
status.abort();
|
|
2713
|
+
} else {
|
|
2714
|
+
status.dirty();
|
|
2715
|
+
}
|
|
2716
|
+
},
|
|
2717
|
+
get path() {
|
|
2718
|
+
return ctx.path;
|
|
2719
|
+
}
|
|
2720
|
+
};
|
|
2721
|
+
checkCtx.addIssue = checkCtx.addIssue.bind(checkCtx);
|
|
2722
|
+
if (effect.type === "preprocess") {
|
|
2723
|
+
const processed = effect.transform(ctx.data, checkCtx);
|
|
2724
|
+
if (ctx.common.async) {
|
|
2725
|
+
return Promise.resolve(processed).then(async (processed2) => {
|
|
2726
|
+
if (status.value === "aborted")
|
|
2727
|
+
return INVALID;
|
|
2728
|
+
const result = await this._def.schema._parseAsync({
|
|
2729
|
+
data: processed2,
|
|
2730
|
+
path: ctx.path,
|
|
2731
|
+
parent: ctx
|
|
2732
|
+
});
|
|
2733
|
+
if (result.status === "aborted")
|
|
2734
|
+
return INVALID;
|
|
2735
|
+
if (result.status === "dirty")
|
|
2736
|
+
return DIRTY(result.value);
|
|
2737
|
+
if (status.value === "dirty")
|
|
2738
|
+
return DIRTY(result.value);
|
|
2739
|
+
return result;
|
|
2740
|
+
});
|
|
2741
|
+
} else {
|
|
2742
|
+
if (status.value === "aborted")
|
|
2743
|
+
return INVALID;
|
|
2744
|
+
const result = this._def.schema._parseSync({
|
|
2745
|
+
data: processed,
|
|
2746
|
+
path: ctx.path,
|
|
2747
|
+
parent: ctx
|
|
2748
|
+
});
|
|
2749
|
+
if (result.status === "aborted")
|
|
2750
|
+
return INVALID;
|
|
2751
|
+
if (result.status === "dirty")
|
|
2752
|
+
return DIRTY(result.value);
|
|
2753
|
+
if (status.value === "dirty")
|
|
2754
|
+
return DIRTY(result.value);
|
|
2755
|
+
return result;
|
|
2756
|
+
}
|
|
2757
|
+
}
|
|
2758
|
+
if (effect.type === "refinement") {
|
|
2759
|
+
const executeRefinement = (acc) => {
|
|
2760
|
+
const result = effect.refinement(acc, checkCtx);
|
|
2761
|
+
if (ctx.common.async) {
|
|
2762
|
+
return Promise.resolve(result);
|
|
2763
|
+
}
|
|
2764
|
+
if (result instanceof Promise) {
|
|
2765
|
+
throw new Error("Async refinement encountered during synchronous parse operation. Use .parseAsync instead.");
|
|
2766
|
+
}
|
|
2767
|
+
return acc;
|
|
112
2768
|
};
|
|
2769
|
+
if (ctx.common.async === false) {
|
|
2770
|
+
const inner = this._def.schema._parseSync({
|
|
2771
|
+
data: ctx.data,
|
|
2772
|
+
path: ctx.path,
|
|
2773
|
+
parent: ctx
|
|
2774
|
+
});
|
|
2775
|
+
if (inner.status === "aborted")
|
|
2776
|
+
return INVALID;
|
|
2777
|
+
if (inner.status === "dirty")
|
|
2778
|
+
status.dirty();
|
|
2779
|
+
executeRefinement(inner.value);
|
|
2780
|
+
return { status: status.value, value: inner.value };
|
|
2781
|
+
} else {
|
|
2782
|
+
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((inner) => {
|
|
2783
|
+
if (inner.status === "aborted")
|
|
2784
|
+
return INVALID;
|
|
2785
|
+
if (inner.status === "dirty")
|
|
2786
|
+
status.dirty();
|
|
2787
|
+
return executeRefinement(inner.value).then(() => {
|
|
2788
|
+
return { status: status.value, value: inner.value };
|
|
2789
|
+
});
|
|
2790
|
+
});
|
|
2791
|
+
}
|
|
2792
|
+
}
|
|
2793
|
+
if (effect.type === "transform") {
|
|
2794
|
+
if (ctx.common.async === false) {
|
|
2795
|
+
const base = this._def.schema._parseSync({
|
|
2796
|
+
data: ctx.data,
|
|
2797
|
+
path: ctx.path,
|
|
2798
|
+
parent: ctx
|
|
2799
|
+
});
|
|
2800
|
+
if (!isValid(base))
|
|
2801
|
+
return INVALID;
|
|
2802
|
+
const result = effect.transform(base.value, checkCtx);
|
|
2803
|
+
if (result instanceof Promise) {
|
|
2804
|
+
throw new Error(`Asynchronous transform encountered during synchronous parse operation. Use .parseAsync instead.`);
|
|
2805
|
+
}
|
|
2806
|
+
return { status: status.value, value: result };
|
|
2807
|
+
} else {
|
|
2808
|
+
return this._def.schema._parseAsync({ data: ctx.data, path: ctx.path, parent: ctx }).then((base) => {
|
|
2809
|
+
if (!isValid(base))
|
|
2810
|
+
return INVALID;
|
|
2811
|
+
return Promise.resolve(effect.transform(base.value, checkCtx)).then((result) => ({
|
|
2812
|
+
status: status.value,
|
|
2813
|
+
value: result
|
|
2814
|
+
}));
|
|
2815
|
+
});
|
|
2816
|
+
}
|
|
2817
|
+
}
|
|
2818
|
+
util.assertNever(effect);
|
|
2819
|
+
}
|
|
2820
|
+
}
|
|
2821
|
+
ZodEffects.create = (schema, effect, params) => {
|
|
2822
|
+
return new ZodEffects({
|
|
2823
|
+
schema,
|
|
2824
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
2825
|
+
effect,
|
|
2826
|
+
...processCreateParams(params)
|
|
2827
|
+
});
|
|
2828
|
+
};
|
|
2829
|
+
ZodEffects.createWithPreprocess = (preprocess, schema, params) => {
|
|
2830
|
+
return new ZodEffects({
|
|
2831
|
+
schema,
|
|
2832
|
+
effect: { type: "preprocess", transform: preprocess },
|
|
2833
|
+
typeName: ZodFirstPartyTypeKind.ZodEffects,
|
|
2834
|
+
...processCreateParams(params)
|
|
2835
|
+
});
|
|
2836
|
+
};
|
|
2837
|
+
class ZodOptional extends ZodType {
|
|
2838
|
+
_parse(input) {
|
|
2839
|
+
const parsedType = this._getType(input);
|
|
2840
|
+
if (parsedType === ZodParsedType.undefined) {
|
|
2841
|
+
return OK(void 0);
|
|
2842
|
+
}
|
|
2843
|
+
return this._def.innerType._parse(input);
|
|
2844
|
+
}
|
|
2845
|
+
unwrap() {
|
|
2846
|
+
return this._def.innerType;
|
|
2847
|
+
}
|
|
2848
|
+
}
|
|
2849
|
+
ZodOptional.create = (type, params) => {
|
|
2850
|
+
return new ZodOptional({
|
|
2851
|
+
innerType: type,
|
|
2852
|
+
typeName: ZodFirstPartyTypeKind.ZodOptional,
|
|
2853
|
+
...processCreateParams(params)
|
|
2854
|
+
});
|
|
2855
|
+
};
|
|
2856
|
+
class ZodNullable extends ZodType {
|
|
2857
|
+
_parse(input) {
|
|
2858
|
+
const parsedType = this._getType(input);
|
|
2859
|
+
if (parsedType === ZodParsedType.null) {
|
|
2860
|
+
return OK(null);
|
|
2861
|
+
}
|
|
2862
|
+
return this._def.innerType._parse(input);
|
|
2863
|
+
}
|
|
2864
|
+
unwrap() {
|
|
2865
|
+
return this._def.innerType;
|
|
2866
|
+
}
|
|
2867
|
+
}
|
|
2868
|
+
ZodNullable.create = (type, params) => {
|
|
2869
|
+
return new ZodNullable({
|
|
2870
|
+
innerType: type,
|
|
2871
|
+
typeName: ZodFirstPartyTypeKind.ZodNullable,
|
|
2872
|
+
...processCreateParams(params)
|
|
2873
|
+
});
|
|
2874
|
+
};
|
|
2875
|
+
class ZodDefault extends ZodType {
|
|
2876
|
+
_parse(input) {
|
|
2877
|
+
const { ctx } = this._processInputParams(input);
|
|
2878
|
+
let data = ctx.data;
|
|
2879
|
+
if (ctx.parsedType === ZodParsedType.undefined) {
|
|
2880
|
+
data = this._def.defaultValue();
|
|
2881
|
+
}
|
|
2882
|
+
return this._def.innerType._parse({
|
|
2883
|
+
data,
|
|
2884
|
+
path: ctx.path,
|
|
2885
|
+
parent: ctx
|
|
113
2886
|
});
|
|
114
|
-
|
|
2887
|
+
}
|
|
2888
|
+
removeDefault() {
|
|
2889
|
+
return this._def.innerType;
|
|
2890
|
+
}
|
|
2891
|
+
}
|
|
2892
|
+
ZodDefault.create = (type, params) => {
|
|
2893
|
+
return new ZodDefault({
|
|
2894
|
+
innerType: type,
|
|
2895
|
+
typeName: ZodFirstPartyTypeKind.ZodDefault,
|
|
2896
|
+
defaultValue: typeof params.default === "function" ? params.default : () => params.default,
|
|
2897
|
+
...processCreateParams(params)
|
|
2898
|
+
});
|
|
2899
|
+
};
|
|
2900
|
+
class ZodCatch extends ZodType {
|
|
2901
|
+
_parse(input) {
|
|
2902
|
+
const { ctx } = this._processInputParams(input);
|
|
2903
|
+
const newCtx = {
|
|
2904
|
+
...ctx,
|
|
2905
|
+
common: {
|
|
2906
|
+
...ctx.common,
|
|
2907
|
+
issues: []
|
|
2908
|
+
}
|
|
2909
|
+
};
|
|
2910
|
+
const result = this._def.innerType._parse({
|
|
2911
|
+
data: newCtx.data,
|
|
2912
|
+
path: newCtx.path,
|
|
2913
|
+
parent: {
|
|
2914
|
+
...newCtx
|
|
2915
|
+
}
|
|
2916
|
+
});
|
|
2917
|
+
if (isAsync(result)) {
|
|
2918
|
+
return result.then((result2) => {
|
|
2919
|
+
return {
|
|
2920
|
+
status: "valid",
|
|
2921
|
+
value: result2.status === "valid" ? result2.value : this._def.catchValue({
|
|
2922
|
+
get error() {
|
|
2923
|
+
return new ZodError(newCtx.common.issues);
|
|
2924
|
+
},
|
|
2925
|
+
input: newCtx.data
|
|
2926
|
+
})
|
|
2927
|
+
};
|
|
2928
|
+
});
|
|
2929
|
+
} else {
|
|
115
2930
|
return {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
2931
|
+
status: "valid",
|
|
2932
|
+
value: result.status === "valid" ? result.value : this._def.catchValue({
|
|
2933
|
+
get error() {
|
|
2934
|
+
return new ZodError(newCtx.common.issues);
|
|
2935
|
+
},
|
|
2936
|
+
input: newCtx.data
|
|
2937
|
+
})
|
|
119
2938
|
};
|
|
2939
|
+
}
|
|
2940
|
+
}
|
|
2941
|
+
removeCatch() {
|
|
2942
|
+
return this._def.innerType;
|
|
2943
|
+
}
|
|
2944
|
+
}
|
|
2945
|
+
ZodCatch.create = (type, params) => {
|
|
2946
|
+
return new ZodCatch({
|
|
2947
|
+
innerType: type,
|
|
2948
|
+
typeName: ZodFirstPartyTypeKind.ZodCatch,
|
|
2949
|
+
catchValue: typeof params.catch === "function" ? params.catch : () => params.catch,
|
|
2950
|
+
...processCreateParams(params)
|
|
2951
|
+
});
|
|
2952
|
+
};
|
|
2953
|
+
class ZodNaN extends ZodType {
|
|
2954
|
+
_parse(input) {
|
|
2955
|
+
const parsedType = this._getType(input);
|
|
2956
|
+
if (parsedType !== ZodParsedType.nan) {
|
|
2957
|
+
const ctx = this._getOrReturnCtx(input);
|
|
2958
|
+
addIssueToContext(ctx, {
|
|
2959
|
+
code: ZodIssueCode.invalid_type,
|
|
2960
|
+
expected: ZodParsedType.nan,
|
|
2961
|
+
received: ctx.parsedType
|
|
2962
|
+
});
|
|
2963
|
+
return INVALID;
|
|
2964
|
+
}
|
|
2965
|
+
return { status: "valid", value: input.data };
|
|
2966
|
+
}
|
|
2967
|
+
}
|
|
2968
|
+
ZodNaN.create = (params) => {
|
|
2969
|
+
return new ZodNaN({
|
|
2970
|
+
typeName: ZodFirstPartyTypeKind.ZodNaN,
|
|
2971
|
+
...processCreateParams(params)
|
|
2972
|
+
});
|
|
2973
|
+
};
|
|
2974
|
+
class ZodBranded extends ZodType {
|
|
2975
|
+
_parse(input) {
|
|
2976
|
+
const { ctx } = this._processInputParams(input);
|
|
2977
|
+
const data = ctx.data;
|
|
2978
|
+
return this._def.type._parse({
|
|
2979
|
+
data,
|
|
2980
|
+
path: ctx.path,
|
|
2981
|
+
parent: ctx
|
|
120
2982
|
});
|
|
121
|
-
this.edges = [...this.edges, ...newEdges];
|
|
122
|
-
const first = graph.firstNode();
|
|
123
|
-
const last = graph.lastNode();
|
|
124
|
-
return [first ? {
|
|
125
|
-
id: prefixed(first.id),
|
|
126
|
-
data: first.data
|
|
127
|
-
} : void 0, last ? {
|
|
128
|
-
id: prefixed(last.id),
|
|
129
|
-
data: last.data
|
|
130
|
-
} : void 0];
|
|
131
|
-
}
|
|
132
|
-
trimFirstNode() {
|
|
133
|
-
const firstNode = this.firstNode();
|
|
134
|
-
if (firstNode && _firstNode(this, [firstNode.id])) this.removeNode(firstNode);
|
|
135
|
-
}
|
|
136
|
-
trimLastNode() {
|
|
137
|
-
const lastNode = this.lastNode();
|
|
138
|
-
if (lastNode && _lastNode(this, [lastNode.id])) this.removeNode(lastNode);
|
|
139
2983
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
const
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
2984
|
+
unwrap() {
|
|
2985
|
+
return this._def.type;
|
|
2986
|
+
}
|
|
2987
|
+
}
|
|
2988
|
+
class ZodPipeline extends ZodType {
|
|
2989
|
+
_parse(input) {
|
|
2990
|
+
const { status, ctx } = this._processInputParams(input);
|
|
2991
|
+
if (ctx.common.async) {
|
|
2992
|
+
const handleAsync = async () => {
|
|
2993
|
+
const inResult = await this._def.in._parseAsync({
|
|
2994
|
+
data: ctx.data,
|
|
2995
|
+
path: ctx.path,
|
|
2996
|
+
parent: ctx
|
|
2997
|
+
});
|
|
2998
|
+
if (inResult.status === "aborted")
|
|
2999
|
+
return INVALID;
|
|
3000
|
+
if (inResult.status === "dirty") {
|
|
3001
|
+
status.dirty();
|
|
3002
|
+
return DIRTY(inResult.value);
|
|
3003
|
+
} else {
|
|
3004
|
+
return this._def.out._parseAsync({
|
|
3005
|
+
data: inResult.value,
|
|
3006
|
+
path: ctx.path,
|
|
3007
|
+
parent: ctx
|
|
3008
|
+
});
|
|
3009
|
+
}
|
|
3010
|
+
};
|
|
3011
|
+
return handleAsync();
|
|
3012
|
+
} else {
|
|
3013
|
+
const inResult = this._def.in._parseSync({
|
|
3014
|
+
data: ctx.data,
|
|
3015
|
+
path: ctx.path,
|
|
3016
|
+
parent: ctx
|
|
3017
|
+
});
|
|
3018
|
+
if (inResult.status === "aborted")
|
|
3019
|
+
return INVALID;
|
|
3020
|
+
if (inResult.status === "dirty") {
|
|
3021
|
+
status.dirty();
|
|
3022
|
+
return {
|
|
3023
|
+
status: "dirty",
|
|
3024
|
+
value: inResult.value
|
|
3025
|
+
};
|
|
3026
|
+
} else {
|
|
3027
|
+
return this._def.out._parseSync({
|
|
3028
|
+
data: inResult.value,
|
|
3029
|
+
path: ctx.path,
|
|
3030
|
+
parent: ctx
|
|
3031
|
+
});
|
|
3032
|
+
}
|
|
3033
|
+
}
|
|
3034
|
+
}
|
|
3035
|
+
static create(a, b) {
|
|
3036
|
+
return new ZodPipeline({
|
|
3037
|
+
in: a,
|
|
3038
|
+
out: b,
|
|
3039
|
+
typeName: ZodFirstPartyTypeKind.ZodPipeline
|
|
3040
|
+
});
|
|
3041
|
+
}
|
|
3042
|
+
}
|
|
3043
|
+
class ZodReadonly extends ZodType {
|
|
3044
|
+
_parse(input) {
|
|
3045
|
+
const result = this._def.innerType._parse(input);
|
|
3046
|
+
const freeze = (data) => {
|
|
3047
|
+
if (isValid(data)) {
|
|
3048
|
+
data.value = Object.freeze(data.value);
|
|
3049
|
+
}
|
|
3050
|
+
return data;
|
|
154
3051
|
};
|
|
155
|
-
return
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
edges: this.edges.map((edge) => ({
|
|
161
|
-
...edge,
|
|
162
|
-
source: getNodeId(edge.source),
|
|
163
|
-
target: getNodeId(edge.target)
|
|
164
|
-
}))
|
|
165
|
-
});
|
|
166
|
-
}
|
|
167
|
-
drawMermaid(params) {
|
|
168
|
-
const { withStyles, curveStyle, nodeColors = {
|
|
169
|
-
default: "fill:#f2f0ff,line-height:1.2",
|
|
170
|
-
first: "fill-opacity:0",
|
|
171
|
-
last: "fill:#bfb6fc"
|
|
172
|
-
}, wrapLabelNWords } = params ?? {};
|
|
173
|
-
const graph = this.reid();
|
|
174
|
-
const firstNode = graph.firstNode();
|
|
175
|
-
const lastNode = graph.lastNode();
|
|
176
|
-
return drawMermaid(graph.nodes, graph.edges, {
|
|
177
|
-
firstNode: firstNode?.id,
|
|
178
|
-
lastNode: lastNode?.id,
|
|
179
|
-
withStyles,
|
|
180
|
-
curveStyle,
|
|
181
|
-
nodeColors,
|
|
182
|
-
wrapLabelNWords
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
async drawMermaidPng(params) {
|
|
186
|
-
const mermaidSyntax = this.drawMermaid(params);
|
|
187
|
-
return drawMermaidImage(mermaidSyntax, { backgroundColor: params?.backgroundColor });
|
|
188
|
-
}
|
|
189
|
-
};
|
|
190
|
-
function _firstNode(graph, exclude = []) {
|
|
191
|
-
const targets = new Set(graph.edges.filter((edge) => !exclude.includes(edge.source)).map((edge) => edge.target));
|
|
192
|
-
const found = [];
|
|
193
|
-
for (const node of Object.values(graph.nodes)) if (!exclude.includes(node.id) && !targets.has(node.id)) found.push(node);
|
|
194
|
-
return found.length === 1 ? found[0] : void 0;
|
|
195
|
-
}
|
|
196
|
-
function _lastNode(graph, exclude = []) {
|
|
197
|
-
const sources = new Set(graph.edges.filter((edge) => !exclude.includes(edge.target)).map((edge) => edge.source));
|
|
198
|
-
const found = [];
|
|
199
|
-
for (const node of Object.values(graph.nodes)) if (!exclude.includes(node.id) && !sources.has(node.id)) found.push(node);
|
|
200
|
-
return found.length === 1 ? found[0] : void 0;
|
|
3052
|
+
return isAsync(result) ? result.then((data) => freeze(data)) : freeze(result);
|
|
3053
|
+
}
|
|
3054
|
+
unwrap() {
|
|
3055
|
+
return this._def.innerType;
|
|
3056
|
+
}
|
|
201
3057
|
}
|
|
3058
|
+
ZodReadonly.create = (type, params) => {
|
|
3059
|
+
return new ZodReadonly({
|
|
3060
|
+
innerType: type,
|
|
3061
|
+
typeName: ZodFirstPartyTypeKind.ZodReadonly,
|
|
3062
|
+
...processCreateParams(params)
|
|
3063
|
+
});
|
|
3064
|
+
};
|
|
3065
|
+
var ZodFirstPartyTypeKind;
|
|
3066
|
+
(function(ZodFirstPartyTypeKind2) {
|
|
3067
|
+
ZodFirstPartyTypeKind2["ZodString"] = "ZodString";
|
|
3068
|
+
ZodFirstPartyTypeKind2["ZodNumber"] = "ZodNumber";
|
|
3069
|
+
ZodFirstPartyTypeKind2["ZodNaN"] = "ZodNaN";
|
|
3070
|
+
ZodFirstPartyTypeKind2["ZodBigInt"] = "ZodBigInt";
|
|
3071
|
+
ZodFirstPartyTypeKind2["ZodBoolean"] = "ZodBoolean";
|
|
3072
|
+
ZodFirstPartyTypeKind2["ZodDate"] = "ZodDate";
|
|
3073
|
+
ZodFirstPartyTypeKind2["ZodSymbol"] = "ZodSymbol";
|
|
3074
|
+
ZodFirstPartyTypeKind2["ZodUndefined"] = "ZodUndefined";
|
|
3075
|
+
ZodFirstPartyTypeKind2["ZodNull"] = "ZodNull";
|
|
3076
|
+
ZodFirstPartyTypeKind2["ZodAny"] = "ZodAny";
|
|
3077
|
+
ZodFirstPartyTypeKind2["ZodUnknown"] = "ZodUnknown";
|
|
3078
|
+
ZodFirstPartyTypeKind2["ZodNever"] = "ZodNever";
|
|
3079
|
+
ZodFirstPartyTypeKind2["ZodVoid"] = "ZodVoid";
|
|
3080
|
+
ZodFirstPartyTypeKind2["ZodArray"] = "ZodArray";
|
|
3081
|
+
ZodFirstPartyTypeKind2["ZodObject"] = "ZodObject";
|
|
3082
|
+
ZodFirstPartyTypeKind2["ZodUnion"] = "ZodUnion";
|
|
3083
|
+
ZodFirstPartyTypeKind2["ZodDiscriminatedUnion"] = "ZodDiscriminatedUnion";
|
|
3084
|
+
ZodFirstPartyTypeKind2["ZodIntersection"] = "ZodIntersection";
|
|
3085
|
+
ZodFirstPartyTypeKind2["ZodTuple"] = "ZodTuple";
|
|
3086
|
+
ZodFirstPartyTypeKind2["ZodRecord"] = "ZodRecord";
|
|
3087
|
+
ZodFirstPartyTypeKind2["ZodMap"] = "ZodMap";
|
|
3088
|
+
ZodFirstPartyTypeKind2["ZodSet"] = "ZodSet";
|
|
3089
|
+
ZodFirstPartyTypeKind2["ZodFunction"] = "ZodFunction";
|
|
3090
|
+
ZodFirstPartyTypeKind2["ZodLazy"] = "ZodLazy";
|
|
3091
|
+
ZodFirstPartyTypeKind2["ZodLiteral"] = "ZodLiteral";
|
|
3092
|
+
ZodFirstPartyTypeKind2["ZodEnum"] = "ZodEnum";
|
|
3093
|
+
ZodFirstPartyTypeKind2["ZodEffects"] = "ZodEffects";
|
|
3094
|
+
ZodFirstPartyTypeKind2["ZodNativeEnum"] = "ZodNativeEnum";
|
|
3095
|
+
ZodFirstPartyTypeKind2["ZodOptional"] = "ZodOptional";
|
|
3096
|
+
ZodFirstPartyTypeKind2["ZodNullable"] = "ZodNullable";
|
|
3097
|
+
ZodFirstPartyTypeKind2["ZodDefault"] = "ZodDefault";
|
|
3098
|
+
ZodFirstPartyTypeKind2["ZodCatch"] = "ZodCatch";
|
|
3099
|
+
ZodFirstPartyTypeKind2["ZodPromise"] = "ZodPromise";
|
|
3100
|
+
ZodFirstPartyTypeKind2["ZodBranded"] = "ZodBranded";
|
|
3101
|
+
ZodFirstPartyTypeKind2["ZodPipeline"] = "ZodPipeline";
|
|
3102
|
+
ZodFirstPartyTypeKind2["ZodReadonly"] = "ZodReadonly";
|
|
3103
|
+
})(ZodFirstPartyTypeKind || (ZodFirstPartyTypeKind = {}));
|
|
3104
|
+
const stringType = ZodString.create;
|
|
3105
|
+
const anyType = ZodAny.create;
|
|
3106
|
+
ZodNever.create;
|
|
3107
|
+
ZodArray.create;
|
|
3108
|
+
const objectType = ZodObject.create;
|
|
3109
|
+
ZodUnion.create;
|
|
3110
|
+
ZodIntersection.create;
|
|
3111
|
+
ZodTuple.create;
|
|
3112
|
+
ZodEnum.create;
|
|
3113
|
+
ZodPromise.create;
|
|
3114
|
+
ZodOptional.create;
|
|
3115
|
+
ZodNullable.create;
|
|
202
3116
|
export {
|
|
203
|
-
|
|
204
|
-
|
|
3117
|
+
ZodType as Schema,
|
|
3118
|
+
ZodAny,
|
|
3119
|
+
ZodArray,
|
|
3120
|
+
ZodBigInt,
|
|
3121
|
+
ZodBoolean,
|
|
3122
|
+
ZodBranded,
|
|
3123
|
+
ZodCatch,
|
|
3124
|
+
ZodDate,
|
|
3125
|
+
ZodDefault,
|
|
3126
|
+
ZodEffects,
|
|
3127
|
+
ZodEnum,
|
|
3128
|
+
ZodFirstPartyTypeKind,
|
|
3129
|
+
ZodIntersection,
|
|
3130
|
+
ZodLazy,
|
|
3131
|
+
ZodLiteral,
|
|
3132
|
+
ZodMap,
|
|
3133
|
+
ZodNaN,
|
|
3134
|
+
ZodNativeEnum,
|
|
3135
|
+
ZodNever,
|
|
3136
|
+
ZodNull,
|
|
3137
|
+
ZodNullable,
|
|
3138
|
+
ZodNumber,
|
|
3139
|
+
ZodObject,
|
|
3140
|
+
ZodOptional,
|
|
3141
|
+
ZodPipeline,
|
|
3142
|
+
ZodPromise,
|
|
3143
|
+
ZodReadonly,
|
|
3144
|
+
ZodType as ZodSchema,
|
|
3145
|
+
ZodSet,
|
|
3146
|
+
ZodString,
|
|
3147
|
+
ZodSymbol,
|
|
3148
|
+
ZodEffects as ZodTransformer,
|
|
3149
|
+
ZodTuple,
|
|
3150
|
+
ZodType,
|
|
3151
|
+
ZodUndefined,
|
|
3152
|
+
ZodUnion,
|
|
3153
|
+
ZodUnknown,
|
|
3154
|
+
ZodVoid,
|
|
3155
|
+
anyType as any,
|
|
3156
|
+
datetimeRegex,
|
|
3157
|
+
objectType as object,
|
|
3158
|
+
stringType as string
|
|
205
3159
|
};
|
|
206
3160
|
//# sourceMappingURL=index.es101.js.map
|