zodvex 0.8.0-beta.1 → 0.8.0-beta.3

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.
Files changed (42) hide show
  1. package/README.md +20 -15
  2. package/dist/cli/index.js +56 -27
  3. package/dist/cli/index.js.map +1 -1
  4. package/dist/codegen/index.js +56 -27
  5. package/dist/codegen/index.js.map +1 -1
  6. package/dist/core/index.js +29 -29
  7. package/dist/core/index.js.map +1 -1
  8. package/dist/index.js +29 -29
  9. package/dist/index.js.map +1 -1
  10. package/dist/internal/functions/contracts.d.ts +1 -1
  11. package/dist/internal/functions/contracts.d.ts.map +1 -1
  12. package/dist/internal/functions/customFunctions.d.ts +5 -4
  13. package/dist/internal/functions/customFunctions.d.ts.map +1 -1
  14. package/dist/internal/functions/init.d.ts +12 -19
  15. package/dist/internal/functions/init.d.ts.map +1 -1
  16. package/dist/internal/mapping/core.d.ts.map +1 -1
  17. package/dist/internal/mapping/handlers/index.d.ts +1 -1
  18. package/dist/internal/mapping/handlers/index.d.ts.map +1 -1
  19. package/dist/internal/mapping/handlers/union.d.ts +1 -0
  20. package/dist/internal/mapping/handlers/union.d.ts.map +1 -1
  21. package/dist/internal/mapping/types.d.ts +24 -6
  22. package/dist/internal/mapping/types.d.ts.map +1 -1
  23. package/dist/legacy/index.js +29 -29
  24. package/dist/legacy/index.js.map +1 -1
  25. package/dist/mini/index.js +29 -29
  26. package/dist/mini/index.js.map +1 -1
  27. package/dist/mini/server/index.js +29 -29
  28. package/dist/mini/server/index.js.map +1 -1
  29. package/dist/public/codegen/generate.d.ts.map +1 -1
  30. package/dist/public/codegen/zodToSource.d.ts.map +1 -1
  31. package/dist/server/index.js +29 -29
  32. package/dist/server/index.js.map +1 -1
  33. package/package.json +1 -1
  34. package/src/internal/functions/contracts.ts +6 -6
  35. package/src/internal/functions/customFunctions.ts +6 -5
  36. package/src/internal/functions/init.ts +23 -12
  37. package/src/internal/mapping/core.ts +26 -19
  38. package/src/internal/mapping/handlers/index.ts +1 -1
  39. package/src/internal/mapping/handlers/union.ts +14 -12
  40. package/src/internal/mapping/types.ts +190 -102
  41. package/src/public/codegen/generate.ts +84 -41
  42. package/src/public/codegen/zodToSource.ts +14 -4
