jazz-tools 0.15.16 → 0.16.0

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 (199) 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 +46 -46
  7. package/CHANGELOG.md +25 -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-MLCNE3TL.js} +594 -598
  15. package/dist/chunk-MLCNE3TL.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 +5 -18
  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 +2 -2
  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 +16 -14
  74. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  75. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +23 -17
  76. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  77. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +24 -16
  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 +18 -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/InstanceOfSchema.d.ts +15 -13
  92. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  93. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +12 -15
  94. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -1
  95. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +17 -20
  96. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -1
  97. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +17 -20
  98. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -1
  99. package/dist/tools/implementation/zodSchema/unionUtils.d.ts +3 -5
  100. package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
  101. package/dist/tools/implementation/zodSchema/zodCo.d.ts +10 -8
  102. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  103. package/dist/tools/implementation/zodSchema/zodReExport.d.ts +4 -8
  104. package/dist/tools/implementation/zodSchema/zodReExport.d.ts.map +1 -1
  105. package/dist/tools/implementation/zodSchema/zodSchema.d.ts +21 -30
  106. package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  107. package/dist/tools/internal.d.ts +3 -2
  108. package/dist/tools/internal.d.ts.map +1 -1
  109. package/dist/tools/lib/utilityTypes.d.ts +10 -0
  110. package/dist/tools/lib/utilityTypes.d.ts.map +1 -0
  111. package/dist/tools/subscribe/utils.d.ts.map +1 -1
  112. package/dist/tools/testing.d.ts +2 -2
  113. package/dist/tools/testing.d.ts.map +1 -1
  114. package/dist/worker/index.d.ts.map +1 -1
  115. package/dist/worker/index.js +2 -2
  116. package/dist/worker/index.js.map +1 -1
  117. package/package.json +5 -5
  118. package/src/browser/index.ts +2 -4
  119. package/src/browser-media-images/index.ts +1 -1
  120. package/src/react/hooks.tsx +2 -2
  121. package/src/react-core/hooks.ts +6 -6
  122. package/src/react-core/tests/useAccount.test.ts +2 -2
  123. package/src/react-core/tests/useCoState.test.ts +3 -2
  124. package/src/react-native-core/hooks.tsx +2 -2
  125. package/src/svelte/jazz.class.svelte.ts +10 -7
  126. package/src/svelte/jazz.svelte.ts +2 -2
  127. package/src/tools/coValues/CoValueBase.ts +8 -20
  128. package/src/tools/coValues/account.ts +18 -14
  129. package/src/tools/coValues/coFeed.ts +0 -4
  130. package/src/tools/coValues/coList.ts +7 -9
  131. package/src/tools/coValues/coMap.ts +1 -6
  132. package/src/tools/coValues/coPlainText.ts +4 -4
  133. package/src/tools/coValues/deepLoading.ts +4 -1
  134. package/src/tools/coValues/extensions/imageDef.ts +3 -3
  135. package/src/tools/coValues/inbox.ts +4 -4
  136. package/src/tools/coValues/interfaces.ts +4 -71
  137. package/src/tools/coValues/request.ts +15 -13
  138. package/src/tools/coValues/schemaUnion.ts +8 -4
  139. package/src/tools/exports.ts +3 -14
  140. package/src/tools/implementation/createContext.ts +9 -9
  141. package/src/tools/implementation/invites.ts +2 -2
  142. package/src/tools/implementation/schemaUtils.ts +18 -0
  143. package/src/tools/implementation/zodSchema/coExport.ts +14 -0
  144. package/src/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.ts +156 -0
  145. package/src/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.ts +133 -0
  146. package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +36 -17
  147. package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +101 -52
  148. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +54 -50
  149. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +54 -46
  150. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +187 -137
  151. package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +29 -27
  152. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +28 -18
  153. package/src/tools/implementation/zodSchema/schemaTypes/CoValueSchema.ts +18 -0
  154. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +45 -36
  155. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +47 -35
  156. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +43 -30
  157. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +28 -23
  158. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +28 -25
  159. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +86 -78
  160. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +91 -82
  161. package/src/tools/implementation/zodSchema/unionUtils.ts +47 -51
  162. package/src/tools/implementation/zodSchema/zodCo.ts +42 -75
  163. package/src/tools/implementation/zodSchema/zodReExport.ts +44 -24
  164. package/src/tools/implementation/zodSchema/zodSchema.ts +64 -102
  165. package/src/tools/internal.ts +3 -2
  166. package/src/tools/lib/utilityTypes.ts +7 -0
  167. package/src/tools/subscribe/utils.ts +4 -2
  168. package/src/tools/testing.ts +4 -4
  169. package/src/tools/tests/ContextManager.test.ts +8 -9
  170. package/src/tools/tests/account.test.ts +65 -3
  171. package/src/tools/tests/coDiscriminatedUnion.test-d.ts +38 -0
  172. package/src/tools/tests/coDiscriminatedUnion.test.ts +219 -1
  173. package/src/tools/tests/coFeed.test-d.ts +4 -3
  174. package/src/tools/tests/coList.test-d.ts +32 -3
  175. package/src/tools/tests/coList.test.ts +20 -2
  176. package/src/tools/tests/coMap.record.test-d.ts +31 -3
  177. package/src/tools/tests/coMap.record.test.ts +9 -9
  178. package/src/tools/tests/coMap.test-d.ts +8 -8
  179. package/src/tools/tests/coMap.test.ts +19 -5
  180. package/src/tools/tests/coOptional.test.ts +63 -1
  181. package/src/tools/tests/createContext.test.ts +7 -9
  182. package/src/tools/tests/deepLoading.test.ts +4 -10
  183. package/src/tools/tests/exportImport.test.ts +2 -2
  184. package/src/tools/tests/groupsAndAccounts.test.ts +5 -4
  185. package/src/tools/tests/inbox.test.ts +3 -2
  186. package/src/tools/tests/load.test.ts +3 -29
  187. package/src/tools/tests/schemaUnion.test.ts +2 -2
  188. package/src/tools/tests/subscribe.test.ts +22 -114
  189. package/src/tools/tests/testing.test.ts +6 -6
  190. package/src/tools/tests/zod.test-d.ts +27 -0
  191. package/src/tools/tests/zod.test.ts +50 -45
  192. package/src/worker/index.ts +0 -1
  193. package/dist/chunk-OSVAAVWQ.js.map +0 -1
  194. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +0 -12
  195. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +0 -1
  196. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +0 -9
  197. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +0 -1
  198. package/src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +0 -172
  199. package/src/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +0 -218
