zodvex 0.2.4 → 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 +155 -192
- package/dist/index.js.map +1 -1
- package/package.json +10 -9
- package/dist/index.d.mts +0 -489
- package/dist/index.mjs +0 -1001
- 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) {
|
|
@@ -151,16 +151,16 @@ function convertDiscriminatedUnionType(actualValidator, visited, zodToConvexInte
|
|
|
151
151
|
return zodToConvexInternal2(opt, branchVisited);
|
|
152
152
|
});
|
|
153
153
|
const [first, second, ...rest] = convexOptions;
|
|
154
|
-
return
|
|
154
|
+
return v.union(
|
|
155
155
|
first,
|
|
156
156
|
second,
|
|
157
157
|
...rest
|
|
158
158
|
);
|
|
159
159
|
} else {
|
|
160
|
-
return
|
|
160
|
+
return v.any();
|
|
161
161
|
}
|
|
162
162
|
} else {
|
|
163
|
-
return
|
|
163
|
+
return v.any();
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
function convertUnionType(actualValidator, visited, zodToConvexInternal2) {
|
|
@@ -175,17 +175,17 @@ function convertUnionType(actualValidator, visited, zodToConvexInternal2) {
|
|
|
175
175
|
});
|
|
176
176
|
if (convexOptions.length >= 2) {
|
|
177
177
|
const [first, second, ...rest] = convexOptions;
|
|
178
|
-
return
|
|
178
|
+
return v.union(
|
|
179
179
|
first,
|
|
180
180
|
second,
|
|
181
181
|
...rest
|
|
182
182
|
);
|
|
183
183
|
} else {
|
|
184
|
-
return
|
|
184
|
+
return v.any();
|
|
185
185
|
}
|
|
186
186
|
}
|
|
187
187
|
} else {
|
|
188
|
-
return
|
|
188
|
+
return v.any();
|
|
189
189
|
}
|
|
190
190
|
}
|
|
191
191
|
function isZid(schema) {
|
|
@@ -194,12 +194,12 @@ function isZid(schema) {
|
|
|
194
194
|
}
|
|
195
195
|
function makeUnion(members) {
|
|
196
196
|
const nonNull = members.filter(Boolean);
|
|
197
|
-
if (nonNull.length === 0) return
|
|
197
|
+
if (nonNull.length === 0) return v.any();
|
|
198
198
|
if (nonNull.length === 1) return nonNull[0];
|
|
199
|
-
return
|
|
199
|
+
return v.union(nonNull[0], nonNull[1], ...nonNull.slice(2));
|
|
200
200
|
}
|
|
201
201
|
function getObjectShape(obj) {
|
|
202
|
-
if (obj instanceof
|
|
202
|
+
if (obj instanceof z.ZodObject) {
|
|
203
203
|
return obj.shape;
|
|
204
204
|
}
|
|
205
205
|
if (obj && typeof obj === "object" && typeof obj.shape === "object") {
|
|
@@ -211,25 +211,25 @@ function getObjectShape(obj) {
|
|
|
211
211
|
// src/mapping/core.ts
|
|
212
212
|
function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set()) {
|
|
213
213
|
if (!zodValidator) {
|
|
214
|
-
return
|
|
214
|
+
return v.any();
|
|
215
215
|
}
|
|
216
216
|
if (visited.has(zodValidator)) {
|
|
217
|
-
return
|
|
217
|
+
return v.any();
|
|
218
218
|
}
|
|
219
219
|
visited.add(zodValidator);
|
|
220
220
|
let actualValidator = zodValidator;
|
|
221
221
|
let isOptional = false;
|
|
222
222
|
let defaultValue = void 0;
|
|
223
223
|
let hasDefault = false;
|
|
224
|
-
if (zodValidator instanceof
|
|
224
|
+
if (zodValidator instanceof z.ZodDefault) {
|
|
225
225
|
hasDefault = true;
|
|
226
226
|
defaultValue = zodValidator.def?.defaultValue;
|
|
227
227
|
actualValidator = zodValidator.def?.innerType;
|
|
228
228
|
}
|
|
229
|
-
if (actualValidator instanceof
|
|
229
|
+
if (actualValidator instanceof z.ZodOptional) {
|
|
230
230
|
isOptional = true;
|
|
231
231
|
actualValidator = actualValidator.unwrap();
|
|
232
|
-
if (actualValidator instanceof
|
|
232
|
+
if (actualValidator instanceof z.ZodDefault) {
|
|
233
233
|
hasDefault = true;
|
|
234
234
|
defaultValue = actualValidator.def?.defaultValue;
|
|
235
235
|
actualValidator = actualValidator.def?.innerType;
|
|
@@ -239,64 +239,64 @@ function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set())
|
|
|
239
239
|
if (isZid(actualValidator)) {
|
|
240
240
|
const metadata2 = registryHelpers.getMetadata(actualValidator);
|
|
241
241
|
const tableName = metadata2?.tableName || "unknown";
|
|
242
|
-
convexValidator =
|
|
242
|
+
convexValidator = v.id(tableName);
|
|
243
243
|
} else {
|
|
244
244
|
const defType = actualValidator.def?.type;
|
|
245
245
|
switch (defType) {
|
|
246
246
|
case "string":
|
|
247
|
-
convexValidator =
|
|
247
|
+
convexValidator = v.string();
|
|
248
248
|
break;
|
|
249
249
|
case "number":
|
|
250
|
-
convexValidator =
|
|
250
|
+
convexValidator = v.float64();
|
|
251
251
|
break;
|
|
252
252
|
case "bigint":
|
|
253
|
-
convexValidator =
|
|
253
|
+
convexValidator = v.int64();
|
|
254
254
|
break;
|
|
255
255
|
case "boolean":
|
|
256
|
-
convexValidator =
|
|
256
|
+
convexValidator = v.boolean();
|
|
257
257
|
break;
|
|
258
258
|
case "date":
|
|
259
|
-
convexValidator =
|
|
259
|
+
convexValidator = v.float64();
|
|
260
260
|
break;
|
|
261
261
|
case "null":
|
|
262
|
-
convexValidator =
|
|
262
|
+
convexValidator = v.null();
|
|
263
263
|
break;
|
|
264
264
|
case "nan":
|
|
265
|
-
convexValidator =
|
|
265
|
+
convexValidator = v.float64();
|
|
266
266
|
break;
|
|
267
267
|
case "array": {
|
|
268
|
-
if (actualValidator instanceof
|
|
268
|
+
if (actualValidator instanceof z.ZodArray) {
|
|
269
269
|
const element = actualValidator.element;
|
|
270
|
-
if (element && element instanceof
|
|
271
|
-
convexValidator =
|
|
270
|
+
if (element && element instanceof z.ZodType) {
|
|
271
|
+
convexValidator = v.array(zodToConvexInternal(element, visited));
|
|
272
272
|
} else {
|
|
273
|
-
convexValidator =
|
|
273
|
+
convexValidator = v.array(v.any());
|
|
274
274
|
}
|
|
275
275
|
} else {
|
|
276
|
-
convexValidator =
|
|
276
|
+
convexValidator = v.array(v.any());
|
|
277
277
|
}
|
|
278
278
|
break;
|
|
279
279
|
}
|
|
280
280
|
case "object": {
|
|
281
|
-
if (actualValidator instanceof
|
|
281
|
+
if (actualValidator instanceof z.ZodObject) {
|
|
282
282
|
const shape = actualValidator.shape;
|
|
283
283
|
const convexShape = {};
|
|
284
284
|
for (const [key, value] of Object.entries(shape)) {
|
|
285
|
-
if (value && value instanceof
|
|
285
|
+
if (value && value instanceof z.ZodType) {
|
|
286
286
|
convexShape[key] = zodToConvexInternal(value, visited);
|
|
287
287
|
}
|
|
288
288
|
}
|
|
289
|
-
convexValidator =
|
|
289
|
+
convexValidator = v.object(convexShape);
|
|
290
290
|
} else {
|
|
291
|
-
convexValidator =
|
|
291
|
+
convexValidator = v.object({});
|
|
292
292
|
}
|
|
293
293
|
break;
|
|
294
294
|
}
|
|
295
295
|
case "union": {
|
|
296
|
-
if (actualValidator instanceof
|
|
296
|
+
if (actualValidator instanceof z.ZodUnion) {
|
|
297
297
|
convexValidator = convertUnionType(actualValidator, visited, zodToConvexInternal);
|
|
298
298
|
} else {
|
|
299
|
-
convexValidator =
|
|
299
|
+
convexValidator = v.any();
|
|
300
300
|
}
|
|
301
301
|
break;
|
|
302
302
|
}
|
|
@@ -309,31 +309,31 @@ function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set())
|
|
|
309
309
|
break;
|
|
310
310
|
}
|
|
311
311
|
case "literal": {
|
|
312
|
-
if (actualValidator instanceof
|
|
312
|
+
if (actualValidator instanceof z.ZodLiteral) {
|
|
313
313
|
const literalValue = actualValidator.value;
|
|
314
314
|
if (literalValue !== void 0 && literalValue !== null) {
|
|
315
|
-
convexValidator =
|
|
315
|
+
convexValidator = v.literal(literalValue);
|
|
316
316
|
} else {
|
|
317
|
-
convexValidator =
|
|
317
|
+
convexValidator = v.any();
|
|
318
318
|
}
|
|
319
319
|
} else {
|
|
320
|
-
convexValidator =
|
|
320
|
+
convexValidator = v.any();
|
|
321
321
|
}
|
|
322
322
|
break;
|
|
323
323
|
}
|
|
324
324
|
case "enum": {
|
|
325
|
-
if (actualValidator instanceof
|
|
325
|
+
if (actualValidator instanceof z.ZodEnum) {
|
|
326
326
|
convexValidator = convertEnumType(actualValidator);
|
|
327
327
|
} else {
|
|
328
|
-
convexValidator =
|
|
328
|
+
convexValidator = v.any();
|
|
329
329
|
}
|
|
330
330
|
break;
|
|
331
331
|
}
|
|
332
332
|
case "record": {
|
|
333
|
-
if (actualValidator instanceof
|
|
333
|
+
if (actualValidator instanceof z.ZodRecord) {
|
|
334
334
|
convexValidator = convertRecordType(actualValidator, visited, zodToConvexInternal);
|
|
335
335
|
} else {
|
|
336
|
-
convexValidator =
|
|
336
|
+
convexValidator = v.record(v.string(), v.any());
|
|
337
337
|
}
|
|
338
338
|
break;
|
|
339
339
|
}
|
|
@@ -347,75 +347,75 @@ function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set())
|
|
|
347
347
|
if (metadata2?.brand && metadata2?.originalSchema) {
|
|
348
348
|
convexValidator = zodToConvexInternal(metadata2.originalSchema, visited);
|
|
349
349
|
} else {
|
|
350
|
-
convexValidator =
|
|
350
|
+
convexValidator = v.any();
|
|
351
351
|
}
|
|
352
352
|
}
|
|
353
353
|
break;
|
|
354
354
|
}
|
|
355
355
|
case "nullable": {
|
|
356
|
-
if (actualValidator instanceof
|
|
356
|
+
if (actualValidator instanceof z.ZodNullable) {
|
|
357
357
|
const result = convertNullableType(actualValidator, visited, zodToConvexInternal);
|
|
358
358
|
convexValidator = result.validator;
|
|
359
359
|
if (result.isOptional) {
|
|
360
360
|
isOptional = true;
|
|
361
361
|
}
|
|
362
362
|
} else {
|
|
363
|
-
convexValidator =
|
|
363
|
+
convexValidator = v.any();
|
|
364
364
|
}
|
|
365
365
|
break;
|
|
366
366
|
}
|
|
367
367
|
case "tuple": {
|
|
368
|
-
if (actualValidator instanceof
|
|
368
|
+
if (actualValidator instanceof z.ZodTuple) {
|
|
369
369
|
const items = actualValidator.def?.items;
|
|
370
370
|
if (items && items.length > 0) {
|
|
371
371
|
const convexShape = {};
|
|
372
372
|
items.forEach((item, index) => {
|
|
373
373
|
convexShape[`_${index}`] = zodToConvexInternal(item, visited);
|
|
374
374
|
});
|
|
375
|
-
convexValidator =
|
|
375
|
+
convexValidator = v.object(convexShape);
|
|
376
376
|
} else {
|
|
377
|
-
convexValidator =
|
|
377
|
+
convexValidator = v.object({});
|
|
378
378
|
}
|
|
379
379
|
} else {
|
|
380
|
-
convexValidator =
|
|
380
|
+
convexValidator = v.object({});
|
|
381
381
|
}
|
|
382
382
|
break;
|
|
383
383
|
}
|
|
384
384
|
case "lazy": {
|
|
385
|
-
if (actualValidator instanceof
|
|
385
|
+
if (actualValidator instanceof z.ZodLazy) {
|
|
386
386
|
try {
|
|
387
387
|
const getter = actualValidator.def?.getter;
|
|
388
388
|
if (getter) {
|
|
389
389
|
const resolvedSchema = getter();
|
|
390
|
-
if (resolvedSchema && resolvedSchema instanceof
|
|
390
|
+
if (resolvedSchema && resolvedSchema instanceof z.ZodType) {
|
|
391
391
|
convexValidator = zodToConvexInternal(resolvedSchema, visited);
|
|
392
392
|
} else {
|
|
393
|
-
convexValidator =
|
|
393
|
+
convexValidator = v.any();
|
|
394
394
|
}
|
|
395
395
|
} else {
|
|
396
|
-
convexValidator =
|
|
396
|
+
convexValidator = v.any();
|
|
397
397
|
}
|
|
398
398
|
} catch {
|
|
399
|
-
convexValidator =
|
|
399
|
+
convexValidator = v.any();
|
|
400
400
|
}
|
|
401
401
|
} else {
|
|
402
|
-
convexValidator =
|
|
402
|
+
convexValidator = v.any();
|
|
403
403
|
}
|
|
404
404
|
break;
|
|
405
405
|
}
|
|
406
406
|
case "any":
|
|
407
|
-
convexValidator =
|
|
407
|
+
convexValidator = v.any();
|
|
408
408
|
break;
|
|
409
409
|
case "unknown":
|
|
410
|
-
convexValidator =
|
|
410
|
+
convexValidator = v.any();
|
|
411
411
|
break;
|
|
412
412
|
case "undefined":
|
|
413
413
|
case "void":
|
|
414
414
|
case "never":
|
|
415
|
-
convexValidator =
|
|
415
|
+
convexValidator = v.any();
|
|
416
416
|
break;
|
|
417
417
|
case "intersection":
|
|
418
|
-
convexValidator =
|
|
418
|
+
convexValidator = v.any();
|
|
419
419
|
break;
|
|
420
420
|
default:
|
|
421
421
|
if (process.env.NODE_ENV !== "production") {
|
|
@@ -425,24 +425,24 @@ function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set())
|
|
|
425
425
|
actualValidator
|
|
426
426
|
);
|
|
427
427
|
}
|
|
428
|
-
convexValidator =
|
|
428
|
+
convexValidator = v.any();
|
|
429
429
|
break;
|
|
430
430
|
}
|
|
431
431
|
}
|
|
432
|
-
const finalValidator = isOptional || hasDefault ?
|
|
432
|
+
const finalValidator = isOptional || hasDefault ? v.optional(convexValidator) : convexValidator;
|
|
433
433
|
if (hasDefault && typeof finalValidator === "object" && finalValidator !== null) {
|
|
434
434
|
finalValidator._zodDefault = defaultValue;
|
|
435
435
|
}
|
|
436
436
|
return finalValidator;
|
|
437
437
|
}
|
|
438
|
-
function zodToConvex(zod
|
|
439
|
-
if (typeof zod
|
|
440
|
-
return zodToConvexFields(zod
|
|
438
|
+
function zodToConvex(zod) {
|
|
439
|
+
if (typeof zod === "object" && zod !== null && !(zod instanceof z.ZodType)) {
|
|
440
|
+
return zodToConvexFields(zod);
|
|
441
441
|
}
|
|
442
|
-
return zodToConvexInternal(zod
|
|
442
|
+
return zodToConvexInternal(zod);
|
|
443
443
|
}
|
|
444
|
-
function zodToConvexFields(zod
|
|
445
|
-
const fields = zod
|
|
444
|
+
function zodToConvexFields(zod) {
|
|
445
|
+
const fields = zod instanceof z.ZodObject ? zod.shape : zod;
|
|
446
446
|
const result = {};
|
|
447
447
|
for (const [key, value] of Object.entries(fields)) {
|
|
448
448
|
result[key] = zodToConvexInternal(value);
|
|
@@ -461,7 +461,7 @@ function convexCodec(schema) {
|
|
|
461
461
|
encode: (value) => toConvexJS(schema, value),
|
|
462
462
|
decode: (value) => fromConvexJS(value, schema),
|
|
463
463
|
pick: (keys) => {
|
|
464
|
-
if (!(schema instanceof
|
|
464
|
+
if (!(schema instanceof z.ZodObject)) {
|
|
465
465
|
throw new Error("pick() can only be called on object schemas");
|
|
466
466
|
}
|
|
467
467
|
const pickObj = Array.isArray(keys) ? keys.reduce((acc, k) => ({ ...acc, [k]: true }), {}) : keys;
|
|
@@ -501,18 +501,18 @@ function schemaToConvex(value, schema) {
|
|
|
501
501
|
if (codec) {
|
|
502
502
|
return codec.toConvex(value, schema);
|
|
503
503
|
}
|
|
504
|
-
if (schema instanceof
|
|
504
|
+
if (schema instanceof z.ZodOptional || schema instanceof z.ZodNullable || schema instanceof z.ZodDefault) {
|
|
505
505
|
const inner = schema.unwrap();
|
|
506
506
|
return schemaToConvex(value, asZodType2(inner));
|
|
507
507
|
}
|
|
508
|
-
if (schema instanceof
|
|
508
|
+
if (schema instanceof z.ZodDate && value instanceof Date) {
|
|
509
509
|
return value.getTime();
|
|
510
510
|
}
|
|
511
|
-
if (schema instanceof
|
|
511
|
+
if (schema instanceof z.ZodArray) {
|
|
512
512
|
if (!Array.isArray(value)) return value;
|
|
513
513
|
return value.map((item) => schemaToConvex(item, schema.element));
|
|
514
514
|
}
|
|
515
|
-
if (schema instanceof
|
|
515
|
+
if (schema instanceof z.ZodObject) {
|
|
516
516
|
if (!value || typeof value !== "object") return value;
|
|
517
517
|
const shape = schema.shape;
|
|
518
518
|
const result = {};
|
|
@@ -523,7 +523,7 @@ function schemaToConvex(value, schema) {
|
|
|
523
523
|
}
|
|
524
524
|
return result;
|
|
525
525
|
}
|
|
526
|
-
if (schema instanceof
|
|
526
|
+
if (schema instanceof z.ZodUnion) {
|
|
527
527
|
for (const option of schema.options) {
|
|
528
528
|
try {
|
|
529
529
|
;
|
|
@@ -533,7 +533,7 @@ function schemaToConvex(value, schema) {
|
|
|
533
533
|
}
|
|
534
534
|
}
|
|
535
535
|
}
|
|
536
|
-
if (schema instanceof
|
|
536
|
+
if (schema instanceof z.ZodRecord) {
|
|
537
537
|
if (!value || typeof value !== "object") return value;
|
|
538
538
|
const result = {};
|
|
539
539
|
for (const [k, v8] of Object.entries(value)) {
|
|
@@ -551,21 +551,21 @@ function fromConvexJS(value, schema) {
|
|
|
551
551
|
if (codec) {
|
|
552
552
|
return codec.fromConvex(value, schema);
|
|
553
553
|
}
|
|
554
|
-
if (schema instanceof
|
|
554
|
+
if (schema instanceof z.ZodOptional || schema instanceof z.ZodNullable || schema instanceof z.ZodDefault) {
|
|
555
555
|
const inner = schema.unwrap();
|
|
556
556
|
return fromConvexJS(value, asZodType2(inner));
|
|
557
557
|
}
|
|
558
|
-
if (schema instanceof
|
|
558
|
+
if (schema instanceof z.ZodDate && typeof value === "number") {
|
|
559
559
|
return new Date(value);
|
|
560
560
|
}
|
|
561
561
|
if (isDateSchema(schema) && typeof value === "number") {
|
|
562
562
|
return new Date(value);
|
|
563
563
|
}
|
|
564
|
-
if (schema instanceof
|
|
564
|
+
if (schema instanceof z.ZodArray) {
|
|
565
565
|
if (!Array.isArray(value)) return value;
|
|
566
566
|
return value.map((item) => fromConvexJS(item, schema.element));
|
|
567
567
|
}
|
|
568
|
-
if (schema instanceof
|
|
568
|
+
if (schema instanceof z.ZodObject) {
|
|
569
569
|
if (!value || typeof value !== "object") return value;
|
|
570
570
|
const shape = schema.shape;
|
|
571
571
|
const result = {};
|
|
@@ -574,7 +574,7 @@ function fromConvexJS(value, schema) {
|
|
|
574
574
|
}
|
|
575
575
|
return result;
|
|
576
576
|
}
|
|
577
|
-
if (schema instanceof
|
|
577
|
+
if (schema instanceof z.ZodUnion) {
|
|
578
578
|
for (const option of schema.options) {
|
|
579
579
|
try {
|
|
580
580
|
const decoded = fromConvexJS(value, option);
|
|
@@ -584,7 +584,7 @@ function fromConvexJS(value, schema) {
|
|
|
584
584
|
}
|
|
585
585
|
}
|
|
586
586
|
}
|
|
587
|
-
if (schema instanceof
|
|
587
|
+
if (schema instanceof z.ZodRecord) {
|
|
588
588
|
if (!value || typeof value !== "object") return value;
|
|
589
589
|
const result = {};
|
|
590
590
|
for (const [k, v8] of Object.entries(value)) {
|
|
@@ -592,7 +592,7 @@ function fromConvexJS(value, schema) {
|
|
|
592
592
|
}
|
|
593
593
|
return result;
|
|
594
594
|
}
|
|
595
|
-
if (schema instanceof
|
|
595
|
+
if (schema instanceof z.ZodTransform) {
|
|
596
596
|
return value;
|
|
597
597
|
}
|
|
598
598
|
return value;
|
|
@@ -621,32 +621,32 @@ function formatZodIssues(error, context) {
|
|
|
621
621
|
};
|
|
622
622
|
}
|
|
623
623
|
function handleZodValidationError(e, context) {
|
|
624
|
-
if (e instanceof
|
|
625
|
-
throw new
|
|
624
|
+
if (e instanceof z.ZodError) {
|
|
625
|
+
throw new ConvexError(formatZodIssues(e, context));
|
|
626
626
|
}
|
|
627
627
|
throw e;
|
|
628
628
|
}
|
|
629
629
|
function zPaginated(item) {
|
|
630
|
-
return
|
|
631
|
-
page:
|
|
632
|
-
isDone:
|
|
633
|
-
continueCursor:
|
|
630
|
+
return z.object({
|
|
631
|
+
page: z.array(item),
|
|
632
|
+
isDone: z.boolean(),
|
|
633
|
+
continueCursor: z.string().nullable().optional()
|
|
634
634
|
});
|
|
635
635
|
}
|
|
636
636
|
function mapDateFieldToNumber(field) {
|
|
637
|
-
if (field instanceof
|
|
638
|
-
return
|
|
637
|
+
if (field instanceof z.ZodDate) {
|
|
638
|
+
return z.number();
|
|
639
639
|
}
|
|
640
|
-
if (field instanceof
|
|
641
|
-
return
|
|
640
|
+
if (field instanceof z.ZodOptional && field.unwrap() instanceof z.ZodDate) {
|
|
641
|
+
return z.number().optional();
|
|
642
642
|
}
|
|
643
|
-
if (field instanceof
|
|
644
|
-
return
|
|
643
|
+
if (field instanceof z.ZodNullable && field.unwrap() instanceof z.ZodDate) {
|
|
644
|
+
return z.number().nullable();
|
|
645
645
|
}
|
|
646
|
-
if (field instanceof
|
|
646
|
+
if (field instanceof z.ZodDefault) {
|
|
647
647
|
const inner = field.removeDefault();
|
|
648
|
-
if (inner instanceof
|
|
649
|
-
return
|
|
648
|
+
if (inner instanceof z.ZodDate) {
|
|
649
|
+
return z.number().optional();
|
|
650
650
|
}
|
|
651
651
|
}
|
|
652
652
|
return field;
|
|
@@ -657,7 +657,7 @@ function toKeys(mask) {
|
|
|
657
657
|
}
|
|
658
658
|
function pickShape(schemaOrShape, mask) {
|
|
659
659
|
const keys = toKeys(mask);
|
|
660
|
-
const shape = schemaOrShape instanceof
|
|
660
|
+
const shape = schemaOrShape instanceof z.ZodObject ? getObjectShape(schemaOrShape) : schemaOrShape || {};
|
|
661
661
|
const out = {};
|
|
662
662
|
for (const k of keys) {
|
|
663
663
|
if (k in shape) out[k] = shape[k];
|
|
@@ -665,29 +665,29 @@ function pickShape(schemaOrShape, mask) {
|
|
|
665
665
|
return out;
|
|
666
666
|
}
|
|
667
667
|
function safePick(schema, mask) {
|
|
668
|
-
return
|
|
668
|
+
return z.object(pickShape(schema, mask));
|
|
669
669
|
}
|
|
670
670
|
function safeOmit(schema, mask) {
|
|
671
671
|
const shape = getObjectShape(schema);
|
|
672
672
|
const omit = new Set(toKeys(mask));
|
|
673
673
|
const keep = Object.keys(shape).filter((k) => !omit.has(k));
|
|
674
674
|
const picked = pickShape(schema, keep);
|
|
675
|
-
return
|
|
675
|
+
return z.object(picked);
|
|
676
676
|
}
|
|
677
677
|
|
|
678
678
|
// src/custom.ts
|
|
679
679
|
function customFnBuilder(builder, customization) {
|
|
680
|
-
const customInput = customization.input ??
|
|
681
|
-
const inputArgs = customization.args ??
|
|
680
|
+
const customInput = customization.input ?? NoOp.input;
|
|
681
|
+
const inputArgs = customization.args ?? NoOp.args;
|
|
682
682
|
return function customBuilder(fn) {
|
|
683
683
|
const { args, handler = fn, returns: maybeObject, ...extra } = fn;
|
|
684
|
-
const returns = maybeObject && !(maybeObject instanceof
|
|
684
|
+
const returns = maybeObject && !(maybeObject instanceof z.ZodType) ? z.object(maybeObject) : maybeObject;
|
|
685
685
|
const returnValidator = returns && !fn.skipConvexValidation ? { returns: zodToConvex(returns) } : void 0;
|
|
686
686
|
if (args && !fn.skipConvexValidation) {
|
|
687
687
|
let argsValidator = args;
|
|
688
688
|
let argsSchema;
|
|
689
|
-
if (argsValidator instanceof
|
|
690
|
-
if (argsValidator instanceof
|
|
689
|
+
if (argsValidator instanceof z.ZodType) {
|
|
690
|
+
if (argsValidator instanceof z.ZodObject) {
|
|
691
691
|
argsSchema = argsValidator;
|
|
692
692
|
argsValidator = argsValidator.shape;
|
|
693
693
|
} else {
|
|
@@ -696,7 +696,7 @@ function customFnBuilder(builder, customization) {
|
|
|
696
696
|
);
|
|
697
697
|
}
|
|
698
698
|
} else {
|
|
699
|
-
argsSchema =
|
|
699
|
+
argsSchema = z.object(argsValidator);
|
|
700
700
|
}
|
|
701
701
|
const convexValidator = zodToConvexFields(argsValidator);
|
|
702
702
|
return builder({
|
|
@@ -804,15 +804,15 @@ function containsCustom(schema, maxDepth = 50, currentDepth = 0) {
|
|
|
804
804
|
let result = false;
|
|
805
805
|
if (schema._def?.typeName === "ZodCustom") {
|
|
806
806
|
result = true;
|
|
807
|
-
} else if (schema instanceof
|
|
807
|
+
} else if (schema instanceof z.ZodUnion) {
|
|
808
808
|
result = schema.options.some(
|
|
809
809
|
(opt) => containsCustom(opt, maxDepth, currentDepth + 1)
|
|
810
810
|
);
|
|
811
|
-
} else if (schema instanceof
|
|
811
|
+
} else if (schema instanceof z.ZodOptional) {
|
|
812
812
|
result = containsCustom(schema.unwrap(), maxDepth, currentDepth + 1);
|
|
813
|
-
} else if (schema instanceof
|
|
813
|
+
} else if (schema instanceof z.ZodNullable) {
|
|
814
814
|
result = containsCustom(schema.unwrap(), maxDepth, currentDepth + 1);
|
|
815
|
-
} else if (schema instanceof
|
|
815
|
+
} else if (schema instanceof z.ZodDefault) {
|
|
816
816
|
result = containsCustom(schema.removeDefault(), maxDepth, currentDepth + 1);
|
|
817
817
|
}
|
|
818
818
|
customCheckCache.set(schema, result);
|
|
@@ -821,15 +821,15 @@ function containsCustom(schema, maxDepth = 50, currentDepth = 0) {
|
|
|
821
821
|
function zQuery(query, input, handler, options) {
|
|
822
822
|
let zodSchema;
|
|
823
823
|
let args;
|
|
824
|
-
if (input instanceof
|
|
824
|
+
if (input instanceof z.ZodObject) {
|
|
825
825
|
const zodObj = input;
|
|
826
826
|
zodSchema = zodObj;
|
|
827
827
|
args = zodToConvexFields(getObjectShape(zodObj));
|
|
828
|
-
} else if (input instanceof
|
|
829
|
-
zodSchema =
|
|
828
|
+
} else if (input instanceof z.ZodType) {
|
|
829
|
+
zodSchema = z.object({ value: input });
|
|
830
830
|
args = { value: zodToConvex(input) };
|
|
831
831
|
} else {
|
|
832
|
-
zodSchema =
|
|
832
|
+
zodSchema = z.object(input);
|
|
833
833
|
args = zodToConvexFields(input);
|
|
834
834
|
}
|
|
835
835
|
const returns = options?.returns && !containsCustom(options.returns) ? zodToConvex(options.returns) : void 0;
|
|
@@ -860,15 +860,15 @@ function zQuery(query, input, handler, options) {
|
|
|
860
860
|
function zMutation(mutation, input, handler, options) {
|
|
861
861
|
let zodSchema;
|
|
862
862
|
let args;
|
|
863
|
-
if (input instanceof
|
|
863
|
+
if (input instanceof z.ZodObject) {
|
|
864
864
|
const zodObj = input;
|
|
865
865
|
zodSchema = zodObj;
|
|
866
866
|
args = zodToConvexFields(getObjectShape(zodObj));
|
|
867
|
-
} else if (input instanceof
|
|
868
|
-
zodSchema =
|
|
867
|
+
} else if (input instanceof z.ZodType) {
|
|
868
|
+
zodSchema = z.object({ value: input });
|
|
869
869
|
args = { value: zodToConvex(input) };
|
|
870
870
|
} else {
|
|
871
|
-
zodSchema =
|
|
871
|
+
zodSchema = z.object(input);
|
|
872
872
|
args = zodToConvexFields(input);
|
|
873
873
|
}
|
|
874
874
|
const returns = options?.returns && !containsCustom(options.returns) ? zodToConvex(options.returns) : void 0;
|
|
@@ -899,15 +899,15 @@ function zMutation(mutation, input, handler, options) {
|
|
|
899
899
|
function zAction(action, input, handler, options) {
|
|
900
900
|
let zodSchema;
|
|
901
901
|
let args;
|
|
902
|
-
if (input instanceof
|
|
902
|
+
if (input instanceof z.ZodObject) {
|
|
903
903
|
const zodObj = input;
|
|
904
904
|
zodSchema = zodObj;
|
|
905
905
|
args = zodToConvexFields(getObjectShape(zodObj));
|
|
906
|
-
} else if (input instanceof
|
|
907
|
-
zodSchema =
|
|
906
|
+
} else if (input instanceof z.ZodType) {
|
|
907
|
+
zodSchema = z.object({ value: input });
|
|
908
908
|
args = { value: zodToConvex(input) };
|
|
909
909
|
} else {
|
|
910
|
-
zodSchema =
|
|
910
|
+
zodSchema = z.object(input);
|
|
911
911
|
args = zodToConvexFields(input);
|
|
912
912
|
}
|
|
913
913
|
const returns = options?.returns && !containsCustom(options.returns) ? zodToConvex(options.returns) : void 0;
|
|
@@ -979,17 +979,17 @@ function zCustomActionBuilder(action, customization) {
|
|
|
979
979
|
function zodDoc(tableName, schema) {
|
|
980
980
|
return schema.extend({
|
|
981
981
|
_id: zid(tableName),
|
|
982
|
-
_creationTime:
|
|
982
|
+
_creationTime: z.number()
|
|
983
983
|
});
|
|
984
984
|
}
|
|
985
985
|
function zodDocOrNull(tableName, schema) {
|
|
986
|
-
return
|
|
986
|
+
return z.union([zodDoc(tableName, schema), z.null()]);
|
|
987
987
|
}
|
|
988
988
|
function zodTable(name, shape) {
|
|
989
989
|
const convexFields = zodToConvexFields(shape);
|
|
990
|
-
const table =
|
|
991
|
-
const zDoc = zodDoc(name,
|
|
992
|
-
const docArray =
|
|
990
|
+
const table = Table(name, convexFields);
|
|
991
|
+
const zDoc = zodDoc(name, z.object(shape));
|
|
992
|
+
const docArray = z.array(zDoc);
|
|
993
993
|
return Object.assign(table, {
|
|
994
994
|
shape,
|
|
995
995
|
zDoc,
|
|
@@ -997,43 +997,6 @@ function zodTable(name, shape) {
|
|
|
997
997
|
});
|
|
998
998
|
}
|
|
999
999
|
|
|
1000
|
-
|
|
1001
|
-
enumerable: true,
|
|
1002
|
-
get: function () { return customFunctions.customCtx; }
|
|
1003
|
-
});
|
|
1004
|
-
exports.convexCodec = convexCodec;
|
|
1005
|
-
exports.customFnBuilder = customFnBuilder;
|
|
1006
|
-
exports.findBaseCodec = findBaseCodec;
|
|
1007
|
-
exports.formatZodIssues = formatZodIssues;
|
|
1008
|
-
exports.fromConvexJS = fromConvexJS;
|
|
1009
|
-
exports.getObjectShape = getObjectShape;
|
|
1010
|
-
exports.handleZodValidationError = handleZodValidationError;
|
|
1011
|
-
exports.isDateSchema = isDateSchema;
|
|
1012
|
-
exports.makeUnion = makeUnion;
|
|
1013
|
-
exports.mapDateFieldToNumber = mapDateFieldToNumber;
|
|
1014
|
-
exports.pick = pick;
|
|
1015
|
-
exports.pickShape = pickShape;
|
|
1016
|
-
exports.registerBaseCodec = registerBaseCodec;
|
|
1017
|
-
exports.registryHelpers = registryHelpers;
|
|
1018
|
-
exports.returnsAs = returnsAs;
|
|
1019
|
-
exports.safeOmit = safeOmit;
|
|
1020
|
-
exports.safePick = safePick;
|
|
1021
|
-
exports.toConvexJS = toConvexJS;
|
|
1022
|
-
exports.zActionBuilder = zActionBuilder;
|
|
1023
|
-
exports.zCustomAction = zCustomAction;
|
|
1024
|
-
exports.zCustomActionBuilder = zCustomActionBuilder;
|
|
1025
|
-
exports.zCustomMutation = zCustomMutation;
|
|
1026
|
-
exports.zCustomMutationBuilder = zCustomMutationBuilder;
|
|
1027
|
-
exports.zCustomQuery = zCustomQuery;
|
|
1028
|
-
exports.zCustomQueryBuilder = zCustomQueryBuilder;
|
|
1029
|
-
exports.zMutationBuilder = zMutationBuilder;
|
|
1030
|
-
exports.zPaginated = zPaginated;
|
|
1031
|
-
exports.zQueryBuilder = zQueryBuilder;
|
|
1032
|
-
exports.zid = zid;
|
|
1033
|
-
exports.zodDoc = zodDoc;
|
|
1034
|
-
exports.zodDocOrNull = zodDocOrNull;
|
|
1035
|
-
exports.zodTable = zodTable;
|
|
1036
|
-
exports.zodToConvex = zodToConvex;
|
|
1037
|
-
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 };
|
|
1038
1001
|
//# sourceMappingURL=index.js.map
|
|
1039
1002
|
//# sourceMappingURL=index.js.map
|