jazz-tools 0.15.16 → 0.16.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.
Files changed (203) hide show
  1. package/.svelte-kit/__package__/jazz.class.svelte.d.ts +2 -2
  2. package/.svelte-kit/__package__/jazz.class.svelte.d.ts.map +1 -1
  3. package/.svelte-kit/__package__/jazz.class.svelte.js +5 -5
  4. package/.svelte-kit/__package__/jazz.svelte.d.ts +2 -2
  5. package/.svelte-kit/__package__/jazz.svelte.d.ts.map +1 -1
  6. package/.turbo/turbo-build.log +44 -44
  7. package/CHANGELOG.md +34 -0
  8. package/dist/browser/index.d.ts +2 -2
  9. package/dist/browser/index.d.ts.map +1 -1
  10. package/dist/browser/index.js.map +1 -1
  11. package/dist/browser-media-images/index.d.ts +3 -1
  12. package/dist/browser-media-images/index.d.ts.map +1 -1
  13. package/dist/browser-media-images/index.js.map +1 -1
  14. package/dist/{chunk-OSVAAVWQ.js → chunk-CL3ROOZM.js} +598 -598
  15. package/dist/chunk-CL3ROOZM.js.map +1 -0
  16. package/dist/index.js +8 -10
  17. package/dist/index.js.map +1 -1
  18. package/dist/react/hooks.d.ts +2 -2
  19. package/dist/react/hooks.d.ts.map +1 -1
  20. package/dist/react/index.js.map +1 -1
  21. package/dist/react-core/hooks.d.ts +2 -2
  22. package/dist/react-core/hooks.d.ts.map +1 -1
  23. package/dist/react-core/index.js +3 -3
  24. package/dist/react-core/index.js.map +1 -1
  25. package/dist/react-native-core/hooks.d.ts +2 -2
  26. package/dist/react-native-core/hooks.d.ts.map +1 -1
  27. package/dist/react-native-core/index.js.map +1 -1
  28. package/dist/svelte/jazz.class.svelte.d.ts +2 -2
  29. package/dist/svelte/jazz.class.svelte.d.ts.map +1 -1
  30. package/dist/svelte/jazz.class.svelte.js +5 -5
  31. package/dist/svelte/jazz.svelte.d.ts +2 -2
  32. package/dist/svelte/jazz.svelte.d.ts.map +1 -1
  33. package/dist/testing.js +3 -3
  34. package/dist/testing.js.map +1 -1
  35. package/dist/tools/coValues/CoValueBase.d.ts +3 -13
  36. package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
  37. package/dist/tools/coValues/account.d.ts +2 -2
  38. package/dist/tools/coValues/account.d.ts.map +1 -1
  39. package/dist/tools/coValues/coFeed.d.ts.map +1 -1
  40. package/dist/tools/coValues/coList.d.ts.map +1 -1
  41. package/dist/tools/coValues/coMap.d.ts +8 -23
  42. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  43. package/dist/tools/coValues/deepLoading.d.ts +4 -1
  44. package/dist/tools/coValues/deepLoading.d.ts.map +1 -1
  45. package/dist/tools/coValues/extensions/imageDef.d.ts +4 -7
  46. package/dist/tools/coValues/extensions/imageDef.d.ts.map +1 -1
  47. package/dist/tools/coValues/inbox.d.ts +2 -2
  48. package/dist/tools/coValues/inbox.d.ts.map +1 -1
  49. package/dist/tools/coValues/interfaces.d.ts +2 -17
  50. package/dist/tools/coValues/interfaces.d.ts.map +1 -1
  51. package/dist/tools/coValues/request.d.ts +3 -3
  52. package/dist/tools/coValues/request.d.ts.map +1 -1
  53. package/dist/tools/coValues/schemaUnion.d.ts +5 -1
  54. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  55. package/dist/tools/exports.d.ts +3 -3
  56. package/dist/tools/exports.d.ts.map +1 -1
  57. package/dist/tools/implementation/createContext.d.ts +4 -4
  58. package/dist/tools/implementation/createContext.d.ts.map +1 -1
  59. package/dist/tools/implementation/invites.d.ts +2 -2
  60. package/dist/tools/implementation/invites.d.ts.map +1 -1
  61. package/dist/tools/implementation/schemaUtils.d.ts +8 -0
  62. package/dist/tools/implementation/schemaUtils.d.ts.map +1 -0
  63. package/dist/tools/implementation/zodSchema/coExport.d.ts +11 -1
  64. package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
  65. package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts +22 -0
  66. package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts.map +1 -0
  67. package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts +10 -0
  68. package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts.map +1 -0
  69. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +11 -11
  70. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  71. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +34 -24
  72. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -1
  73. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +17 -14
  74. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  75. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +24 -17
  76. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  77. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +31 -34
  78. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  79. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts +20 -9
  80. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts.map +1 -1
  81. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +19 -12
  82. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  83. package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts +18 -0
  84. package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts.map +1 -0
  85. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +14 -9
  86. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  87. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +14 -9
  88. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  89. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +14 -9
  90. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  91. package/dist/tools/implementation/zodSchema/typeConverters/CoFieldInit.d.ts +10 -0
  92. package/dist/tools/implementation/zodSchema/typeConverters/CoFieldInit.d.ts.map +1 -0
  93. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +15 -13
  94. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  95. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +12 -15
  96. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -1
  97. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +17 -20
  98. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -1
  99. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +17 -20
  100. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -1
  101. package/dist/tools/implementation/zodSchema/unionUtils.d.ts +3 -5
  102. package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
  103. package/dist/tools/implementation/zodSchema/zodCo.d.ts +10 -8
  104. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  105. package/dist/tools/implementation/zodSchema/zodReExport.d.ts +4 -8
  106. package/dist/tools/implementation/zodSchema/zodReExport.d.ts.map +1 -1
  107. package/dist/tools/implementation/zodSchema/zodSchema.d.ts +21 -30
  108. package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  109. package/dist/tools/internal.d.ts +3 -2
  110. package/dist/tools/internal.d.ts.map +1 -1
  111. package/dist/tools/lib/utilityTypes.d.ts +18 -0
  112. package/dist/tools/lib/utilityTypes.d.ts.map +1 -0
  113. package/dist/tools/subscribe/utils.d.ts.map +1 -1
  114. package/dist/tools/testing.d.ts +2 -2
  115. package/dist/tools/testing.d.ts.map +1 -1
  116. package/dist/worker/index.d.ts.map +1 -1
  117. package/dist/worker/index.js +2 -2
  118. package/dist/worker/index.js.map +1 -1
  119. package/package.json +5 -5
  120. package/src/browser/index.ts +2 -4
  121. package/src/browser-media-images/index.ts +1 -1
  122. package/src/react/hooks.tsx +2 -2
  123. package/src/react-core/hooks.ts +6 -6
  124. package/src/react-core/tests/useAccount.test.ts +2 -2
  125. package/src/react-core/tests/useCoState.test.ts +3 -2
  126. package/src/react-native-core/hooks.tsx +2 -2
  127. package/src/svelte/jazz.class.svelte.ts +10 -7
  128. package/src/svelte/jazz.svelte.ts +2 -2
  129. package/src/tools/coValues/CoValueBase.ts +8 -20
  130. package/src/tools/coValues/account.ts +18 -14
  131. package/src/tools/coValues/coFeed.ts +0 -4
  132. package/src/tools/coValues/coList.ts +7 -9
  133. package/src/tools/coValues/coMap.ts +5 -13
  134. package/src/tools/coValues/coPlainText.ts +4 -4
  135. package/src/tools/coValues/deepLoading.ts +4 -1
  136. package/src/tools/coValues/extensions/imageDef.ts +3 -3
  137. package/src/tools/coValues/inbox.ts +4 -4
  138. package/src/tools/coValues/interfaces.ts +4 -71
  139. package/src/tools/coValues/request.ts +17 -15
  140. package/src/tools/coValues/schemaUnion.ts +8 -4
  141. package/src/tools/exports.ts +3 -14
  142. package/src/tools/implementation/createContext.ts +9 -9
  143. package/src/tools/implementation/invites.ts +2 -2
  144. package/src/tools/implementation/schemaUtils.ts +18 -0
  145. package/src/tools/implementation/zodSchema/coExport.ts +14 -0
  146. package/src/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.ts +156 -0
  147. package/src/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.ts +149 -0
  148. package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +36 -17
  149. package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +101 -52
  150. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +54 -50
  151. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +54 -46
  152. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +190 -161
  153. package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +29 -27
  154. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +28 -18
  155. package/src/tools/implementation/zodSchema/schemaTypes/CoValueSchema.ts +18 -0
  156. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +45 -36
  157. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +47 -35
  158. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +43 -30
  159. package/src/tools/implementation/zodSchema/typeConverters/CoFieldInit.ts +13 -0
  160. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +30 -26
  161. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +31 -28
  162. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +89 -80
  163. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +95 -84
  164. package/src/tools/implementation/zodSchema/unionUtils.ts +47 -51
  165. package/src/tools/implementation/zodSchema/zodCo.ts +43 -76
  166. package/src/tools/implementation/zodSchema/zodReExport.ts +44 -24
  167. package/src/tools/implementation/zodSchema/zodSchema.ts +65 -103
  168. package/src/tools/internal.ts +3 -2
  169. package/src/tools/lib/utilityTypes.ts +16 -0
  170. package/src/tools/subscribe/utils.ts +4 -2
  171. package/src/tools/testing.ts +4 -4
  172. package/src/tools/tests/ContextManager.test.ts +8 -9
  173. package/src/tools/tests/account.test.ts +65 -3
  174. package/src/tools/tests/coDiscriminatedUnion.test-d.ts +38 -0
  175. package/src/tools/tests/coDiscriminatedUnion.test.ts +219 -1
  176. package/src/tools/tests/coFeed.test-d.ts +4 -3
  177. package/src/tools/tests/coFeed.test.ts +8 -6
  178. package/src/tools/tests/coList.test-d.ts +32 -3
  179. package/src/tools/tests/coList.test.ts +30 -2
  180. package/src/tools/tests/coMap.record.test-d.ts +31 -3
  181. package/src/tools/tests/coMap.record.test.ts +18 -9
  182. package/src/tools/tests/coMap.test-d.ts +36 -8
  183. package/src/tools/tests/coMap.test.ts +87 -6
  184. package/src/tools/tests/coOptional.test.ts +63 -1
  185. package/src/tools/tests/createContext.test.ts +7 -9
  186. package/src/tools/tests/deepLoading.test.ts +4 -10
  187. package/src/tools/tests/exportImport.test.ts +2 -2
  188. package/src/tools/tests/groupsAndAccounts.test.ts +5 -4
  189. package/src/tools/tests/inbox.test.ts +3 -2
  190. package/src/tools/tests/load.test.ts +3 -29
  191. package/src/tools/tests/schemaUnion.test.ts +2 -2
  192. package/src/tools/tests/subscribe.test.ts +22 -114
  193. package/src/tools/tests/testing.test.ts +6 -6
  194. package/src/tools/tests/zod.test-d.ts +27 -0
  195. package/src/tools/tests/zod.test.ts +88 -45
  196. package/src/worker/index.ts +0 -1
  197. package/dist/chunk-OSVAAVWQ.js.map +0 -1
  198. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +0 -12
  199. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +0 -1
  200. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +0 -9
  201. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +0 -1
  202. package/src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +0 -172
  203. package/src/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +0 -218
