zodvex 0.2.3 → 0.2.5
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.js +163 -196
- package/dist/index.js.map +1 -1
- package/package.json +12 -10
- package/src/mapping/handlers/union.ts +15 -8
- package/dist/index.d.mts +0 -489
- package/dist/index.mjs +0 -997
- package/dist/index.mjs.map +0 -1
package/dist/index.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
var server = require('convex-helpers/server');
|
|
1
|
+
import { NoOp } from 'convex-helpers/server/customFunctions';
|
|
2
|
+
export { customCtx } from 'convex-helpers/server/customFunctions';
|
|
3
|
+
import { z } from 'zod';
|
|
4
|
+
import { v, ConvexError } from 'convex/values';
|
|
5
|
+
import { Table } from 'convex-helpers/server';
|
|
7
6
|
|
|
7
|
+
// src/index.ts
|
|
8
8
|
var metadata = /* @__PURE__ */ new WeakMap();
|
|
9
9
|
var registryHelpers = {
|
|
10
10
|
getMetadata: (type) => metadata.get(type),
|
|
11
11
|
setMetadata: (type, meta) => metadata.set(type, meta)
|
|
12
12
|
};
|
|
13
13
|
function zid(tableName) {
|
|
14
|
-
const baseSchema =
|
|
14
|
+
const baseSchema = z.string().refine((val) => typeof val === "string" && val.length > 0, {
|
|
15
15
|
message: `Invalid ID for table "${tableName}"`
|
|
16
16
|
}).transform((val) => {
|
|
17
17
|
return val;
|
|
@@ -32,8 +32,8 @@ function findBaseCodec(schema) {
|
|
|
32
32
|
return baseCodecs.find((codec) => codec.check(schema));
|
|
33
33
|
}
|
|
34
34
|
registerBaseCodec({
|
|
35
|
-
check: (schema) => schema instanceof
|
|
36
|
-
toValidator: () =>
|
|
35
|
+
check: (schema) => schema instanceof z.ZodDate,
|
|
36
|
+
toValidator: () => v.float64(),
|
|
37
37
|
fromConvex: (value) => {
|
|
38
38
|
if (typeof value === "number") {
|
|
39
39
|
return new Date(value);
|
|
@@ -51,8 +51,8 @@ function asZodType(schema) {
|
|
|
51
51
|
return schema;
|
|
52
52
|
}
|
|
53
53
|
function isDateSchema(schema) {
|
|
54
|
-
if (schema instanceof
|
|
55
|
-
if (schema instanceof
|
|
54
|
+
if (schema instanceof z.ZodDate) return true;
|
|
55
|
+
if (schema instanceof z.ZodOptional || schema instanceof z.ZodNullable) {
|
|
56
56
|
return isDateSchema(asZodType(schema.unwrap()));
|
|
57
57
|
}
|
|
58
58
|
return false;
|
|
@@ -60,45 +60,45 @@ function isDateSchema(schema) {
|
|
|
60
60
|
function convertEnumType(actualValidator) {
|
|
61
61
|
const options = actualValidator.options;
|
|
62
62
|
if (options && Array.isArray(options) && options.length > 0) {
|
|
63
|
-
const validLiterals = options.filter((opt) => opt !== void 0 && opt !== null).map((opt) =>
|
|
63
|
+
const validLiterals = options.filter((opt) => opt !== void 0 && opt !== null).map((opt) => v.literal(opt));
|
|
64
64
|
if (validLiterals.length === 1) {
|
|
65
65
|
const [first] = validLiterals;
|
|
66
66
|
return first;
|
|
67
67
|
} else if (validLiterals.length >= 2) {
|
|
68
68
|
const [first, second, ...rest] = validLiterals;
|
|
69
|
-
return
|
|
69
|
+
return v.union(
|
|
70
70
|
first,
|
|
71
71
|
second,
|
|
72
72
|
...rest
|
|
73
73
|
);
|
|
74
74
|
} else {
|
|
75
|
-
return
|
|
75
|
+
return v.any();
|
|
76
76
|
}
|
|
77
77
|
} else {
|
|
78
|
-
return
|
|
78
|
+
return v.any();
|
|
79
79
|
}
|
|
80
80
|
}
|
|
81
81
|
function convertNullableType(actualValidator, visited, zodToConvexInternal2) {
|
|
82
82
|
const innerSchema = actualValidator.unwrap();
|
|
83
|
-
if (innerSchema && innerSchema instanceof
|
|
84
|
-
if (innerSchema instanceof
|
|
83
|
+
if (innerSchema && innerSchema instanceof z.ZodType) {
|
|
84
|
+
if (innerSchema instanceof z.ZodOptional) {
|
|
85
85
|
const innerInnerSchema = innerSchema.unwrap();
|
|
86
86
|
const innerInnerValidator = zodToConvexInternal2(innerInnerSchema, visited);
|
|
87
87
|
return {
|
|
88
|
-
validator:
|
|
88
|
+
validator: v.union(innerInnerValidator, v.null()),
|
|
89
89
|
isOptional: true
|
|
90
90
|
// Mark as optional so it gets wrapped later
|
|
91
91
|
};
|
|
92
92
|
} else {
|
|
93
93
|
const innerValidator = zodToConvexInternal2(innerSchema, visited);
|
|
94
94
|
return {
|
|
95
|
-
validator:
|
|
95
|
+
validator: v.union(innerValidator, v.null()),
|
|
96
96
|
isOptional: false
|
|
97
97
|
};
|
|
98
98
|
}
|
|
99
99
|
} else {
|
|
100
100
|
return {
|
|
101
|
-
validator:
|
|
101
|
+
validator: v.any(),
|
|
102
102
|
isOptional: false
|
|
103
103
|
};
|
|
104
104
|
}
|
|
@@ -108,37 +108,37 @@ function convertRecordType(actualValidator, visited, zodToConvexInternal2) {
|
|
|
108
108
|
if (!valueType) {
|
|
109
109
|
valueType = actualValidator._def?.keyType;
|
|
110
110
|
}
|
|
111
|
-
if (valueType && valueType instanceof
|
|
112
|
-
const isZodOptional = valueType instanceof
|
|
111
|
+
if (valueType && valueType instanceof z.ZodType) {
|
|
112
|
+
const isZodOptional = valueType instanceof z.ZodOptional || valueType instanceof z.ZodDefault || valueType instanceof z.ZodDefault && valueType.def.innerType instanceof z.ZodOptional;
|
|
113
113
|
if (isZodOptional) {
|
|
114
114
|
let innerType;
|
|
115
115
|
let recordDefaultValue = void 0;
|
|
116
116
|
let recordHasDefault = false;
|
|
117
|
-
if (valueType instanceof
|
|
117
|
+
if (valueType instanceof z.ZodDefault) {
|
|
118
118
|
recordHasDefault = true;
|
|
119
119
|
recordDefaultValue = valueType.def.defaultValue;
|
|
120
120
|
const innerFromDefault = valueType.def.innerType;
|
|
121
|
-
if (innerFromDefault instanceof
|
|
121
|
+
if (innerFromDefault instanceof z.ZodOptional) {
|
|
122
122
|
innerType = innerFromDefault.unwrap();
|
|
123
123
|
} else {
|
|
124
124
|
innerType = innerFromDefault;
|
|
125
125
|
}
|
|
126
|
-
} else if (valueType instanceof
|
|
126
|
+
} else if (valueType instanceof z.ZodOptional) {
|
|
127
127
|
innerType = valueType.unwrap();
|
|
128
128
|
} else {
|
|
129
129
|
innerType = valueType;
|
|
130
130
|
}
|
|
131
131
|
const innerConvex = zodToConvexInternal2(innerType, visited);
|
|
132
|
-
const unionValidator =
|
|
132
|
+
const unionValidator = v.union(innerConvex, v.null());
|
|
133
133
|
if (recordHasDefault) {
|
|
134
134
|
unionValidator._zodDefault = recordDefaultValue;
|
|
135
135
|
}
|
|
136
|
-
return
|
|
136
|
+
return v.record(v.string(), unionValidator);
|
|
137
137
|
} else {
|
|
138
|
-
return
|
|
138
|
+
return v.record(v.string(), zodToConvexInternal2(valueType, visited));
|
|
139
139
|
}
|
|
140
140
|
} else {
|
|
141
|
-
return
|
|
141
|
+
return v.record(v.string(), v.any());
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
144
|
function convertDiscriminatedUnionType(actualValidator, visited, zodToConvexInternal2) {
|
|
@@ -146,18 +146,21 @@ function convertDiscriminatedUnionType(actualValidator, visited, zodToConvexInte
|
|
|
146
146
|
if (options) {
|
|
147
147
|
const opts = Array.isArray(options) ? options : Array.from(options);
|
|
148
148
|
if (opts.length >= 2) {
|
|
149
|
-
const convexOptions = opts.map((opt) =>
|
|
149
|
+
const convexOptions = opts.map((opt) => {
|
|
150
|
+
const branchVisited = new Set(visited);
|
|
151
|
+
return zodToConvexInternal2(opt, branchVisited);
|
|
152
|
+
});
|
|
150
153
|
const [first, second, ...rest] = convexOptions;
|
|
151
|
-
return
|
|
154
|
+
return v.union(
|
|
152
155
|
first,
|
|
153
156
|
second,
|
|
154
157
|
...rest
|
|
155
158
|
);
|
|
156
159
|
} else {
|
|
157
|
-
return
|
|
160
|
+
return v.any();
|
|
158
161
|
}
|
|
159
162
|
} else {
|
|
160
|
-
return
|
|
163
|
+
return v.any();
|
|
161
164
|
}
|
|
162
165
|
}
|
|
163
166
|
function convertUnionType(actualValidator, visited, zodToConvexInternal2) {
|
|
@@ -166,22 +169,23 @@ function convertUnionType(actualValidator, visited, zodToConvexInternal2) {
|
|
|
166
169
|
if (options.length === 1) {
|
|
167
170
|
return zodToConvexInternal2(options[0], visited);
|
|
168
171
|
} else {
|
|
169
|
-
const convexOptions = options.map(
|
|
170
|
-
|
|
171
|
-
|
|
172
|
+
const convexOptions = options.map((opt) => {
|
|
173
|
+
const branchVisited = new Set(visited);
|
|
174
|
+
return zodToConvexInternal2(opt, branchVisited);
|
|
175
|
+
});
|
|
172
176
|
if (convexOptions.length >= 2) {
|
|
173
177
|
const [first, second, ...rest] = convexOptions;
|
|
174
|
-
return
|
|
178
|
+
return v.union(
|
|
175
179
|
first,
|
|
176
180
|
second,
|
|
177
181
|
...rest
|
|
178
182
|
);
|
|
179
183
|
} else {
|
|
180
|
-
return
|
|
184
|
+
return v.any();
|
|
181
185
|
}
|
|
182
186
|
}
|
|
183
187
|
} else {
|
|
184
|
-
return
|
|
188
|
+
return v.any();
|
|
185
189
|
}
|
|
186
190
|
}
|
|
187
191
|
function isZid(schema) {
|
|
@@ -190,12 +194,12 @@ function isZid(schema) {
|
|
|
190
194
|
}
|
|
191
195
|
function makeUnion(members) {
|
|
192
196
|
const nonNull = members.filter(Boolean);
|
|
193
|
-
if (nonNull.length === 0) return
|
|
197
|
+
if (nonNull.length === 0) return v.any();
|
|
194
198
|
if (nonNull.length === 1) return nonNull[0];
|
|
195
|
-
return
|
|
199
|
+
return v.union(nonNull[0], nonNull[1], ...nonNull.slice(2));
|
|
196
200
|
}
|
|
197
201
|
function getObjectShape(obj) {
|
|
198
|
-
if (obj instanceof
|
|
202
|
+
if (obj instanceof z.ZodObject) {
|
|
199
203
|
return obj.shape;
|
|
200
204
|
}
|
|
201
205
|
if (obj && typeof obj === "object" && typeof obj.shape === "object") {
|
|
@@ -207,25 +211,25 @@ function getObjectShape(obj) {
|
|
|
207
211
|
// src/mapping/core.ts
|
|
208
212
|
function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set()) {
|
|
209
213
|
if (!zodValidator) {
|
|
210
|
-
return
|
|
214
|
+
return v.any();
|
|
211
215
|
}
|
|
212
216
|
if (visited.has(zodValidator)) {
|
|
213
|
-
return
|
|
217
|
+
return v.any();
|
|
214
218
|
}
|
|
215
219
|
visited.add(zodValidator);
|
|
216
220
|
let actualValidator = zodValidator;
|
|
217
221
|
let isOptional = false;
|
|
218
222
|
let defaultValue = void 0;
|
|
219
223
|
let hasDefault = false;
|
|
220
|
-
if (zodValidator instanceof
|
|
224
|
+
if (zodValidator instanceof z.ZodDefault) {
|
|
221
225
|
hasDefault = true;
|
|
222
226
|
defaultValue = zodValidator.def?.defaultValue;
|
|
223
227
|
actualValidator = zodValidator.def?.innerType;
|
|
224
228
|
}
|
|
225
|
-
if (actualValidator instanceof
|
|
229
|
+
if (actualValidator instanceof z.ZodOptional) {
|
|
226
230
|
isOptional = true;
|
|
227
231
|
actualValidator = actualValidator.unwrap();
|
|
228
|
-
if (actualValidator instanceof
|
|
232
|
+
if (actualValidator instanceof z.ZodDefault) {
|
|
229
233
|
hasDefault = true;
|
|
230
234
|
defaultValue = actualValidator.def?.defaultValue;
|
|
231
235
|
actualValidator = actualValidator.def?.innerType;
|
|
@@ -235,64 +239,64 @@ function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set())
|
|
|
235
239
|
if (isZid(actualValidator)) {
|
|
236
240
|
const metadata2 = registryHelpers.getMetadata(actualValidator);
|
|
237
241
|
const tableName = metadata2?.tableName || "unknown";
|
|
238
|
-
convexValidator =
|
|
242
|
+
convexValidator = v.id(tableName);
|
|
239
243
|
} else {
|
|
240
244
|
const defType = actualValidator.def?.type;
|
|
241
245
|
switch (defType) {
|
|
242
246
|
case "string":
|
|
243
|
-
convexValidator =
|
|
247
|
+
convexValidator = v.string();
|
|
244
248
|
break;
|
|
245
249
|
case "number":
|
|
246
|
-
convexValidator =
|
|
250
|
+
convexValidator = v.float64();
|
|
247
251
|
break;
|
|
248
252
|
case "bigint":
|
|
249
|
-
convexValidator =
|
|
253
|
+
convexValidator = v.int64();
|
|
250
254
|
break;
|
|
251
255
|
case "boolean":
|
|
252
|
-
convexValidator =
|
|
256
|
+
convexValidator = v.boolean();
|
|
253
257
|
break;
|
|
254
258
|
case "date":
|
|
255
|
-
convexValidator =
|
|
259
|
+
convexValidator = v.float64();
|
|
256
260
|
break;
|
|
257
261
|
case "null":
|
|
258
|
-
convexValidator =
|
|
262
|
+
convexValidator = v.null();
|
|
259
263
|
break;
|
|
260
264
|
case "nan":
|
|
261
|
-
convexValidator =
|
|
265
|
+
convexValidator = v.float64();
|
|
262
266
|
break;
|
|
263
267
|
case "array": {
|
|
264
|
-
if (actualValidator instanceof
|
|
268
|
+
if (actualValidator instanceof z.ZodArray) {
|
|
265
269
|
const element = actualValidator.element;
|
|
266
|
-
if (element && element instanceof
|
|
267
|
-
convexValidator =
|
|
270
|
+
if (element && element instanceof z.ZodType) {
|
|
271
|
+
convexValidator = v.array(zodToConvexInternal(element, visited));
|
|
268
272
|
} else {
|
|
269
|
-
convexValidator =
|
|
273
|
+
convexValidator = v.array(v.any());
|
|
270
274
|
}
|
|
271
275
|
} else {
|
|
272
|
-
convexValidator =
|
|
276
|
+
convexValidator = v.array(v.any());
|
|
273
277
|
}
|
|
274
278
|
break;
|
|
275
279
|
}
|
|
276
280
|
case "object": {
|
|
277
|
-
if (actualValidator instanceof
|
|
281
|
+
if (actualValidator instanceof z.ZodObject) {
|
|
278
282
|
const shape = actualValidator.shape;
|
|
279
283
|
const convexShape = {};
|
|
280
284
|
for (const [key, value] of Object.entries(shape)) {
|
|
281
|
-
if (value && value instanceof
|
|
285
|
+
if (value && value instanceof z.ZodType) {
|
|
282
286
|
convexShape[key] = zodToConvexInternal(value, visited);
|
|
283
287
|
}
|
|
284
288
|
}
|
|
285
|
-
convexValidator =
|
|
289
|
+
convexValidator = v.object(convexShape);
|
|
286
290
|
} else {
|
|
287
|
-
convexValidator =
|
|
291
|
+
convexValidator = v.object({});
|
|
288
292
|
}
|
|
289
293
|
break;
|
|
290
294
|
}
|
|
291
295
|
case "union": {
|
|
292
|
-
if (actualValidator instanceof
|
|
296
|
+
if (actualValidator instanceof z.ZodUnion) {
|
|
293
297
|
convexValidator = convertUnionType(actualValidator, visited, zodToConvexInternal);
|
|
294
298
|
} else {
|
|
295
|
-
convexValidator =
|
|
299
|
+
convexValidator = v.any();
|
|
296
300
|
}
|
|
297
301
|
break;
|
|
298
302
|
}
|
|
@@ -305,31 +309,31 @@ function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set())
|
|
|
305
309
|
break;
|
|
306
310
|
}
|
|
307
311
|
case "literal": {
|
|
308
|
-
if (actualValidator instanceof
|
|
312
|
+
if (actualValidator instanceof z.ZodLiteral) {
|
|
309
313
|
const literalValue = actualValidator.value;
|
|
310
314
|
if (literalValue !== void 0 && literalValue !== null) {
|
|
311
|
-
convexValidator =
|
|
315
|
+
convexValidator = v.literal(literalValue);
|
|
312
316
|
} else {
|
|
313
|
-
convexValidator =
|
|
317
|
+
convexValidator = v.any();
|
|
314
318
|
}
|
|
315
319
|
} else {
|
|
316
|
-
convexValidator =
|
|
320
|
+
convexValidator = v.any();
|
|
317
321
|
}
|
|
318
322
|
break;
|
|
319
323
|
}
|
|
320
324
|
case "enum": {
|
|
321
|
-
if (actualValidator instanceof
|
|
325
|
+
if (actualValidator instanceof z.ZodEnum) {
|
|
322
326
|
convexValidator = convertEnumType(actualValidator);
|
|
323
327
|
} else {
|
|
324
|
-
convexValidator =
|
|
328
|
+
convexValidator = v.any();
|
|
325
329
|
}
|
|
326
330
|
break;
|
|
327
331
|
}
|
|
328
332
|
case "record": {
|
|
329
|
-
if (actualValidator instanceof
|
|
333
|
+
if (actualValidator instanceof z.ZodRecord) {
|
|
330
334
|
convexValidator = convertRecordType(actualValidator, visited, zodToConvexInternal);
|
|
331
335
|
} else {
|
|
332
|
-
convexValidator =
|
|
336
|
+
convexValidator = v.record(v.string(), v.any());
|
|
333
337
|
}
|
|
334
338
|
break;
|
|
335
339
|
}
|
|
@@ -343,75 +347,75 @@ function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set())
|
|
|
343
347
|
if (metadata2?.brand && metadata2?.originalSchema) {
|
|
344
348
|
convexValidator = zodToConvexInternal(metadata2.originalSchema, visited);
|
|
345
349
|
} else {
|
|
346
|
-
convexValidator =
|
|
350
|
+
convexValidator = v.any();
|
|
347
351
|
}
|
|
348
352
|
}
|
|
349
353
|
break;
|
|
350
354
|
}
|
|
351
355
|
case "nullable": {
|
|
352
|
-
if (actualValidator instanceof
|
|
356
|
+
if (actualValidator instanceof z.ZodNullable) {
|
|
353
357
|
const result = convertNullableType(actualValidator, visited, zodToConvexInternal);
|
|
354
358
|
convexValidator = result.validator;
|
|
355
359
|
if (result.isOptional) {
|
|
356
360
|
isOptional = true;
|
|
357
361
|
}
|
|
358
362
|
} else {
|
|
359
|
-
convexValidator =
|
|
363
|
+
convexValidator = v.any();
|
|
360
364
|
}
|
|
361
365
|
break;
|
|
362
366
|
}
|
|
363
367
|
case "tuple": {
|
|
364
|
-
if (actualValidator instanceof
|
|
368
|
+
if (actualValidator instanceof z.ZodTuple) {
|
|
365
369
|
const items = actualValidator.def?.items;
|
|
366
370
|
if (items && items.length > 0) {
|
|
367
371
|
const convexShape = {};
|
|
368
372
|
items.forEach((item, index) => {
|
|
369
373
|
convexShape[`_${index}`] = zodToConvexInternal(item, visited);
|
|
370
374
|
});
|
|
371
|
-
convexValidator =
|
|
375
|
+
convexValidator = v.object(convexShape);
|
|
372
376
|
} else {
|
|
373
|
-
convexValidator =
|
|
377
|
+
convexValidator = v.object({});
|
|
374
378
|
}
|
|
375
379
|
} else {
|
|
376
|
-
convexValidator =
|
|
380
|
+
convexValidator = v.object({});
|
|
377
381
|
}
|
|
378
382
|
break;
|
|
379
383
|
}
|
|
380
384
|
case "lazy": {
|
|
381
|
-
if (actualValidator instanceof
|
|
385
|
+
if (actualValidator instanceof z.ZodLazy) {
|
|
382
386
|
try {
|
|
383
387
|
const getter = actualValidator.def?.getter;
|
|
384
388
|
if (getter) {
|
|
385
389
|
const resolvedSchema = getter();
|
|
386
|
-
if (resolvedSchema && resolvedSchema instanceof
|
|
390
|
+
if (resolvedSchema && resolvedSchema instanceof z.ZodType) {
|
|
387
391
|
convexValidator = zodToConvexInternal(resolvedSchema, visited);
|
|
388
392
|
} else {
|
|
389
|
-
convexValidator =
|
|
393
|
+
convexValidator = v.any();
|
|
390
394
|
}
|
|
391
395
|
} else {
|
|
392
|
-
convexValidator =
|
|
396
|
+
convexValidator = v.any();
|
|
393
397
|
}
|
|
394
398
|
} catch {
|
|
395
|
-
convexValidator =
|
|
399
|
+
convexValidator = v.any();
|
|
396
400
|
}
|
|
397
401
|
} else {
|
|
398
|
-
convexValidator =
|
|
402
|
+
convexValidator = v.any();
|
|
399
403
|
}
|
|
400
404
|
break;
|
|
401
405
|
}
|
|
402
406
|
case "any":
|
|
403
|
-
convexValidator =
|
|
407
|
+
convexValidator = v.any();
|
|
404
408
|
break;
|
|
405
409
|
case "unknown":
|
|
406
|
-
convexValidator =
|
|
410
|
+
convexValidator = v.any();
|
|
407
411
|
break;
|
|
408
412
|
case "undefined":
|
|
409
413
|
case "void":
|
|
410
414
|
case "never":
|
|
411
|
-
convexValidator =
|
|
415
|
+
convexValidator = v.any();
|
|
412
416
|
break;
|
|
413
417
|
case "intersection":
|
|
414
|
-
convexValidator =
|
|
418
|
+
convexValidator = v.any();
|
|
415
419
|
break;
|
|
416
420
|
default:
|
|
417
421
|
if (process.env.NODE_ENV !== "production") {
|
|
@@ -421,24 +425,24 @@ function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set())
|
|
|
421
425
|
actualValidator
|
|
422
426
|
);
|
|
423
427
|
}
|
|
424
|
-
convexValidator =
|
|
428
|
+
convexValidator = v.any();
|
|
425
429
|
break;
|
|
426
430
|
}
|
|
427
431
|
}
|
|
428
|
-
const finalValidator = isOptional || hasDefault ?
|
|
432
|
+
const finalValidator = isOptional || hasDefault ? v.optional(convexValidator) : convexValidator;
|
|
429
433
|
if (hasDefault && typeof finalValidator === "object" && finalValidator !== null) {
|
|
430
434
|
finalValidator._zodDefault = defaultValue;
|
|
431
435
|
}
|
|
432
436
|
return finalValidator;
|
|
433
437
|
}
|
|
434
|
-
function zodToConvex(zod
|
|
435
|
-
if (typeof zod
|
|
436
|
-
return zodToConvexFields(zod
|
|
438
|
+
function zodToConvex(zod) {
|
|
439
|
+
if (typeof zod === "object" && zod !== null && !(zod instanceof z.ZodType)) {
|
|
440
|
+
return zodToConvexFields(zod);
|
|
437
441
|
}
|
|
438
|
-
return zodToConvexInternal(zod
|
|
442
|
+
return zodToConvexInternal(zod);
|
|
439
443
|
}
|
|
440
|
-
function zodToConvexFields(zod
|
|
441
|
-
const fields = zod
|
|
444
|
+
function zodToConvexFields(zod) {
|
|
445
|
+
const fields = zod instanceof z.ZodObject ? zod.shape : zod;
|
|
442
446
|
const result = {};
|
|
443
447
|
for (const [key, value] of Object.entries(fields)) {
|
|
444
448
|
result[key] = zodToConvexInternal(value);
|
|
@@ -457,7 +461,7 @@ function convexCodec(schema) {
|
|
|
457
461
|
encode: (value) => toConvexJS(schema, value),
|
|
458
462
|
decode: (value) => fromConvexJS(value, schema),
|
|
459
463
|
pick: (keys) => {
|
|
460
|
-
if (!(schema instanceof
|
|
464
|
+
if (!(schema instanceof z.ZodObject)) {
|
|
461
465
|
throw new Error("pick() can only be called on object schemas");
|
|
462
466
|
}
|
|
463
467
|
const pickObj = Array.isArray(keys) ? keys.reduce((acc, k) => ({ ...acc, [k]: true }), {}) : keys;
|
|
@@ -497,18 +501,18 @@ function schemaToConvex(value, schema) {
|
|
|
497
501
|
if (codec) {
|
|
498
502
|
return codec.toConvex(value, schema);
|
|
499
503
|
}
|
|
500
|
-
if (schema instanceof
|
|
504
|
+
if (schema instanceof z.ZodOptional || schema instanceof z.ZodNullable || schema instanceof z.ZodDefault) {
|
|
501
505
|
const inner = schema.unwrap();
|
|
502
506
|
return schemaToConvex(value, asZodType2(inner));
|
|
503
507
|
}
|
|
504
|
-
if (schema instanceof
|
|
508
|
+
if (schema instanceof z.ZodDate && value instanceof Date) {
|
|
505
509
|
return value.getTime();
|
|
506
510
|
}
|
|
507
|
-
if (schema instanceof
|
|
511
|
+
if (schema instanceof z.ZodArray) {
|
|
508
512
|
if (!Array.isArray(value)) return value;
|
|
509
513
|
return value.map((item) => schemaToConvex(item, schema.element));
|
|
510
514
|
}
|
|
511
|
-
if (schema instanceof
|
|
515
|
+
if (schema instanceof z.ZodObject) {
|
|
512
516
|
if (!value || typeof value !== "object") return value;
|
|
513
517
|
const shape = schema.shape;
|
|
514
518
|
const result = {};
|
|
@@ -519,7 +523,7 @@ function schemaToConvex(value, schema) {
|
|
|
519
523
|
}
|
|
520
524
|
return result;
|
|
521
525
|
}
|
|
522
|
-
if (schema instanceof
|
|
526
|
+
if (schema instanceof z.ZodUnion) {
|
|
523
527
|
for (const option of schema.options) {
|
|
524
528
|
try {
|
|
525
529
|
;
|
|
@@ -529,7 +533,7 @@ function schemaToConvex(value, schema) {
|
|
|
529
533
|
}
|
|
530
534
|
}
|
|
531
535
|
}
|
|
532
|
-
if (schema instanceof
|
|
536
|
+
if (schema instanceof z.ZodRecord) {
|
|
533
537
|
if (!value || typeof value !== "object") return value;
|
|
534
538
|
const result = {};
|
|
535
539
|
for (const [k, v8] of Object.entries(value)) {
|
|
@@ -547,21 +551,21 @@ function fromConvexJS(value, schema) {
|
|
|
547
551
|
if (codec) {
|
|
548
552
|
return codec.fromConvex(value, schema);
|
|
549
553
|
}
|
|
550
|
-
if (schema instanceof
|
|
554
|
+
if (schema instanceof z.ZodOptional || schema instanceof z.ZodNullable || schema instanceof z.ZodDefault) {
|
|
551
555
|
const inner = schema.unwrap();
|
|
552
556
|
return fromConvexJS(value, asZodType2(inner));
|
|
553
557
|
}
|
|
554
|
-
if (schema instanceof
|
|
558
|
+
if (schema instanceof z.ZodDate && typeof value === "number") {
|
|
555
559
|
return new Date(value);
|
|
556
560
|
}
|
|
557
561
|
if (isDateSchema(schema) && typeof value === "number") {
|
|
558
562
|
return new Date(value);
|
|
559
563
|
}
|
|
560
|
-
if (schema instanceof
|
|
564
|
+
if (schema instanceof z.ZodArray) {
|
|
561
565
|
if (!Array.isArray(value)) return value;
|
|
562
566
|
return value.map((item) => fromConvexJS(item, schema.element));
|
|
563
567
|
}
|
|
564
|
-
if (schema instanceof
|
|
568
|
+
if (schema instanceof z.ZodObject) {
|
|
565
569
|
if (!value || typeof value !== "object") return value;
|
|
566
570
|
const shape = schema.shape;
|
|
567
571
|
const result = {};
|
|
@@ -570,7 +574,7 @@ function fromConvexJS(value, schema) {
|
|
|
570
574
|
}
|
|
571
575
|
return result;
|
|
572
576
|
}
|
|
573
|
-
if (schema instanceof
|
|
577
|
+
if (schema instanceof z.ZodUnion) {
|
|
574
578
|
for (const option of schema.options) {
|
|
575
579
|
try {
|
|
576
580
|
const decoded = fromConvexJS(value, option);
|
|
@@ -580,7 +584,7 @@ function fromConvexJS(value, schema) {
|
|
|
580
584
|
}
|
|
581
585
|
}
|
|
582
586
|
}
|
|
583
|
-
if (schema instanceof
|
|
587
|
+
if (schema instanceof z.ZodRecord) {
|
|
584
588
|
if (!value || typeof value !== "object") return value;
|
|
585
589
|
const result = {};
|
|
586
590
|
for (const [k, v8] of Object.entries(value)) {
|
|
@@ -588,7 +592,7 @@ function fromConvexJS(value, schema) {
|
|
|
588
592
|
}
|
|
589
593
|
return result;
|
|
590
594
|
}
|
|
591
|
-
if (schema instanceof
|
|
595
|
+
if (schema instanceof z.ZodTransform) {
|
|
592
596
|
return value;
|
|
593
597
|
}
|
|
594
598
|
return value;
|
|
@@ -617,32 +621,32 @@ function formatZodIssues(error, context) {
|
|
|
617
621
|
};
|
|
618
622
|
}
|
|
619
623
|
function handleZodValidationError(e, context) {
|
|
620
|
-
if (e instanceof
|
|
621
|
-
throw new
|
|
624
|
+
if (e instanceof z.ZodError) {
|
|
625
|
+
throw new ConvexError(formatZodIssues(e, context));
|
|
622
626
|
}
|
|
623
627
|
throw e;
|
|
624
628
|
}
|
|
625
629
|
function zPaginated(item) {
|
|
626
|
-
return
|
|
627
|
-
page:
|
|
628
|
-
isDone:
|
|
629
|
-
continueCursor:
|
|
630
|
+
return z.object({
|
|
631
|
+
page: z.array(item),
|
|
632
|
+
isDone: z.boolean(),
|
|
633
|
+
continueCursor: z.string().nullable().optional()
|
|
630
634
|
});
|
|
631
635
|
}
|
|
632
636
|
function mapDateFieldToNumber(field) {
|
|
633
|
-
if (field instanceof
|
|
634
|
-
return
|
|
637
|
+
if (field instanceof z.ZodDate) {
|
|
638
|
+
return z.number();
|
|
635
639
|
}
|
|
636
|
-
if (field instanceof
|
|
637
|
-
return
|
|
640
|
+
if (field instanceof z.ZodOptional && field.unwrap() instanceof z.ZodDate) {
|
|
641
|
+
return z.number().optional();
|
|
638
642
|
}
|
|
639
|
-
if (field instanceof
|
|
640
|
-
return
|
|
643
|
+
if (field instanceof z.ZodNullable && field.unwrap() instanceof z.ZodDate) {
|
|
644
|
+
return z.number().nullable();
|
|
641
645
|
}
|
|
642
|
-
if (field instanceof
|
|
646
|
+
if (field instanceof z.ZodDefault) {
|
|
643
647
|
const inner = field.removeDefault();
|
|
644
|
-
if (inner instanceof
|
|
645
|
-
return
|
|
648
|
+
if (inner instanceof z.ZodDate) {
|
|
649
|
+
return z.number().optional();
|
|
646
650
|
}
|
|
647
651
|
}
|
|
648
652
|
return field;
|
|
@@ -653,7 +657,7 @@ function toKeys(mask) {
|
|
|
653
657
|
}
|
|
654
658
|
function pickShape(schemaOrShape, mask) {
|
|
655
659
|
const keys = toKeys(mask);
|
|
656
|
-
const shape = schemaOrShape instanceof
|
|
660
|
+
const shape = schemaOrShape instanceof z.ZodObject ? getObjectShape(schemaOrShape) : schemaOrShape || {};
|
|
657
661
|
const out = {};
|
|
658
662
|
for (const k of keys) {
|
|
659
663
|
if (k in shape) out[k] = shape[k];
|
|
@@ -661,29 +665,29 @@ function pickShape(schemaOrShape, mask) {
|
|
|
661
665
|
return out;
|
|
662
666
|
}
|
|
663
667
|
function safePick(schema, mask) {
|
|
664
|
-
return
|
|
668
|
+
return z.object(pickShape(schema, mask));
|
|
665
669
|
}
|
|
666
670
|
function safeOmit(schema, mask) {
|
|
667
671
|
const shape = getObjectShape(schema);
|
|
668
672
|
const omit = new Set(toKeys(mask));
|
|
669
673
|
const keep = Object.keys(shape).filter((k) => !omit.has(k));
|
|
670
674
|
const picked = pickShape(schema, keep);
|
|
671
|
-
return
|
|
675
|
+
return z.object(picked);
|
|
672
676
|
}
|
|
673
677
|
|
|
674
678
|
// src/custom.ts
|
|
675
679
|
function customFnBuilder(builder, customization) {
|
|
676
|
-
const customInput = customization.input ??
|
|
677
|
-
const inputArgs = customization.args ??
|
|
680
|
+
const customInput = customization.input ?? NoOp.input;
|
|
681
|
+
const inputArgs = customization.args ?? NoOp.args;
|
|
678
682
|
return function customBuilder(fn) {
|
|
679
683
|
const { args, handler = fn, returns: maybeObject, ...extra } = fn;
|
|
680
|
-
const returns = maybeObject && !(maybeObject instanceof
|
|
684
|
+
const returns = maybeObject && !(maybeObject instanceof z.ZodType) ? z.object(maybeObject) : maybeObject;
|
|
681
685
|
const returnValidator = returns && !fn.skipConvexValidation ? { returns: zodToConvex(returns) } : void 0;
|
|
682
686
|
if (args && !fn.skipConvexValidation) {
|
|
683
687
|
let argsValidator = args;
|
|
684
688
|
let argsSchema;
|
|
685
|
-
if (argsValidator instanceof
|
|
686
|
-
if (argsValidator instanceof
|
|
689
|
+
if (argsValidator instanceof z.ZodType) {
|
|
690
|
+
if (argsValidator instanceof z.ZodObject) {
|
|
687
691
|
argsSchema = argsValidator;
|
|
688
692
|
argsValidator = argsValidator.shape;
|
|
689
693
|
} else {
|
|
@@ -692,7 +696,7 @@ function customFnBuilder(builder, customization) {
|
|
|
692
696
|
);
|
|
693
697
|
}
|
|
694
698
|
} else {
|
|
695
|
-
argsSchema =
|
|
699
|
+
argsSchema = z.object(argsValidator);
|
|
696
700
|
}
|
|
697
701
|
const convexValidator = zodToConvexFields(argsValidator);
|
|
698
702
|
return builder({
|
|
@@ -800,15 +804,15 @@ function containsCustom(schema, maxDepth = 50, currentDepth = 0) {
|
|
|
800
804
|
let result = false;
|
|
801
805
|
if (schema._def?.typeName === "ZodCustom") {
|
|
802
806
|
result = true;
|
|
803
|
-
} else if (schema instanceof
|
|
807
|
+
} else if (schema instanceof z.ZodUnion) {
|
|
804
808
|
result = schema.options.some(
|
|
805
809
|
(opt) => containsCustom(opt, maxDepth, currentDepth + 1)
|
|
806
810
|
);
|
|
807
|
-
} else if (schema instanceof
|
|
811
|
+
} else if (schema instanceof z.ZodOptional) {
|
|
808
812
|
result = containsCustom(schema.unwrap(), maxDepth, currentDepth + 1);
|
|
809
|
-
} else if (schema instanceof
|
|
813
|
+
} else if (schema instanceof z.ZodNullable) {
|
|
810
814
|
result = containsCustom(schema.unwrap(), maxDepth, currentDepth + 1);
|
|
811
|
-
} else if (schema instanceof
|
|
815
|
+
} else if (schema instanceof z.ZodDefault) {
|
|
812
816
|
result = containsCustom(schema.removeDefault(), maxDepth, currentDepth + 1);
|
|
813
817
|
}
|
|
814
818
|
customCheckCache.set(schema, result);
|
|
@@ -817,15 +821,15 @@ function containsCustom(schema, maxDepth = 50, currentDepth = 0) {
|
|
|
817
821
|
function zQuery(query, input, handler, options) {
|
|
818
822
|
let zodSchema;
|
|
819
823
|
let args;
|
|
820
|
-
if (input instanceof
|
|
824
|
+
if (input instanceof z.ZodObject) {
|
|
821
825
|
const zodObj = input;
|
|
822
826
|
zodSchema = zodObj;
|
|
823
827
|
args = zodToConvexFields(getObjectShape(zodObj));
|
|
824
|
-
} else if (input instanceof
|
|
825
|
-
zodSchema =
|
|
828
|
+
} else if (input instanceof z.ZodType) {
|
|
829
|
+
zodSchema = z.object({ value: input });
|
|
826
830
|
args = { value: zodToConvex(input) };
|
|
827
831
|
} else {
|
|
828
|
-
zodSchema =
|
|
832
|
+
zodSchema = z.object(input);
|
|
829
833
|
args = zodToConvexFields(input);
|
|
830
834
|
}
|
|
831
835
|
const returns = options?.returns && !containsCustom(options.returns) ? zodToConvex(options.returns) : void 0;
|
|
@@ -856,15 +860,15 @@ function zQuery(query, input, handler, options) {
|
|
|
856
860
|
function zMutation(mutation, input, handler, options) {
|
|
857
861
|
let zodSchema;
|
|
858
862
|
let args;
|
|
859
|
-
if (input instanceof
|
|
863
|
+
if (input instanceof z.ZodObject) {
|
|
860
864
|
const zodObj = input;
|
|
861
865
|
zodSchema = zodObj;
|
|
862
866
|
args = zodToConvexFields(getObjectShape(zodObj));
|
|
863
|
-
} else if (input instanceof
|
|
864
|
-
zodSchema =
|
|
867
|
+
} else if (input instanceof z.ZodType) {
|
|
868
|
+
zodSchema = z.object({ value: input });
|
|
865
869
|
args = { value: zodToConvex(input) };
|
|
866
870
|
} else {
|
|
867
|
-
zodSchema =
|
|
871
|
+
zodSchema = z.object(input);
|
|
868
872
|
args = zodToConvexFields(input);
|
|
869
873
|
}
|
|
870
874
|
const returns = options?.returns && !containsCustom(options.returns) ? zodToConvex(options.returns) : void 0;
|
|
@@ -895,15 +899,15 @@ function zMutation(mutation, input, handler, options) {
|
|
|
895
899
|
function zAction(action, input, handler, options) {
|
|
896
900
|
let zodSchema;
|
|
897
901
|
let args;
|
|
898
|
-
if (input instanceof
|
|
902
|
+
if (input instanceof z.ZodObject) {
|
|
899
903
|
const zodObj = input;
|
|
900
904
|
zodSchema = zodObj;
|
|
901
905
|
args = zodToConvexFields(getObjectShape(zodObj));
|
|
902
|
-
} else if (input instanceof
|
|
903
|
-
zodSchema =
|
|
906
|
+
} else if (input instanceof z.ZodType) {
|
|
907
|
+
zodSchema = z.object({ value: input });
|
|
904
908
|
args = { value: zodToConvex(input) };
|
|
905
909
|
} else {
|
|
906
|
-
zodSchema =
|
|
910
|
+
zodSchema = z.object(input);
|
|
907
911
|
args = zodToConvexFields(input);
|
|
908
912
|
}
|
|
909
913
|
const returns = options?.returns && !containsCustom(options.returns) ? zodToConvex(options.returns) : void 0;
|
|
@@ -975,17 +979,17 @@ function zCustomActionBuilder(action, customization) {
|
|
|
975
979
|
function zodDoc(tableName, schema) {
|
|
976
980
|
return schema.extend({
|
|
977
981
|
_id: zid(tableName),
|
|
978
|
-
_creationTime:
|
|
982
|
+
_creationTime: z.number()
|
|
979
983
|
});
|
|
980
984
|
}
|
|
981
985
|
function zodDocOrNull(tableName, schema) {
|
|
982
|
-
return
|
|
986
|
+
return z.union([zodDoc(tableName, schema), z.null()]);
|
|
983
987
|
}
|
|
984
988
|
function zodTable(name, shape) {
|
|
985
989
|
const convexFields = zodToConvexFields(shape);
|
|
986
|
-
const table =
|
|
987
|
-
const zDoc = zodDoc(name,
|
|
988
|
-
const docArray =
|
|
990
|
+
const table = Table(name, convexFields);
|
|
991
|
+
const zDoc = zodDoc(name, z.object(shape));
|
|
992
|
+
const docArray = z.array(zDoc);
|
|
989
993
|
return Object.assign(table, {
|
|
990
994
|
shape,
|
|
991
995
|
zDoc,
|
|
@@ -993,43 +997,6 @@ function zodTable(name, shape) {
|
|
|
993
997
|
});
|
|
994
998
|
}
|
|
995
999
|
|
|
996
|
-
|
|
997
|
-
enumerable: true,
|
|
998
|
-
get: function () { return customFunctions.customCtx; }
|
|
999
|
-
});
|
|
1000
|
-
exports.convexCodec = convexCodec;
|
|
1001
|
-
exports.customFnBuilder = customFnBuilder;
|
|
1002
|
-
exports.findBaseCodec = findBaseCodec;
|
|
1003
|
-
exports.formatZodIssues = formatZodIssues;
|
|
1004
|
-
exports.fromConvexJS = fromConvexJS;
|
|
1005
|
-
exports.getObjectShape = getObjectShape;
|
|
1006
|
-
exports.handleZodValidationError = handleZodValidationError;
|
|
1007
|
-
exports.isDateSchema = isDateSchema;
|
|
1008
|
-
exports.makeUnion = makeUnion;
|
|
1009
|
-
exports.mapDateFieldToNumber = mapDateFieldToNumber;
|
|
1010
|
-
exports.pick = pick;
|
|
1011
|
-
exports.pickShape = pickShape;
|
|
1012
|
-
exports.registerBaseCodec = registerBaseCodec;
|
|
1013
|
-
exports.registryHelpers = registryHelpers;
|
|
1014
|
-
exports.returnsAs = returnsAs;
|
|
1015
|
-
exports.safeOmit = safeOmit;
|
|
1016
|
-
exports.safePick = safePick;
|
|
1017
|
-
exports.toConvexJS = toConvexJS;
|
|
1018
|
-
exports.zActionBuilder = zActionBuilder;
|
|
1019
|
-
exports.zCustomAction = zCustomAction;
|
|
1020
|
-
exports.zCustomActionBuilder = zCustomActionBuilder;
|
|
1021
|
-
exports.zCustomMutation = zCustomMutation;
|
|
1022
|
-
exports.zCustomMutationBuilder = zCustomMutationBuilder;
|
|
1023
|
-
exports.zCustomQuery = zCustomQuery;
|
|
1024
|
-
exports.zCustomQueryBuilder = zCustomQueryBuilder;
|
|
1025
|
-
exports.zMutationBuilder = zMutationBuilder;
|
|
1026
|
-
exports.zPaginated = zPaginated;
|
|
1027
|
-
exports.zQueryBuilder = zQueryBuilder;
|
|
1028
|
-
exports.zid = zid;
|
|
1029
|
-
exports.zodDoc = zodDoc;
|
|
1030
|
-
exports.zodDocOrNull = zodDocOrNull;
|
|
1031
|
-
exports.zodTable = zodTable;
|
|
1032
|
-
exports.zodToConvex = zodToConvex;
|
|
1033
|
-
exports.zodToConvexFields = zodToConvexFields;
|
|
1000
|
+
export { convexCodec, customFnBuilder, findBaseCodec, formatZodIssues, fromConvexJS, getObjectShape, handleZodValidationError, isDateSchema, makeUnion, mapDateFieldToNumber, pick, pickShape, registerBaseCodec, registryHelpers, returnsAs, safeOmit, safePick, toConvexJS, zActionBuilder, zCustomAction, zCustomActionBuilder, zCustomMutation, zCustomMutationBuilder, zCustomQuery, zCustomQueryBuilder, zMutationBuilder, zPaginated, zQueryBuilder, zid, zodDoc, zodDocOrNull, zodTable, zodToConvex, zodToConvexFields };
|
|
1034
1001
|
//# sourceMappingURL=index.js.map
|
|
1035
1002
|
//# sourceMappingURL=index.js.map
|