zodvex 0.2.4 → 0.3.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/dist/index.js CHANGED
@@ -1,21 +1,20 @@
1
- 'use strict';
2
-
3
- var customFunctions = require('convex-helpers/server/customFunctions');
4
- var zod = require('zod');
5
- var values = require('convex/values');
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 { defineTable } from 'convex/server';
6
+ import { Table } from 'convex-helpers/server';
7
7
 
8
+ // src/index.ts
8
9
  var metadata = /* @__PURE__ */ new WeakMap();
9
10
  var registryHelpers = {
10
11
  getMetadata: (type) => metadata.get(type),
11
12
  setMetadata: (type, meta) => metadata.set(type, meta)
12
13
  };
13
14
  function zid(tableName) {
14
- const baseSchema = zod.z.string().refine((val) => typeof val === "string" && val.length > 0, {
15
+ const baseSchema = z.string().refine((val) => typeof val === "string" && val.length > 0, {
15
16
  message: `Invalid ID for table "${tableName}"`
16
- }).transform((val) => {
17
- return val;
18
- }).brand(`ConvexId_${tableName}`).describe(`convexId:${tableName}`);
17
+ }).describe(`convexId:${tableName}`);
19
18
  registryHelpers.setMetadata(baseSchema, {
20
19
  isConvexId: true,
21
20
  tableName
@@ -32,8 +31,8 @@ function findBaseCodec(schema) {
32
31
  return baseCodecs.find((codec) => codec.check(schema));
33
32
  }
34
33
  registerBaseCodec({
35
- check: (schema) => schema instanceof zod.z.ZodDate,
36
- toValidator: () => values.v.float64(),
34
+ check: (schema) => schema instanceof z.ZodDate,
35
+ toValidator: () => v.float64(),
37
36
  fromConvex: (value) => {
38
37
  if (typeof value === "number") {
39
38
  return new Date(value);
@@ -51,54 +50,104 @@ function asZodType(schema) {
51
50
  return schema;
52
51
  }
53
52
  function isDateSchema(schema) {
54
- if (schema instanceof zod.z.ZodDate) return true;
55
- if (schema instanceof zod.z.ZodOptional || schema instanceof zod.z.ZodNullable) {
53
+ if (schema instanceof z.ZodDate) return true;
54
+ if (schema instanceof z.ZodOptional || schema instanceof z.ZodNullable) {
56
55
  return isDateSchema(asZodType(schema.unwrap()));
57
56
  }
58
57
  return false;
59
58
  }
59
+ function isZidSchema(schema) {
60
+ const description = schema.description;
61
+ return typeof description === "string" && description.startsWith("convexId:");
62
+ }
63
+ function getZidTableName(schema) {
64
+ const description = schema.description;
65
+ if (typeof description === "string" && description.startsWith("convexId:")) {
66
+ return description.slice("convexId:".length);
67
+ }
68
+ return void 0;
69
+ }
70
+ function zodvexJSONSchemaOverride(ctx) {
71
+ const { zodSchema, jsonSchema } = ctx;
72
+ if (isZidSchema(zodSchema)) {
73
+ const tableName = getZidTableName(zodSchema);
74
+ jsonSchema.type = "string";
75
+ if (tableName) {
76
+ jsonSchema.format = `convex-id:${tableName}`;
77
+ }
78
+ if (zodSchema.description) {
79
+ jsonSchema.description = zodSchema.description;
80
+ }
81
+ return;
82
+ }
83
+ if (zodSchema instanceof z.ZodDate || zodSchema.type === "date") {
84
+ jsonSchema.type = "string";
85
+ jsonSchema.format = "date-time";
86
+ return;
87
+ }
88
+ }
89
+ function composeOverrides(...overrides) {
90
+ return (ctx) => {
91
+ for (const override of overrides) {
92
+ override?.(ctx);
93
+ }
94
+ };
95
+ }
96
+ function toJSONSchema(schema, options) {
97
+ const userOverride = options?.override;
98
+ return z.toJSONSchema(schema, {
99
+ ...options,
100
+ // Default to 'any' so transforms don't throw
101
+ unrepresentable: options?.unrepresentable ?? "any",
102
+ // Chain our override with user's override
103
+ override: (ctx) => {
104
+ zodvexJSONSchemaOverride(ctx);
105
+ userOverride?.(ctx);
106
+ }
107
+ });
108
+ }
60
109
  function convertEnumType(actualValidator) {
61
110
  const options = actualValidator.options;
62
111
  if (options && Array.isArray(options) && options.length > 0) {
63
- const validLiterals = options.filter((opt) => opt !== void 0 && opt !== null).map((opt) => values.v.literal(opt));
112
+ const validLiterals = options.filter((opt) => opt !== void 0 && opt !== null).map((opt) => v.literal(opt));
64
113
  if (validLiterals.length === 1) {
65
114
  const [first] = validLiterals;
66
115
  return first;
67
116
  } else if (validLiterals.length >= 2) {
68
117
  const [first, second, ...rest] = validLiterals;
69
- return values.v.union(
118
+ return v.union(
70
119
  first,
71
120
  second,
72
121
  ...rest
73
122
  );
74
123
  } else {
75
- return values.v.any();
124
+ return v.any();
76
125
  }
77
126
  } else {
78
- return values.v.any();
127
+ return v.any();
79
128
  }
80
129
  }
81
130
  function convertNullableType(actualValidator, visited, zodToConvexInternal2) {
82
131
  const innerSchema = actualValidator.unwrap();
83
- if (innerSchema && innerSchema instanceof zod.z.ZodType) {
84
- if (innerSchema instanceof zod.z.ZodOptional) {
132
+ if (innerSchema && innerSchema instanceof z.ZodType) {
133
+ if (innerSchema instanceof z.ZodOptional) {
85
134
  const innerInnerSchema = innerSchema.unwrap();
86
135
  const innerInnerValidator = zodToConvexInternal2(innerInnerSchema, visited);
87
136
  return {
88
- validator: values.v.union(innerInnerValidator, values.v.null()),
137
+ validator: v.union(innerInnerValidator, v.null()),
89
138
  isOptional: true
90
139
  // Mark as optional so it gets wrapped later
91
140
  };
92
141
  } else {
93
142
  const innerValidator = zodToConvexInternal2(innerSchema, visited);
94
143
  return {
95
- validator: values.v.union(innerValidator, values.v.null()),
144
+ validator: v.union(innerValidator, v.null()),
96
145
  isOptional: false
97
146
  };
98
147
  }
99
148
  } else {
100
149
  return {
101
- validator: values.v.any(),
150
+ validator: v.any(),
102
151
  isOptional: false
103
152
  };
104
153
  }
@@ -108,37 +157,37 @@ function convertRecordType(actualValidator, visited, zodToConvexInternal2) {
108
157
  if (!valueType) {
109
158
  valueType = actualValidator._def?.keyType;
110
159
  }
111
- if (valueType && valueType instanceof zod.z.ZodType) {
112
- const isZodOptional = valueType instanceof zod.z.ZodOptional || valueType instanceof zod.z.ZodDefault || valueType instanceof zod.z.ZodDefault && valueType.def.innerType instanceof zod.z.ZodOptional;
160
+ if (valueType && valueType instanceof z.ZodType) {
161
+ const isZodOptional = valueType instanceof z.ZodOptional || valueType instanceof z.ZodDefault || valueType instanceof z.ZodDefault && valueType.def.innerType instanceof z.ZodOptional;
113
162
  if (isZodOptional) {
114
163
  let innerType;
115
164
  let recordDefaultValue = void 0;
116
165
  let recordHasDefault = false;
117
- if (valueType instanceof zod.z.ZodDefault) {
166
+ if (valueType instanceof z.ZodDefault) {
118
167
  recordHasDefault = true;
119
168
  recordDefaultValue = valueType.def.defaultValue;
120
169
  const innerFromDefault = valueType.def.innerType;
121
- if (innerFromDefault instanceof zod.z.ZodOptional) {
170
+ if (innerFromDefault instanceof z.ZodOptional) {
122
171
  innerType = innerFromDefault.unwrap();
123
172
  } else {
124
173
  innerType = innerFromDefault;
125
174
  }
126
- } else if (valueType instanceof zod.z.ZodOptional) {
175
+ } else if (valueType instanceof z.ZodOptional) {
127
176
  innerType = valueType.unwrap();
128
177
  } else {
129
178
  innerType = valueType;
130
179
  }
131
180
  const innerConvex = zodToConvexInternal2(innerType, visited);
132
- const unionValidator = values.v.union(innerConvex, values.v.null());
181
+ const unionValidator = v.union(innerConvex, v.null());
133
182
  if (recordHasDefault) {
134
183
  unionValidator._zodDefault = recordDefaultValue;
135
184
  }
136
- return values.v.record(values.v.string(), unionValidator);
185
+ return v.record(v.string(), unionValidator);
137
186
  } else {
138
- return values.v.record(values.v.string(), zodToConvexInternal2(valueType, visited));
187
+ return v.record(v.string(), zodToConvexInternal2(valueType, visited));
139
188
  }
140
189
  } else {
141
- return values.v.record(values.v.string(), values.v.any());
190
+ return v.record(v.string(), v.any());
142
191
  }
143
192
  }
144
193
  function convertDiscriminatedUnionType(actualValidator, visited, zodToConvexInternal2) {
@@ -151,16 +200,16 @@ function convertDiscriminatedUnionType(actualValidator, visited, zodToConvexInte
151
200
  return zodToConvexInternal2(opt, branchVisited);
152
201
  });
153
202
  const [first, second, ...rest] = convexOptions;
154
- return values.v.union(
203
+ return v.union(
155
204
  first,
156
205
  second,
157
206
  ...rest
158
207
  );
159
208
  } else {
160
- return values.v.any();
209
+ return v.any();
161
210
  }
162
211
  } else {
163
- return values.v.any();
212
+ return v.any();
164
213
  }
165
214
  }
166
215
  function convertUnionType(actualValidator, visited, zodToConvexInternal2) {
@@ -175,17 +224,17 @@ function convertUnionType(actualValidator, visited, zodToConvexInternal2) {
175
224
  });
176
225
  if (convexOptions.length >= 2) {
177
226
  const [first, second, ...rest] = convexOptions;
178
- return values.v.union(
227
+ return v.union(
179
228
  first,
180
229
  second,
181
230
  ...rest
182
231
  );
183
232
  } else {
184
- return values.v.any();
233
+ return v.any();
185
234
  }
186
235
  }
187
236
  } else {
188
- return values.v.any();
237
+ return v.any();
189
238
  }
190
239
  }
191
240
  function isZid(schema) {
@@ -194,12 +243,12 @@ function isZid(schema) {
194
243
  }
195
244
  function makeUnion(members) {
196
245
  const nonNull = members.filter(Boolean);
197
- if (nonNull.length === 0) return values.v.any();
246
+ if (nonNull.length === 0) return v.any();
198
247
  if (nonNull.length === 1) return nonNull[0];
199
- return values.v.union(nonNull[0], nonNull[1], ...nonNull.slice(2));
248
+ return v.union(nonNull[0], nonNull[1], ...nonNull.slice(2));
200
249
  }
201
250
  function getObjectShape(obj) {
202
- if (obj instanceof zod.z.ZodObject) {
251
+ if (obj instanceof z.ZodObject) {
203
252
  return obj.shape;
204
253
  }
205
254
  if (obj && typeof obj === "object" && typeof obj.shape === "object") {
@@ -211,25 +260,25 @@ function getObjectShape(obj) {
211
260
  // src/mapping/core.ts
212
261
  function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set()) {
213
262
  if (!zodValidator) {
214
- return values.v.any();
263
+ return v.any();
215
264
  }
216
265
  if (visited.has(zodValidator)) {
217
- return values.v.any();
266
+ return v.any();
218
267
  }
219
268
  visited.add(zodValidator);
220
269
  let actualValidator = zodValidator;
221
270
  let isOptional = false;
222
271
  let defaultValue = void 0;
223
272
  let hasDefault = false;
224
- if (zodValidator instanceof zod.z.ZodDefault) {
273
+ if (zodValidator instanceof z.ZodDefault) {
225
274
  hasDefault = true;
226
275
  defaultValue = zodValidator.def?.defaultValue;
227
276
  actualValidator = zodValidator.def?.innerType;
228
277
  }
229
- if (actualValidator instanceof zod.z.ZodOptional) {
278
+ if (actualValidator instanceof z.ZodOptional) {
230
279
  isOptional = true;
231
280
  actualValidator = actualValidator.unwrap();
232
- if (actualValidator instanceof zod.z.ZodDefault) {
281
+ if (actualValidator instanceof z.ZodDefault) {
233
282
  hasDefault = true;
234
283
  defaultValue = actualValidator.def?.defaultValue;
235
284
  actualValidator = actualValidator.def?.innerType;
@@ -239,64 +288,64 @@ function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set())
239
288
  if (isZid(actualValidator)) {
240
289
  const metadata2 = registryHelpers.getMetadata(actualValidator);
241
290
  const tableName = metadata2?.tableName || "unknown";
242
- convexValidator = values.v.id(tableName);
291
+ convexValidator = v.id(tableName);
243
292
  } else {
244
293
  const defType = actualValidator.def?.type;
245
294
  switch (defType) {
246
295
  case "string":
247
- convexValidator = values.v.string();
296
+ convexValidator = v.string();
248
297
  break;
249
298
  case "number":
250
- convexValidator = values.v.float64();
299
+ convexValidator = v.float64();
251
300
  break;
252
301
  case "bigint":
253
- convexValidator = values.v.int64();
302
+ convexValidator = v.int64();
254
303
  break;
255
304
  case "boolean":
256
- convexValidator = values.v.boolean();
305
+ convexValidator = v.boolean();
257
306
  break;
258
307
  case "date":
259
- convexValidator = values.v.float64();
308
+ convexValidator = v.float64();
260
309
  break;
261
310
  case "null":
262
- convexValidator = values.v.null();
311
+ convexValidator = v.null();
263
312
  break;
264
313
  case "nan":
265
- convexValidator = values.v.float64();
314
+ convexValidator = v.float64();
266
315
  break;
267
316
  case "array": {
268
- if (actualValidator instanceof zod.z.ZodArray) {
317
+ if (actualValidator instanceof z.ZodArray) {
269
318
  const element = actualValidator.element;
270
- if (element && element instanceof zod.z.ZodType) {
271
- convexValidator = values.v.array(zodToConvexInternal(element, visited));
319
+ if (element && element instanceof z.ZodType) {
320
+ convexValidator = v.array(zodToConvexInternal(element, visited));
272
321
  } else {
273
- convexValidator = values.v.array(values.v.any());
322
+ convexValidator = v.array(v.any());
274
323
  }
275
324
  } else {
276
- convexValidator = values.v.array(values.v.any());
325
+ convexValidator = v.array(v.any());
277
326
  }
278
327
  break;
279
328
  }
280
329
  case "object": {
281
- if (actualValidator instanceof zod.z.ZodObject) {
330
+ if (actualValidator instanceof z.ZodObject) {
282
331
  const shape = actualValidator.shape;
283
332
  const convexShape = {};
284
333
  for (const [key, value] of Object.entries(shape)) {
285
- if (value && value instanceof zod.z.ZodType) {
334
+ if (value && value instanceof z.ZodType) {
286
335
  convexShape[key] = zodToConvexInternal(value, visited);
287
336
  }
288
337
  }
289
- convexValidator = values.v.object(convexShape);
338
+ convexValidator = v.object(convexShape);
290
339
  } else {
291
- convexValidator = values.v.object({});
340
+ convexValidator = v.object({});
292
341
  }
293
342
  break;
294
343
  }
295
344
  case "union": {
296
- if (actualValidator instanceof zod.z.ZodUnion) {
345
+ if (actualValidator instanceof z.ZodUnion) {
297
346
  convexValidator = convertUnionType(actualValidator, visited, zodToConvexInternal);
298
347
  } else {
299
- convexValidator = values.v.any();
348
+ convexValidator = v.any();
300
349
  }
301
350
  break;
302
351
  }
@@ -309,31 +358,31 @@ function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set())
309
358
  break;
310
359
  }
311
360
  case "literal": {
312
- if (actualValidator instanceof zod.z.ZodLiteral) {
361
+ if (actualValidator instanceof z.ZodLiteral) {
313
362
  const literalValue = actualValidator.value;
314
363
  if (literalValue !== void 0 && literalValue !== null) {
315
- convexValidator = values.v.literal(literalValue);
364
+ convexValidator = v.literal(literalValue);
316
365
  } else {
317
- convexValidator = values.v.any();
366
+ convexValidator = v.any();
318
367
  }
319
368
  } else {
320
- convexValidator = values.v.any();
369
+ convexValidator = v.any();
321
370
  }
322
371
  break;
323
372
  }
324
373
  case "enum": {
325
- if (actualValidator instanceof zod.z.ZodEnum) {
374
+ if (actualValidator instanceof z.ZodEnum) {
326
375
  convexValidator = convertEnumType(actualValidator);
327
376
  } else {
328
- convexValidator = values.v.any();
377
+ convexValidator = v.any();
329
378
  }
330
379
  break;
331
380
  }
332
381
  case "record": {
333
- if (actualValidator instanceof zod.z.ZodRecord) {
382
+ if (actualValidator instanceof z.ZodRecord) {
334
383
  convexValidator = convertRecordType(actualValidator, visited, zodToConvexInternal);
335
384
  } else {
336
- convexValidator = values.v.record(values.v.string(), values.v.any());
385
+ convexValidator = v.record(v.string(), v.any());
337
386
  }
338
387
  break;
339
388
  }
@@ -347,75 +396,75 @@ function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set())
347
396
  if (metadata2?.brand && metadata2?.originalSchema) {
348
397
  convexValidator = zodToConvexInternal(metadata2.originalSchema, visited);
349
398
  } else {
350
- convexValidator = values.v.any();
399
+ convexValidator = v.any();
351
400
  }
352
401
  }
353
402
  break;
354
403
  }
355
404
  case "nullable": {
356
- if (actualValidator instanceof zod.z.ZodNullable) {
405
+ if (actualValidator instanceof z.ZodNullable) {
357
406
  const result = convertNullableType(actualValidator, visited, zodToConvexInternal);
358
407
  convexValidator = result.validator;
359
408
  if (result.isOptional) {
360
409
  isOptional = true;
361
410
  }
362
411
  } else {
363
- convexValidator = values.v.any();
412
+ convexValidator = v.any();
364
413
  }
365
414
  break;
366
415
  }
367
416
  case "tuple": {
368
- if (actualValidator instanceof zod.z.ZodTuple) {
417
+ if (actualValidator instanceof z.ZodTuple) {
369
418
  const items = actualValidator.def?.items;
370
419
  if (items && items.length > 0) {
371
420
  const convexShape = {};
372
421
  items.forEach((item, index) => {
373
422
  convexShape[`_${index}`] = zodToConvexInternal(item, visited);
374
423
  });
375
- convexValidator = values.v.object(convexShape);
424
+ convexValidator = v.object(convexShape);
376
425
  } else {
377
- convexValidator = values.v.object({});
426
+ convexValidator = v.object({});
378
427
  }
379
428
  } else {
380
- convexValidator = values.v.object({});
429
+ convexValidator = v.object({});
381
430
  }
382
431
  break;
383
432
  }
384
433
  case "lazy": {
385
- if (actualValidator instanceof zod.z.ZodLazy) {
434
+ if (actualValidator instanceof z.ZodLazy) {
386
435
  try {
387
436
  const getter = actualValidator.def?.getter;
388
437
  if (getter) {
389
438
  const resolvedSchema = getter();
390
- if (resolvedSchema && resolvedSchema instanceof zod.z.ZodType) {
439
+ if (resolvedSchema && resolvedSchema instanceof z.ZodType) {
391
440
  convexValidator = zodToConvexInternal(resolvedSchema, visited);
392
441
  } else {
393
- convexValidator = values.v.any();
442
+ convexValidator = v.any();
394
443
  }
395
444
  } else {
396
- convexValidator = values.v.any();
445
+ convexValidator = v.any();
397
446
  }
398
447
  } catch {
399
- convexValidator = values.v.any();
448
+ convexValidator = v.any();
400
449
  }
401
450
  } else {
402
- convexValidator = values.v.any();
451
+ convexValidator = v.any();
403
452
  }
404
453
  break;
405
454
  }
406
455
  case "any":
407
- convexValidator = values.v.any();
456
+ convexValidator = v.any();
408
457
  break;
409
458
  case "unknown":
410
- convexValidator = values.v.any();
459
+ convexValidator = v.any();
411
460
  break;
412
461
  case "undefined":
413
462
  case "void":
414
463
  case "never":
415
- convexValidator = values.v.any();
464
+ convexValidator = v.any();
416
465
  break;
417
466
  case "intersection":
418
- convexValidator = values.v.any();
467
+ convexValidator = v.any();
419
468
  break;
420
469
  default:
421
470
  if (process.env.NODE_ENV !== "production") {
@@ -425,24 +474,24 @@ function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set())
425
474
  actualValidator
426
475
  );
427
476
  }
428
- convexValidator = values.v.any();
477
+ convexValidator = v.any();
429
478
  break;
430
479
  }
431
480
  }
432
- const finalValidator = isOptional || hasDefault ? values.v.optional(convexValidator) : convexValidator;
481
+ const finalValidator = isOptional || hasDefault ? v.optional(convexValidator) : convexValidator;
433
482
  if (hasDefault && typeof finalValidator === "object" && finalValidator !== null) {
434
483
  finalValidator._zodDefault = defaultValue;
435
484
  }
436
485
  return finalValidator;
437
486
  }
438
- function zodToConvex(zod$1) {
439
- if (typeof zod$1 === "object" && zod$1 !== null && !(zod$1 instanceof zod.z.ZodType)) {
440
- return zodToConvexFields(zod$1);
487
+ function zodToConvex(zod) {
488
+ if (typeof zod === "object" && zod !== null && !(zod instanceof z.ZodType)) {
489
+ return zodToConvexFields(zod);
441
490
  }
442
- return zodToConvexInternal(zod$1);
491
+ return zodToConvexInternal(zod);
443
492
  }
444
- function zodToConvexFields(zod$1) {
445
- const fields = zod$1 instanceof zod.z.ZodObject ? zod$1.shape : zod$1;
493
+ function zodToConvexFields(zod) {
494
+ const fields = zod instanceof z.ZodObject ? zod.shape : zod;
446
495
  const result = {};
447
496
  for (const [key, value] of Object.entries(fields)) {
448
497
  result[key] = zodToConvexInternal(value);
@@ -461,7 +510,7 @@ function convexCodec(schema) {
461
510
  encode: (value) => toConvexJS(schema, value),
462
511
  decode: (value) => fromConvexJS(value, schema),
463
512
  pick: (keys) => {
464
- if (!(schema instanceof zod.z.ZodObject)) {
513
+ if (!(schema instanceof z.ZodObject)) {
465
514
  throw new Error("pick() can only be called on object schemas");
466
515
  }
467
516
  const pickObj = Array.isArray(keys) ? keys.reduce((acc, k) => ({ ...acc, [k]: true }), {}) : keys;
@@ -501,18 +550,18 @@ function schemaToConvex(value, schema) {
501
550
  if (codec) {
502
551
  return codec.toConvex(value, schema);
503
552
  }
504
- if (schema instanceof zod.z.ZodOptional || schema instanceof zod.z.ZodNullable || schema instanceof zod.z.ZodDefault) {
553
+ if (schema instanceof z.ZodOptional || schema instanceof z.ZodNullable || schema instanceof z.ZodDefault) {
505
554
  const inner = schema.unwrap();
506
555
  return schemaToConvex(value, asZodType2(inner));
507
556
  }
508
- if (schema instanceof zod.z.ZodDate && value instanceof Date) {
557
+ if (schema instanceof z.ZodDate && value instanceof Date) {
509
558
  return value.getTime();
510
559
  }
511
- if (schema instanceof zod.z.ZodArray) {
560
+ if (schema instanceof z.ZodArray) {
512
561
  if (!Array.isArray(value)) return value;
513
562
  return value.map((item) => schemaToConvex(item, schema.element));
514
563
  }
515
- if (schema instanceof zod.z.ZodObject) {
564
+ if (schema instanceof z.ZodObject) {
516
565
  if (!value || typeof value !== "object") return value;
517
566
  const shape = schema.shape;
518
567
  const result = {};
@@ -523,7 +572,7 @@ function schemaToConvex(value, schema) {
523
572
  }
524
573
  return result;
525
574
  }
526
- if (schema instanceof zod.z.ZodUnion) {
575
+ if (schema instanceof z.ZodUnion) {
527
576
  for (const option of schema.options) {
528
577
  try {
529
578
  ;
@@ -533,7 +582,7 @@ function schemaToConvex(value, schema) {
533
582
  }
534
583
  }
535
584
  }
536
- if (schema instanceof zod.z.ZodRecord) {
585
+ if (schema instanceof z.ZodRecord) {
537
586
  if (!value || typeof value !== "object") return value;
538
587
  const result = {};
539
588
  for (const [k, v8] of Object.entries(value)) {
@@ -551,21 +600,21 @@ function fromConvexJS(value, schema) {
551
600
  if (codec) {
552
601
  return codec.fromConvex(value, schema);
553
602
  }
554
- if (schema instanceof zod.z.ZodOptional || schema instanceof zod.z.ZodNullable || schema instanceof zod.z.ZodDefault) {
603
+ if (schema instanceof z.ZodOptional || schema instanceof z.ZodNullable || schema instanceof z.ZodDefault) {
555
604
  const inner = schema.unwrap();
556
605
  return fromConvexJS(value, asZodType2(inner));
557
606
  }
558
- if (schema instanceof zod.z.ZodDate && typeof value === "number") {
607
+ if (schema instanceof z.ZodDate && typeof value === "number") {
559
608
  return new Date(value);
560
609
  }
561
610
  if (isDateSchema(schema) && typeof value === "number") {
562
611
  return new Date(value);
563
612
  }
564
- if (schema instanceof zod.z.ZodArray) {
613
+ if (schema instanceof z.ZodArray) {
565
614
  if (!Array.isArray(value)) return value;
566
615
  return value.map((item) => fromConvexJS(item, schema.element));
567
616
  }
568
- if (schema instanceof zod.z.ZodObject) {
617
+ if (schema instanceof z.ZodObject) {
569
618
  if (!value || typeof value !== "object") return value;
570
619
  const shape = schema.shape;
571
620
  const result = {};
@@ -574,7 +623,7 @@ function fromConvexJS(value, schema) {
574
623
  }
575
624
  return result;
576
625
  }
577
- if (schema instanceof zod.z.ZodUnion) {
626
+ if (schema instanceof z.ZodUnion) {
578
627
  for (const option of schema.options) {
579
628
  try {
580
629
  const decoded = fromConvexJS(value, option);
@@ -584,7 +633,7 @@ function fromConvexJS(value, schema) {
584
633
  }
585
634
  }
586
635
  }
587
- if (schema instanceof zod.z.ZodRecord) {
636
+ if (schema instanceof z.ZodRecord) {
588
637
  if (!value || typeof value !== "object") return value;
589
638
  const result = {};
590
639
  for (const [k, v8] of Object.entries(value)) {
@@ -592,7 +641,7 @@ function fromConvexJS(value, schema) {
592
641
  }
593
642
  return result;
594
643
  }
595
- if (schema instanceof zod.z.ZodTransform) {
644
+ if (schema instanceof z.ZodTransform) {
596
645
  return value;
597
646
  }
598
647
  return value;
@@ -621,32 +670,32 @@ function formatZodIssues(error, context) {
621
670
  };
622
671
  }
623
672
  function handleZodValidationError(e, context) {
624
- if (e instanceof zod.z.ZodError) {
625
- throw new values.ConvexError(formatZodIssues(e, context));
673
+ if (e instanceof z.ZodError) {
674
+ throw new ConvexError(formatZodIssues(e, context));
626
675
  }
627
676
  throw e;
628
677
  }
629
678
  function zPaginated(item) {
630
- return zod.z.object({
631
- page: zod.z.array(item),
632
- isDone: zod.z.boolean(),
633
- continueCursor: zod.z.string().nullable().optional()
679
+ return z.object({
680
+ page: z.array(item),
681
+ isDone: z.boolean(),
682
+ continueCursor: z.string().nullable().optional()
634
683
  });
635
684
  }
636
685
  function mapDateFieldToNumber(field) {
637
- if (field instanceof zod.z.ZodDate) {
638
- return zod.z.number();
686
+ if (field instanceof z.ZodDate) {
687
+ return z.number();
639
688
  }
640
- if (field instanceof zod.z.ZodOptional && field.unwrap() instanceof zod.z.ZodDate) {
641
- return zod.z.number().optional();
689
+ if (field instanceof z.ZodOptional && field.unwrap() instanceof z.ZodDate) {
690
+ return z.number().optional();
642
691
  }
643
- if (field instanceof zod.z.ZodNullable && field.unwrap() instanceof zod.z.ZodDate) {
644
- return zod.z.number().nullable();
692
+ if (field instanceof z.ZodNullable && field.unwrap() instanceof z.ZodDate) {
693
+ return z.number().nullable();
645
694
  }
646
- if (field instanceof zod.z.ZodDefault) {
695
+ if (field instanceof z.ZodDefault) {
647
696
  const inner = field.removeDefault();
648
- if (inner instanceof zod.z.ZodDate) {
649
- return zod.z.number().optional();
697
+ if (inner instanceof z.ZodDate) {
698
+ return z.number().optional();
650
699
  }
651
700
  }
652
701
  return field;
@@ -657,7 +706,7 @@ function toKeys(mask) {
657
706
  }
658
707
  function pickShape(schemaOrShape, mask) {
659
708
  const keys = toKeys(mask);
660
- const shape = schemaOrShape instanceof zod.z.ZodObject ? getObjectShape(schemaOrShape) : schemaOrShape || {};
709
+ const shape = schemaOrShape instanceof z.ZodObject ? getObjectShape(schemaOrShape) : schemaOrShape || {};
661
710
  const out = {};
662
711
  for (const k of keys) {
663
712
  if (k in shape) out[k] = shape[k];
@@ -665,29 +714,30 @@ function pickShape(schemaOrShape, mask) {
665
714
  return out;
666
715
  }
667
716
  function safePick(schema, mask) {
668
- return zod.z.object(pickShape(schema, mask));
717
+ return z.object(pickShape(schema, mask));
669
718
  }
670
719
  function safeOmit(schema, mask) {
671
720
  const shape = getObjectShape(schema);
672
721
  const omit = new Set(toKeys(mask));
673
722
  const keep = Object.keys(shape).filter((k) => !omit.has(k));
674
723
  const picked = pickShape(schema, keep);
675
- return zod.z.object(picked);
724
+ return z.object(picked);
676
725
  }
677
726
 
678
727
  // src/custom.ts
679
728
  function customFnBuilder(builder, customization) {
680
- const customInput = customization.input ?? customFunctions.NoOp.input;
681
- const inputArgs = customization.args ?? customFunctions.NoOp.args;
729
+ const customInput = customization.input ?? NoOp.input;
730
+ const inputArgs = customization.args ?? NoOp.args;
682
731
  return function customBuilder(fn) {
683
732
  const { args, handler = fn, returns: maybeObject, ...extra } = fn;
684
- const returns = maybeObject && !(maybeObject instanceof zod.z.ZodType) ? zod.z.object(maybeObject) : maybeObject;
685
- const returnValidator = returns && !fn.skipConvexValidation ? { returns: zodToConvex(returns) } : void 0;
686
- if (args && !fn.skipConvexValidation) {
733
+ const skipConvexValidation = fn.skipConvexValidation ?? false;
734
+ const returns = maybeObject && !(maybeObject instanceof z.ZodType) ? z.object(maybeObject) : maybeObject;
735
+ const returnValidator = returns && !skipConvexValidation ? { returns: zodToConvex(returns) } : void 0;
736
+ if (args) {
687
737
  let argsValidator = args;
688
738
  let argsSchema;
689
- if (argsValidator instanceof zod.z.ZodType) {
690
- if (argsValidator instanceof zod.z.ZodObject) {
739
+ if (argsValidator instanceof z.ZodType) {
740
+ if (argsValidator instanceof z.ZodObject) {
691
741
  argsSchema = argsValidator;
692
742
  argsValidator = argsValidator.shape;
693
743
  } else {
@@ -696,11 +746,11 @@ function customFnBuilder(builder, customization) {
696
746
  );
697
747
  }
698
748
  } else {
699
- argsSchema = zod.z.object(argsValidator);
749
+ argsSchema = z.object(argsValidator);
700
750
  }
701
- const convexValidator = zodToConvexFields(argsValidator);
751
+ const convexArgs = skipConvexValidation ? inputArgs : { ...zodToConvexFields(argsValidator), ...inputArgs };
702
752
  return builder({
703
- args: { ...convexValidator, ...inputArgs },
753
+ args: convexArgs,
704
754
  ...returnValidator,
705
755
  handler: async (ctx, allArgs) => {
706
756
  const added = await customInput(
@@ -720,7 +770,7 @@ function customFnBuilder(builder, customization) {
720
770
  const addedArgs = added?.args ?? {};
721
771
  const finalArgs = { ...baseArgs, ...addedArgs };
722
772
  const ret = await handler(finalCtx, finalArgs);
723
- if (returns && !fn.skipConvexValidation) {
773
+ if (returns) {
724
774
  let validated;
725
775
  try {
726
776
  validated = returns.parse(ret);
@@ -757,7 +807,7 @@ function customFnBuilder(builder, customization) {
757
807
  const addedArgs = added?.args ?? {};
758
808
  const finalArgs = { ...baseArgs, ...addedArgs };
759
809
  const ret = await handler(finalCtx, finalArgs);
760
- if (returns && !fn.skipConvexValidation) {
810
+ if (returns) {
761
811
  let validated;
762
812
  try {
763
813
  validated = returns.parse(ret);
@@ -802,17 +852,17 @@ function containsCustom(schema, maxDepth = 50, currentDepth = 0) {
802
852
  return false;
803
853
  }
804
854
  let result = false;
805
- if (schema._def?.typeName === "ZodCustom") {
855
+ if (schema instanceof z.ZodCustom) {
806
856
  result = true;
807
- } else if (schema instanceof zod.z.ZodUnion) {
857
+ } else if (schema instanceof z.ZodUnion) {
808
858
  result = schema.options.some(
809
859
  (opt) => containsCustom(opt, maxDepth, currentDepth + 1)
810
860
  );
811
- } else if (schema instanceof zod.z.ZodOptional) {
861
+ } else if (schema instanceof z.ZodOptional) {
812
862
  result = containsCustom(schema.unwrap(), maxDepth, currentDepth + 1);
813
- } else if (schema instanceof zod.z.ZodNullable) {
863
+ } else if (schema instanceof z.ZodNullable) {
814
864
  result = containsCustom(schema.unwrap(), maxDepth, currentDepth + 1);
815
- } else if (schema instanceof zod.z.ZodDefault) {
865
+ } else if (schema instanceof z.ZodDefault) {
816
866
  result = containsCustom(schema.removeDefault(), maxDepth, currentDepth + 1);
817
867
  }
818
868
  customCheckCache.set(schema, result);
@@ -821,15 +871,15 @@ function containsCustom(schema, maxDepth = 50, currentDepth = 0) {
821
871
  function zQuery(query, input, handler, options) {
822
872
  let zodSchema;
823
873
  let args;
824
- if (input instanceof zod.z.ZodObject) {
874
+ if (input instanceof z.ZodObject) {
825
875
  const zodObj = input;
826
876
  zodSchema = zodObj;
827
877
  args = zodToConvexFields(getObjectShape(zodObj));
828
- } else if (input instanceof zod.z.ZodType) {
829
- zodSchema = zod.z.object({ value: input });
878
+ } else if (input instanceof z.ZodType) {
879
+ zodSchema = z.object({ value: input });
830
880
  args = { value: zodToConvex(input) };
831
881
  } else {
832
- zodSchema = zod.z.object(input);
882
+ zodSchema = z.object(input);
833
883
  args = zodToConvexFields(input);
834
884
  }
835
885
  const returns = options?.returns && !containsCustom(options.returns) ? zodToConvex(options.returns) : void 0;
@@ -860,15 +910,15 @@ function zQuery(query, input, handler, options) {
860
910
  function zMutation(mutation, input, handler, options) {
861
911
  let zodSchema;
862
912
  let args;
863
- if (input instanceof zod.z.ZodObject) {
913
+ if (input instanceof z.ZodObject) {
864
914
  const zodObj = input;
865
915
  zodSchema = zodObj;
866
916
  args = zodToConvexFields(getObjectShape(zodObj));
867
- } else if (input instanceof zod.z.ZodType) {
868
- zodSchema = zod.z.object({ value: input });
917
+ } else if (input instanceof z.ZodType) {
918
+ zodSchema = z.object({ value: input });
869
919
  args = { value: zodToConvex(input) };
870
920
  } else {
871
- zodSchema = zod.z.object(input);
921
+ zodSchema = z.object(input);
872
922
  args = zodToConvexFields(input);
873
923
  }
874
924
  const returns = options?.returns && !containsCustom(options.returns) ? zodToConvex(options.returns) : void 0;
@@ -899,15 +949,15 @@ function zMutation(mutation, input, handler, options) {
899
949
  function zAction(action, input, handler, options) {
900
950
  let zodSchema;
901
951
  let args;
902
- if (input instanceof zod.z.ZodObject) {
952
+ if (input instanceof z.ZodObject) {
903
953
  const zodObj = input;
904
954
  zodSchema = zodObj;
905
955
  args = zodToConvexFields(getObjectShape(zodObj));
906
- } else if (input instanceof zod.z.ZodType) {
907
- zodSchema = zod.z.object({ value: input });
956
+ } else if (input instanceof z.ZodType) {
957
+ zodSchema = z.object({ value: input });
908
958
  args = { value: zodToConvex(input) };
909
959
  } else {
910
- zodSchema = zod.z.object(input);
960
+ zodSchema = z.object(input);
911
961
  args = zodToConvexFields(input);
912
962
  }
913
963
  const returns = options?.returns && !containsCustom(options.returns) ? zodToConvex(options.returns) : void 0;
@@ -976,64 +1026,78 @@ function zCustomActionBuilder(action, customization) {
976
1026
  customization
977
1027
  );
978
1028
  }
1029
+ function addSystemFields(tableName, schema) {
1030
+ if (schema instanceof z.ZodUnion || schema instanceof z.ZodDiscriminatedUnion) {
1031
+ const options = schema.options.map((variant) => {
1032
+ if (variant instanceof z.ZodObject) {
1033
+ return variant.extend({
1034
+ _id: zid(tableName),
1035
+ _creationTime: z.number()
1036
+ });
1037
+ }
1038
+ return variant;
1039
+ });
1040
+ return z.union(options);
1041
+ }
1042
+ if (schema instanceof z.ZodObject) {
1043
+ return schema.extend({
1044
+ _id: zid(tableName),
1045
+ _creationTime: z.number()
1046
+ });
1047
+ }
1048
+ return schema;
1049
+ }
979
1050
  function zodDoc(tableName, schema) {
980
1051
  return schema.extend({
981
1052
  _id: zid(tableName),
982
- _creationTime: zod.z.number()
1053
+ _creationTime: z.number()
983
1054
  });
984
1055
  }
985
1056
  function zodDocOrNull(tableName, schema) {
986
- return zod.z.union([zodDoc(tableName, schema), zod.z.null()]);
1057
+ return z.union([zodDoc(tableName, schema), z.null()]);
987
1058
  }
988
- function zodTable(name, shape) {
989
- const convexFields = zodToConvexFields(shape);
990
- const table = server.Table(name, convexFields);
991
- const zDoc = zodDoc(name, zod.z.object(shape));
992
- const docArray = zod.z.array(zDoc);
993
- return Object.assign(table, {
994
- shape,
995
- zDoc,
996
- docArray
997
- });
1059
+ function isObjectShape(input) {
1060
+ if (!input || typeof input !== "object") return false;
1061
+ if (input instanceof z.ZodType) return false;
1062
+ for (const key in input) {
1063
+ if (!(input[key] instanceof z.ZodType)) {
1064
+ return false;
1065
+ }
1066
+ }
1067
+ return true;
1068
+ }
1069
+ function zodTable(name, schemaOrShape) {
1070
+ if (isObjectShape(schemaOrShape)) {
1071
+ const shape = schemaOrShape;
1072
+ const convexFields = zodToConvexFields(shape);
1073
+ const table = Table(
1074
+ name,
1075
+ convexFields
1076
+ );
1077
+ const zDoc = zodDoc(name, z.object(shape));
1078
+ const docArray = z.array(zDoc);
1079
+ return Object.assign(table, {
1080
+ shape,
1081
+ zDoc,
1082
+ docArray
1083
+ });
1084
+ } else {
1085
+ const schema = schemaOrShape;
1086
+ const convexValidator = zodToConvex(schema);
1087
+ const table = defineTable(convexValidator);
1088
+ const withFields = addSystemFields(name, schema);
1089
+ const docArray = z.array(withFields);
1090
+ return {
1091
+ table,
1092
+ tableName: name,
1093
+ validator: convexValidator,
1094
+ schema,
1095
+ docArray,
1096
+ withSystemFields: () => addSystemFields(name, schema)
1097
+ };
1098
+ }
998
1099
  }
999
1100
 
1000
- Object.defineProperty(exports, "customCtx", {
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;
1101
+ export { addSystemFields, composeOverrides, convexCodec, customFnBuilder, findBaseCodec, formatZodIssues, fromConvexJS, getObjectShape, getZidTableName, handleZodValidationError, isDateSchema, isZidSchema, makeUnion, mapDateFieldToNumber, pick, pickShape, registerBaseCodec, registryHelpers, returnsAs, safeOmit, safePick, toConvexJS, toJSONSchema, zActionBuilder, zCustomAction, zCustomActionBuilder, zCustomMutation, zCustomMutationBuilder, zCustomQuery, zCustomQueryBuilder, zMutationBuilder, zPaginated, zQueryBuilder, zid, zodDoc, zodDocOrNull, zodTable, zodToConvex, zodToConvexFields, zodvexJSONSchemaOverride };
1038
1102
  //# sourceMappingURL=index.js.map
1039
1103
  //# sourceMappingURL=index.js.map