@@ -0,0 +1,156 @@
1
+ import { RawCoList, RawCoMap } from "cojson";
2
+ import {
3
+ Account,
4
+ CoDiscriminatedUnionSchema,
5
+ CoFeed,
6
+ CoFeedSchema,
7
+ CoList,
8
+ CoListSchema,
9
+ CoMap,
10
+ CoPlainText,
11
+ CoRichText,
12
+ CoValueClass,
13
+ FileStream,
14
+ FileStreamSchema,
15
+ PlainTextSchema,
16
+ SchemaUnion,
17
+ enrichAccountSchema,
18
+ enrichCoMapSchema,
19
+ isCoValueClass,
20
+ } from "../../../internal.js";
21
+ import { coField } from "../../schema.js";
22
+
23
+ import { CoreCoValueSchema } from "../schemaTypes/CoValueSchema.js";
24
+ import { RichTextSchema } from "../schemaTypes/RichTextSchema.js";
25
+ import { schemaUnionDiscriminatorFor } from "../unionUtils.js";
26
+ import {
27
+ AnyCoreCoValueSchema,
28
+ AnyZodOrCoValueSchema,
29
+ CoValueClassFromAnySchema,
30
+ CoValueClassOrSchema,
31
+ CoValueSchemaFromCoreSchema,
32
+ } from "../zodSchema.js";
33
+ import {
34
+ SchemaField,
35
+ schemaFieldToCoFieldDef,
36
+ } from "./schemaFieldToCoFieldDef.js";
37
+
38
+ // Note: if you're editing this function, edit the `isAnyCoValueSchema`
39
+ // function in `zodReExport.ts` as well
40
+ export function isAnyCoValueSchema(
41
+ schema: AnyZodOrCoValueSchema | CoValueClass,
42
+ ): schema is AnyCoreCoValueSchema {
43
+ return "collaborative" in schema && schema.collaborative === true;
44
+ }
45
+
46
+ export function isCoValueSchema(
47
+ schema: AnyZodOrCoValueSchema | CoValueClass,
48
+ ): schema is CoValueSchemaFromCoreSchema<AnyCoreCoValueSchema> {
49
+ return isAnyCoValueSchema(schema) && "getCoValueClass" in schema;
50
+ }
51
+
52
+ /**
53
+ * Convert a "core" CoValue schema into a CoValue schema.
54
+ * See {@link CoreCoValueSchema} for more information.
55
+ *
56
+ * @returns The CoValue schema matching the provided CoreCoValueSchema
57
+ */
58
+ export function hydrateCoreCoValueSchema<S extends AnyCoreCoValueSchema>(
59
+ schema: S,
60
+ ): CoValueSchemaFromCoreSchema<S> {
61
+ if (isCoValueSchema(schema)) {
62
+ // If the schema is already a CoValue schema, return it as is
63
+ return schema as any;
64
+ }
65
+
66
+ if (schema.builtin === "CoOptional") {
67
+ throw new Error(
68
+ `co.optional() of collaborative types is not supported as top-level schema: ${JSON.stringify(schema)}`,
69
+ );
70
+ } else if (schema.builtin === "CoMap" || schema.builtin === "Account") {
71
+ const def = schema.getDefinition();
72
+ const ClassToExtend = schema.builtin === "Account" ? Account : CoMap;
73
+
74
+ const coValueClass = class ZCoMap extends ClassToExtend {
75
+ constructor(options: { fromRaw: RawCoMap } | undefined) {
76
+ super(options);
77
+ for (const [fieldName, fieldType] of Object.entries(def.shape)) {
78
+ (this as any)[fieldName] = schemaFieldToCoFieldDef(
79
+ fieldType as SchemaField,
80
+ );
81
+ }
82
+ if (def.catchall) {
83
+ (this as any)[coField.items] = schemaFieldToCoFieldDef(
84
+ def.catchall as SchemaField,
85
+ );
86
+ }
87
+ }
88
+ };
89
+
90
+ const coValueSchema =
91
+ ClassToExtend === Account
92
+ ? enrichAccountSchema(schema as any, coValueClass as any)
93
+ : enrichCoMapSchema(schema as any, coValueClass as any);
94
+
95
+ return coValueSchema as unknown as CoValueSchemaFromCoreSchema<S>;
96
+ } else if (schema.builtin === "CoList") {
97
+ const element = schema.element;
98
+ const coValueClass = class ZCoList extends CoList {
99
+ constructor(options: { fromRaw: RawCoList } | undefined) {
100
+ super(options);
101
+ (this as any)[coField.items] = schemaFieldToCoFieldDef(
102
+ element as SchemaField,
103
+ );
104
+ }
105
+ };
106
+
107
+ const coValueSchema = new CoListSchema(element, coValueClass as any);
108
+
109
+ return coValueSchema as unknown as CoValueSchemaFromCoreSchema<S>;
110
+ } else if (schema.builtin === "CoFeed") {
111
+ const coValueClass = CoFeed.Of(
112
+ schemaFieldToCoFieldDef(schema.element as SchemaField),
113
+ );
114
+ const coValueSchema = new CoFeedSchema(schema.element, coValueClass);
115
+ return coValueSchema as unknown as CoValueSchemaFromCoreSchema<S>;
116
+ } else if (schema.builtin === "FileStream") {
117
+ const coValueClass = FileStream;
118
+ return new FileStreamSchema(coValueClass) as CoValueSchemaFromCoreSchema<S>;
119
+ } else if (schema.builtin === "CoPlainText") {
120
+ const coValueClass = CoPlainText;
121
+ return new PlainTextSchema(coValueClass) as CoValueSchemaFromCoreSchema<S>;
122
+ } else if (schema.builtin === "CoRichText") {
123
+ const coValueClass = CoRichText;
124
+ return new RichTextSchema(coValueClass) as CoValueSchemaFromCoreSchema<S>;
125
+ } else if (schema.builtin === "CoDiscriminatedUnion") {
126
+ const coValueClass = SchemaUnion.Of(schemaUnionDiscriminatorFor(schema));
127
+ const coValueSchema = new CoDiscriminatedUnionSchema(schema, coValueClass);
128
+ return coValueSchema as CoValueSchemaFromCoreSchema<S>;
129
+ } else {
130
+ const notReachable: never = schema;
131
+ throw new Error(
132
+ `Unsupported zod CoValue type for top-level schema: ${JSON.stringify(notReachable, undefined, 2)}`,
133
+ );
134
+ }
135
+ }
136
+
137
+ /**
138
+ * Convert a CoValue class or a CoValue schema into a CoValue class.
139
+ *
140
+ * This function bridges the gap between CoValue classes created with the class syntax,
141
+ * and CoValue classes created with our `co.` definers.
142
+ *
143
+ * @param schema A CoValue class or a CoValue schema
144
+ * @returns The same CoValue class, or a CoValue class that matches the provided schema
145
+ */
146
+ export function coValueClassFromCoValueClassOrSchema<
147
+ S extends CoValueClassOrSchema,
148
+ >(schema: S): CoValueClassFromAnySchema<S> {
149
+ if (isCoValueClass(schema)) {
150
+ return schema as any;
151
+ } else if (isCoValueSchema(schema)) {
152
+ return schema.getCoValueClass() as any;
153
+ }
154
+
155
+ throw new Error(`Unsupported schema: ${JSON.stringify(schema)}`);
156
+ }
@@ -0,0 +1,149 @@
1
+ import { CoValueClass, isCoValueClass } from "../../../internal.js";
2
+ import { coField } from "../../schema.js";
3
+ import { CoreCoValueSchema } from "../schemaTypes/CoValueSchema.js";
4
+ import { isUnionOfPrimitivesDeeply } from "../unionUtils.js";
5
+ import {
6
+ ZodCatch,
7
+ ZodDefault,
8
+ ZodLazy,
9
+ ZodReadonly,
10
+ z,
11
+ } from "../zodReExport.js";
12
+ import { ZodPrimitiveSchema } from "../zodSchema.js";
13
+ import { isCoValueSchema } from "./coValueSchemaTransformation.js";
14
+
15
+ /**
16
+ * Types of objects that can be nested inside CoValue schema containers
17
+ */
18
+ export type SchemaField =
19
+ // Schemas created with co.map(), co.record(), co.list(), etc.
20
+ | CoreCoValueSchema
21
+ // CoValue classes created with class syntax, or framework-provided classes like Group
22
+ | CoValueClass
23
+ | ZodPrimitiveSchema
24
+ | z.core.$ZodOptional<z.core.$ZodType>
25
+ | z.core.$ZodNullable<z.core.$ZodType>
26
+ | z.core.$ZodUnion<z.core.$ZodType[]>
27
+ | z.core.$ZodDiscriminatedUnion<z.core.$ZodType[]>
28
+ | z.core.$ZodObject<z.core.$ZodLooseShape>
29
+ | z.core.$ZodArray<z.core.$ZodType>
30
+ | z.core.$ZodTuple<z.core.$ZodType[]>
31
+ | z.core.$ZodReadonly<z.core.$ZodType>
32
+ | z.core.$ZodLazy<z.core.$ZodType>
33
+ | z.core.$ZodTemplateLiteral<any>
34
+ | z.core.$ZodLiteral<any>
35
+ | z.core.$ZodCatch<z.core.$ZodType>
36
+ | z.core.$ZodEnum<any>
37
+ | z.core.$ZodDefault<z.core.$ZodType>
38
+ | z.core.$ZodCatch<z.core.$ZodType>;
39
+
40
+ export function schemaFieldToCoFieldDef(schema: SchemaField) {
41
+ if (isCoValueClass(schema)) {
42
+ return coField.ref(schema);
43
+ } else if (isCoValueSchema(schema)) {
44
+ if (schema.builtin === "CoOptional") {
45
+ return coField.ref(schema.getCoValueClass(), {
46
+ optional: true,
47
+ });
48
+ }
49
+ return coField.ref(schema.getCoValueClass());
50
+ } else {
51
+ if ("_zod" in schema) {
52
+ const zodSchemaDef = schema._zod.def;
53
+ if (
54
+ zodSchemaDef.type === "optional" ||
55
+ zodSchemaDef.type === "nullable"
56
+ ) {
57
+ const inner = zodSchemaDef.innerType as ZodPrimitiveSchema;
58
+ const coFieldDef: any = schemaFieldToCoFieldDef(inner);
59
+ if (
60
+ zodSchemaDef.type === "nullable" &&
61
+ coFieldDef === coField.optional.Date
62
+ ) {
63
+ // We do not currently have a way to encode null Date coFields.
64
+ // We only support encoding optional (i.e. Date | undefined) coFields.
65
+ throw new Error("Nullable z.date() is not supported");
66
+ }
67
+ // Primitive coField types support null and undefined as values,
68
+ // so we can just return the inner type here and rely on support
69
+ // for null/undefined at the type level
70
+ return coFieldDef;
71
+ } else if (zodSchemaDef.type === "string") {
72
+ return coField.string;
73
+ } else if (zodSchemaDef.type === "number") {
74
+ return coField.number;
75
+ } else if (zodSchemaDef.type === "boolean") {
76
+ return coField.boolean;
77
+ } else if (zodSchemaDef.type === "null") {
78
+ return coField.null;
79
+ } else if (zodSchemaDef.type === "enum") {
80
+ return coField.string;
81
+ } else if (zodSchemaDef.type === "readonly") {
82
+ return schemaFieldToCoFieldDef(
83
+ (schema as unknown as ZodReadonly).def.innerType as SchemaField,
84
+ );
85
+ } else if (zodSchemaDef.type === "date") {
86
+ return coField.optional.Date;
87
+ } else if (zodSchemaDef.type === "template_literal") {
88
+ return coField.string;
89
+ } else if (zodSchemaDef.type === "lazy") {
90
+ // Mostly to support z.json()
91
+ return schemaFieldToCoFieldDef(
92
+ (schema as unknown as ZodLazy).unwrap() as SchemaField,
93
+ );
94
+ } else if (
95
+ zodSchemaDef.type === "default" ||
96
+ zodSchemaDef.type === "catch"
97
+ ) {
98
+ console.warn(
99
+ "z.default()/z.catch() are not supported in collaborative schemas. They will be ignored.",
100
+ );
101
+
102
+ return schemaFieldToCoFieldDef(
103
+ (schema as unknown as ZodDefault | ZodCatch).def
104
+ .innerType as SchemaField,
105
+ );
106
+ } else if (zodSchemaDef.type === "literal") {
107
+ if (
108
+ zodSchemaDef.values.some((literal) => typeof literal === "undefined")
109
+ ) {
110
+ throw new Error("z.literal() with undefined is not supported");
111
+ }
112
+ if (zodSchemaDef.values.some((literal) => literal === null)) {
113
+ throw new Error("z.literal() with null is not supported");
114
+ }
115
+ if (
116
+ zodSchemaDef.values.some((literal) => typeof literal === "bigint")
117
+ ) {
118
+ throw new Error("z.literal() with bigint is not supported");
119
+ }
120
+ return coField.literal(
121
+ ...(zodSchemaDef.values as Exclude<
122
+ (typeof zodSchemaDef.values)[number],
123
+ undefined | null | bigint
124
+ >[]),
125
+ );
126
+ } else if (
127
+ zodSchemaDef.type === "object" ||
128
+ zodSchemaDef.type === "array" ||
129
+ zodSchemaDef.type === "tuple"
130
+ ) {
131
+ return coField.json();
132
+ } else if (zodSchemaDef.type === "union") {
133
+ if (isUnionOfPrimitivesDeeply(schema)) {
134
+ return coField.json();
135
+ } else {
136
+ throw new Error(
137
+ "z.union()/z.discriminatedUnion() of collaborative types is not supported. Use co.discriminatedUnion() instead.",
138
+ );
139
+ }
140
+ } else {
141
+ throw new Error(
142
+ `Unsupported zod type: ${(schema._zod?.def as any)?.type || JSON.stringify(schema)}`,
143
+ );
144
+ }
145
+ } else {
146
+ throw new Error(`Unsupported zod type: ${schema}`);
147
+ }
148
+ }
149
+ }
@@ -9,8 +9,13 @@ import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
9
9
  import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