@@ -0,0 +1,133 @@
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.$ZodUnion<z.core.$ZodType[]>
26
+ | z.core.$ZodDiscriminatedUnion<z.core.$ZodType[]>
27
+ | z.core.$ZodObject<z.core.$ZodLooseShape>
28
+ | z.core.$ZodArray<z.core.$ZodType>
29
+ | z.core.$ZodTuple<z.core.$ZodType[]>
30
+ | z.core.$ZodReadonly<z.core.$ZodType>
31
+ | z.core.$ZodLazy<z.core.$ZodType>
32
+ | z.core.$ZodTemplateLiteral<any>
33
+ | z.core.$ZodLiteral<any>
34
+ | z.core.$ZodCatch<z.core.$ZodType>
35
+ | z.core.$ZodEnum<any>
36
+ | z.core.$ZodDefault<z.core.$ZodType>
37
+ | z.core.$ZodCatch<z.core.$ZodType>;
38
+
39
+ export function schemaFieldToCoFieldDef(schema: SchemaField) {
40
+ if (isCoValueClass(schema)) {
41
+ return coField.ref(schema);
42
+ } else if (isCoValueSchema(schema)) {
43
+ if (schema.builtin === "CoOptional") {
44
+ return coField.ref(schema.getCoValueClass(), {
45
+ optional: true,
46
+ });
47
+ }
48
+ return coField.ref(schema.getCoValueClass());
49
+ } else {
50
+ if ("_zod" in schema) {
51
+ const zodSchemaDef = schema._zod.def;
52
+ if (zodSchemaDef.type === "optional") {
53
+ const inner = zodSchemaDef.innerType as ZodPrimitiveSchema;
54
+ return schemaFieldToCoFieldDef(inner);
55
+ } else if (zodSchemaDef.type === "string") {
56
+ return coField.string;
57
+ } else if (zodSchemaDef.type === "number") {
58
+ return coField.number;
59
+ } else if (zodSchemaDef.type === "boolean") {
60
+ return coField.boolean;
61
+ } else if (zodSchemaDef.type === "null") {
62
+ return coField.null;
63
+ } else if (zodSchemaDef.type === "enum") {
64
+ return coField.string;
65
+ } else if (zodSchemaDef.type === "readonly") {
66
+ return schemaFieldToCoFieldDef(
67
+ (schema as unknown as ZodReadonly).def.innerType as SchemaField,
68
+ );
69
+ } else if (zodSchemaDef.type === "date") {
70
+ return coField.optional.Date;
71
+ } else if (zodSchemaDef.type === "template_literal") {
72
+ return coField.string;
73
+ } else if (zodSchemaDef.type === "lazy") {
74
+ // Mostly to support z.json()
75
+ return schemaFieldToCoFieldDef(
76
+ (schema as unknown as ZodLazy).unwrap() as SchemaField,
77
+ );
78
+ } else if (
79
+ zodSchemaDef.type === "default" ||
80
+ zodSchemaDef.type === "catch"
81
+ ) {
82
+ console.warn(
83
+ "z.default()/z.catch() are not supported in collaborative schemas. They will be ignored.",
84
+ );
85
+
86
+ return schemaFieldToCoFieldDef(
87
+ (schema as unknown as ZodDefault | ZodCatch).def
88
+ .innerType as SchemaField,
89
+ );
90
+ } else if (zodSchemaDef.type === "literal") {
91
+ if (
92
+ zodSchemaDef.values.some((literal) => typeof literal === "undefined")
93
+ ) {
94
+ throw new Error("z.literal() with undefined is not supported");
95
+ }
96
+ if (zodSchemaDef.values.some((literal) => literal === null)) {
97
+ throw new Error("z.literal() with null is not supported");
98
+ }
99
+ if (
100
+ zodSchemaDef.values.some((literal) => typeof literal === "bigint")
101
+ ) {
102
+ throw new Error("z.literal() with bigint is not supported");
103
+ }
104
+ return coField.literal(
105
+ ...(zodSchemaDef.values as Exclude<
106
+ (typeof zodSchemaDef.values)[number],
107
+ undefined | null | bigint
108
+ >[]),
109
+ );
110
+ } else if (
111
+ zodSchemaDef.type === "object" ||
112
+ zodSchemaDef.type === "array" ||
113
+ zodSchemaDef.type === "tuple"
114
+ ) {
115
+ return coField.json();
116
+ } else if (zodSchemaDef.type === "union") {
117
+ if (isUnionOfPrimitivesDeeply(schema)) {
118
+ return coField.json();
119
+ } else {
120
+ throw new Error(
121
+ "z.union()/z.discriminatedUnion() of collaborative types is not supported. Use co.discriminatedUnion() instead.",
122
+ );
123
+ }
124
+ } else {
125
+ throw new Error(
126
+ `Unsupported zod type: ${(schema._zod?.def as any)?.type || JSON.stringify(schema)}`,
127
+ );
128
+ }
129
+ } else {
130
+ throw new Error(`Unsupported zod type: ${schema}`);
131
+ }
132
+ }
133
+ }
@@ -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]>>;
@@ -1,36 +1,43 @@
1
1
  import {
2
2
  Account,
3
+ AnyZodOrCoValueSchema,
3
4
  CoFeed,
4
5
  Group,
6
+ NotNull,
5
7
  RefsToResolve,
6
8
  RefsToResolveStrict,
7
9
  Resolved,
10
+ Simplify,
8
11
  SubscribeListenerOptions,
12
+ coOptionalDefiner,
9
13
  } from "../../../internal.js";