package/README.md CHANGED
@@ -208,26 +208,31 @@ Guides: [Codegen](./docs/guide/codegen.md) | Example: [examples/task-manager/](.
208
208
 
209
209
  ## Supported Types
210
210
 
211
- | Zod Type | Convex Validator |
212
- | -------------------- | ------------------------------------------- |
213
- | `z.string()` | `v.string()` |
214
- | `z.number()` | `v.float64()` |
215
- | `z.bigint()` | `v.int64()` |
216
- | `z.boolean()` | `v.boolean()` |
217
- | `z.null()` | `v.null()` |
218
- | `z.array(T)` | `v.array(T)` |
219
- | `z.object({...})` | `v.object({...})` |
220
- | `z.record(T)` | `v.record(v.string(), T)` |
221
- | `z.union([...])` | `v.union(...)` |
222
- | `z.literal(x)` | `v.literal(x)` |
223
- | `z.enum(['a', 'b'])` | `v.union(v.literal('a'), v.literal('b'))` ¹ |
224
- | `z.optional(T)` | `v.optional(T)` |
225
- | `z.nullable(T)` | `v.union(T, v.null())` |
211
+ | Zod Type | Convex Validator |
212
+ | -------------------------------------- | ------------------------------------------- |
213
+ | `z.string()` | `v.string()` |
214
+ | `z.number()` | `v.float64()` |
215
+ | `z.bigint()` | `v.int64()` |
216
+ | `z.boolean()` | `v.boolean()` |
217
+ | `z.null()` | `v.null()` |
218
+ | `z.array(T)` | `v.array(T)` |
219
+ | `z.object({...})` | `v.object({...})` |
220
+ | `z.record(T)` | `v.record(v.string(), T)` |
221
+ | `z.tuple([A, B])` | `v.array(v.union(A, B))` ² |
222
+ | `z.union([...])` | `v.union(...)` |
223
+ | `z.literal(x)` | `v.literal(x)` |
224
+ | `z.literal([a, b])` | `v.union(v.literal(a), v.literal(b))` |
225
+ | `z.literal(null)` | `v.null()` |
226
+ | `z.enum(['a', 'b'])` | `v.union(v.literal('a'), v.literal('b'))` ¹ |
227
+ | `z.optional(T)` | `v.optional(T)` |
228
+ | `z.nullable(T)` | `v.union(T, v.null())` |
226
229
 
227
230
  > **Note:** Native `z.date()` is **not supported** — use `zx.date()` instead. See [Date Handling](./docs/guide/date-handling.md) for details.
228
231
 
229
232
  ¹ Enum unions carry a cosmetically different TypeScript signature than hand-written `v.union(v.literal(...))` calls — identical values, runtime validation, and type safety. Details: [Mapping Helpers](./docs/guide/mapping-helpers.md#zod-v4-enum-type-note).
230
233
 
234
+ ² Convex has no fixed-length tuple validator. Tuples map to arrays whose elements are the union of item (and rest) validators, so element count and position are enforced only where zodvex runs Zod parsing (e.g. declared function args), not by Convex storage validation.
235
+
231
236
  For zodvex's own types — `zx.id()`, `zx.date()`, `zx.codec()` — and exactly how they map to Convex validators, see [The zx Namespace](./docs/guide/zx-namespace.md).
232
237
 
233
238
  ## Upgrading?
package/dist/cli/index.js CHANGED
@@ -1975,10 +1975,9 @@ function zodToSource(schema, ctx) {
1975
1975
  return `z.enum([${values}])`;
1976
1976
  }
1977
1977
  if (schema instanceof $ZodLiteral) {
1978
- const values = schema._zod.def.values;
1979
- const value = values.values().next().value;
1980
- if (typeof value === "string") return `z.literal(${JSON.stringify(value)})`;
1981
- return `z.literal(${value})`;
1978
+ const values = [...schema._zod.def.values];
1979
+ const render = (v7) => typeof v7 === "string" ? JSON.stringify(v7) : v7 === null ? "null" : v7 === void 0 ? "undefined" : typeof v7 === "bigint" ? `${v7}n` : String(v7);
1980
+ return values.length === 1 ? `z.literal(${render(values[0])})` : `z.literal([${values.map(render).join(", ")}])`;
1982
1981
  }
1983
1982
  if (schema instanceof $ZodUnion) {
1984
1983
  const members = schema._zod.def.options.map((s) => zodToSource(s, ctx)).join(", ");
@@ -2012,34 +2011,64 @@ const __lazy = (key, build) => {
2012
2011
  function fingerprintCodec(schema) {
2013
2012
  if (!(schema instanceof $ZodCodec)) return "";
2014
2013
  const def = schema._zod.def;
2014
+ const wire = fingerprintTree(def.in);
2015
+ const runtime = fingerprintTree(def.out);
2016
+ if (wire === null || runtime === null) return "";
2015
2017
  const transform = typeof def.transform === "function" ? def.transform.toString() : "";
2016
2018
  const reverse = typeof def.reverseTransform === "function" ? def.reverseTransform.toString() : "";
2017
- return `${fingerprintLeaf(def.in)}|${fingerprintLeaf(def.out)}|${transform}|${reverse}`;
2019
+ return `${zodToSource(def.in)}#${wire}|${zodToSource(def.out)}#${runtime}|${transform}|${reverse}`;
2018
2020
  }
2019
- function fingerprintLeaf(schema) {
2020
- return `${zodToSource(schema)}#${fingerprintChecks(schema)}`;
2021
+ var UNCOMPARABLE = /* @__PURE__ */ Symbol("zodvex.fingerprint.uncomparable");
2022
+ function fingerprintTree(schema) {
2023
+ try {
2024
+ return serializeSchema(schema, /* @__PURE__ */ new Set());
2025
+ } catch (error) {
2026
+ if (error === UNCOMPARABLE) return null;
2027
+ throw error;
2028
+ }
2021
2029
  }
2022
- function fingerprintChecks(schema) {
2023
- const checks = schema?._zod?.def?.checks;
2024
- if (!Array.isArray(checks) || checks.length === 0) return "";
2030
+ var IGNORED_DEF_KEYS = /* @__PURE__ */ new Set(["error", "message"]);
2031
+ function serializeSchema(schema, path7) {
2032
+ if (path7.has(schema)) throw UNCOMPARABLE;
2033
+ const def = schema._zod.def;
2034
+ if (def.type === "lazy") throw UNCOMPARABLE;
2035
+ path7.add(schema);
2025
2036
  const parts = [];
2026
- for (const check of checks) {
2027
- const def = check?._zod?.def;
2028
- if (!def) continue;
2029
- const checkType = def.check ?? def.type ?? "check";
2030
- const data = {};
2031
- for (const [k, v7] of Object.entries(def)) {
2032
- if (k === "check" || k === "type" || k === "error" || k === "message") continue;
2033
- const t = typeof v7;
2034
- if (t === "string" || t === "number" || t === "boolean" || v7 === null) {
2035
- data[k] = v7;
2036
- } else if (v7 instanceof RegExp) {
2037
- data[k] = v7.source;
2038
- }
2039
- }
2040
- parts.push(`${checkType}(${JSON.stringify(data)})`);
2037
+ for (const key of Object.keys(def).sort()) {
2038
+ if (IGNORED_DEF_KEYS.has(key)) continue;
2039
+ const value = key === "checks" ? serializeChecks(def.checks, path7) : serializeValue(def[key], path7);
2040
+ parts.push(`${key}=${value}`);
2041
2041
  }
2042
- return parts.sort().join("&");
2042
+ path7.delete(schema);
2043
+ return `{${parts.join(",")}}`;
2044
+ }
2045
+ function serializeChecks(checks, path7) {
2046
+ if (!Array.isArray(checks)) return "[]";
2047
+ const parts = checks.map((check) => {
2048
+ const def = check?._zod?.def;
2049
+ if (!def) return "?";
2050
+ if (def.check === "custom") throw UNCOMPARABLE;
2051
+ const fields = Object.keys(def).sort().filter((key) => !IGNORED_DEF_KEYS.has(key)).map((key) => `${key}=${serializeValue(def[key], path7)}`);
2052
+ return `(${fields.join(",")})`;
2053
+ });
2054
+ return `[${parts.join("&")}]`;
2055
+ }
2056
+ function serializeValue(value, path7) {
2057
+ if (value instanceof $ZodType) return serializeSchema(value, path7);
2058
+ if (typeof value === "function") return `fn:${value.toString()}`;
2059
+ if (typeof value === "bigint") return `${value}n`;
2060
+ if (typeof value === "symbol") return value.toString();
2061
+ if (value === void 0) return "undefined";
2062
+ if (value === null || typeof value !== "object") return JSON.stringify(value);
2063
+ if (value instanceof RegExp) return `re:/${value.source}/${value.flags}`;
2064
+ if (value instanceof Date) return `date:${value.toISOString()}`;
2065
+ if (Array.isArray(value)) return `[${value.map((v7) => serializeValue(v7, path7)).join(",")}]`;
2066
+ const proto = Object.getPrototypeOf(value);
2067
+ if (proto !== Object.prototype && proto !== null) throw UNCOMPARABLE;
2068
+ const entries = Object.keys(value).sort().map(
2069
+ (key) => `${JSON.stringify(key)}:${serializeValue(value[key], path7)}`
2070
+ );
2071
+ return `{${entries.join(",")}}`;
2043
2072
  }
2044
2073
  function generateSchemaFile(models) {
2045
2074
  const exports$1 = models.map((m) => {
@@ -2298,7 +2327,7 @@ function generateApiFile(functions, models, codecs, modelCodecs, functionCodecs,
2298
2327
  if (undiscoverable.length > 0) {
2299
2328
  const list = undiscoverable.map((u) => ` - ${u.fn}() at ${u.path}`).join("\n");
2300
2329
  throw new Error(
2301
- `[zodvex] ${undiscoverable.length} codec(s) in function args/returns have no importable reference (not exported standalone, not embedded in a model). The generated client cannot encode or decode them, which silently breaks the codec boundary. Export each codec standalone (or add it to a model) so codegen can import it:
2330
+ `[zodvex] ${undiscoverable.length} codec(s) in function args/returns have no importable reference (not exported standalone, not embedded in a model, or refined with .refine()/.superRefine()/.check(), which codegen cannot match by structure). The generated client cannot encode or decode them, which silently breaks the codec boundary. Export each codec standalone (or add it to a model) so codegen can import it:
2302
2331
  ${list}`
2303
2332
  );
2304
2333
  }