zodvex 0.2.0 → 0.2.2

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.mjs CHANGED
@@ -1,28 +1,14 @@
1
+ import { NoOp } from 'convex-helpers/server/customFunctions';
2
+ export { customCtx } from 'convex-helpers/server/customFunctions';
1
3
  import { z } from 'zod';
2
4
  import { v, ConvexError } from 'convex/values';
3
- import { NoOp } from 'convex-helpers/server/customFunctions';
4
5
  import { Table } from 'convex-helpers/server';
5
6
 
6
- var __defProp = Object.defineProperty;
7
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
8
- var __getOwnPropNames = Object.getOwnPropertyNames;
9
- var __hasOwnProp = Object.prototype.hasOwnProperty;
10
- var __esm = (fn, res) => function __init() {
11
- return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
12
- };
13
- var __export = (target, all) => {
14
- for (var name in all)
15
- __defProp(target, name, { get: all[name], enumerable: true });
16
- };
17
- var __copyProps = (to, from, except, desc) => {
18
- if (from && typeof from === "object" || typeof from === "function") {
19
- for (let key of __getOwnPropNames(from))
20
- if (!__hasOwnProp.call(to, key) && key !== except)
21
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
22
- }
23
- return to;
7
+ var metadata = /* @__PURE__ */ new WeakMap();
8
+ var registryHelpers = {
9
+ getMetadata: (type) => metadata.get(type),
10
+ setMetadata: (type, meta) => metadata.set(type, meta)
24
11
  };
25
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
26
12
  function zid(tableName) {
27
13
  const baseSchema = z.string().refine((val) => typeof val === "string" && val.length > 0, {
28
14
  message: `Invalid ID for table "${tableName}"`
@@ -37,22 +23,29 @@ function zid(tableName) {
37
23
  branded._tableName = tableName;
38
24
  return branded;
39
25
  }
40
- var metadata, registryHelpers;
41
- var init_ids = __esm({
42
- "src/ids.ts"() {
43
- metadata = /* @__PURE__ */ new WeakMap();
44
- registryHelpers = {
45
- getMetadata: (type) => metadata.get(type),
46
- setMetadata: (type, meta) => metadata.set(type, meta)
47
- };
48
- }
49
- });
26
+ var baseCodecs = [];
50
27
  function registerBaseCodec(codec) {
51
28
  baseCodecs.unshift(codec);
52
29
  }
53
30
  function findBaseCodec(schema) {
54
31
  return baseCodecs.find((codec) => codec.check(schema));
55
32
  }
33
+ registerBaseCodec({
34
+ check: (schema) => schema instanceof z.ZodDate,
35
+ toValidator: () => v.float64(),
36
+ fromConvex: (value) => {
37
+ if (typeof value === "number") {
38
+ return new Date(value);
39
+ }
40
+ return value;
41
+ },
42
+ toConvex: (value) => {
43
+ if (value instanceof Date) {
44
+ return value.getTime();
45
+ }
46
+ return value;
47
+ }
48
+ });
56
49
  function asZodType(schema) {
57
50
  return schema;
58
51
  }
@@ -63,28 +56,6 @@ function isDateSchema(schema) {
63
56
  }
64
57
  return false;
65
58
  }
66
- var baseCodecs;
67
- var init_registry = __esm({
68
- "src/registry.ts"() {
69
- baseCodecs = [];
70
- registerBaseCodec({
71
- check: (schema) => schema instanceof z.ZodDate,
72
- toValidator: () => v.float64(),
73
- fromConvex: (value) => {
74
- if (typeof value === "number") {
75
- return new Date(value);
76
- }
77
- return value;
78
- },
79
- toConvex: (value) => {
80
- if (value instanceof Date) {
81
- return value.getTime();
82
- }
83
- return value;
84
- }
85
- });
86
- }
87
- });
88
59
  function convertEnumType(actualValidator) {
89
60
  const options = actualValidator.options;
90
61
  if (options && Array.isArray(options) && options.length > 0) {
@@ -106,10 +77,6 @@ function convertEnumType(actualValidator) {
106
77
  return v.any();
107
78
  }
108
79
  }
109
- var init_enum = __esm({
110
- "src/mapping/handlers/enum.ts"() {
111
- }
112
- });
113
80
  function convertNullableType(actualValidator, visited, zodToConvexInternal2) {
114
81
  const innerSchema = actualValidator.unwrap();
115
82
  if (innerSchema && innerSchema instanceof z.ZodType) {
@@ -135,10 +102,6 @@ function convertNullableType(actualValidator, visited, zodToConvexInternal2) {
135
102
  };
136
103
  }
137
104
  }
138
- var init_nullable = __esm({
139
- "src/mapping/handlers/nullable.ts"() {
140
- }
141
- });
142
105
  function convertRecordType(actualValidator, visited, zodToConvexInternal2) {
143
106
  let valueType = actualValidator._def?.valueType;
144
107
  if (!valueType) {
@@ -177,10 +140,6 @@ function convertRecordType(actualValidator, visited, zodToConvexInternal2) {
177
140
  return v.record(v.string(), v.any());
178
141
  }
179
142
  }
180
- var init_record = __esm({
181
- "src/mapping/handlers/record.ts"() {
182
- }
183
- });
184
143
  function convertDiscriminatedUnionType(actualValidator, visited, zodToConvexInternal2) {
185
144
  const options = actualValidator.def?.options || actualValidator.def?.optionsMap?.values();
186
145
  if (options) {
@@ -224,20 +183,6 @@ function convertUnionType(actualValidator, visited, zodToConvexInternal2) {
224
183
  return v.any();
225
184
  }
226
185
  }
227
- var init_union = __esm({
228
- "src/mapping/handlers/union.ts"() {
229
- }
230
- });
231
-
232
- // src/mapping/handlers/index.ts
233
- var init_handlers = __esm({
234
- "src/mapping/handlers/index.ts"() {
235
- init_enum();
236
- init_nullable();
237
- init_record();
238
- init_union();
239
- }
240
- });
241
186
  function isZid(schema) {
242
187
  const metadata2 = registryHelpers.getMetadata(schema);
243
188
  return metadata2?.isConvexId === true && metadata2?.tableName && typeof metadata2.tableName === "string";
@@ -257,12 +202,12 @@ function getObjectShape(obj) {
257
202
  }
258
203
  return {};
259
204
  }
260
- var init_utils = __esm({
261
- "src/mapping/utils.ts"() {
262
- init_ids();
263
- }
264
- });
205
+
206
+ // src/mapping/core.ts
265
207
  function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set()) {
208
+ if (!zodValidator) {
209
+ return v.any();
210
+ }
266
211
  if (visited.has(zodValidator)) {
267
212
  return v.any();
268
213
  }
@@ -468,6 +413,13 @@ function zodToConvexInternal(zodValidator, visited = /* @__PURE__ */ new Set())
468
413
  convexValidator = v.any();
469
414
  break;
470
415
  default:
416
+ if (process.env.NODE_ENV !== "production") {
417
+ console.warn(
418
+ `[zodvex] Unrecognized Zod type "${defType}" encountered. Falling back to v.any().`,
419
+ "Schema:",
420
+ actualValidator
421
+ );
422
+ }
471
423
  convexValidator = v.any();
472
424
  break;
473
425
  }
@@ -492,33 +444,8 @@ function zodToConvexFields(zod) {
492
444
  }
493
445
  return result;
494
446
  }
495
- var init_core = __esm({
496
- "src/mapping/core.ts"() {
497
- init_ids();
498
- init_registry();
499
- init_handlers();
500
- init_utils();
501
- }
502
- });
503
-
504
- // src/mapping/index.ts
505
- var mapping_exports = {};
506
- __export(mapping_exports, {
507
- getObjectShape: () => getObjectShape,
508
- makeUnion: () => makeUnion,
509
- zodToConvex: () => zodToConvex,
510
- zodToConvexFields: () => zodToConvexFields
511
- });
512
- var init_mapping = __esm({
513
- "src/mapping/index.ts"() {
514
- init_core();
515
- init_utils();
516
- }
517
- });
518
447
 
519
448
  // src/codec.ts
520
- init_mapping();
521
- init_registry();
522
449
  function asZodType2(schema) {
523
450
  return schema;
524
451
  }
@@ -665,7 +592,6 @@ function fromConvexJS(value, schema) {
665
592
  }
666
593
  return value;
667
594
  }
668
- init_mapping();
669
595
  function pick(obj, keys) {
670
596
  const result = {};
671
597
  for (const key of keys) {
@@ -726,8 +652,7 @@ function toKeys(mask) {
726
652
  }
727
653
  function pickShape(schemaOrShape, mask) {
728
654
  const keys = toKeys(mask);
729
- const { getObjectShape: getObjectShape3 } = (init_mapping(), __toCommonJS(mapping_exports));
730
- const shape = schemaOrShape instanceof z.ZodObject ? getObjectShape3(schemaOrShape) : schemaOrShape || {};
655
+ const shape = schemaOrShape instanceof z.ZodObject ? getObjectShape(schemaOrShape) : schemaOrShape || {};
731
656
  const out = {};
732
657
  for (const k of keys) {
733
658
  if (k in shape) out[k] = shape[k];
@@ -738,8 +663,7 @@ function safePick(schema, mask) {
738
663
  return z.object(pickShape(schema, mask));
739
664
  }
740
665
  function safeOmit(schema, mask) {
741
- const { getObjectShape: getObjectShape3 } = (init_mapping(), __toCommonJS(mapping_exports));
742
- const shape = getObjectShape3(schema);
666
+ const shape = getObjectShape(schema);
743
667
  const omit = new Set(toKeys(mask));
744
668
  const keep = Object.keys(shape).filter((k) => !omit.has(k));
745
669
  const picked = pickShape(schema, keep);
@@ -787,10 +711,9 @@ function customFnBuilder(builder, customization) {
787
711
  handleZodValidationError(parsed.error, "args");
788
712
  }
789
713
  const finalCtx = { ...ctx, ...added?.ctx ?? {} };
790
- const finalArgs = {
791
- ...parsed.data,
792
- ...added?.args ?? {}
793
- };
714
+ const baseArgs = parsed.data;
715
+ const addedArgs = added?.args ?? {};
716
+ const finalArgs = { ...baseArgs, ...addedArgs };
794
717
  const ret = await handler(finalCtx, finalArgs);
795
718
  if (returns && !fn.skipConvexValidation) {
796
719
  let validated;
@@ -848,24 +771,6 @@ function customFnBuilder(builder, customization) {
848
771
  function zCustomQuery(query, customization) {
849
772
  return customFnBuilder(query, customization);
850
773
  }
851
- function zStrictQuery(query, customization) {
852
- return customFnBuilder(
853
- query,
854
- customization
855
- );
856
- }
857
- function zStrictMutation(mutation, customization) {
858
- return customFnBuilder(
859
- mutation,
860
- customization
861
- );
862
- }
863
- function zStrictAction(action, customization) {
864
- return customFnBuilder(
865
- action,
866
- customization
867
- );
868
- }
869
774
  function zCustomMutation(mutation, customization) {
870
775
  return customFnBuilder(
871
776
  mutation,
@@ -878,66 +783,28 @@ function zCustomAction(action, customization) {
878
783
  customization
879
784
  );
880
785
  }
881
-
882
- // src/index.ts
883
- init_ids();
884
- init_mapping();
885
- init_registry();
886
-
887
- // src/tables.ts
888
- init_ids();
889
- init_mapping();
890
- function zodDoc(tableName, schema) {
891
- return schema.extend({
892
- _id: zid(tableName),
893
- _creationTime: z.number()
894
- });
895
- }
896
- function zodDocOrNull(tableName, schema) {
897
- return z.union([zodDoc(tableName, schema), z.null()]);
898
- }
899
- function zodTable(name, shape) {
900
- const convexFields = zodToConvexFields(shape);
901
- const table = Table(name, convexFields);
902
- return Object.assign(table, {
903
- shape,
904
- zDoc: zodDoc(name, z.object(shape))
905
- });
906
- }
907
- function zodTableWithDocs(name, schema) {
908
- const convexFields = zodToConvexFields(schema.shape);
909
- const shape = getObjectShape(schema);
910
- const mapped = {};
911
- for (const [k, field] of Object.entries(shape)) {
912
- mapped[k] = mapDateFieldToNumber(field);
913
- }
914
- const docSchema = z.object({
915
- ...mapped,
916
- _id: zid(name),
917
- _creationTime: z.number()
918
- });
919
- const docArray = z.array(docSchema);
920
- const base = Table(name, convexFields);
921
- return { ...base, schema, docSchema, docArray };
922
- }
923
- init_mapping();
924
786
  var customCheckCache = /* @__PURE__ */ new WeakMap();
925
- function containsCustom(schema) {
787
+ function containsCustom(schema, maxDepth = 50, currentDepth = 0) {
926
788
  const cached = customCheckCache.get(schema);
927
789
  if (cached !== void 0) {
928
790
  return cached;
929
791
  }
792
+ if (currentDepth > maxDepth) {
793
+ return false;
794
+ }
930
795
  let result = false;
931
796
  if (schema._def?.typeName === "ZodCustom") {
932
797
  result = true;
933
798
  } else if (schema instanceof z.ZodUnion) {
934
- result = schema.options.some(containsCustom);
799
+ result = schema.options.some(
800
+ (opt) => containsCustom(opt, maxDepth, currentDepth + 1)
801
+ );
935
802
  } else if (schema instanceof z.ZodOptional) {
936
- result = containsCustom(schema.unwrap());
803
+ result = containsCustom(schema.unwrap(), maxDepth, currentDepth + 1);
937
804
  } else if (schema instanceof z.ZodNullable) {
938
- result = containsCustom(schema.unwrap());
805
+ result = containsCustom(schema.unwrap(), maxDepth, currentDepth + 1);
939
806
  } else if (schema instanceof z.ZodDefault) {
940
- result = containsCustom(schema.removeDefault());
807
+ result = containsCustom(schema.removeDefault(), maxDepth, currentDepth + 1);
941
808
  }
942
809
  customCheckCache.set(schema, result);
943
810
  return result;
@@ -981,9 +848,6 @@ function zQuery(query, input, handler, options) {
981
848
  }
982
849
  });
983
850
  }
984
- function zInternalQuery(internalQuery, input, handler, options) {
985
- return zQuery(internalQuery, input, handler, options);
986
- }
987
851
  function zMutation(mutation, input, handler, options) {
988
852
  let zodSchema;
989
853
  let args;
@@ -1023,9 +887,6 @@ function zMutation(mutation, input, handler, options) {
1023
887
  }
1024
888
  });
1025
889
  }
1026
- function zInternalMutation(internalMutation, input, handler, options) {
1027
- return zMutation(internalMutation, input, handler, options);
1028
- }
1029
890
  function zAction(action, input, handler, options) {
1030
891
  let zodSchema;
1031
892
  let args;
@@ -1065,10 +926,68 @@ function zAction(action, input, handler, options) {
1065
926
  }
1066
927
  });
1067
928
  }
1068
- function zInternalAction(internalAction, input, handler, options) {
1069
- return zAction(internalAction, input, handler, options);
929
+
930
+ // src/builders.ts
931
+ function zQueryBuilder(builder) {
932
+ return (config) => {
933
+ return zQuery(builder, config.args ?? {}, config.handler, {
934
+ returns: config.returns
935
+ });
936
+ };
937
+ }
938
+ function zMutationBuilder(builder) {
939
+ return (config) => {
940
+ return zMutation(builder, config.args ?? {}, config.handler, {
941
+ returns: config.returns
942
+ });
943
+ };
944
+ }
945
+ function zActionBuilder(builder) {
946
+ return (config) => {
947
+ return zAction(builder, config.args ?? {}, config.handler, {
948
+ returns: config.returns
949
+ });
950
+ };
951
+ }
952
+ function zCustomQueryBuilder(query, customization) {
953
+ return customFnBuilder(
954
+ query,
955
+ customization
956
+ );
957
+ }
958
+ function zCustomMutationBuilder(mutation, customization) {
959
+ return customFnBuilder(
960
+ mutation,
961
+ customization
962
+ );
963
+ }
964
+ function zCustomActionBuilder(action, customization) {
965
+ return customFnBuilder(
966
+ action,
967
+ customization
968
+ );
969
+ }
970
+ function zodDoc(tableName, schema) {
971
+ return schema.extend({
972
+ _id: zid(tableName),
973
+ _creationTime: z.number()
974
+ });
975
+ }
976
+ function zodDocOrNull(tableName, schema) {
977
+ return z.union([zodDoc(tableName, schema), z.null()]);
978
+ }
979
+ function zodTable(name, shape) {
980
+ const convexFields = zodToConvexFields(shape);
981
+ const table = Table(name, convexFields);
982
+ const zDoc = zodDoc(name, z.object(shape));
983
+ const docArray = z.array(zDoc);
984
+ return Object.assign(table, {
985
+ shape,
986
+ zDoc,
987
+ docArray
988
+ });
1070
989
  }
1071
990
 
1072
- export { convexCodec, findBaseCodec, formatZodIssues, fromConvexJS, getObjectShape, handleZodValidationError, isDateSchema, makeUnion, mapDateFieldToNumber, pick, pickShape, registerBaseCodec, registryHelpers, returnsAs, safeOmit, safePick, toConvexJS, zAction, zCustomAction, zCustomMutation, zCustomQuery, zInternalAction, zInternalMutation, zInternalQuery, zMutation, zPaginated, zQuery, zStrictAction, zStrictMutation, zStrictQuery, zid, zodDoc, zodDocOrNull, zodTable, zodTableWithDocs, zodToConvex, zodToConvexFields };
991
+ 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 };
1073
992
  //# sourceMappingURL=index.mjs.map
1074
993
  //# sourceMappingURL=index.mjs.map