10
10
  import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
11
11
  import { z } from "../zodReExport.js";
12
- import { Loaded, ResolveQuery } from "../zodSchema.js";
13
- import { AnyCoMapSchema, CoMapSchema } from "./CoMapSchema.js";
12
+ import { AnyZodOrCoValueSchema, Loaded, ResolveQuery } from "../zodSchema.js";
13
+ import {
14
+ CoMapSchema,
15
+ CoMapSchemaDefinition,
16
+ CoreCoMapSchema,
17
+ createCoreCoMapSchema,
18
+ } from "./CoMapSchema.js";
14
19
 
15
20
  export type BaseProfileShape = {
16
21
  name: z.core.$ZodString<string>;
@@ -19,8 +24,8 @@ export type BaseProfileShape = {
19
24
  };
20
25
 
21
26
  export type BaseAccountShape = {
22
- profile: AnyCoMapSchema<BaseProfileShape>;
23
- root: AnyCoMapSchema;
27
+ profile: CoreCoMapSchema<BaseProfileShape>;
28
+ root: CoreCoMapSchema;
24
29
  };
25
30
 
26
31
  export type DefaultAccountShape = {
@@ -28,9 +33,18 @@ export type DefaultAccountShape = {
28
33
  root: CoMapSchema<{}>;
29
34
  };
30
35
 
31
- export type AccountSchema<
36
+ export interface AccountSchema<
32
37
  Shape extends BaseAccountShape = DefaultAccountShape,
33
- > = Omit<CoMapSchema<Shape>, "create" | "load" | "withMigration"> & {
38
+ > extends CoreAccountSchema<Shape>,
39
+ Omit<
40
+ CoMapSchema<Shape>,
41
+ | "builtin"
42
+ | "getDefinition"
43
+ | "create"
44
+ | "load"
45
+ | "withMigration"
46
+ | "getCoValueClass"
47
+ > {
34
48
  builtin: "Account";
35
49
 
36
50
  create: (options: {
@@ -63,10 +77,19 @@ export type AccountSchema<
63
77
  ): AccountSchema<Shape>;
64
78
 
65
79
  getCoValueClass: () => typeof Account;
66
- };
80
+ }
81
+
82
+ export function createCoreAccountSchema<Shape extends BaseAccountShape>(
83
+ shape: Shape,
84
+ ): CoreAccountSchema<Shape> {
85
+ return {
86
+ ...createCoreCoMapSchema(shape),
87
+ builtin: "Account" as const,
88
+ };
89
+ }
67
90
 
68
91
  export function enrichAccountSchema<Shape extends BaseAccountShape>(
69
- schema: AnyAccountSchema<Shape>,
92
+ schema: CoreAccountSchema<Shape>,
70
93
  coValueClass: typeof Account,
71
94
  ): AccountSchema<Shape> {
72
95
  const enrichedSchema = Object.assign(schema, {
@@ -90,9 +113,6 @@ export function enrichAccountSchema<Shape extends BaseAccountShape>(
90
113
  // @ts-expect-error
91
114
  return coValueClass.subscribe(...args);
92
115
  },
93
- withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
94
- return Object.assign(schema, helpers(schema));
95
- },
96
116
  fromRaw: (...args: any[]) => {
97
117
  // @ts-expect-error
98
118
  return coValueClass.fromRaw(...args);
@@ -127,16 +147,15 @@ export type DefaultProfileShape = {
127
147
 
128
148
  export type CoProfileSchema<
129
149
  Shape extends z.core.$ZodLooseShape = DefaultProfileShape,
130
- Config extends z.core.$ZodObjectConfig = z.core.$ZodObjectConfig,
131
- > = CoMapSchema<Shape & DefaultProfileShape, Config, Group>;
150
+ CatchAll extends AnyZodOrCoValueSchema | unknown = unknown,
151
+ > = CoMapSchema<Shape & DefaultProfileShape, CatchAll, Group>;
132
152
 
133
153
  // less precise version to avoid circularity issues and allow matching against
134
- export type AnyAccountSchema<
154
+ export interface CoreAccountSchema<
135
155
  Shape extends z.core.$ZodLooseShape = z.core.$ZodLooseShape,
136
- > = z.core.$ZodObject<Shape> & {
137
- collaborative: true;
156
+ > extends Omit<CoreCoMapSchema<Shape>, "builtin"> {
138
157
  builtin: "Account";
139
- };
158
+ }
140
159
 
141
160
  export type AccountInstance<Shape extends z.core.$ZodLooseShape> = {
142
161
  -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;
@@ -1,34 +1,61 @@
1
1
  import {
2
2
  Account,
3
3
  AnonymousJazzAgent,
4
- AnyCoSchema,
4
+ InstanceOfSchema,
5
5
  InstanceOrPrimitiveOfSchemaCoValuesNullable,
6
- RefsToResolve,
7
- RefsToResolveStrict,
8
6
  Resolved,
9
7
  SchemaUnion,
8
+ SchemaUnionConcreteSubclass,
10
9
  SubscribeListenerOptions,
10
+ coOptionalDefiner,
11
11
  } from "../../../internal.js";
12
12
  import { z } from "../zodReExport.js";
13
+ import { CoOptionalSchema } from "./CoOptionalSchema.js";
14
+ import { CoreCoValueSchema } from "./CoValueSchema.js";
13
15
 
14
- export type AnyDiscriminableCoSchema = AnyCoSchema &
15
- z.core.$ZodTypeDiscriminable;
16
+ export interface DiscriminableCoValueSchemaDefinition {
17
+ discriminatorMap: z.core.$ZodDiscriminatedUnionInternals["propValues"];
18
+ }
19
+
20
+ export interface DiscriminableCoreCoValueSchema extends CoreCoValueSchema {
21
+ getDefinition: () => DiscriminableCoValueSchemaDefinition;
22
+ }
23
+
24
+ export interface CoDiscriminatedUnionSchemaDefinition<
25
+ Options extends DiscriminableCoValueSchemas,
26
+ > extends DiscriminableCoValueSchemaDefinition {
27
+ discriminator: string;
28
+ options: Options;
29
+ }
30
+
31
+ export type DiscriminableCoValueSchemas = [
32
+ DiscriminableCoreCoValueSchema,
33
+ ...DiscriminableCoreCoValueSchema[],
34
+ ];
35
+
36
+ export interface CoreCoDiscriminatedUnionSchema<
37
+ Options extends DiscriminableCoValueSchemas = DiscriminableCoValueSchemas,
38
+ > extends DiscriminableCoreCoValueSchema {
39
+ builtin: "CoDiscriminatedUnion";
40
+ getDefinition: () => CoDiscriminatedUnionSchemaDefinition<Options>;
41
+ }
42
+ export class CoDiscriminatedUnionSchema<
43
+ Options extends DiscriminableCoValueSchemas,
44
+ > implements CoreCoDiscriminatedUnionSchema<Options>
45
+ {
46
+ readonly collaborative = true as const;
47
+ readonly builtin = "CoDiscriminatedUnion" as const;
48
+ readonly getDefinition: () => CoDiscriminatedUnionSchemaDefinition<Options>;
16
49
 
17
- export type AnyCoDiscriminatedUnionSchema<
18
- Types extends readonly [
19
- AnyDiscriminableCoSchema,
20
- ...AnyDiscriminableCoSchema[],
21
- ],
22
- > = z.ZodDiscriminatedUnion<Types> & {
23
- collaborative: true;
24
- };
50
+ constructor(
51
+ coreSchema: CoreCoDiscriminatedUnionSchema<Options>,
52
+ private coValueClass: SchemaUnionConcreteSubclass<
53
+ InstanceOfSchema<Options[number]>
54
+ >,
55
+ ) {
56
+ this.getDefinition = coreSchema.getDefinition;
57
+ }
25
58
 
26
- export type CoDiscriminatedUnionSchema<
27
- Types extends readonly [
28
- AnyDiscriminableCoSchema,
29
- ...AnyDiscriminableCoSchema[],
30
- ],
31
- > = AnyCoDiscriminatedUnionSchema<Types> & {
32
59
  load(
33
60
  id: string,
34
61
  options?: {
@@ -36,55 +63,77 @@ export type CoDiscriminatedUnionSchema<
36
63
  skipRetry?: boolean;
37
64
  },
38
65
  ): Promise<Resolved<
39
- CoDiscriminatedUnionInstanceCoValuesNullable<Types> & SchemaUnion,
66
+ CoDiscriminatedUnionInstanceCoValuesNullable<Options> & SchemaUnion,
40
67
  true
41
- > | null>;
68
+ > | null> {
69
+ return this.coValueClass.load(id, options) as any;
70
+ }
42
71
 
43
72
  subscribe(
44
73
  id: string,
45
74
  options: SubscribeListenerOptions<
46
- CoDiscriminatedUnionInstanceCoValuesNullable<Types> & SchemaUnion,
75
+ CoDiscriminatedUnionInstanceCoValuesNullable<Options> & SchemaUnion,
47
76
  true
48
77
  >,
49
78
  listener: (
50
79
  value: Resolved<
51
- CoDiscriminatedUnionInstanceCoValuesNullable<Types> & SchemaUnion,
80
+ CoDiscriminatedUnionInstanceCoValuesNullable<Options> & SchemaUnion,
52
81
  true
53
82
  >,
54
83
  unsubscribe: () => void,
55
84
  ) => void,
56
- ): () => void;
85
+ ): () => void {
86
+ // @ts-expect-error
87
+ return this.coValueClass.subscribe(id, options, listener);
88
+ }
57
89
 
58
- getCoValueClass: () => typeof SchemaUnion;
59
- };
90
+ getCoValueClass(): SchemaUnionConcreteSubclass<
91
+ InstanceOfSchema<Options[number]>
92
+ > {
93
+ return this.coValueClass;
94
+ }
95
+
96
+ optional(): CoOptionalSchema<this> {
97
+ return coOptionalDefiner(this);
98
+ }
99
+ }
60
100
 
61
- export function enrichCoDiscriminatedUnionSchema<
62
- Types extends readonly [
63
- AnyDiscriminableCoSchema,
64
- ...AnyDiscriminableCoSchema[],
65
- ],
101
+ export function createCoreCoDiscriminatedUnionSchema<
102
+ Options extends DiscriminableCoValueSchemas,
66
103
  >(
67
- schema: z.ZodDiscriminatedUnion<Types>,
68
- coValueClass: typeof SchemaUnion,
69
- ): CoDiscriminatedUnionSchema<Types> {
70
- return Object.assign(schema, {
71
- load: (...args: [any, ...any]) => {
72
- // @ts-expect-error
73
- return coValueClass.load(...args);
74
- },
75
- subscribe: (...args: [any, ...any[]]) => {
76
- // @ts-expect-error
77
- return coValueClass.subscribe(...args);
78
- },
79
- getCoValueClass: () => {
80
- return coValueClass;
81
- },
82
- }) as unknown as CoDiscriminatedUnionSchema<Types>;
104
+ discriminator: string,
105
+ schemas: Options,
106
+ ): CoreCoDiscriminatedUnionSchema<Options> {
107
+ return {
108
+ collaborative: true as const,
109
+ builtin: "CoDiscriminatedUnion" as const,
110
+ getDefinition: () => ({
111
+ discriminator,
112
+ get discriminatorMap() {
113
+ const propValues: DiscriminableCoValueSchemaDefinition["discriminatorMap"] =
114
+ {};
115
+ for (const option of schemas) {
116
+ const dm = option.getDefinition().discriminatorMap;
117
+ if (!dm || Object.keys(dm).length === 0)
118
+ throw new Error(
119
+ `Invalid discriminated union option at index "${schemas.indexOf(option)}"`,
120
+ );
121
+ for (const [k, v] of Object.entries(dm)) {
122
+ propValues[k] ??= new Set();
123
+ for (const val of v) {
124
+ propValues[k].add(val);
125
+ }
126
+ }
127
+ }
128
+ return propValues;
129
+ },
130
+ get options() {
131
+ return schemas;
132
+ },
133
+ }),
134
+ };
83
135
  }
84
136
 
85
137
  type CoDiscriminatedUnionInstanceCoValuesNullable<
86
- Types extends readonly [
87
- AnyDiscriminableCoSchema,
88
- ...AnyDiscriminableCoSchema[],
89
- ],
90
- > = NonNullable<InstanceOrPrimitiveOfSchemaCoValuesNullable<Types[number]>>;
138
+ Options extends DiscriminableCoValueSchemas,
139
+ > = NonNullable<InstanceOrPrimitiveOfSchemaCoValuesNullable<Options[number]>>;