10
14
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
11
- import { InstanceOfSchema } from "../typeConverters/InstanceOfSchema.js";
12
15
  import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
13
16
  import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
14
- import { z } from "../zodReExport.js";
17
+ import { CoOptionalSchema } from "./CoOptionalSchema.js";
18
+ import { CoreCoValueSchema } from "./CoValueSchema.js";
15
19
 
16
- type CoFeedInit<T extends z.core.$ZodType> = Array<
17
- T extends z.core.$ZodOptional<any>
18
- ? InstanceOrPrimitiveOfSchemaCoValuesNullable<T>
19
- : NonNullable<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>>
20
+ type CoFeedInit<T extends AnyZodOrCoValueSchema> = Simplify<
21
+ Array<NotNull<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>>>
20
22
  >;
21
23
 
22
- export type CoFeedSchema<T extends z.core.$ZodType> = z.core.$ZodCustom<
23
- CoFeed<InstanceOfSchema<T>>,
24
- unknown
25
- > & {
26
- collaborative: true;
27
- builtin: "CoFeed";
28
- element: T;
24
+ export class CoFeedSchema<T extends AnyZodOrCoValueSchema>
25
+ implements CoreCoFeedSchema<T>
26
+ {
27
+ collaborative = true as const;
28
+ builtin = "CoFeed" as const;
29
+
30
+ constructor(
31
+ public element: T,
32
+ private coValueClass: typeof CoFeed,
33
+ ) {}
29
34
 
30
35
  create(
31
36
  init: CoFeedInit<T>,
32
37
  options?: { owner: Account | Group } | Account | Group,
33
- ): CoFeedInstance<T>;
38
+ ): CoFeedInstance<T> {
39
+ return this.coValueClass.create(init, options) as CoFeedInstance<T>;
40
+ }
34
41
 
35
42
  load<const R extends RefsToResolve<CoFeedInstanceCoValuesNullable<T>> = true>(
36
43
  id: string,
@@ -38,7 +45,10 @@ export type CoFeedSchema<T extends z.core.$ZodType> = z.core.$ZodCustom<
38
45
  resolve?: RefsToResolveStrict<CoFeedInstanceCoValuesNullable<T>, R>;
39
46
  loadAs?: Account | AnonymousJazzAgent;
40
47
  },
41
- ): Promise<Resolved<CoFeedInstanceCoValuesNullable<T>, R> | null>;
48
+ ): Promise<Resolved<CoFeedInstanceCoValuesNullable<T>, R> | null> {
49
+ // @ts-expect-error
50
+ return this.coValueClass.load(id, options);
51
+ }
42
52
 
