attaform 0.15.1 → 0.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -12
- package/dist/chunks/devtools.cjs +4 -4
- package/dist/chunks/devtools.cjs.map +1 -1
- package/dist/chunks/devtools.mjs +2 -2
- package/dist/chunks/indexeddb.cjs +4 -4
- package/dist/chunks/indexeddb.cjs.map +1 -1
- package/dist/chunks/indexeddb.mjs +1 -1
- package/dist/chunks/local-storage.cjs +2 -2
- package/dist/chunks/local-storage.cjs.map +1 -1
- package/dist/chunks/local-storage.mjs +1 -1
- package/dist/chunks/session-storage.cjs +2 -2
- package/dist/chunks/session-storage.cjs.map +1 -1
- package/dist/chunks/session-storage.mjs +1 -1
- package/dist/index.cjs +23 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +75 -70
- package/dist/index.d.mts +75 -70
- package/dist/index.d.ts +75 -70
- package/dist/index.mjs +6 -6
- package/dist/nuxt.cjs +5 -11
- package/dist/nuxt.cjs.map +1 -1
- package/dist/nuxt.d.cts +8 -0
- package/dist/nuxt.d.mts +8 -0
- package/dist/nuxt.d.ts +8 -0
- package/dist/nuxt.mjs +6 -12
- package/dist/nuxt.mjs.map +1 -1
- package/dist/runtime/plugins/attaform.cjs +3 -2
- package/dist/runtime/plugins/attaform.cjs.map +1 -1
- package/dist/runtime/plugins/attaform.mjs +2 -1
- package/dist/runtime/plugins/attaform.mjs.map +1 -1
- package/dist/shared/{attaform.BwaYWtMs.d.cts → attaform.B7rzpK1U.d.cts} +34 -5
- package/dist/shared/{attaform.BwaYWtMs.d.mts → attaform.B7rzpK1U.d.mts} +34 -5
- package/dist/shared/{attaform.BwaYWtMs.d.ts → attaform.B7rzpK1U.d.ts} +34 -5
- package/dist/shared/attaform.BAuJTWuT.d.mts +84 -0
- package/dist/shared/{attaform.CRk8NhlD.mjs → attaform.BfMxsfmE.mjs} +428 -49
- package/dist/shared/attaform.BfMxsfmE.mjs.map +1 -0
- package/dist/shared/attaform.Bp1c-uGF.cjs +1561 -0
- package/dist/shared/attaform.Bp1c-uGF.cjs.map +1 -0
- package/dist/shared/{attaform.CDJVeoJU.cjs → attaform.C9Ph2SMx.cjs} +49 -42
- package/dist/shared/{attaform.qxyip_aN.mjs.map → attaform.C9Ph2SMx.cjs.map} +1 -1
- package/dist/shared/attaform.CINUMjPq.mjs +29 -0
- package/dist/shared/attaform.CINUMjPq.mjs.map +1 -0
- package/dist/shared/attaform.CJttVxRj.cjs +32 -0
- package/dist/shared/attaform.CJttVxRj.cjs.map +1 -0
- package/dist/shared/attaform.CvOXSpCb.mjs +1908 -0
- package/dist/shared/attaform.CvOXSpCb.mjs.map +1 -0
- package/dist/shared/{attaform.qxyip_aN.mjs → attaform.DILbdvfo.mjs} +12 -5
- package/dist/shared/{attaform.CDJVeoJU.cjs.map → attaform.DILbdvfo.mjs.map} +1 -1
- package/dist/shared/attaform.DdnithOf.mjs +1555 -0
- package/dist/shared/attaform.DdnithOf.mjs.map +1 -0
- package/dist/shared/attaform.DfrYByDj.cjs +1916 -0
- package/dist/shared/attaform.DfrYByDj.cjs.map +1 -0
- package/dist/shared/{attaform.BOi138GE.cjs → attaform.c_NzdRyc.cjs} +4 -4
- package/dist/shared/{attaform.BOi138GE.cjs.map → attaform.c_NzdRyc.cjs.map} +1 -1
- package/dist/shared/{attaform.DXye3JKf.mjs → attaform.jrxE_xZw.mjs} +2 -2
- package/dist/shared/{attaform.DXye3JKf.mjs.map → attaform.jrxE_xZw.mjs.map} +1 -1
- package/dist/shared/attaform.ls_7jBYc.d.ts +84 -0
- package/dist/shared/{attaform.BgYBU8gV.cjs → attaform.rIRYSUI1.cjs} +461 -61
- package/dist/shared/attaform.rIRYSUI1.cjs.map +1 -0
- package/dist/shared/attaform.xIcmqscx.d.cts +84 -0
- package/dist/vite.cjs +62 -9
- package/dist/vite.cjs.map +1 -1
- package/dist/vite.d.cts +23 -32
- package/dist/vite.d.mts +23 -32
- package/dist/vite.d.ts +23 -32
- package/dist/vite.mjs +62 -9
- package/dist/vite.mjs.map +1 -1
- package/dist/zod-v3.cjs +9 -1553
- package/dist/zod-v3.cjs.map +1 -1
- package/dist/zod-v3.mjs +3 -1553
- package/dist/zod-v3.mjs.map +1 -1
- package/dist/zod-v4.cjs +21 -0
- package/dist/zod-v4.cjs.map +1 -0
- package/dist/zod-v4.d.cts +104 -0
- package/dist/zod-v4.d.mts +104 -0
- package/dist/zod-v4.d.ts +104 -0
- package/dist/zod-v4.mjs +4 -0
- package/dist/zod-v4.mjs.map +1 -0
- package/dist/zod.cjs +19 -1900
- package/dist/zod.cjs.map +1 -1
- package/dist/zod.d.cts +27 -155
- package/dist/zod.d.mts +27 -155
- package/dist/zod.d.ts +27 -155
- package/dist/zod.mjs +19 -1896
- package/dist/zod.mjs.map +1 -1
- package/package.json +6 -2
- package/dist/shared/attaform.BgYBU8gV.cjs.map +0 -1
- package/dist/shared/attaform.CRk8NhlD.mjs.map +0 -1
- package/dist/shared/attaform.RypIkgVy.cjs +0 -417
- package/dist/shared/attaform.RypIkgVy.cjs.map +0 -1
- package/dist/shared/attaform.a99dQV7Q.mjs +0 -392
- package/dist/shared/attaform.a99dQV7Q.mjs.map +0 -1
package/dist/zod.cjs
CHANGED
|
@@ -1,1915 +1,34 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
const
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
3
|
+
const plugin = require('./shared/attaform.rIRYSUI1.cjs');
|
|
4
|
+
const useForm$1 = require('./shared/attaform.Bp1c-uGF.cjs');
|
|
5
|
+
const index = require('./shared/attaform.DfrYByDj.cjs');
|
|
6
|
+
const useFormContext = require('./shared/attaform.C9Ph2SMx.cjs');
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
const
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
list.push(payload);
|
|
14
|
-
fieldMetaLists.set(schema, list);
|
|
15
|
-
return originalAdd(schema, payload);
|
|
16
|
-
};
|
|
17
|
-
function getFieldMetaList(schema) {
|
|
18
|
-
return fieldMetaLists.get(schema) ?? [];
|
|
19
|
-
}
|
|
20
|
-
function withMeta(schema, payload) {
|
|
21
|
-
const existing = fieldMeta.get(schema) ?? {};
|
|
22
|
-
const cloned = schema.clone();
|
|
23
|
-
fieldMeta.add(cloned, { ...existing, ...payload });
|
|
24
|
-
return cloned;
|
|
25
|
-
}
|
|
26
|
-
function getFieldMeta(schema) {
|
|
27
|
-
return fieldMeta.get(schema);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
let warnedZodCodeMissing = false;
|
|
31
|
-
function zodIssuesToValidationErrors(issues, formKey) {
|
|
32
|
-
return issues.map((issue) => {
|
|
33
|
-
let code;
|
|
34
|
-
if (typeof issue.code === "string" && issue.code.length > 0) {
|
|
35
|
-
code = `zod:${issue.code}`;
|
|
36
|
-
} else {
|
|
37
|
-
code = "zod:unknown";
|
|
38
|
-
if (sensitiveNames.__DEV__ && !warnedZodCodeMissing) {
|
|
39
|
-
warnedZodCodeMissing = true;
|
|
40
|
-
console.warn(
|
|
41
|
-
"[attaform] zod-v4 adapter received an issue with no string `code`; stamping `'zod:unknown'`. This usually means a custom Zod plugin emitted an issue without the standard code field."
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
}
|
|
45
|
-
return {
|
|
46
|
-
message: issue.message,
|
|
47
|
-
path: issue.path.map((seg) => typeof seg === "number" ? seg : String(seg)),
|
|
48
|
-
formKey,
|
|
49
|
-
code
|
|
50
|
-
};
|
|
51
|
-
});
|
|
52
|
-
}
|
|
53
|
-
class UnsupportedSchemaError extends sensitiveNames.AttaformError {
|
|
8
|
+
function isZodV4SchemaShape(value) {
|
|
9
|
+
if (typeof value !== "object" || value === null) return false;
|
|
10
|
+
const def = value.def;
|
|
11
|
+
if (typeof def !== "object" || def === null) return false;
|
|
12
|
+
return typeof def.type === "string";
|
|
54
13
|
}
|
|
55
14
|
|
|
56
|
-
function
|
|
57
|
-
if (
|
|
58
|
-
|
|
59
|
-
}
|
|
60
|
-
function kindOf(schema) {
|
|
61
|
-
const def = readDef(schema);
|
|
62
|
-
const rawType = def?.type;
|
|
63
|
-
if (rawType === void 0) return "unknown";
|
|
64
|
-
switch (rawType) {
|
|
65
|
-
case "object":
|
|
66
|
-
return "object";
|
|
67
|
-
case "array":
|
|
68
|
-
return "array";
|
|
69
|
-
case "set":
|
|
70
|
-
return "set";
|
|
71
|
-
case "record":
|
|
72
|
-
return "record";
|
|
73
|
-
case "tuple":
|
|
74
|
-
return "tuple";
|
|
75
|
-
case "union":
|
|
76
|
-
return def?.discriminator !== void 0 ? "discriminated-union" : "union";
|
|
77
|
-
case "discriminated_union":
|
|
78
|
-
case "discriminatedUnion":
|
|
79
|
-
return "discriminated-union";
|
|
80
|
-
case "string":
|
|
81
|
-
return "string";
|
|
82
|
-
case "number":
|
|
83
|
-
return "number";
|
|
84
|
-
case "boolean":
|
|
85
|
-
return "boolean";
|
|
86
|
-
case "bigint":
|
|
87
|
-
return "bigint";
|
|
88
|
-
case "date":
|
|
89
|
-
return "date";
|
|
90
|
-
case "enum":
|
|
91
|
-
return "enum";
|
|
92
|
-
case "literal":
|
|
93
|
-
return "literal";
|
|
94
|
-
case "null":
|
|
95
|
-
return "null";
|
|
96
|
-
case "undefined":
|
|
97
|
-
return "undefined";
|
|
98
|
-
case "optional":
|
|
99
|
-
return "optional";
|
|
100
|
-
case "nullable":
|
|
101
|
-
return "nullable";
|
|
102
|
-
case "default":
|
|
103
|
-
case "prefault":
|
|
104
|
-
return "default";
|
|
105
|
-
case "pipe":
|
|
106
|
-
return "pipe";
|
|
107
|
-
case "readonly":
|
|
108
|
-
return "readonly";
|
|
109
|
-
case "any":
|
|
110
|
-
return "any";
|
|
111
|
-
case "nan":
|
|
112
|
-
return "nan";
|
|
113
|
-
case "void":
|
|
114
|
-
return "void";
|
|
115
|
-
case "never":
|
|
116
|
-
return "never";
|
|
117
|
-
case "lazy":
|
|
118
|
-
return "lazy";
|
|
119
|
-
case "intersection":
|
|
120
|
-
return "intersection";
|
|
121
|
-
case "catch":
|
|
122
|
-
return "catch";
|
|
123
|
-
case "promise":
|
|
124
|
-
return "promise";
|
|
125
|
-
case "custom":
|
|
126
|
-
return "custom";
|
|
127
|
-
case "template_literal":
|
|
128
|
-
case "templateLiteral":
|
|
129
|
-
return "template-literal";
|
|
130
|
-
default:
|
|
131
|
-
return "unknown";
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
function getObjectShape(schema) {
|
|
135
|
-
const s = schema;
|
|
136
|
-
return s.shape;
|
|
137
|
-
}
|
|
138
|
-
function getArrayElement(schema) {
|
|
139
|
-
const def = readDef(schema);
|
|
140
|
-
return def?.element;
|
|
141
|
-
}
|
|
142
|
-
function getSetValueType(schema) {
|
|
143
|
-
const def = readDef(schema);
|
|
144
|
-
return def?.valueType;
|
|
145
|
-
}
|
|
146
|
-
function getRecordKeyType(schema) {
|
|
147
|
-
const def = readDef(schema);
|
|
148
|
-
return def?.keyType;
|
|
149
|
-
}
|
|
150
|
-
function getRecordValueType(schema) {
|
|
151
|
-
const def = readDef(schema);
|
|
152
|
-
return def?.valueType;
|
|
153
|
-
}
|
|
154
|
-
function getTupleItems(schema) {
|
|
155
|
-
const def = readDef(schema);
|
|
156
|
-
return def?.items ?? [];
|
|
157
|
-
}
|
|
158
|
-
function getUnionOptions(schema) {
|
|
159
|
-
const def = readDef(schema);
|
|
160
|
-
return def?.options ?? [];
|
|
161
|
-
}
|
|
162
|
-
function getLiteralValues(schema) {
|
|
163
|
-
const def = readDef(schema);
|
|
164
|
-
return def?.values ?? [];
|
|
165
|
-
}
|
|
166
|
-
function getEnumValues(schema) {
|
|
167
|
-
const def = readDef(schema);
|
|
168
|
-
const entries = def?.entries;
|
|
169
|
-
if (entries === void 0) return [];
|
|
170
|
-
return Object.values(entries);
|
|
171
|
-
}
|
|
172
|
-
function unwrapInner(schema) {
|
|
173
|
-
const def = readDef(schema);
|
|
174
|
-
return def?.innerType;
|
|
175
|
-
}
|
|
176
|
-
function unwrapPipe(schema) {
|
|
177
|
-
const def = readDef(schema);
|
|
178
|
-
return def?.in ?? def?.out;
|
|
179
|
-
}
|
|
180
|
-
function unwrapLazy(schema) {
|
|
181
|
-
const def = readDef(schema);
|
|
182
|
-
const getter = def?.getter;
|
|
183
|
-
if (typeof getter !== "function") return void 0;
|
|
184
|
-
return getter();
|
|
185
|
-
}
|
|
186
|
-
function getLazyGetter(schema) {
|
|
187
|
-
const def = readDef(schema);
|
|
188
|
-
return typeof def?.getter === "function" ? def.getter : void 0;
|
|
189
|
-
}
|
|
190
|
-
function getIntersectionLeft(schema) {
|
|
191
|
-
const def = readDef(schema);
|
|
192
|
-
return def?.left;
|
|
193
|
-
}
|
|
194
|
-
function getIntersectionRight(schema) {
|
|
195
|
-
const def = readDef(schema);
|
|
196
|
-
return def?.right;
|
|
197
|
-
}
|
|
198
|
-
function getCatchDefault(schema) {
|
|
199
|
-
const def = readDef(schema);
|
|
200
|
-
const cv = def?.catchValue;
|
|
201
|
-
if (typeof cv !== "function") return void 0;
|
|
202
|
-
try {
|
|
203
|
-
return cv({ error: new Error("atta:default-values"), input: void 0 });
|
|
204
|
-
} catch {
|
|
205
|
-
return void 0;
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
function getDefaultValue(schema) {
|
|
209
|
-
const def = readDef(schema);
|
|
210
|
-
return def?.defaultValue;
|
|
211
|
-
}
|
|
212
|
-
function hasChecks(schema) {
|
|
213
|
-
const def = readDef(schema);
|
|
214
|
-
const checks = def?.checks;
|
|
215
|
-
return Array.isArray(checks) && checks.length > 0;
|
|
216
|
-
}
|
|
217
|
-
function getChecks(schema) {
|
|
218
|
-
const def = readDef(schema);
|
|
219
|
-
const checks = def?.checks;
|
|
220
|
-
return Array.isArray(checks) ? checks : [];
|
|
221
|
-
}
|
|
222
|
-
function getDiscriminator(schema) {
|
|
223
|
-
const def = readDef(schema);
|
|
224
|
-
return def?.discriminator;
|
|
225
|
-
}
|
|
226
|
-
function getDiscriminatedOptions(schema) {
|
|
227
|
-
const def = readDef(schema);
|
|
228
|
-
const options = def?.options;
|
|
229
|
-
return Array.isArray(options) ? options : [];
|
|
230
|
-
}
|
|
231
|
-
function assertZodVersion(schema) {
|
|
232
|
-
const def = readDef(schema);
|
|
233
|
-
if (def?.type === void 0) {
|
|
234
|
-
throw new Error("[attaform/zod] schema is not zod v4. Install zod@^4 or use /zod-v3.");
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
function containsAsyncRefine(schema, seen) {
|
|
238
|
-
const visited = seen ?? /* @__PURE__ */ new WeakSet();
|
|
239
|
-
const candidate = schema;
|
|
240
|
-
if (typeof candidate !== "object" || candidate === null) return false;
|
|
241
|
-
if (visited.has(candidate)) return false;
|
|
242
|
-
visited.add(candidate);
|
|
243
|
-
const checks = getChecks(schema);
|
|
244
|
-
for (const check of checks) {
|
|
245
|
-
if (isAsyncCheck(check)) return true;
|
|
246
|
-
}
|
|
247
|
-
const def = readDef(schema);
|
|
248
|
-
if (def === void 0) return false;
|
|
249
|
-
if (def.innerType !== void 0 && containsAsyncRefine(def.innerType, visited)) {
|
|
250
|
-
return true;
|
|
251
|
-
}
|
|
252
|
-
if (def.element !== void 0 && containsAsyncRefine(def.element, visited)) {
|
|
253
|
-
return true;
|
|
254
|
-
}
|
|
255
|
-
if (def.in !== void 0 && containsAsyncRefine(def.in, visited)) {
|
|
256
|
-
return true;
|
|
257
|
-
}
|
|
258
|
-
if (def.out !== void 0 && containsAsyncRefine(def.out, visited)) {
|
|
259
|
-
return true;
|
|
260
|
-
}
|
|
261
|
-
if (def.left !== void 0 && containsAsyncRefine(def.left, visited)) {
|
|
262
|
-
return true;
|
|
263
|
-
}
|
|
264
|
-
if (def.right !== void 0 && containsAsyncRefine(def.right, visited)) {
|
|
265
|
-
return true;
|
|
266
|
-
}
|
|
267
|
-
if (def.keyType !== void 0 && containsAsyncRefine(def.keyType, visited)) {
|
|
268
|
-
return true;
|
|
269
|
-
}
|
|
270
|
-
if (def.valueType !== void 0 && containsAsyncRefine(def.valueType, visited)) {
|
|
271
|
-
return true;
|
|
272
|
-
}
|
|
273
|
-
if (def.shape !== void 0) {
|
|
274
|
-
for (const sub of Object.values(def.shape)) {
|
|
275
|
-
if (containsAsyncRefine(sub, visited)) return true;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
if (def.entries !== void 0) {
|
|
279
|
-
for (const sub of Object.values(def.entries)) {
|
|
280
|
-
if (containsAsyncRefine(sub, visited)) return true;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
if (def.options !== void 0) {
|
|
284
|
-
for (const sub of def.options) {
|
|
285
|
-
if (containsAsyncRefine(sub, visited)) return true;
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
if (def.items !== void 0) {
|
|
289
|
-
for (const sub of def.items) {
|
|
290
|
-
if (containsAsyncRefine(sub, visited)) return true;
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
if (typeof def.getter === "function") {
|
|
294
|
-
try {
|
|
295
|
-
const inner = def.getter();
|
|
296
|
-
if (containsAsyncRefine(inner, visited)) return true;
|
|
297
|
-
} catch {
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
return false;
|
|
301
|
-
}
|
|
302
|
-
function isAsyncCheck(check) {
|
|
303
|
-
if (typeof check !== "object" || check === null) return false;
|
|
304
|
-
const c = check;
|
|
305
|
-
const fn = c._def?.fn ?? c.def?.fn ?? c._zod?.def?.fn;
|
|
306
|
-
if (typeof fn !== "function") return false;
|
|
307
|
-
return fn.constructor.name === "AsyncFunction";
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
const UNSUPPORTED = ["promise", "custom", "template-literal"];
|
|
311
|
-
function labelPath(path) {
|
|
312
|
-
return path.length === 0 ? "<root>" : path.join(".");
|
|
313
|
-
}
|
|
314
|
-
function assertSupportedKinds(schema, path = [], lazyGetters = []) {
|
|
315
|
-
const kind = kindOf(schema);
|
|
316
|
-
if (UNSUPPORTED.includes(kind)) {
|
|
317
|
-
throw new UnsupportedSchemaError(
|
|
318
|
-
`[attaform/zod] unsupported kind '${kind}' at '${labelPath(path)}'`
|
|
319
|
-
);
|
|
320
|
-
}
|
|
321
|
-
switch (kind) {
|
|
322
|
-
case "object": {
|
|
323
|
-
const shape = getObjectShape(schema);
|
|
324
|
-
for (const [key, sub] of Object.entries(shape)) {
|
|
325
|
-
assertSupportedKinds(sub, [...path, key], lazyGetters);
|
|
326
|
-
}
|
|
327
|
-
return;
|
|
328
|
-
}
|
|
329
|
-
case "array":
|
|
330
|
-
assertSupportedKinds(getArrayElement(schema), [...path, "*"], lazyGetters);
|
|
331
|
-
return;
|
|
332
|
-
case "set":
|
|
333
|
-
assertSupportedKinds(getSetValueType(schema), [...path, "*"], lazyGetters);
|
|
334
|
-
return;
|
|
335
|
-
case "record":
|
|
336
|
-
assertSupportedKinds(getRecordValueType(schema), [...path, "*"], lazyGetters);
|
|
337
|
-
return;
|
|
338
|
-
case "tuple": {
|
|
339
|
-
const items = getTupleItems(schema);
|
|
340
|
-
items.forEach((item, i) => assertSupportedKinds(item, [...path, String(i)], lazyGetters));
|
|
341
|
-
return;
|
|
342
|
-
}
|
|
343
|
-
case "union": {
|
|
344
|
-
const options = getUnionOptions(schema);
|
|
345
|
-
options.forEach((opt, i) => assertSupportedKinds(opt, [...path, `|${i}`], lazyGetters));
|
|
346
|
-
return;
|
|
347
|
-
}
|
|
348
|
-
case "discriminated-union": {
|
|
349
|
-
const options = getDiscriminatedOptions(schema);
|
|
350
|
-
options.forEach((opt, i) => assertSupportedKinds(opt, [...path, `|${i}`], lazyGetters));
|
|
351
|
-
return;
|
|
352
|
-
}
|
|
353
|
-
case "optional":
|
|
354
|
-
case "nullable":
|
|
355
|
-
case "default":
|
|
356
|
-
case "readonly":
|
|
357
|
-
case "catch": {
|
|
358
|
-
const inner = unwrapInner(schema);
|
|
359
|
-
if (inner !== void 0) assertSupportedKinds(inner, path, lazyGetters);
|
|
360
|
-
return;
|
|
361
|
-
}
|
|
362
|
-
case "pipe": {
|
|
363
|
-
const inner = unwrapPipe(schema);
|
|
364
|
-
if (inner !== void 0) assertSupportedKinds(inner, path, lazyGetters);
|
|
365
|
-
return;
|
|
366
|
-
}
|
|
367
|
-
case "lazy": {
|
|
368
|
-
const getter = getLazyGetter(schema);
|
|
369
|
-
if (getter !== void 0 && lazyGetters.includes(getter)) {
|
|
370
|
-
throw new UnsupportedSchemaError(
|
|
371
|
-
`[attaform/zod] Recursive z.lazy() at '${labelPath(path)}'`
|
|
372
|
-
);
|
|
373
|
-
}
|
|
374
|
-
const inner = unwrapLazy(schema);
|
|
375
|
-
if (inner !== void 0) {
|
|
376
|
-
assertSupportedKinds(
|
|
377
|
-
inner,
|
|
378
|
-
path,
|
|
379
|
-
getter === void 0 ? lazyGetters : [...lazyGetters, getter]
|
|
380
|
-
);
|
|
381
|
-
}
|
|
382
|
-
return;
|
|
383
|
-
}
|
|
384
|
-
case "intersection": {
|
|
385
|
-
const left = getIntersectionLeft(schema);
|
|
386
|
-
const right = getIntersectionRight(schema);
|
|
387
|
-
if (left !== void 0) assertSupportedKinds(left, [...path, "left"], lazyGetters);
|
|
388
|
-
if (right !== void 0) assertSupportedKinds(right, [...path, "right"], lazyGetters);
|
|
389
|
-
return;
|
|
390
|
-
}
|
|
391
|
-
// Leaves: nothing to descend into.
|
|
392
|
-
case "string":
|
|
393
|
-
case "number":
|
|
394
|
-
case "boolean":
|
|
395
|
-
case "bigint":
|
|
396
|
-
case "date":
|
|
397
|
-
case "enum":
|
|
398
|
-
case "literal":
|
|
399
|
-
case "null":
|
|
400
|
-
case "undefined":
|
|
401
|
-
case "nan":
|
|
402
|
-
case "any":
|
|
403
|
-
case "unknown":
|
|
404
|
-
case "void":
|
|
405
|
-
case "never":
|
|
406
|
-
case "promise":
|
|
407
|
-
case "custom":
|
|
408
|
-
case "template-literal":
|
|
409
|
-
return;
|
|
410
|
-
default: {
|
|
411
|
-
const _exhaustive = kind;
|
|
412
|
-
throw new Error(`assertSupportedKinds: unhandled ZodKind '${_exhaustive}'`);
|
|
413
|
-
}
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
function unwrapToDiscriminatedUnion(schema) {
|
|
418
|
-
let current = schema;
|
|
419
|
-
for (let i = 0; i < 64; i++) {
|
|
420
|
-
const kind = kindOf(current);
|
|
421
|
-
if (kind === "discriminated-union") return current;
|
|
422
|
-
let next;
|
|
423
|
-
if (kind === "optional" || kind === "nullable" || kind === "default" || kind === "readonly") {
|
|
424
|
-
next = unwrapInner(current);
|
|
425
|
-
} else if (kind === "pipe") {
|
|
426
|
-
next = unwrapPipe(current);
|
|
427
|
-
}
|
|
428
|
-
if (next === void 0) return void 0;
|
|
429
|
-
current = next;
|
|
430
|
-
}
|
|
431
|
-
return void 0;
|
|
432
|
-
}
|
|
433
|
-
function getDiscriminatedUnionFirstOption(schema) {
|
|
434
|
-
const options = getDiscriminatedOptions(schema);
|
|
435
|
-
return options[0];
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
const cyclicSentinel = "<cyclic>";
|
|
439
|
-
function fingerprintZodSchema(schema) {
|
|
440
|
-
const cache = /* @__PURE__ */ new WeakMap();
|
|
441
|
-
const inProgress = /* @__PURE__ */ new WeakSet();
|
|
442
|
-
return visit(schema, cache, inProgress);
|
|
443
|
-
}
|
|
444
|
-
function visit(schema, cache, inProgress) {
|
|
445
|
-
const cached = cache.get(schema);
|
|
446
|
-
if (cached !== void 0) return cached;
|
|
447
|
-
if (inProgress.has(schema)) return cyclicSentinel;
|
|
448
|
-
inProgress.add(schema);
|
|
449
|
-
try {
|
|
450
|
-
const computed = computeFingerprint(schema, cache, inProgress);
|
|
451
|
-
cache.set(schema, computed);
|
|
452
|
-
return computed;
|
|
453
|
-
} finally {
|
|
454
|
-
inProgress.delete(schema);
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
function computeFingerprint(schema, cache, inProgress) {
|
|
458
|
-
const kind = kindOf(schema);
|
|
459
|
-
const recurse = (child) => visit(child, cache, inProgress);
|
|
460
|
-
switch (kind) {
|
|
461
|
-
// Kind-only leaves: no further structure to descend into.
|
|
462
|
-
case "boolean":
|
|
463
|
-
case "null":
|
|
464
|
-
case "undefined":
|
|
465
|
-
case "any":
|
|
466
|
-
case "unknown":
|
|
467
|
-
case "nan":
|
|
468
|
-
case "void":
|
|
469
|
-
case "never":
|
|
470
|
-
return kind;
|
|
471
|
-
// Leaves with checks (min/max/email/regex/...). Checks are
|
|
472
|
-
// canonicalised and sorted so `.min(3).max(10)` and `.max(10).min(3)`
|
|
473
|
-
// produce identical fingerprints.
|
|
474
|
-
case "string":
|
|
475
|
-
case "number":
|
|
476
|
-
case "bigint":
|
|
477
|
-
case "date":
|
|
478
|
-
return `${kind}${formatChecks(schema)}`;
|
|
479
|
-
case "literal": {
|
|
480
|
-
const values = [...getLiteralValues(schema)].sort((a, b) => {
|
|
481
|
-
const as = canonicalStringify(a);
|
|
482
|
-
const bs = canonicalStringify(b);
|
|
483
|
-
return as < bs ? -1 : as > bs ? 1 : 0;
|
|
484
|
-
});
|
|
485
|
-
return `literal:${canonicalStringify(values)}`;
|
|
486
|
-
}
|
|
487
|
-
case "enum":
|
|
488
|
-
return `enum:${canonicalStringify([...getEnumValues(schema)].sort((a, b) => compare(a, b)))}`;
|
|
489
|
-
case "object": {
|
|
490
|
-
const shape = getObjectShape(schema);
|
|
491
|
-
const sortedEntries = Object.entries(shape).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([k, v]) => `${JSON.stringify(k)}:${recurse(v)}`);
|
|
492
|
-
return `object{${sortedEntries.join(",")}}${formatChecks(schema)}`;
|
|
493
|
-
}
|
|
494
|
-
case "array":
|
|
495
|
-
return `array[${recurse(getArrayElement(schema))}]${formatChecks(schema)}`;
|
|
496
|
-
case "set":
|
|
497
|
-
return `set<${recurse(getSetValueType(schema))}>${formatChecks(schema)}`;
|
|
498
|
-
case "tuple":
|
|
499
|
-
return `tuple[${getTupleItems(schema).map(recurse).join(",")}]`;
|
|
500
|
-
case "record":
|
|
501
|
-
return `record<${recurse(getRecordKeyType(schema))},${recurse(getRecordValueType(schema))}>`;
|
|
502
|
-
case "union": {
|
|
503
|
-
const options = getUnionOptions(schema).map(recurse).sort();
|
|
504
|
-
return `union(${options.join("|")})`;
|
|
505
|
-
}
|
|
506
|
-
case "discriminated-union": {
|
|
507
|
-
const disc = getDiscriminator(schema) ?? "?";
|
|
508
|
-
const options = getDiscriminatedOptions(schema).map(recurse).sort();
|
|
509
|
-
return `dunion[${JSON.stringify(disc)}](${options.join("|")})`;
|
|
510
|
-
}
|
|
511
|
-
case "optional": {
|
|
512
|
-
const inner = unwrapInner(schema);
|
|
513
|
-
return inner === void 0 ? "optional(?)" : `optional(${recurse(inner)})`;
|
|
514
|
-
}
|
|
515
|
-
case "nullable": {
|
|
516
|
-
const inner = unwrapInner(schema);
|
|
517
|
-
return inner === void 0 ? "nullable(?)" : `nullable(${recurse(inner)})`;
|
|
518
|
-
}
|
|
519
|
-
case "default": {
|
|
520
|
-
const inner = unwrapInner(schema);
|
|
521
|
-
return `default[${stableValueRepr(getDefaultValue, schema)}](${inner === void 0 ? "?" : recurse(inner)})`;
|
|
522
|
-
}
|
|
523
|
-
case "readonly": {
|
|
524
|
-
const inner = unwrapInner(schema);
|
|
525
|
-
return inner === void 0 ? "readonly(?)" : `readonly(${recurse(inner)})`;
|
|
526
|
-
}
|
|
527
|
-
case "pipe": {
|
|
528
|
-
const inner = unwrapPipe(schema);
|
|
529
|
-
return inner === void 0 ? "pipe(?)" : `pipe(${recurse(inner)})`;
|
|
530
|
-
}
|
|
531
|
-
case "catch": {
|
|
532
|
-
const inner = unwrapInner(schema);
|
|
533
|
-
return `catch[${stableValueRepr(getCatchDefault, schema)}](${inner === void 0 ? "?" : recurse(inner)})`;
|
|
534
|
-
}
|
|
535
|
-
case "lazy": {
|
|
536
|
-
const inner = unwrapLazy(schema);
|
|
537
|
-
return inner === void 0 ? "lazy(?)" : `lazy(${recurse(inner)})`;
|
|
538
|
-
}
|
|
539
|
-
case "intersection": {
|
|
540
|
-
const left = getIntersectionLeft(schema);
|
|
541
|
-
const right = getIntersectionRight(schema);
|
|
542
|
-
const leftFp = left === void 0 ? "?" : recurse(left);
|
|
543
|
-
const rightFp = right === void 0 ? "?" : recurse(right);
|
|
544
|
-
const parts = [leftFp, rightFp].sort();
|
|
545
|
-
return `intersection(${parts.join("&")})`;
|
|
546
|
-
}
|
|
547
|
-
// Structural shape isn't observable for these. Bucket them into
|
|
548
|
-
// kind-only fingerprints — a schema-mismatch warning can't do
|
|
549
|
-
// better than "both are `custom`" here, but that still catches
|
|
550
|
-
// `object` vs `custom` mismatches.
|
|
551
|
-
case "promise":
|
|
552
|
-
case "custom":
|
|
553
|
-
case "template-literal":
|
|
554
|
-
return `${kind}:*`;
|
|
555
|
-
default: {
|
|
556
|
-
const _ = kind;
|
|
557
|
-
return `unknown:${String(_)}`;
|
|
558
|
-
}
|
|
559
|
-
}
|
|
560
|
-
}
|
|
561
|
-
function stableValueRepr(get, schema) {
|
|
562
|
-
const first = get(schema);
|
|
563
|
-
const second = get(schema);
|
|
564
|
-
if (!Object.is(first, second) || typeof first === "function") return "fn:*";
|
|
565
|
-
return canonicalStringify(first);
|
|
566
|
-
}
|
|
567
|
-
function formatChecks(schema) {
|
|
568
|
-
const checks = getChecks(schema);
|
|
569
|
-
if (checks.length === 0) return "";
|
|
570
|
-
const parts = checks.map((c) => serializeCheck(c)).sort();
|
|
571
|
-
return `[${parts.join(";")}]`;
|
|
572
|
-
}
|
|
573
|
-
function serializeCheck(check) {
|
|
574
|
-
if (check !== null && typeof check === "object") {
|
|
575
|
-
const def = check._zod?.def;
|
|
576
|
-
if (def !== void 0) return canonicalStringify(def);
|
|
577
|
-
}
|
|
578
|
-
return canonicalStringify(check);
|
|
579
|
-
}
|
|
580
|
-
function canonicalStringify(value, seen = /* @__PURE__ */ new WeakSet()) {
|
|
581
|
-
if (value === null) return "null";
|
|
582
|
-
if (value === void 0) return "undefined";
|
|
583
|
-
const t = typeof value;
|
|
584
|
-
if (t === "string") return JSON.stringify(value);
|
|
585
|
-
if (t === "number" || t === "boolean") return String(value);
|
|
586
|
-
if (t === "bigint") return `${String(value)}n`;
|
|
587
|
-
if (t === "function") return "fn:*";
|
|
588
|
-
if (t === "symbol") return "symbol:*";
|
|
589
|
-
if (Array.isArray(value)) {
|
|
590
|
-
if (seen.has(value)) return "<cyclic>";
|
|
591
|
-
seen.add(value);
|
|
592
|
-
try {
|
|
593
|
-
const parts = value.map((v) => canonicalStringify(v, seen));
|
|
594
|
-
return `[${parts.join(",")}]`;
|
|
595
|
-
} finally {
|
|
596
|
-
seen.delete(value);
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
if (t === "object") {
|
|
600
|
-
const obj = value;
|
|
601
|
-
if (seen.has(obj)) return "<cyclic>";
|
|
602
|
-
seen.add(obj);
|
|
603
|
-
try {
|
|
604
|
-
if (value instanceof Date) return `date:${value.getTime()}`;
|
|
605
|
-
if (value instanceof RegExp) return `regex:${String(value)}`;
|
|
606
|
-
const entries = Object.entries(obj).sort(([a], [b]) => a < b ? -1 : a > b ? 1 : 0).map(([k, v]) => `${JSON.stringify(k)}:${canonicalStringify(v, seen)}`);
|
|
607
|
-
return `{${entries.join(",")}}`;
|
|
608
|
-
} finally {
|
|
609
|
-
seen.delete(obj);
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
return "unknown";
|
|
613
|
-
}
|
|
614
|
-
function compare(a, b) {
|
|
615
|
-
const as = String(a);
|
|
616
|
-
const bs = String(b);
|
|
617
|
-
return as < bs ? -1 : as > bs ? 1 : 0;
|
|
618
|
-
}
|
|
619
|
-
|
|
620
|
-
const PERMISSIVE = /* @__PURE__ */ new Set([
|
|
621
|
-
"string",
|
|
622
|
-
"number",
|
|
623
|
-
"boolean",
|
|
624
|
-
"bigint",
|
|
625
|
-
"date",
|
|
626
|
-
"null",
|
|
627
|
-
"undefined",
|
|
628
|
-
"object",
|
|
629
|
-
"array",
|
|
630
|
-
"symbol",
|
|
631
|
-
"function",
|
|
632
|
-
"map",
|
|
633
|
-
"set"
|
|
634
|
-
]);
|
|
635
|
-
const MAX_LAZY_DEPTH = 64;
|
|
636
|
-
function slimPrimitivesOf(schema) {
|
|
637
|
-
return walk(schema, 0);
|
|
638
|
-
}
|
|
639
|
-
function walk(schema, depth) {
|
|
640
|
-
if (depth > MAX_LAZY_DEPTH) return new Set(PERMISSIVE);
|
|
641
|
-
const kind = kindOf(schema);
|
|
642
|
-
switch (kind) {
|
|
643
|
-
case "string":
|
|
644
|
-
return /* @__PURE__ */ new Set(["string"]);
|
|
645
|
-
case "number":
|
|
646
|
-
case "nan":
|
|
647
|
-
return /* @__PURE__ */ new Set(["number"]);
|
|
648
|
-
case "boolean":
|
|
649
|
-
return /* @__PURE__ */ new Set(["boolean"]);
|
|
650
|
-
case "bigint":
|
|
651
|
-
return /* @__PURE__ */ new Set(["bigint"]);
|
|
652
|
-
case "date":
|
|
653
|
-
return /* @__PURE__ */ new Set(["date"]);
|
|
654
|
-
case "null":
|
|
655
|
-
return /* @__PURE__ */ new Set(["null"]);
|
|
656
|
-
case "undefined":
|
|
657
|
-
case "void":
|
|
658
|
-
return /* @__PURE__ */ new Set(["undefined"]);
|
|
659
|
-
case "enum": {
|
|
660
|
-
const values = getEnumValues(schema);
|
|
661
|
-
const out = /* @__PURE__ */ new Set();
|
|
662
|
-
for (const v of values) {
|
|
663
|
-
if (typeof v === "string") out.add("string");
|
|
664
|
-
else if (typeof v === "number") out.add("number");
|
|
665
|
-
}
|
|
666
|
-
return out.size === 0 ? /* @__PURE__ */ new Set(["string"]) : out;
|
|
667
|
-
}
|
|
668
|
-
case "literal": {
|
|
669
|
-
const values = getLiteralValues(schema);
|
|
670
|
-
const out = /* @__PURE__ */ new Set();
|
|
671
|
-
for (const v of values) out.add(slimKindOfRaw(v));
|
|
672
|
-
return out.size === 0 ? new Set(PERMISSIVE) : out;
|
|
673
|
-
}
|
|
674
|
-
case "object":
|
|
675
|
-
case "record":
|
|
676
|
-
return /* @__PURE__ */ new Set(["object"]);
|
|
677
|
-
case "array":
|
|
678
|
-
case "tuple":
|
|
679
|
-
return /* @__PURE__ */ new Set(["array"]);
|
|
680
|
-
case "set":
|
|
681
|
-
return /* @__PURE__ */ new Set(["set"]);
|
|
682
|
-
case "optional": {
|
|
683
|
-
const inner = unwrapInner(schema);
|
|
684
|
-
const innerSet = inner === void 0 ? /* @__PURE__ */ new Set() : walk(inner, depth + 1);
|
|
685
|
-
innerSet.add("undefined");
|
|
686
|
-
return innerSet;
|
|
687
|
-
}
|
|
688
|
-
case "nullable": {
|
|
689
|
-
const inner = unwrapInner(schema);
|
|
690
|
-
const innerSet = inner === void 0 ? /* @__PURE__ */ new Set() : walk(inner, depth + 1);
|
|
691
|
-
innerSet.add("null");
|
|
692
|
-
return innerSet;
|
|
693
|
-
}
|
|
694
|
-
case "default":
|
|
695
|
-
case "readonly":
|
|
696
|
-
case "catch": {
|
|
697
|
-
const inner = unwrapInner(schema);
|
|
698
|
-
return inner === void 0 ? new Set(PERMISSIVE) : walk(inner, depth + 1);
|
|
699
|
-
}
|
|
700
|
-
case "pipe": {
|
|
701
|
-
const inner = unwrapPipe(schema);
|
|
702
|
-
return inner === void 0 ? new Set(PERMISSIVE) : walk(inner, depth + 1);
|
|
703
|
-
}
|
|
704
|
-
case "lazy": {
|
|
705
|
-
const inner = unwrapLazy(schema);
|
|
706
|
-
return inner === void 0 ? new Set(PERMISSIVE) : walk(inner, depth + 1);
|
|
707
|
-
}
|
|
708
|
-
case "union":
|
|
709
|
-
case "discriminated-union": {
|
|
710
|
-
const options = getUnionOptions(schema);
|
|
711
|
-
const out = /* @__PURE__ */ new Set();
|
|
712
|
-
for (const opt of options) {
|
|
713
|
-
for (const k of walk(opt, depth + 1)) out.add(k);
|
|
714
|
-
}
|
|
715
|
-
return out.size === 0 ? new Set(PERMISSIVE) : out;
|
|
716
|
-
}
|
|
717
|
-
case "intersection": {
|
|
718
|
-
const left = getIntersectionLeft(schema);
|
|
719
|
-
const right = getIntersectionRight(schema);
|
|
720
|
-
const leftSet = left === void 0 ? new Set(PERMISSIVE) : walk(left, depth + 1);
|
|
721
|
-
const rightSet = right === void 0 ? new Set(PERMISSIVE) : walk(right, depth + 1);
|
|
722
|
-
const out = /* @__PURE__ */ new Set();
|
|
723
|
-
for (const k of leftSet) if (rightSet.has(k)) out.add(k);
|
|
724
|
-
return out;
|
|
725
|
-
}
|
|
726
|
-
case "never":
|
|
727
|
-
return /* @__PURE__ */ new Set();
|
|
728
|
-
case "any":
|
|
729
|
-
case "unknown":
|
|
730
|
-
return new Set(PERMISSIVE);
|
|
731
|
-
// Kinds we don't understand at the slim level: be permissive to
|
|
732
|
-
// avoid false-rejecting legitimate writes against schema shapes
|
|
733
|
-
// we haven't characterised.
|
|
734
|
-
case "promise":
|
|
735
|
-
case "custom":
|
|
736
|
-
case "template-literal":
|
|
737
|
-
default:
|
|
738
|
-
return new Set(PERMISSIVE);
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
function slimKindOfRaw(value) {
|
|
742
|
-
if (value === null) return "null";
|
|
743
|
-
if (value === void 0) return "undefined";
|
|
744
|
-
if (Array.isArray(value)) return "array";
|
|
745
|
-
if (value instanceof Date) return "date";
|
|
746
|
-
const t = typeof value;
|
|
747
|
-
switch (t) {
|
|
748
|
-
case "string":
|
|
749
|
-
return "string";
|
|
750
|
-
case "number":
|
|
751
|
-
return "number";
|
|
752
|
-
case "boolean":
|
|
753
|
-
return "boolean";
|
|
754
|
-
case "bigint":
|
|
755
|
-
return "bigint";
|
|
756
|
-
case "symbol":
|
|
757
|
-
return "symbol";
|
|
758
|
-
case "undefined":
|
|
759
|
-
return "undefined";
|
|
760
|
-
case "object":
|
|
761
|
-
return "object";
|
|
762
|
-
case "function":
|
|
763
|
-
return "function";
|
|
764
|
-
default:
|
|
765
|
-
return "object";
|
|
766
|
-
}
|
|
767
|
-
}
|
|
768
|
-
|
|
769
|
-
const PATH_SEPARATOR$1 = ".";
|
|
770
|
-
function getNestedZodSchemasAtPath(schema, path) {
|
|
771
|
-
if (Array.isArray(path)) return walkSegments(schema, path.map(String));
|
|
772
|
-
const pathString = path;
|
|
773
|
-
if (pathString.length === 0) return [schema];
|
|
774
|
-
return walkSegments(schema, pathString.split(PATH_SEPARATOR$1));
|
|
775
|
-
}
|
|
776
|
-
function walkSegments(schema, segments) {
|
|
777
|
-
if (segments.length === 0) return [schema];
|
|
778
|
-
const [head, ...rest] = segments;
|
|
779
|
-
if (head === void 0) return [schema];
|
|
780
|
-
const kind = kindOf(schema);
|
|
781
|
-
switch (kind) {
|
|
782
|
-
case "object": {
|
|
783
|
-
const shape = getObjectShape(schema);
|
|
784
|
-
if (!Object.hasOwn(shape, head)) return [];
|
|
785
|
-
const next = shape[head];
|
|
786
|
-
return next === void 0 ? [] : walkSegments(next, rest);
|
|
787
|
-
}
|
|
788
|
-
case "array":
|
|
789
|
-
return walkSegments(getArrayElement(schema), rest);
|
|
790
|
-
case "set":
|
|
791
|
-
return walkSegments(getSetValueType(schema), rest);
|
|
792
|
-
case "record":
|
|
793
|
-
return walkSegments(getRecordValueType(schema), rest);
|
|
794
|
-
case "tuple": {
|
|
795
|
-
const index = Number(head);
|
|
796
|
-
if (!Number.isInteger(index)) return [];
|
|
797
|
-
const items = getTupleItems(schema);
|
|
798
|
-
const item = items[index];
|
|
799
|
-
return item === void 0 ? [] : walkSegments(item, rest);
|
|
800
|
-
}
|
|
801
|
-
case "union":
|
|
802
|
-
return getUnionOptions(schema).flatMap((opt) => walkSegments(opt, segments));
|
|
803
|
-
case "discriminated-union": {
|
|
804
|
-
const options = getDiscriminatedOptions(schema);
|
|
805
|
-
const matching = options.filter((opt) => {
|
|
806
|
-
const shape = getObjectShape(opt);
|
|
807
|
-
return Object.hasOwn(shape, head);
|
|
808
|
-
});
|
|
809
|
-
const candidates = matching.length > 0 ? matching : options;
|
|
810
|
-
return candidates.flatMap((opt) => walkSegments(opt, segments));
|
|
811
|
-
}
|
|
812
|
-
case "optional":
|
|
813
|
-
case "nullable":
|
|
814
|
-
case "default":
|
|
815
|
-
case "readonly":
|
|
816
|
-
case "catch": {
|
|
817
|
-
const inner = unwrapInner(schema);
|
|
818
|
-
return inner === void 0 ? [] : walkSegments(inner, segments);
|
|
819
|
-
}
|
|
820
|
-
case "pipe": {
|
|
821
|
-
const inner = unwrapPipe(schema);
|
|
822
|
-
return inner === void 0 ? [] : walkSegments(inner, segments);
|
|
823
|
-
}
|
|
824
|
-
case "lazy": {
|
|
825
|
-
const inner = unwrapLazy(schema);
|
|
826
|
-
return inner === void 0 ? [] : walkSegments(inner, segments);
|
|
827
|
-
}
|
|
828
|
-
case "intersection": {
|
|
829
|
-
const left = getIntersectionLeft(schema);
|
|
830
|
-
const right = getIntersectionRight(schema);
|
|
831
|
-
const leftResults = left === void 0 ? [] : walkSegments(left, segments);
|
|
832
|
-
const rightResults = right === void 0 ? [] : walkSegments(right, segments);
|
|
833
|
-
return [...leftResults, ...rightResults];
|
|
834
|
-
}
|
|
835
|
-
// Leaf types — can't descend further.
|
|
836
|
-
case "string":
|
|
837
|
-
case "number":
|
|
838
|
-
case "bigint":
|
|
839
|
-
case "boolean":
|
|
840
|
-
case "undefined":
|
|
841
|
-
case "null":
|
|
842
|
-
case "void":
|
|
843
|
-
case "never":
|
|
844
|
-
case "any":
|
|
845
|
-
case "unknown":
|
|
846
|
-
case "date":
|
|
847
|
-
case "enum":
|
|
848
|
-
case "literal":
|
|
849
|
-
case "nan":
|
|
850
|
-
case "promise":
|
|
851
|
-
case "custom":
|
|
852
|
-
case "template-literal":
|
|
853
|
-
return [];
|
|
854
|
-
default: {
|
|
855
|
-
const _exhaustive = kind;
|
|
856
|
-
throw new Error(`walkSegments: unhandled ZodKind '${_exhaustive}'`);
|
|
857
|
-
}
|
|
858
|
-
}
|
|
859
|
-
}
|
|
860
|
-
|
|
861
|
-
function carryChecks(rebuilt, original, stripConfig) {
|
|
862
|
-
if (stripConfig.stripRefinements === true) return rebuilt;
|
|
863
|
-
if (!hasChecks(original)) return rebuilt;
|
|
864
|
-
const all = getChecks(original);
|
|
865
|
-
const checks = stripConfig.shouldKeepCheck === void 0 ? all : all.filter(stripConfig.shouldKeepCheck);
|
|
866
|
-
if (checks.length === 0) return rebuilt;
|
|
867
|
-
return rebuilt.check(
|
|
868
|
-
...checks
|
|
869
|
-
);
|
|
870
|
-
}
|
|
871
|
-
function stripRefinements(schema) {
|
|
872
|
-
const kind = kindOf(schema);
|
|
873
|
-
switch (kind) {
|
|
874
|
-
case "string":
|
|
875
|
-
return hasChecks(schema) ? zod.z.string() : schema;
|
|
876
|
-
case "number":
|
|
877
|
-
return hasChecks(schema) ? zod.z.number() : schema;
|
|
878
|
-
case "bigint":
|
|
879
|
-
return hasChecks(schema) ? zod.z.bigint() : schema;
|
|
880
|
-
case "array": {
|
|
881
|
-
const element = getArrayElement(schema);
|
|
882
|
-
return zod.z.array(stripRefinements(element));
|
|
883
|
-
}
|
|
884
|
-
case "set": {
|
|
885
|
-
const valueType = getSetValueType(schema);
|
|
886
|
-
return zod.z.set(stripRefinements(valueType));
|
|
887
|
-
}
|
|
888
|
-
case "tuple": {
|
|
889
|
-
const items = getTupleItems(schema).map(stripRefinements);
|
|
890
|
-
return zod.z.tuple(items);
|
|
891
|
-
}
|
|
892
|
-
case "object": {
|
|
893
|
-
const shape = getObjectShape(schema);
|
|
894
|
-
const next = {};
|
|
895
|
-
for (const [k, v] of Object.entries(shape)) {
|
|
896
|
-
next[k] = stripRefinements(v);
|
|
897
|
-
}
|
|
898
|
-
return zod.z.object(next);
|
|
899
|
-
}
|
|
900
|
-
case "record": {
|
|
901
|
-
const keyType = getRecordKeyType(
|
|
902
|
-
schema
|
|
903
|
-
);
|
|
904
|
-
const valueType = stripRefinements(getRecordValueType(schema));
|
|
905
|
-
return zod.z.record(keyType, valueType);
|
|
906
|
-
}
|
|
907
|
-
case "union": {
|
|
908
|
-
const options = getUnionOptions(schema).map(stripRefinements);
|
|
909
|
-
return zod.z.union(options);
|
|
910
|
-
}
|
|
911
|
-
case "discriminated-union": {
|
|
912
|
-
const options = getDiscriminatedOptions(schema).map(
|
|
913
|
-
(opt) => stripRefinements(opt)
|
|
914
|
-
);
|
|
915
|
-
const discriminator = getDiscriminator(schema);
|
|
916
|
-
if (discriminator === void 0) return schema;
|
|
917
|
-
return zod.z.discriminatedUnion(
|
|
918
|
-
discriminator,
|
|
919
|
-
options
|
|
920
|
-
);
|
|
921
|
-
}
|
|
922
|
-
// Wrappers: strip the inner, preserve the wrapper semantics where
|
|
923
|
-
// possible. For stripping refinements we typically want wrappers to
|
|
924
|
-
// survive (optional/nullable/default), so leave them alone.
|
|
925
|
-
case "optional":
|
|
926
|
-
case "nullable":
|
|
927
|
-
case "default":
|
|
928
|
-
case "readonly":
|
|
929
|
-
case "pipe":
|
|
930
|
-
return schema;
|
|
931
|
-
case "lazy": {
|
|
932
|
-
const inner = unwrapLazy(schema);
|
|
933
|
-
if (inner === void 0) return schema;
|
|
934
|
-
const slimmedInner = stripRefinements(inner);
|
|
935
|
-
return zod.z.lazy(() => slimmedInner);
|
|
936
|
-
}
|
|
937
|
-
case "intersection": {
|
|
938
|
-
const left = getIntersectionLeft(schema);
|
|
939
|
-
const right = getIntersectionRight(schema);
|
|
940
|
-
if (left === void 0 || right === void 0) return schema;
|
|
941
|
-
return zod.z.intersection(stripRefinements(left), stripRefinements(right));
|
|
942
|
-
}
|
|
943
|
-
case "catch": {
|
|
944
|
-
const inner = unwrapInner(schema);
|
|
945
|
-
if (inner === void 0) return schema;
|
|
946
|
-
const slimmedInner = stripRefinements(inner);
|
|
947
|
-
return slimmedInner.catch(getCatchDefault(schema));
|
|
948
|
-
}
|
|
949
|
-
// Leaf types without refinements, or Zod features we don't rewrite.
|
|
950
|
-
case "boolean":
|
|
951
|
-
case "date":
|
|
952
|
-
case "enum":
|
|
953
|
-
case "literal":
|
|
954
|
-
case "null":
|
|
955
|
-
case "undefined":
|
|
956
|
-
case "any":
|
|
957
|
-
case "unknown":
|
|
958
|
-
case "nan":
|
|
959
|
-
case "void":
|
|
960
|
-
case "never":
|
|
961
|
-
case "promise":
|
|
962
|
-
case "custom":
|
|
963
|
-
case "template-literal":
|
|
964
|
-
return schema;
|
|
965
|
-
default: {
|
|
966
|
-
const _exhaustive = kind;
|
|
967
|
-
throw new Error(`stripRefinements: unhandled ZodKind '${_exhaustive}'`);
|
|
968
|
-
}
|
|
969
|
-
}
|
|
970
|
-
}
|
|
971
|
-
function stripAsyncChecks(schema) {
|
|
972
|
-
const config = {
|
|
973
|
-
stripRefinements: false,
|
|
974
|
-
shouldKeepCheck: (c) => !isAsyncCheck(c)
|
|
975
|
-
};
|
|
976
|
-
const seen = /* @__PURE__ */ new WeakSet();
|
|
977
|
-
function recurse(s) {
|
|
978
|
-
if (seen.has(s)) return s;
|
|
979
|
-
seen.add(s);
|
|
980
|
-
const kind = kindOf(s);
|
|
981
|
-
switch (kind) {
|
|
982
|
-
case "string":
|
|
983
|
-
return hasChecks(s) ? carryChecks(zod.z.string(), s, config) : s;
|
|
984
|
-
case "number":
|
|
985
|
-
return hasChecks(s) ? carryChecks(zod.z.number(), s, config) : s;
|
|
986
|
-
case "bigint":
|
|
987
|
-
return hasChecks(s) ? carryChecks(zod.z.bigint(), s, config) : s;
|
|
988
|
-
case "array": {
|
|
989
|
-
const element = getArrayElement(s);
|
|
990
|
-
return carryChecks(zod.z.array(recurse(element)), s, config);
|
|
991
|
-
}
|
|
992
|
-
case "set": {
|
|
993
|
-
const valueType = getSetValueType(s);
|
|
994
|
-
return carryChecks(zod.z.set(recurse(valueType)), s, config);
|
|
995
|
-
}
|
|
996
|
-
case "tuple": {
|
|
997
|
-
const items = getTupleItems(s).map(recurse);
|
|
998
|
-
const rebuilt = zod.z.tuple(
|
|
999
|
-
items
|
|
1000
|
-
);
|
|
1001
|
-
return carryChecks(rebuilt, s, config);
|
|
1002
|
-
}
|
|
1003
|
-
case "object": {
|
|
1004
|
-
const shape = getObjectShape(s);
|
|
1005
|
-
const next = {};
|
|
1006
|
-
for (const [k, v] of Object.entries(shape)) {
|
|
1007
|
-
next[k] = recurse(v);
|
|
1008
|
-
}
|
|
1009
|
-
return carryChecks(zod.z.object(next), s, config);
|
|
1010
|
-
}
|
|
1011
|
-
case "record": {
|
|
1012
|
-
const keyType = getRecordKeyType(s);
|
|
1013
|
-
const valueType = recurse(getRecordValueType(s));
|
|
1014
|
-
const rebuilt = zod.z.record(keyType, valueType);
|
|
1015
|
-
return carryChecks(rebuilt, s, config);
|
|
1016
|
-
}
|
|
1017
|
-
case "union": {
|
|
1018
|
-
const options = getUnionOptions(s).map(recurse);
|
|
1019
|
-
const rebuilt = zod.z.union(
|
|
1020
|
-
options
|
|
1021
|
-
);
|
|
1022
|
-
return carryChecks(rebuilt, s, config);
|
|
1023
|
-
}
|
|
1024
|
-
case "discriminated-union": {
|
|
1025
|
-
const options = getDiscriminatedOptions(s).map((opt) => recurse(opt));
|
|
1026
|
-
const discriminator = getDiscriminator(s);
|
|
1027
|
-
if (discriminator === void 0) return s;
|
|
1028
|
-
return zod.z.discriminatedUnion(
|
|
1029
|
-
discriminator,
|
|
1030
|
-
options
|
|
1031
|
-
);
|
|
1032
|
-
}
|
|
1033
|
-
case "optional": {
|
|
1034
|
-
const inner = unwrapInner(s);
|
|
1035
|
-
if (inner === void 0) return s;
|
|
1036
|
-
return recurse(inner).optional();
|
|
1037
|
-
}
|
|
1038
|
-
case "nullable": {
|
|
1039
|
-
const inner = unwrapInner(s);
|
|
1040
|
-
if (inner === void 0) return s;
|
|
1041
|
-
return recurse(inner).nullable();
|
|
1042
|
-
}
|
|
1043
|
-
case "default": {
|
|
1044
|
-
const inner = unwrapInner(s);
|
|
1045
|
-
if (inner === void 0) return s;
|
|
1046
|
-
return recurse(inner).default(getDefaultValue(s));
|
|
1047
|
-
}
|
|
1048
|
-
case "readonly": {
|
|
1049
|
-
const inner = unwrapInner(s);
|
|
1050
|
-
if (inner === void 0) return s;
|
|
1051
|
-
return recurse(inner).readonly();
|
|
1052
|
-
}
|
|
1053
|
-
case "pipe":
|
|
1054
|
-
return s;
|
|
1055
|
-
case "lazy": {
|
|
1056
|
-
const inner = unwrapLazy(s);
|
|
1057
|
-
if (inner === void 0) return s;
|
|
1058
|
-
const stripped = recurse(inner);
|
|
1059
|
-
return zod.z.lazy(() => stripped);
|
|
1060
|
-
}
|
|
1061
|
-
case "intersection": {
|
|
1062
|
-
const left = getIntersectionLeft(s);
|
|
1063
|
-
const right = getIntersectionRight(s);
|
|
1064
|
-
if (left === void 0 || right === void 0) return s;
|
|
1065
|
-
return zod.z.intersection(recurse(left), recurse(right));
|
|
1066
|
-
}
|
|
1067
|
-
case "catch": {
|
|
1068
|
-
const inner = unwrapInner(s);
|
|
1069
|
-
if (inner === void 0) return s;
|
|
1070
|
-
return recurse(inner).catch(getCatchDefault(s));
|
|
1071
|
-
}
|
|
1072
|
-
// Leaves with no checks — pass through unchanged.
|
|
1073
|
-
case "boolean":
|
|
1074
|
-
case "date":
|
|
1075
|
-
case "enum":
|
|
1076
|
-
case "literal":
|
|
1077
|
-
case "null":
|
|
1078
|
-
case "undefined":
|
|
1079
|
-
case "any":
|
|
1080
|
-
case "unknown":
|
|
1081
|
-
case "nan":
|
|
1082
|
-
case "void":
|
|
1083
|
-
case "never":
|
|
1084
|
-
case "promise":
|
|
1085
|
-
case "custom":
|
|
1086
|
-
case "template-literal":
|
|
1087
|
-
return s;
|
|
1088
|
-
default: {
|
|
1089
|
-
const _exhaustive = kind;
|
|
1090
|
-
throw new Error(`stripAsyncChecks: unhandled ZodKind '${_exhaustive}'`);
|
|
1091
|
-
}
|
|
1092
|
-
}
|
|
1093
|
-
}
|
|
1094
|
-
return recurse(schema);
|
|
1095
|
-
}
|
|
1096
|
-
function getSlimSchema(schema, stripConfig) {
|
|
1097
|
-
const kind = kindOf(schema);
|
|
1098
|
-
switch (kind) {
|
|
1099
|
-
case "optional": {
|
|
1100
|
-
const inner = unwrapInner(schema) ?? schema;
|
|
1101
|
-
const slimmedInner = getSlimSchema(inner, stripConfig);
|
|
1102
|
-
return stripConfig.stripOptional === true ? slimmedInner : slimmedInner.optional();
|
|
1103
|
-
}
|
|
1104
|
-
case "nullable": {
|
|
1105
|
-
const inner = unwrapInner(schema) ?? schema;
|
|
1106
|
-
const slimmedInner = getSlimSchema(inner, stripConfig);
|
|
1107
|
-
return stripConfig.stripNullable === true ? slimmedInner : slimmedInner.nullable();
|
|
1108
|
-
}
|
|
1109
|
-
case "default": {
|
|
1110
|
-
const inner = unwrapInner(schema) ?? schema;
|
|
1111
|
-
const slimmedInner = getSlimSchema(inner, stripConfig);
|
|
1112
|
-
if (stripConfig.stripDefaultValues === true) return slimmedInner;
|
|
1113
|
-
const defaultValue = getDefaultValue(schema);
|
|
1114
|
-
return slimmedInner.default(defaultValue);
|
|
1115
|
-
}
|
|
1116
|
-
case "readonly": {
|
|
1117
|
-
const inner = unwrapInner(schema);
|
|
1118
|
-
return inner === void 0 ? schema : getSlimSchema(inner, stripConfig).readonly();
|
|
1119
|
-
}
|
|
1120
|
-
case "pipe": {
|
|
1121
|
-
if (stripConfig.stripPipe === true) {
|
|
1122
|
-
const inner = unwrapPipe(schema) ?? schema;
|
|
1123
|
-
return getSlimSchema(inner, stripConfig);
|
|
1124
|
-
}
|
|
1125
|
-
return schema;
|
|
1126
|
-
}
|
|
1127
|
-
case "object": {
|
|
1128
|
-
const shape = getObjectShape(schema);
|
|
1129
|
-
const next = {};
|
|
1130
|
-
for (const [k, v] of Object.entries(shape)) {
|
|
1131
|
-
next[k] = getSlimSchema(v, stripConfig);
|
|
1132
|
-
}
|
|
1133
|
-
return carryChecks(zod.z.object(next), schema, stripConfig);
|
|
1134
|
-
}
|
|
1135
|
-
case "array": {
|
|
1136
|
-
const element = getArrayElement(schema);
|
|
1137
|
-
return carryChecks(zod.z.array(getSlimSchema(element, stripConfig)), schema, stripConfig);
|
|
1138
|
-
}
|
|
1139
|
-
case "set": {
|
|
1140
|
-
const valueType = getSetValueType(schema);
|
|
1141
|
-
return carryChecks(zod.z.set(getSlimSchema(valueType, stripConfig)), schema, stripConfig);
|
|
1142
|
-
}
|
|
1143
|
-
case "tuple": {
|
|
1144
|
-
const items = getTupleItems(schema).map((it) => getSlimSchema(it, stripConfig));
|
|
1145
|
-
const rebuilt = zod.z.tuple(
|
|
1146
|
-
items
|
|
1147
|
-
);
|
|
1148
|
-
return carryChecks(rebuilt, schema, stripConfig);
|
|
1149
|
-
}
|
|
1150
|
-
case "record": {
|
|
1151
|
-
const keyType = getRecordKeyType(schema);
|
|
1152
|
-
const valueType = getSlimSchema(getRecordValueType(schema), stripConfig);
|
|
1153
|
-
const rebuilt = zod.z.record(keyType, valueType);
|
|
1154
|
-
return carryChecks(rebuilt, schema, stripConfig);
|
|
1155
|
-
}
|
|
1156
|
-
case "union": {
|
|
1157
|
-
const options = getUnionOptions(schema).map((opt) => getSlimSchema(opt, stripConfig));
|
|
1158
|
-
const rebuilt = zod.z.union(options);
|
|
1159
|
-
return carryChecks(rebuilt, schema, stripConfig);
|
|
1160
|
-
}
|
|
1161
|
-
case "discriminated-union": {
|
|
1162
|
-
const options = getDiscriminatedOptions(schema).map(
|
|
1163
|
-
(opt) => getSlimSchema(opt, stripConfig)
|
|
1164
|
-
);
|
|
1165
|
-
const discriminator = getDiscriminator(schema);
|
|
1166
|
-
if (discriminator === void 0) return schema;
|
|
1167
|
-
return zod.z.discriminatedUnion(
|
|
1168
|
-
discriminator,
|
|
1169
|
-
options
|
|
1170
|
-
);
|
|
1171
|
-
}
|
|
1172
|
-
// Leaves: strip refinements if requested, otherwise pass through.
|
|
1173
|
-
case "string":
|
|
1174
|
-
case "number":
|
|
1175
|
-
case "bigint":
|
|
1176
|
-
return stripConfig.stripRefinements === true && hasChecks(schema) ? stripRefinements(schema) : schema;
|
|
1177
|
-
case "boolean":
|
|
1178
|
-
case "date":
|
|
1179
|
-
case "enum":
|
|
1180
|
-
case "literal":
|
|
1181
|
-
case "null":
|
|
1182
|
-
case "undefined":
|
|
1183
|
-
case "any":
|
|
1184
|
-
case "unknown":
|
|
1185
|
-
case "nan":
|
|
1186
|
-
case "void":
|
|
1187
|
-
case "never":
|
|
1188
|
-
case "promise":
|
|
1189
|
-
case "custom":
|
|
1190
|
-
case "template-literal":
|
|
1191
|
-
return schema;
|
|
1192
|
-
case "lazy": {
|
|
1193
|
-
const inner = unwrapLazy(schema);
|
|
1194
|
-
if (inner === void 0) return schema;
|
|
1195
|
-
const slimmedInner = getSlimSchema(inner, stripConfig);
|
|
1196
|
-
return zod.z.lazy(() => slimmedInner);
|
|
1197
|
-
}
|
|
1198
|
-
case "intersection": {
|
|
1199
|
-
const left = getIntersectionLeft(schema);
|
|
1200
|
-
const right = getIntersectionRight(schema);
|
|
1201
|
-
if (left === void 0 || right === void 0) return schema;
|
|
1202
|
-
return zod.z.intersection(getSlimSchema(left, stripConfig), getSlimSchema(right, stripConfig));
|
|
1203
|
-
}
|
|
1204
|
-
case "catch": {
|
|
1205
|
-
const inner = unwrapInner(schema);
|
|
1206
|
-
if (inner === void 0) return schema;
|
|
1207
|
-
const slimmedInner = getSlimSchema(inner, stripConfig);
|
|
1208
|
-
return slimmedInner.catch(getCatchDefault(schema));
|
|
1209
|
-
}
|
|
1210
|
-
default: {
|
|
1211
|
-
const _exhaustive = kind;
|
|
1212
|
-
throw new Error(`getSlimSchema: unhandled ZodKind '${_exhaustive}'`);
|
|
1213
|
-
}
|
|
1214
|
-
}
|
|
1215
|
-
}
|
|
1216
|
-
|
|
1217
|
-
function deriveDefault(schema, useDefault) {
|
|
1218
|
-
return defaultForKind(kindOf(schema), schema, useDefault);
|
|
1219
|
-
}
|
|
1220
|
-
function defaultForKind(kind, schema, useDefault) {
|
|
1221
|
-
switch (kind) {
|
|
1222
|
-
case "object": {
|
|
1223
|
-
const shape = getObjectShape(schema);
|
|
1224
|
-
const out = {};
|
|
1225
|
-
for (const [key, subSchema] of Object.entries(shape)) {
|
|
1226
|
-
out[key] = deriveDefault(subSchema, useDefault);
|
|
1227
|
-
}
|
|
1228
|
-
return out;
|
|
1229
|
-
}
|
|
1230
|
-
case "default": {
|
|
1231
|
-
if (useDefault) return getDefaultValue(schema);
|
|
1232
|
-
const inner = unwrapInner(schema);
|
|
1233
|
-
return inner === void 0 ? void 0 : deriveDefault(inner, useDefault);
|
|
1234
|
-
}
|
|
1235
|
-
case "optional":
|
|
1236
|
-
return void 0;
|
|
1237
|
-
case "nullable":
|
|
1238
|
-
return null;
|
|
1239
|
-
case "readonly": {
|
|
1240
|
-
const inner = unwrapInner(schema);
|
|
1241
|
-
return inner === void 0 ? void 0 : deriveDefault(inner, useDefault);
|
|
1242
|
-
}
|
|
1243
|
-
case "pipe": {
|
|
1244
|
-
const inner = unwrapPipe(schema);
|
|
1245
|
-
return inner === void 0 ? void 0 : deriveDefault(inner, useDefault);
|
|
1246
|
-
}
|
|
1247
|
-
case "array":
|
|
1248
|
-
return [];
|
|
1249
|
-
case "set":
|
|
1250
|
-
return /* @__PURE__ */ new Set();
|
|
1251
|
-
case "record":
|
|
1252
|
-
return {};
|
|
1253
|
-
case "tuple": {
|
|
1254
|
-
const items = getTupleItems(schema);
|
|
1255
|
-
return items.map((item) => deriveDefault(item, useDefault));
|
|
1256
|
-
}
|
|
1257
|
-
case "union": {
|
|
1258
|
-
const options = getUnionOptions(schema);
|
|
1259
|
-
const first = options[0];
|
|
1260
|
-
return first === void 0 ? void 0 : deriveDefault(first, useDefault);
|
|
1261
|
-
}
|
|
1262
|
-
case "discriminated-union": {
|
|
1263
|
-
const first = getDiscriminatedUnionFirstOption(schema);
|
|
1264
|
-
return first === void 0 ? void 0 : deriveDefault(first, useDefault);
|
|
1265
|
-
}
|
|
1266
|
-
case "string":
|
|
1267
|
-
return "";
|
|
1268
|
-
case "number":
|
|
1269
|
-
return 0;
|
|
1270
|
-
case "bigint":
|
|
1271
|
-
return 0n;
|
|
1272
|
-
case "boolean":
|
|
1273
|
-
return false;
|
|
1274
|
-
case "date":
|
|
1275
|
-
return /* @__PURE__ */ new Date(0);
|
|
1276
|
-
case "null":
|
|
1277
|
-
return null;
|
|
1278
|
-
case "undefined":
|
|
1279
|
-
return void 0;
|
|
1280
|
-
case "enum": {
|
|
1281
|
-
const values = getEnumValues(schema);
|
|
1282
|
-
return values[0];
|
|
1283
|
-
}
|
|
1284
|
-
case "literal": {
|
|
1285
|
-
const values = getLiteralValues(schema);
|
|
1286
|
-
return values[0];
|
|
1287
|
-
}
|
|
1288
|
-
case "nan":
|
|
1289
|
-
return NaN;
|
|
1290
|
-
case "lazy": {
|
|
1291
|
-
const inner = unwrapLazy(schema);
|
|
1292
|
-
return inner === void 0 ? void 0 : deriveDefault(inner, useDefault);
|
|
1293
|
-
}
|
|
1294
|
-
case "intersection": {
|
|
1295
|
-
const left = getIntersectionLeft(schema);
|
|
1296
|
-
const right = getIntersectionRight(schema);
|
|
1297
|
-
const l = left === void 0 ? void 0 : deriveDefault(left, useDefault);
|
|
1298
|
-
const r = right === void 0 ? void 0 : deriveDefault(right, useDefault);
|
|
1299
|
-
return mergeDeep(l, r);
|
|
1300
|
-
}
|
|
1301
|
-
case "catch": {
|
|
1302
|
-
if (useDefault) return getCatchDefault(schema);
|
|
1303
|
-
const inner = unwrapInner(schema);
|
|
1304
|
-
return inner === void 0 ? void 0 : deriveDefault(inner, useDefault);
|
|
1305
|
-
}
|
|
1306
|
-
case "any":
|
|
1307
|
-
case "unknown":
|
|
1308
|
-
case "void":
|
|
1309
|
-
case "never":
|
|
1310
|
-
case "promise":
|
|
1311
|
-
case "custom":
|
|
1312
|
-
case "template-literal":
|
|
1313
|
-
return void 0;
|
|
1314
|
-
default: {
|
|
1315
|
-
const _exhaustive = kind;
|
|
1316
|
-
throw new Error(`deriveDefault: unhandled ZodKind '${_exhaustive}'`);
|
|
1317
|
-
}
|
|
1318
|
-
}
|
|
1319
|
-
}
|
|
1320
|
-
function mergeDeep(base, override) {
|
|
1321
|
-
if (override === void 0) return base;
|
|
1322
|
-
if (!useFormContext.isPlainRecord(override)) return override;
|
|
1323
|
-
if (!useFormContext.isPlainRecord(base)) return override;
|
|
1324
|
-
const result = { ...base };
|
|
1325
|
-
for (const key of Object.keys(override)) {
|
|
1326
|
-
const oVal = override[key];
|
|
1327
|
-
const bVal = base[key];
|
|
1328
|
-
if (useFormContext.isPlainRecord(oVal) && useFormContext.isPlainRecord(bVal)) {
|
|
1329
|
-
result[key] = mergeDeep(bVal, oVal);
|
|
1330
|
-
} else if (oVal !== void 0) {
|
|
1331
|
-
result[key] = oVal;
|
|
1332
|
-
}
|
|
1333
|
-
}
|
|
1334
|
-
return result;
|
|
1335
|
-
}
|
|
1336
|
-
function getDefaultValuesFromZodSchema(opts) {
|
|
1337
|
-
const { schema, useDefaultSchemaValues, constraints } = opts;
|
|
1338
|
-
const initial = deriveDefault(schema, useDefaultSchemaValues);
|
|
1339
|
-
const merged = mergeDeep(initial, constraints);
|
|
1340
|
-
const slimSchema = getSlimSchema(schema, {
|
|
1341
|
-
stripDefaultValues: true,
|
|
1342
|
-
stripPipe: true,
|
|
1343
|
-
stripRefinements: true
|
|
1344
|
-
});
|
|
1345
|
-
const firstParse = slimSchema.safeParse(merged);
|
|
1346
|
-
if (firstParse.success) {
|
|
1347
|
-
return { data: firstParse.data, success: true, slimSchema };
|
|
1348
|
-
}
|
|
1349
|
-
let fixedData = merged;
|
|
1350
|
-
for (const issue of firstParse.error.issues) {
|
|
1351
|
-
const pathSegments = issue.path.map((seg) => typeof seg === "number" ? seg : String(seg));
|
|
1352
|
-
const candidates = getNestedZodSchemasAtPath(slimSchema, pathSegments);
|
|
1353
|
-
if (candidates.length === 0) continue;
|
|
1354
|
-
const candidate = candidates[0];
|
|
1355
|
-
if (candidate === void 0) continue;
|
|
1356
|
-
const valueAtPath = useFormContext.getAtPath(merged, pathSegments);
|
|
1357
|
-
const slimKinds = slimPrimitivesOf(candidate);
|
|
1358
|
-
if (slimKinds.size > 0 && slimKinds.has(useFormContext.slimKindOf(valueAtPath))) {
|
|
1359
|
-
continue;
|
|
1360
|
-
}
|
|
1361
|
-
const fixValue = defaultFromIssue(issue, candidate, useDefaultSchemaValues);
|
|
1362
|
-
if (fixValue === SKIP) continue;
|
|
1363
|
-
fixedData = pathSegments.length === 0 ? fixValue : useFormContext.setAtPath(fixedData, pathSegments, fixValue);
|
|
1364
|
-
}
|
|
1365
|
-
const secondParse = slimSchema.safeParse(fixedData);
|
|
1366
|
-
if (secondParse.success) {
|
|
1367
|
-
return { data: secondParse.data, success: true, slimSchema };
|
|
1368
|
-
}
|
|
1369
|
-
return { data: fixedData, success: false, slimSchema };
|
|
1370
|
-
}
|
|
1371
|
-
const SKIP = Symbol("atta:skip-fix");
|
|
1372
|
-
function defaultFromIssue(issue, candidate, useDefaultSchemaValues) {
|
|
1373
|
-
if (issue.code === "invalid_type") {
|
|
1374
|
-
const du = unwrapToDiscriminatedUnion(candidate);
|
|
1375
|
-
if (du !== void 0) {
|
|
1376
|
-
const first = getDiscriminatedUnionFirstOption(du);
|
|
1377
|
-
if (first !== void 0) return deriveDefault(first, useDefaultSchemaValues);
|
|
1378
|
-
}
|
|
1379
|
-
return deriveDefault(candidate, useDefaultSchemaValues);
|
|
1380
|
-
}
|
|
1381
|
-
if (issue.code === "invalid_value") {
|
|
1382
|
-
const values = issue.values;
|
|
1383
|
-
if (values !== void 0 && values.length > 0) return values[0];
|
|
1384
|
-
return deriveDefault(candidate, useDefaultSchemaValues);
|
|
1385
|
-
}
|
|
1386
|
-
return deriveDefault(candidate, useDefaultSchemaValues);
|
|
1387
|
-
}
|
|
1388
|
-
|
|
1389
|
-
const PATH_SEPARATOR = ".";
|
|
1390
|
-
function unwrapStructuralWrappers(schema) {
|
|
1391
|
-
let current = schema;
|
|
1392
|
-
for (let i = 0; i < 64; i++) {
|
|
1393
|
-
const outerKind = kindOf(current);
|
|
1394
|
-
if (outerKind !== "optional" && outerKind !== "nullable") break;
|
|
1395
|
-
const inner = unwrapInner(current);
|
|
1396
|
-
if (inner === void 0) return current;
|
|
1397
|
-
if (!isStructuralKind(kindOf(inner))) break;
|
|
1398
|
-
current = inner;
|
|
1399
|
-
}
|
|
1400
|
-
return current;
|
|
1401
|
-
}
|
|
1402
|
-
function peelAllWrappers(schema) {
|
|
1403
|
-
let current = schema;
|
|
1404
|
-
for (let i = 0; i < 64; i++) {
|
|
1405
|
-
const k = kindOf(current);
|
|
1406
|
-
let inner;
|
|
1407
|
-
if (k === "optional" || k === "nullable" || k === "default" || k === "readonly" || k === "catch") {
|
|
1408
|
-
inner = unwrapInner(current);
|
|
1409
|
-
} else if (k === "pipe") {
|
|
1410
|
-
inner = unwrapPipe(current);
|
|
1411
|
-
} else if (k === "lazy") {
|
|
1412
|
-
inner = unwrapLazy(current);
|
|
1413
|
-
} else {
|
|
1414
|
-
return current;
|
|
1415
|
-
}
|
|
1416
|
-
if (inner === void 0) return current;
|
|
1417
|
-
current = inner;
|
|
1418
|
-
}
|
|
1419
|
-
return current;
|
|
1420
|
-
}
|
|
1421
|
-
const STRUCTURAL_KINDS = /* @__PURE__ */ new Set([
|
|
1422
|
-
"object",
|
|
1423
|
-
"array",
|
|
1424
|
-
"tuple",
|
|
1425
|
-
"record",
|
|
1426
|
-
"discriminated-union",
|
|
1427
|
-
"union",
|
|
1428
|
-
"intersection",
|
|
1429
|
-
"optional",
|
|
1430
|
-
"nullable",
|
|
1431
|
-
"default",
|
|
1432
|
-
"readonly",
|
|
1433
|
-
"catch",
|
|
1434
|
-
"pipe",
|
|
1435
|
-
"lazy"
|
|
1436
|
-
]);
|
|
1437
|
-
function isStructuralKind(kind) {
|
|
1438
|
-
return STRUCTURAL_KINDS.has(kind);
|
|
1439
|
-
}
|
|
1440
|
-
const MAX_REQUIRED_DEPTH = 64;
|
|
1441
|
-
function isLeafRequired(schema, depth = 0) {
|
|
1442
|
-
if (depth > MAX_REQUIRED_DEPTH) return true;
|
|
1443
|
-
const kind = kindOf(schema);
|
|
1444
|
-
if (kind === "optional" || kind === "nullable" || kind === "default" || kind === "catch" || kind === "undefined" || kind === "null" || kind === "void") {
|
|
1445
|
-
return false;
|
|
1446
|
-
}
|
|
1447
|
-
if (kind === "readonly") {
|
|
1448
|
-
const inner = unwrapInner(schema);
|
|
1449
|
-
return inner === void 0 ? true : isLeafRequired(inner, depth + 1);
|
|
1450
|
-
}
|
|
1451
|
-
if (kind === "pipe") {
|
|
1452
|
-
const inner = unwrapPipe(schema);
|
|
1453
|
-
return inner === void 0 ? true : isLeafRequired(inner, depth + 1);
|
|
1454
|
-
}
|
|
1455
|
-
if (kind === "lazy") {
|
|
1456
|
-
const inner = unwrapLazy(schema);
|
|
1457
|
-
return inner === void 0 ? true : isLeafRequired(inner, depth + 1);
|
|
1458
|
-
}
|
|
1459
|
-
if (kind === "union" || kind === "discriminated-union") {
|
|
1460
|
-
const options = kind === "discriminated-union" ? getDiscriminatedOptions(schema) : getUnionOptions(schema);
|
|
1461
|
-
if (options.length === 0) return true;
|
|
1462
|
-
return options.every((opt) => isLeafRequired(opt, depth + 1));
|
|
1463
|
-
}
|
|
1464
|
-
if (kind === "intersection") {
|
|
1465
|
-
const left = getIntersectionLeft(schema);
|
|
1466
|
-
const right = getIntersectionRight(schema);
|
|
1467
|
-
const leftReq = left === void 0 ? true : isLeafRequired(left, depth + 1);
|
|
1468
|
-
const rightReq = right === void 0 ? true : isLeafRequired(right, depth + 1);
|
|
1469
|
-
return leftReq || rightReq;
|
|
1470
|
-
}
|
|
1471
|
-
return true;
|
|
1472
|
-
}
|
|
1473
|
-
function zodV4Adapter(rootSchema) {
|
|
1474
|
-
assertZodVersion(rootSchema);
|
|
1475
|
-
assertSupportedKinds(rootSchema);
|
|
1476
|
-
return (formKey) => {
|
|
1477
|
-
const leafCache = /* @__PURE__ */ new Map();
|
|
1478
|
-
let asyncValidationFlag = null;
|
|
1479
|
-
return {
|
|
1480
|
-
fingerprint: () => fingerprintZodSchema(rootSchema),
|
|
1481
|
-
needsAsyncValidation() {
|
|
1482
|
-
asyncValidationFlag ?? (asyncValidationFlag = containsAsyncRefine(rootSchema));
|
|
1483
|
-
return asyncValidationFlag;
|
|
1484
|
-
},
|
|
1485
|
-
getDefaultValues(config) {
|
|
1486
|
-
const { data } = getDefaultValuesFromZodSchema({
|
|
1487
|
-
schema: rootSchema,
|
|
1488
|
-
useDefaultSchemaValues: config.useDefaultSchemaValues,
|
|
1489
|
-
constraints: config.constraints
|
|
1490
|
-
});
|
|
1491
|
-
if (config.strict !== false) {
|
|
1492
|
-
try {
|
|
1493
|
-
const strictResult = rootSchema.safeParse(data);
|
|
1494
|
-
if (strictResult.success) {
|
|
1495
|
-
return { data: strictResult.data, errors: void 0, success: true, formKey };
|
|
1496
|
-
}
|
|
1497
|
-
return {
|
|
1498
|
-
data,
|
|
1499
|
-
errors: zodIssuesToValidationErrors(strictResult.error.issues, formKey),
|
|
1500
|
-
success: false,
|
|
1501
|
-
formKey
|
|
1502
|
-
};
|
|
1503
|
-
} catch {
|
|
1504
|
-
try {
|
|
1505
|
-
const syncOnly = stripAsyncChecks(rootSchema);
|
|
1506
|
-
const syncResult = syncOnly.safeParse(data);
|
|
1507
|
-
if (syncResult.success) {
|
|
1508
|
-
return { data, errors: void 0, success: true, formKey };
|
|
1509
|
-
}
|
|
1510
|
-
return {
|
|
1511
|
-
data,
|
|
1512
|
-
errors: zodIssuesToValidationErrors(syncResult.error.issues, formKey),
|
|
1513
|
-
success: false,
|
|
1514
|
-
formKey
|
|
1515
|
-
};
|
|
1516
|
-
} catch {
|
|
1517
|
-
return { data, errors: void 0, success: true, formKey };
|
|
1518
|
-
}
|
|
1519
|
-
}
|
|
1520
|
-
}
|
|
1521
|
-
return { data, errors: void 0, success: true, formKey };
|
|
1522
|
-
},
|
|
1523
|
-
getDefaultAtPath(path) {
|
|
1524
|
-
if (path.length === 0) return deriveDefault(rootSchema, true);
|
|
1525
|
-
const [first] = getNestedZodSchemasAtPath(rootSchema, path);
|
|
1526
|
-
if (first === void 0) return void 0;
|
|
1527
|
-
return deriveDefault(unwrapStructuralWrappers(first), true);
|
|
1528
|
-
},
|
|
1529
|
-
arrayShapeAtPath(path) {
|
|
1530
|
-
if (path.length === 0) return void 0;
|
|
1531
|
-
const [first] = getNestedZodSchemasAtPath(rootSchema, path);
|
|
1532
|
-
if (first === void 0) return void 0;
|
|
1533
|
-
const peeled = peelAllWrappers(first);
|
|
1534
|
-
const kind = kindOf(peeled);
|
|
1535
|
-
if (kind === "tuple") return getTupleItems(peeled).length;
|
|
1536
|
-
if (kind === "array") return null;
|
|
1537
|
-
return void 0;
|
|
1538
|
-
},
|
|
1539
|
-
getSchemasAtPath(path) {
|
|
1540
|
-
const resolved = getNestedZodSchemasAtPath(rootSchema, path);
|
|
1541
|
-
return resolved.map(
|
|
1542
|
-
(schema) => ({
|
|
1543
|
-
fingerprint: () => fingerprintZodSchema(schema),
|
|
1544
|
-
needsAsyncValidation: () => containsAsyncRefine(schema),
|
|
1545
|
-
getDefaultValues: () => ({
|
|
1546
|
-
data: deriveDefault(schema, true),
|
|
1547
|
-
errors: void 0,
|
|
1548
|
-
success: true,
|
|
1549
|
-
formKey
|
|
1550
|
-
}),
|
|
1551
|
-
getSchemasAtPath: () => [],
|
|
1552
|
-
validateAtPath: async (data) => {
|
|
1553
|
-
const result = await schema.safeParseAsync(data);
|
|
1554
|
-
if (result.success) {
|
|
1555
|
-
return { data: result.data, errors: void 0, success: true, formKey };
|
|
1556
|
-
}
|
|
1557
|
-
return {
|
|
1558
|
-
data: void 0,
|
|
1559
|
-
errors: zodIssuesToValidationErrors(result.error.issues, formKey),
|
|
1560
|
-
success: false,
|
|
1561
|
-
formKey
|
|
1562
|
-
};
|
|
1563
|
-
}
|
|
1564
|
-
})
|
|
1565
|
-
);
|
|
1566
|
-
},
|
|
1567
|
-
getSlimPrimitiveTypesAtPath(path) {
|
|
1568
|
-
if (path.length === 0) return /* @__PURE__ */ new Set(["object"]);
|
|
1569
|
-
const resolved = getNestedZodSchemasAtPath(rootSchema, path);
|
|
1570
|
-
if (resolved.length === 0) return /* @__PURE__ */ new Set();
|
|
1571
|
-
const out = /* @__PURE__ */ new Set();
|
|
1572
|
-
for (const candidate of resolved) {
|
|
1573
|
-
for (const k of slimPrimitivesOf(candidate)) out.add(k);
|
|
1574
|
-
}
|
|
1575
|
-
return out;
|
|
1576
|
-
},
|
|
1577
|
-
isLeafAtPath(path) {
|
|
1578
|
-
const cacheKey = paths.canonicalizePath(path).key;
|
|
1579
|
-
const cached = leafCache.get(cacheKey);
|
|
1580
|
-
if (cached !== void 0) return cached;
|
|
1581
|
-
const prim = this.getSlimPrimitiveTypesAtPath(path);
|
|
1582
|
-
const isLeaf = prim.size > 0 && !prim.has("object") && !prim.has("array") && !prim.has("map") && !prim.has("set");
|
|
1583
|
-
leafCache.set(cacheKey, isLeaf);
|
|
1584
|
-
return isLeaf;
|
|
1585
|
-
},
|
|
1586
|
-
isRequiredAtPath(path) {
|
|
1587
|
-
if (path.length === 0) return true;
|
|
1588
|
-
const resolved = getNestedZodSchemasAtPath(rootSchema, path);
|
|
1589
|
-
if (resolved.length === 0) return false;
|
|
1590
|
-
return resolved.every((candidate) => isLeafRequired(candidate));
|
|
1591
|
-
},
|
|
1592
|
-
getFieldMetaAtPath(path) {
|
|
1593
|
-
return resolveFieldMetaAtPath(rootSchema, path);
|
|
1594
|
-
},
|
|
1595
|
-
getUnionDiscriminatorAtPath(path) {
|
|
1596
|
-
const candidates = path.length === 0 ? [rootSchema] : getNestedZodSchemasAtPath(rootSchema, path);
|
|
1597
|
-
let matchedUnion;
|
|
1598
|
-
for (const candidate of candidates) {
|
|
1599
|
-
const du = unwrapToDiscriminatedUnion(candidate);
|
|
1600
|
-
if (du === void 0) continue;
|
|
1601
|
-
if (matchedUnion !== void 0 && matchedUnion !== du) return void 0;
|
|
1602
|
-
matchedUnion = du;
|
|
1603
|
-
}
|
|
1604
|
-
if (matchedUnion === void 0) return void 0;
|
|
1605
|
-
const discKey = getDiscriminator(matchedUnion);
|
|
1606
|
-
if (discKey === void 0) return void 0;
|
|
1607
|
-
const options = getDiscriminatedOptions(matchedUnion);
|
|
1608
|
-
return {
|
|
1609
|
-
discriminatorKey: discKey,
|
|
1610
|
-
getVariantDefault(value) {
|
|
1611
|
-
for (const opt of options) {
|
|
1612
|
-
const shape = getObjectShape(opt);
|
|
1613
|
-
const litSchema = shape[discKey];
|
|
1614
|
-
if (litSchema === void 0) continue;
|
|
1615
|
-
if (kindOf(litSchema) !== "literal") continue;
|
|
1616
|
-
const literalValues = getLiteralValues(litSchema);
|
|
1617
|
-
if (literalValues.includes(value)) return deriveDefault(opt, true);
|
|
1618
|
-
}
|
|
1619
|
-
return void 0;
|
|
1620
|
-
}
|
|
1621
|
-
};
|
|
1622
|
-
},
|
|
1623
|
-
validateAtPath(data, path, options) {
|
|
1624
|
-
const trySync = options?.sync === true;
|
|
1625
|
-
if (trySync) {
|
|
1626
|
-
try {
|
|
1627
|
-
return runSync();
|
|
1628
|
-
} catch {
|
|
1629
|
-
}
|
|
1630
|
-
}
|
|
1631
|
-
return runAsync();
|
|
1632
|
-
function runSync() {
|
|
1633
|
-
if (path === void 0) {
|
|
1634
|
-
const result = rootSchema.safeParse(data);
|
|
1635
|
-
return result.success ? { data: result.data, errors: void 0, success: true, formKey } : {
|
|
1636
|
-
data: void 0,
|
|
1637
|
-
errors: zodIssuesToValidationErrors(result.error.issues, formKey),
|
|
1638
|
-
success: false,
|
|
1639
|
-
formKey
|
|
1640
|
-
};
|
|
1641
|
-
}
|
|
1642
|
-
const resolved = getNestedZodSchemasAtPath(rootSchema, path);
|
|
1643
|
-
if (resolved.length === 0) return pathNotFound(path);
|
|
1644
|
-
const aggregated = [];
|
|
1645
|
-
for (const candidate of resolved) {
|
|
1646
|
-
const result = candidate.safeParse(data);
|
|
1647
|
-
if (result.success) {
|
|
1648
|
-
return { data: result.data, errors: void 0, success: true, formKey };
|
|
1649
|
-
}
|
|
1650
|
-
aggregated.push(...zodIssuesToValidationErrors(result.error.issues, formKey));
|
|
1651
|
-
}
|
|
1652
|
-
return { data: void 0, errors: aggregated, success: false, formKey };
|
|
1653
|
-
}
|
|
1654
|
-
async function runAsync() {
|
|
1655
|
-
if (path === void 0) {
|
|
1656
|
-
const result = await rootSchema.safeParseAsync(data);
|
|
1657
|
-
return result.success ? { data: result.data, errors: void 0, success: true, formKey } : {
|
|
1658
|
-
data: void 0,
|
|
1659
|
-
errors: zodIssuesToValidationErrors(result.error.issues, formKey),
|
|
1660
|
-
success: false,
|
|
1661
|
-
formKey
|
|
1662
|
-
};
|
|
1663
|
-
}
|
|
1664
|
-
const resolved = getNestedZodSchemasAtPath(rootSchema, path);
|
|
1665
|
-
if (resolved.length === 0) return pathNotFound(path);
|
|
1666
|
-
const aggregated = [];
|
|
1667
|
-
for (const candidate of resolved) {
|
|
1668
|
-
const result = await candidate.safeParseAsync(data);
|
|
1669
|
-
if (result.success) {
|
|
1670
|
-
return { data: result.data, errors: void 0, success: true, formKey };
|
|
1671
|
-
}
|
|
1672
|
-
aggregated.push(...zodIssuesToValidationErrors(result.error.issues, formKey));
|
|
1673
|
-
}
|
|
1674
|
-
return { data: void 0, errors: aggregated, success: false, formKey };
|
|
1675
|
-
}
|
|
1676
|
-
function pathNotFound(p) {
|
|
1677
|
-
return {
|
|
1678
|
-
data: void 0,
|
|
1679
|
-
errors: [
|
|
1680
|
-
{
|
|
1681
|
-
message: `Path '${p.join(PATH_SEPARATOR)}' did not resolve to any schema`,
|
|
1682
|
-
path: [...p],
|
|
1683
|
-
formKey,
|
|
1684
|
-
code: useFormContext.AttaformErrorCode.PathNotFound
|
|
1685
|
-
}
|
|
1686
|
-
],
|
|
1687
|
-
success: false,
|
|
1688
|
-
formKey
|
|
1689
|
-
};
|
|
1690
|
-
}
|
|
1691
|
-
}
|
|
1692
|
-
};
|
|
1693
|
-
};
|
|
1694
|
-
}
|
|
1695
|
-
const pathMetaCache = /* @__PURE__ */ new WeakMap();
|
|
1696
|
-
function getPathMetaMap(rootSchema) {
|
|
1697
|
-
const cached = pathMetaCache.get(rootSchema);
|
|
1698
|
-
if (cached !== void 0) return cached;
|
|
1699
|
-
const map = /* @__PURE__ */ new Map();
|
|
1700
|
-
const counters = /* @__PURE__ */ new Map();
|
|
1701
|
-
const lastPathPerSchema = /* @__PURE__ */ new Map();
|
|
1702
|
-
const inProgress = /* @__PURE__ */ new WeakSet();
|
|
1703
|
-
walkForMeta(rootSchema, [], map, counters, lastPathPerSchema, inProgress);
|
|
1704
|
-
for (const [schema, lastPath] of lastPathPerSchema) {
|
|
1705
|
-
const list = getFieldMetaList(schema);
|
|
1706
|
-
const consumed = counters.get(schema) ?? 0;
|
|
1707
|
-
if (list.length <= consumed) continue;
|
|
1708
|
-
const surplus = list.slice(consumed).reduce((acc, p) => ({ ...acc, ...p }), {});
|
|
1709
|
-
const existing = map.get(lastPath) ?? {};
|
|
1710
|
-
map.set(lastPath, { ...existing, ...surplus });
|
|
1711
|
-
}
|
|
1712
|
-
pathMetaCache.set(rootSchema, map);
|
|
1713
|
-
return map;
|
|
1714
|
-
}
|
|
1715
|
-
function walkForMeta(schema, path, map, counters, lastPathPerSchema, inProgress) {
|
|
1716
|
-
if (inProgress.has(schema)) return;
|
|
1717
|
-
inProgress.add(schema);
|
|
1718
|
-
try {
|
|
1719
|
-
const pathKey = paths.canonicalizePath(path).key;
|
|
1720
|
-
if (!map.has(pathKey)) {
|
|
1721
|
-
const payload = consumePayload(schema, counters);
|
|
1722
|
-
if (payload !== void 0) {
|
|
1723
|
-
map.set(pathKey, payload);
|
|
1724
|
-
lastPathPerSchema.set(schema, pathKey);
|
|
1725
|
-
}
|
|
1726
|
-
}
|
|
1727
|
-
const peeled = peelAllWrappers(schema);
|
|
1728
|
-
if (peeled !== schema && !map.has(pathKey)) {
|
|
1729
|
-
const payload = consumePayload(peeled, counters);
|
|
1730
|
-
if (payload !== void 0) {
|
|
1731
|
-
map.set(pathKey, payload);
|
|
1732
|
-
lastPathPerSchema.set(peeled, pathKey);
|
|
1733
|
-
}
|
|
1734
|
-
}
|
|
1735
|
-
const kind = kindOf(schema);
|
|
1736
|
-
switch (kind) {
|
|
1737
|
-
case "object": {
|
|
1738
|
-
const shape = getObjectShape(schema);
|
|
1739
|
-
for (const [key, child] of Object.entries(shape)) {
|
|
1740
|
-
walkForMeta(child, [...path, key], map, counters, lastPathPerSchema, inProgress);
|
|
1741
|
-
}
|
|
1742
|
-
return;
|
|
1743
|
-
}
|
|
1744
|
-
case "array": {
|
|
1745
|
-
walkForMeta(
|
|
1746
|
-
getArrayElement(schema),
|
|
1747
|
-
[...path, 0],
|
|
1748
|
-
map,
|
|
1749
|
-
counters,
|
|
1750
|
-
lastPathPerSchema,
|
|
1751
|
-
inProgress
|
|
1752
|
-
);
|
|
1753
|
-
return;
|
|
1754
|
-
}
|
|
1755
|
-
case "tuple": {
|
|
1756
|
-
const items = getTupleItems(schema);
|
|
1757
|
-
for (let i = 0; i < items.length; i++) {
|
|
1758
|
-
const item = items[i];
|
|
1759
|
-
if (item !== void 0)
|
|
1760
|
-
walkForMeta(item, [...path, i], map, counters, lastPathPerSchema, inProgress);
|
|
1761
|
-
}
|
|
1762
|
-
return;
|
|
1763
|
-
}
|
|
1764
|
-
case "set":
|
|
1765
|
-
walkForMeta(
|
|
1766
|
-
getSetValueType(schema),
|
|
1767
|
-
[...path, 0],
|
|
1768
|
-
map,
|
|
1769
|
-
counters,
|
|
1770
|
-
lastPathPerSchema,
|
|
1771
|
-
inProgress
|
|
1772
|
-
);
|
|
1773
|
-
return;
|
|
1774
|
-
case "record":
|
|
1775
|
-
walkForMeta(
|
|
1776
|
-
getRecordValueType(schema),
|
|
1777
|
-
[...path, "*"],
|
|
1778
|
-
map,
|
|
1779
|
-
counters,
|
|
1780
|
-
lastPathPerSchema,
|
|
1781
|
-
inProgress
|
|
1782
|
-
);
|
|
1783
|
-
return;
|
|
1784
|
-
case "union": {
|
|
1785
|
-
for (const opt of getUnionOptions(schema)) {
|
|
1786
|
-
walkForMeta(opt, path, map, counters, lastPathPerSchema, inProgress);
|
|
1787
|
-
}
|
|
1788
|
-
return;
|
|
1789
|
-
}
|
|
1790
|
-
case "discriminated-union": {
|
|
1791
|
-
for (const opt of getDiscriminatedOptions(schema)) {
|
|
1792
|
-
walkForMeta(opt, path, map, counters, lastPathPerSchema, inProgress);
|
|
1793
|
-
}
|
|
1794
|
-
return;
|
|
1795
|
-
}
|
|
1796
|
-
case "optional":
|
|
1797
|
-
case "nullable":
|
|
1798
|
-
case "default":
|
|
1799
|
-
case "readonly":
|
|
1800
|
-
case "catch": {
|
|
1801
|
-
const inner = unwrapInner(schema);
|
|
1802
|
-
if (inner !== void 0)
|
|
1803
|
-
walkForMeta(inner, path, map, counters, lastPathPerSchema, inProgress);
|
|
1804
|
-
return;
|
|
1805
|
-
}
|
|
1806
|
-
case "pipe": {
|
|
1807
|
-
const inner = unwrapPipe(schema);
|
|
1808
|
-
if (inner !== void 0)
|
|
1809
|
-
walkForMeta(inner, path, map, counters, lastPathPerSchema, inProgress);
|
|
1810
|
-
return;
|
|
1811
|
-
}
|
|
1812
|
-
case "lazy": {
|
|
1813
|
-
const inner = unwrapLazy(schema);
|
|
1814
|
-
if (inner !== void 0)
|
|
1815
|
-
walkForMeta(inner, path, map, counters, lastPathPerSchema, inProgress);
|
|
1816
|
-
return;
|
|
1817
|
-
}
|
|
1818
|
-
case "intersection": {
|
|
1819
|
-
const left = getIntersectionLeft(schema);
|
|
1820
|
-
const right = getIntersectionRight(schema);
|
|
1821
|
-
if (left !== void 0)
|
|
1822
|
-
walkForMeta(left, path, map, counters, lastPathPerSchema, inProgress);
|
|
1823
|
-
if (right !== void 0)
|
|
1824
|
-
walkForMeta(right, path, map, counters, lastPathPerSchema, inProgress);
|
|
1825
|
-
return;
|
|
1826
|
-
}
|
|
1827
|
-
// Leaf kinds — no children to descend into; metadata for the
|
|
1828
|
-
// path itself was captured above. Listed explicitly so the
|
|
1829
|
-
// exhaustiveness check catches any new kind landing in Zod
|
|
1830
|
-
// without a corresponding decision here.
|
|
1831
|
-
case "string":
|
|
1832
|
-
case "number":
|
|
1833
|
-
case "bigint":
|
|
1834
|
-
case "boolean":
|
|
1835
|
-
case "date":
|
|
1836
|
-
case "enum":
|
|
1837
|
-
case "literal":
|
|
1838
|
-
case "null":
|
|
1839
|
-
case "undefined":
|
|
1840
|
-
case "any":
|
|
1841
|
-
case "unknown":
|
|
1842
|
-
case "nan":
|
|
1843
|
-
case "void":
|
|
1844
|
-
case "never":
|
|
1845
|
-
case "promise":
|
|
1846
|
-
case "custom":
|
|
1847
|
-
case "template-literal":
|
|
1848
|
-
return;
|
|
1849
|
-
}
|
|
1850
|
-
} finally {
|
|
1851
|
-
inProgress.delete(schema);
|
|
15
|
+
function useForm(configuration) {
|
|
16
|
+
if (configuration === void 0 || configuration === null || configuration.schema === void 0) {
|
|
17
|
+
throw new plugin.InvalidUseFormConfigError();
|
|
1852
18
|
}
|
|
1853
|
-
}
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
if (list.length === 0) return void 0;
|
|
1857
|
-
const idx = counters.get(schema) ?? 0;
|
|
1858
|
-
const payload = list[Math.min(idx, list.length - 1)];
|
|
1859
|
-
counters.set(schema, idx + 1);
|
|
1860
|
-
return payload;
|
|
1861
|
-
}
|
|
1862
|
-
function resolveFieldMetaAtPath(rootSchema, path) {
|
|
1863
|
-
const lastSegment = path.length === 0 ? "" : path[path.length - 1];
|
|
1864
|
-
const candidates = path.length === 0 ? [rootSchema] : getNestedZodSchemasAtPath(rootSchema, path);
|
|
1865
|
-
const target = candidates[0];
|
|
1866
|
-
if (target === void 0) {
|
|
1867
|
-
return {
|
|
1868
|
-
label: useFormContext.humanize(lastSegment),
|
|
1869
|
-
description: void 0,
|
|
1870
|
-
placeholder: void 0,
|
|
1871
|
-
meta: Object.freeze({})
|
|
1872
|
-
};
|
|
19
|
+
const { schema } = configuration;
|
|
20
|
+
if (isZodV4SchemaShape(schema)) {
|
|
21
|
+
return index.useForm(configuration);
|
|
1873
22
|
}
|
|
1874
|
-
|
|
1875
|
-
const pathKey = paths.canonicalizePath(path).key;
|
|
1876
|
-
const peeled = peelAllWrappers(target);
|
|
1877
|
-
const payload = pathMap.get(pathKey) ?? getFieldMeta(target) ?? (peeled !== target ? getFieldMeta(peeled) : void 0);
|
|
1878
|
-
const targetDescription = readDescription(target);
|
|
1879
|
-
const peeledDescription = peeled !== target ? readDescription(peeled) : void 0;
|
|
1880
|
-
const schemaDescription = targetDescription ?? peeledDescription;
|
|
1881
|
-
return {
|
|
1882
|
-
label: payload?.label ?? useFormContext.humanize(lastSegment),
|
|
1883
|
-
description: payload?.description ?? schemaDescription ?? void 0,
|
|
1884
|
-
placeholder: payload?.placeholder ?? void 0,
|
|
1885
|
-
meta: Object.freeze({ ...payload ?? {} })
|
|
1886
|
-
};
|
|
1887
|
-
}
|
|
1888
|
-
function readDescription(schema) {
|
|
1889
|
-
const candidate = schema.description;
|
|
1890
|
-
return typeof candidate === "string" ? candidate : void 0;
|
|
1891
|
-
}
|
|
1892
|
-
|
|
1893
|
-
function useForm(configuration) {
|
|
1894
|
-
const adapter = zodV4Adapter(
|
|
1895
|
-
configuration.schema
|
|
1896
|
-
);
|
|
1897
|
-
return useFormContext.useAbstractForm({
|
|
1898
|
-
...configuration,
|
|
1899
|
-
schema: adapter
|
|
1900
|
-
});
|
|
23
|
+
return useForm$1.useForm(configuration);
|
|
1901
24
|
}
|
|
1902
25
|
|
|
26
|
+
exports.useRegister = plugin.useRegister;
|
|
27
|
+
exports.fieldMeta = index.fieldMeta;
|
|
28
|
+
exports.withMeta = index.withMeta;
|
|
1903
29
|
exports.AttaformErrorCode = useFormContext.AttaformErrorCode;
|
|
1904
30
|
exports.injectForm = useFormContext.injectForm;
|
|
1905
31
|
exports.isUnset = useFormContext.isUnset;
|
|
1906
32
|
exports.unset = useFormContext.unset;
|
|
1907
|
-
exports.useRegister = sensitiveNames.useRegister;
|
|
1908
|
-
exports.UnsupportedSchemaError = UnsupportedSchemaError;
|
|
1909
|
-
exports.assertZodVersion = assertZodVersion;
|
|
1910
|
-
exports.fieldMeta = fieldMeta;
|
|
1911
|
-
exports.kindOf = kindOf;
|
|
1912
33
|
exports.useForm = useForm;
|
|
1913
|
-
exports.withMeta = withMeta;
|
|
1914
|
-
exports.zodAdapter = zodV4Adapter;
|
|
1915
34
|
//# sourceMappingURL=zod.cjs.map
|