43
53
  subscribe(
44
54
  id: string,
@@ -57,47 +67,41 @@ export type CoFeedSchema<T extends z.core.$ZodType> = z.core.$ZodCustom<
57
67
  unsubscribe: () => void,
58
68
  ) => void,
59
69
  ): () => void;
70
+ subscribe(...args: [any, ...any[]]) {
71
+ // @ts-expect-error
72
+ return this.coValueClass.subscribe(...args);
73
+ }
60
74
 
61
- getCoValueClass: () => typeof CoFeed;
62
- };
75
+ getCoValueClass(): typeof CoFeed {
76
+ return this.coValueClass;
77
+ }
63
78
 
64
- export function enrichCoFeedSchema<T extends z.core.$ZodType>(
65
- schema: AnyCoFeedSchema<T>,
66
- coValueClass: typeof CoFeed,
67
- ): CoFeedSchema<T> {
68
- return Object.assign(schema, {
69
- create: (...args: [any, ...any[]]) => {
70
- return coValueClass.create(...args);
71
- },
72
- load: (...args: [any, ...any[]]) => {
73
- // @ts-expect-error
74
- return coValueClass.load(...args);
75
- },
76
- subscribe: (...args: [any, ...any[]]) => {
77
- // @ts-expect-error
78
- return coValueClass.subscribe(...args);
79
- },
80
- withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
81
- return Object.assign(schema, helpers(schema));
82
- },
83
- getCoValueClass: () => {
84
- return coValueClass;
85
- },
86
- }) as unknown as CoFeedSchema<T>;
79
+ optional(): CoOptionalSchema<this> {
80
+ return coOptionalDefiner(this);
81
+ }
87
82
  }
88
83
 
89
- // less precise version to avoid circularity issues and allow matching against
90
- export type AnyCoFeedSchema<T extends z.core.$ZodType = z.core.$ZodType> =
91
- z.core.$ZodCustom<any, unknown> & {
92
- collaborative: true;
93
- builtin: "CoFeed";
94
- element: T;
84
+ export function createCoreCoFeedSchema<T extends AnyZodOrCoValueSchema>(
85
+ element: T,
86
+ ): CoreCoFeedSchema<T> {
87
+ return {
88
+ collaborative: true as const,
89
+ builtin: "CoFeed" as const,
90
+ element,
95
91
  };
92
+ }
93
+
94
+ // less precise version to avoid circularity issues and allow matching against
95
+ export interface CoreCoFeedSchema<
96
+ T extends AnyZodOrCoValueSchema = AnyZodOrCoValueSchema,
97
+ > extends CoreCoValueSchema {
98
+ builtin: "CoFeed";
99
+ element: T;
100
+ }
96
101
 
97
- export type CoFeedInstance<T extends z.core.$ZodType> = CoFeed<
102
+ export type CoFeedInstance<T extends AnyZodOrCoValueSchema> = CoFeed<
98
103
  InstanceOrPrimitiveOfSchema<T>
99
104
  >;
100
105
 
101
- export type CoFeedInstanceCoValuesNullable<T extends z.core.$ZodType> = CoFeed<
102
- InstanceOrPrimitiveOfSchemaCoValuesNullable<T>
103
- >;
106
+ export type CoFeedInstanceCoValuesNullable<T extends AnyZodOrCoValueSchema> =
107
+ CoFeed<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>>;