jazz-tools 0.15.9 → 0.15.11

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 (104) hide show
  1. package/.turbo/turbo-build.log +38 -38
  2. package/CHANGELOG.md +20 -0
  3. package/dist/{chunk-5PFEKHX5.js → chunk-MLLG3GBR.js} +422 -303
  4. package/dist/chunk-MLLG3GBR.js.map +1 -0
  5. package/dist/index.js +1 -1
  6. package/dist/react/index.js +2 -0
  7. package/dist/react/index.js.map +1 -1
  8. package/dist/react/testing.js +2 -0
  9. package/dist/react/testing.js.map +1 -1
  10. package/dist/testing.js +1 -1
  11. package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
  12. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  13. package/dist/tools/coValues/inbox.d.ts.map +1 -1
  14. package/dist/tools/coValues/schemaUnion.d.ts +27 -1
  15. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  16. package/dist/tools/implementation/zodSchema/coExport.d.ts +1 -1
  17. package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
  18. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +6 -3
  19. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +1 -1
  20. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +6 -25
  21. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +1 -1
  22. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +2 -1
  23. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  24. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +30 -0
  25. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -0
  26. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +2 -1
  27. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  28. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +3 -3
  29. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  30. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +3 -3
  31. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  32. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts +11 -0
  33. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts.map +1 -0
  34. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +2 -3
  35. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  36. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +5 -2
  37. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  38. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +5 -2
  39. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  40. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +5 -2
  41. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  42. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +7 -12
  43. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  44. package/dist/tools/implementation/zodSchema/unionUtils.d.ts +2 -3
  45. package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
  46. package/dist/tools/implementation/zodSchema/zodCo.d.ts +6 -6
  47. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  48. package/dist/tools/implementation/zodSchema/zodReExport.d.ts +8 -1
  49. package/dist/tools/implementation/zodSchema/zodReExport.d.ts.map +1 -1
  50. package/dist/tools/implementation/zodSchema/zodSchema.d.ts +18 -13
  51. package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  52. package/dist/tools/internal.d.ts +1 -0
  53. package/dist/tools/internal.d.ts.map +1 -1
  54. package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
  55. package/dist/tools/tests/coDiscriminatedUnion.test-d.d.ts +2 -0
  56. package/dist/tools/tests/coDiscriminatedUnion.test-d.d.ts.map +1 -0
  57. package/dist/tools/tests/coDiscriminatedUnion.test.d.ts +2 -0
  58. package/dist/tools/tests/coDiscriminatedUnion.test.d.ts.map +1 -0
  59. package/dist/tools/tests/coOptional.test.d.ts +2 -0
  60. package/dist/tools/tests/coOptional.test.d.ts.map +1 -0
  61. package/dist/tools/tests/zod.test-d.d.ts +2 -0
  62. package/dist/tools/tests/zod.test-d.d.ts.map +1 -0
  63. package/package.json +5 -4
  64. package/src/react-core/tests/useAccount.test.ts +1 -11
  65. package/src/tools/coValues/CoValueBase.ts +2 -2
  66. package/src/tools/coValues/inbox.ts +0 -2
  67. package/src/tools/coValues/schemaUnion.ts +62 -0
  68. package/src/tools/implementation/zodSchema/coExport.ts +2 -0
  69. package/src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +29 -12
  70. package/src/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +93 -63
  71. package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +62 -3
  72. package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +90 -0
  73. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +27 -2
  74. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +27 -5
  75. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +60 -5
  76. package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +33 -0
  77. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +3 -5
  78. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +32 -2
  79. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +30 -2
  80. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +26 -2
  81. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +10 -13
  82. package/src/tools/implementation/zodSchema/unionUtils.ts +22 -14
  83. package/src/tools/implementation/zodSchema/zodCo.ts +68 -256
  84. package/src/tools/implementation/zodSchema/zodReExport.ts +35 -5
  85. package/src/tools/implementation/zodSchema/zodSchema.ts +78 -19
  86. package/src/tools/internal.ts +1 -0
  87. package/src/tools/subscribe/SubscriptionScope.ts +1 -2
  88. package/src/tools/tests/ContextManager.test.ts +5 -6
  89. package/src/tools/tests/coDiscriminatedUnion.test-d.ts +57 -0
  90. package/src/tools/tests/coDiscriminatedUnion.test.ts +93 -0
  91. package/src/tools/tests/coList.test.ts +4 -12
  92. package/src/tools/tests/coMap.record.test.ts +4 -4
  93. package/src/tools/tests/coMap.test-d.ts +3 -3
  94. package/src/tools/tests/coMap.test.ts +10 -10
  95. package/src/tools/tests/coOptional.test.ts +39 -0
  96. package/src/tools/tests/createContext.test.ts +5 -4
  97. package/src/tools/tests/deepLoading.test.ts +9 -9
  98. package/src/tools/tests/inbox.test.ts +3 -11
  99. package/src/tools/tests/patterns/requestToJoin.test.ts +1 -1
  100. package/src/tools/tests/schemaUnion.test.ts +11 -22
  101. package/src/tools/tests/subscribe.test.ts +25 -39
  102. package/src/tools/tests/zod.test-d.ts +70 -0
  103. package/src/tools/tests/zod.test.ts +64 -5
  104. package/dist/chunk-5PFEKHX5.js.map +0 -1
@@ -19,9 +19,7 @@ type CoListInit<T extends z.core.$ZodType> = Array<
19
19
  : NonNullable<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>>
20
20
  >;
21
21
 
22
- export type CoListSchema<T extends z.core.$ZodType> = z.core.$ZodArray<T> & {
23
- collaborative: true;
24
-
22
+ export type CoListSchema<T extends z.core.$ZodType> = AnyCoListSchema<T> & {
25
23
  create: (
26
24
  items: CoListInit<T>,
27
25
  options?: { owner: Account | Group } | Account | Group,
@@ -52,10 +50,34 @@ export type CoListSchema<T extends z.core.$ZodType> = z.core.$ZodArray<T> & {
52
50
  helpers: (Self: S) => T,
53
51
  ): WithHelpers<S, T>;
54
52
 
55
- getCoSchema: () => typeof CoList;
53
+ getCoValueClass: () => typeof CoList;
56
54
  };
57
55
 
58
- // less precise verion to avoid circularity issues and allow matching against
56
+ export function enrichCoListSchema<T extends z.core.$ZodType>(
57
+ schema: AnyCoListSchema<T>,
58
+ coValueClass: typeof CoList,
59
+ ): CoListSchema<T> {
60
+ return Object.assign(schema, {
61
+ create: (...args: [any, ...any[]]) => {
62
+ return coValueClass.create(...args);
63
+ },
64
+ load: (...args: [any, ...any[]]) => {
65
+ return coValueClass.load(...args);
66
+ },
67
+ subscribe: (...args: [any, ...any[]]) => {
68
+ // @ts-expect-error
69
+ return coValueClass.subscribe(...args);
70
+ },
71
+ withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
72
+ return Object.assign(schema, helpers(schema));
73
+ },
74
+ getCoValueClass: () => {
75
+ return coValueClass;
76
+ },
77
+ }) as unknown as CoListSchema<T>;
78
+ }
79
+
80
+ // less precise version to avoid circularity issues and allow matching against
59
81
  export type AnyCoListSchema<T extends z.core.$ZodType = z.core.$ZodType> =
60
82
  z.core.$ZodArray<T> & { collaborative: true };
61
83
 
@@ -8,6 +8,7 @@ import {
8
8
  Resolved,
9
9
  Simplify,
10
10
  SubscribeListenerOptions,
11
+ zodSchemaToCoSchema,
11
12
  } from "../../../internal.js";
12
13
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
13
14
  import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
@@ -19,10 +20,8 @@ export type CoMapSchema<
19
20
  Shape extends z.core.$ZodLooseShape,
20
21
  Config extends z.core.$ZodObjectConfig = z.core.$ZodObjectConfig,
21
22
  Owner extends Account | Group = Account | Group,
22
- > = z.core.$ZodObject<Shape, Config> &
23
+ > = AnyCoMapSchema<Shape, Config> &
23
24
  z.$ZodTypeDiscriminable & {
24
- collaborative: true;
25
-
26
25
  create: (
27
26
  init: Simplify<CoMapInitZod<Shape>>,
28
27
  options?:
@@ -145,9 +144,65 @@ export type CoMapSchema<
145
144
  ) => undefined,
146
145
  ): CoMapSchema<Shape, Config, Owner>;
147
146
 
148
- getCoSchema: () => typeof CoMap;
147
+ getCoValueClass: () => typeof CoMap;
149
148
  };
150
149
 
150
+ export function enrichCoMapSchema<
151
+ Shape extends z.core.$ZodLooseShape,
152
+ Config extends z.core.$ZodObjectConfig,
153
+ >(
154
+ schema: AnyCoMapSchema<Shape, Config>,
155
+ coValueClass: typeof CoMap,
156
+ ): CoMapSchema<Shape, Config> {
157
+ // @ts-expect-error schema is actually a z.ZodObject, but we need to use z.core.$ZodObject to avoid circularity issues
158
+ const baseCatchall = schema.catchall;
159
+ const coValueSchema = Object.assign(schema, {
160
+ create: (...args: [any, ...any[]]) => {
161
+ return coValueClass.create(...args);
162
+ },
163
+ load: (...args: [any, ...any[]]) => {
164
+ return coValueClass.load(...args);
165
+ },
166
+ subscribe: (...args: [any, ...any[]]) => {
167
+ // @ts-expect-error
168
+ return coValueClass.subscribe(...args);
169
+ },
170
+ findUnique: (...args: [any, ...any[]]) => {
171
+ // @ts-expect-error
172
+ return coValueClass.findUnique(...args);
173
+ },
174
+ upsertUnique: (...args: [any, ...any[]]) => {
175
+ // @ts-expect-error
176
+ return coValueClass.upsertUnique(...args);
177
+ },
178
+ loadUnique: (...args: [any, ...any[]]) => {
179
+ // @ts-expect-error
180
+ return coValueClass.loadUnique(...args);
181
+ },
182
+ catchall: (index: z.core.$ZodType) => {
183
+ const newSchema = baseCatchall(index);
184
+ // TODO avoid repeating this with coMapDefiner
185
+ const enrichedSchema = Object.assign(newSchema, {
186
+ collaborative: true,
187
+ }) as AnyCoMapSchema<Shape, Config>;
188
+ return zodSchemaToCoSchema(enrichedSchema);
189
+ },
190
+ withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
191
+ return Object.assign(schema, helpers(schema));
192
+ },
193
+ withMigration: (migration: (value: any) => undefined) => {
194
+ // @ts-expect-error TODO check
195
+ coValueClass.prototype.migrate = migration;
196
+
197
+ return coValueSchema;
198
+ },
199
+ getCoValueClass: () => {
200
+ return coValueClass;
201
+ },
202
+ }) as unknown as CoMapSchema<Shape, Config>;
203
+ return coValueSchema;
204
+ }
205
+
151
206
  export type optionalKeys<Shape extends z.core.$ZodLooseShape> = {
152
207
  [key in keyof Shape]: Shape[key] extends z.core.$ZodOptional<any>
153
208
  ? key
@@ -170,7 +225,7 @@ export type CoMapInitZod<Shape extends z.core.$ZodLooseShape> = {
170
225
  >;
171
226
  } & { [key in keyof Shape]?: unknown };
172
227
 
173
- // less precise verion to avoid circularity issues and allow matching against
228
+ // less precise version to avoid circularity issues and allow matching against
174
229
  export type AnyCoMapSchema<
175
230
  Shape extends z.core.$ZodLooseShape = z.core.$ZodLooseShape,
176
231
  Config extends z.core.$ZodObjectConfig = z.core.$ZodObjectConfig,
@@ -0,0 +1,33 @@
1
+ import { isAnyCoValueSchema } from "../runtimeConverters/zodSchemaToCoSchema.js";
2
+ import { z } from "../zodReExport.js";
3
+ import { AnyCoSchema, CoValueSchemaFromZodSchema } from "../zodSchema.js";
4
+
5
+ export type AnyCoOptionalSchema<
6
+ Shape extends z.core.$ZodType = z.core.$ZodType,
7
+ > = z.ZodOptional<Shape> & {
8
+ collaborative: true;
9
+ };
10
+
11
+ export type CoOptionalSchema<Shape extends z.core.$ZodType = z.core.$ZodType> =
12
+ AnyCoOptionalSchema<Shape> & {
13
+ getCoValueClass: () => CoValueSchemaFromZodSchema<AnyCoSchema>["getCoValueClass"];
14
+ };
15
+
16
+ export function createCoOptionalSchema<T extends AnyCoSchema>(
17
+ schema: T,
18
+ ): CoOptionalSchema<T> {
19
+ return Object.assign(z.optional(schema), {
20
+ collaborative: true,
21
+ getCoValueClass: () => {
22
+ return (
23
+ schema as CoValueSchemaFromZodSchema<AnyCoSchema>
24
+ ).getCoValueClass();
25
+ },
26
+ }) as unknown as CoOptionalSchema<T>;
27
+ }
28
+
29
+ export function isAnyCoOptionalSchema(
30
+ schema: z.core.$ZodType,
31
+ ): schema is CoOptionalSchema<z.core.$ZodType> {
32
+ return isAnyCoValueSchema(schema) && schema._zod.def.type === "optional";
33
+ }
@@ -28,9 +28,7 @@ type CoRecordInit<
28
28
  export type CoRecordSchema<
29
29
  K extends z.core.$ZodString<string>,
30
30
  V extends z.core.$ZodType,
31
- > = z.core.$ZodRecord<K, V> & {
32
- collaborative: true;
33
-
31
+ > = AnyCoRecordSchema<K, V> & {
34
32
  create: (
35
33
  init: Simplify<CoRecordInit<K, V>>,
36
34
  options?:
@@ -83,10 +81,10 @@ export type CoRecordSchema<
83
81
  this: S,
84
82
  helpers: (Self: S) => T,
85
83
  ): WithHelpers<S, T>;
86
- getCoSchema: () => typeof CoMap;
84
+ getCoValueClass: () => typeof CoMap;
87
85
  };
88
86
 
89
- // less precise verion to avoid circularity issues and allow matching against
87
+ // less precise version to avoid circularity issues and allow matching against
90
88
  export type AnyCoRecordSchema<
91
89
  K extends z.core.$ZodString<string> = z.core.$ZodString<string>,
92
90
  V extends z.core.$ZodType = z.core.$ZodType,
@@ -6,9 +6,12 @@ import {
6
6
  } from "../../../internal.js";
7
7
  import { z } from "../zodReExport.js";
8
8
 
9
- export type FileStreamSchema = z.core.$ZodCustom<FileStream, unknown> & {
9
+ export type AnyFileStreamSchema = z.core.$ZodCustom<FileStream, unknown> & {
10
10
  collaborative: true;
11
11
  builtin: "FileStream";
12
+ };
13
+
14
+ export type FileStreamSchema = AnyFileStreamSchema & {
12
15
  create(options?: { owner?: Account | Group } | Account | Group): FileStream;
13
16
  createFromBlob(
14
17
  blob: Blob | File,
@@ -40,5 +43,32 @@ export type FileStreamSchema = z.core.$ZodCustom<FileStream, unknown> & {
40
43
  id: string,
41
44
  listener: (value: FileStream, unsubscribe: () => void) => void,
42
45
  ): () => void;
43
- getCoSchema: () => typeof FileStream;
46
+ getCoValueClass: () => typeof FileStream;
44
47
  };
48
+
49
+ export function enrichFileStreamSchema(
50
+ schema: AnyFileStreamSchema,
51
+ coValueClass: typeof FileStream,
52
+ ): FileStreamSchema {
53
+ return Object.assign(schema, {
54
+ create: (...args: any[]) => {
55
+ return coValueClass.create(...args);
56
+ },
57
+ createFromBlob: (...args: [any, ...any[]]) => {
58
+ return coValueClass.createFromBlob(...args);
59
+ },
60
+ load: (...args: [any, ...any[]]) => {
61
+ return coValueClass.load(...args);
62
+ },
63
+ loadAsBlob: (...args: [any, ...any[]]) => {
64
+ return coValueClass.loadAsBlob(...args);
65
+ },
66
+ subscribe: (...args: [any, ...any[]]) => {
67
+ // @ts-expect-error
68
+ return coValueClass.subscribe(...args);
69
+ },
70
+ getCoValueClass: () => {
71
+ return coValueClass;
72
+ },
73
+ }) as unknown as FileStreamSchema;
74
+ }
@@ -3,9 +3,12 @@ import { Account, CoPlainText, Group } from "../../../internal.js";
3
3
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
4
4
  import { z } from "../zodReExport.js";
5
5
 
6
- export type PlainTextSchema = z.core.$ZodCustom<CoPlainText, unknown> & {
6
+ export type AnyPlainTextSchema = z.core.$ZodCustom<CoPlainText, unknown> & {
7
7
  collaborative: true;
8
8
  builtin: "CoPlainText";
9
+ };
10
+
11
+ export type PlainTextSchema = AnyPlainTextSchema & {
9
12
  create(
10
13
  text: string,
11
14
  options?: { owner: Account | Group } | Account | Group,
@@ -24,5 +27,30 @@ export type PlainTextSchema = z.core.$ZodCustom<CoPlainText, unknown> & {
24
27
  listener: (value: CoPlainText, unsubscribe: () => void) => void,
25
28
  ): () => void;
26
29
  fromRaw(raw: RawCoPlainText): CoPlainText;
27
- getCoSchema: () => typeof CoPlainText;
30
+ getCoValueClass: () => typeof CoPlainText;
28
31
  };
32
+
33
+ export function enrichPlainTextSchema(
34
+ schema: AnyPlainTextSchema,
35
+ coValueClass: typeof CoPlainText,
36
+ ): PlainTextSchema {
37
+ return Object.assign(schema, {
38
+ create: (...args: [any, ...any[]]) => {
39
+ return coValueClass.create(...args);
40
+ },
41
+ load: (...args: [any, ...any[]]) => {
42
+ return coValueClass.load(...args);
43
+ },
44
+ subscribe: (...args: [any, ...any[]]) => {
45
+ // @ts-expect-error
46
+ return coValueClass.subscribe(...args);
47
+ },
48
+ fromRaw: (...args: [any, ...any[]]) => {
49
+ // @ts-expect-error
50
+ return coValueClass.fromRaw(...args);
51
+ },
52
+ getCoValueClass: () => {
53
+ return coValueClass;
54
+ },
55
+ }) as unknown as PlainTextSchema;
56
+ }
@@ -2,9 +2,12 @@ import { Account, CoRichText, Group } from "../../../internal.js";
2
2
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
3
3
  import { z } from "../zodReExport.js";
4
4
 
5
- export type RichTextSchema = z.core.$ZodCustom<CoRichText, unknown> & {
5
+ export type AnyRichTextSchema = z.core.$ZodCustom<CoRichText, unknown> & {
6
6
  collaborative: true;
7
7
  builtin: "CoRichText";
8
+ };
9
+
10
+ export type RichTextSchema = AnyRichTextSchema & {
8
11
  create(
9
12
  text: string,
10
13
  options?: { owner: Account | Group } | Account | Group,
@@ -22,5 +25,26 @@ export type RichTextSchema = z.core.$ZodCustom<CoRichText, unknown> & {
22
25
  id: string,
23
26
  listener: (value: CoRichText, unsubscribe: () => void) => void,
24
27
  ): () => void;
25
- getCoSchema: () => typeof CoRichText;
28
+ getCoValueClass: () => typeof CoRichText;
26
29
  };
30
+
31
+ export function enrichRichTextSchema(
32
+ schema: AnyRichTextSchema,
33
+ coValueClass: typeof CoRichText,
34
+ ): RichTextSchema {
35
+ return Object.assign(schema, {
36
+ create: (...args: [any, ...any[]]) => {
37
+ return coValueClass.create(...args);
38
+ },
39
+ load: (...args: [any, ...any[]]) => {
40
+ return coValueClass.load(...args);
41
+ },
42
+ subscribe: (...args: [any, ...any[]]) => {
43
+ // @ts-expect-error
44
+ return coValueClass.subscribe(...args);
45
+ },
46
+ getCoValueClass: () => {
47
+ return coValueClass;
48
+ },
49
+ }) as unknown as RichTextSchema;
50
+ }
@@ -1,5 +1,7 @@
1
1
  import {
2
2
  Account,
3
+ AnyAccountSchema,
4
+ AnyCoRecordSchema,
3
5
  CoFeed,
4
6
  CoList,
5
7
  CoMap,
@@ -11,24 +13,19 @@ import {
11
13
  import { AnyCoFeedSchema } from "../schemaTypes/CoFeedSchema.js";
12
14
  import { AnyCoListSchema } from "../schemaTypes/CoListSchema.js";
13
15
  import { AnyCoMapSchema } from "../schemaTypes/CoMapSchema.js";
14
- import { FileStreamSchema } from "../schemaTypes/FileStreamSchema.js";
15
- import { PlainTextSchema } from "../schemaTypes/PlainTextSchema.js";
16
- import { RichTextSchema } from "../schemaTypes/RichTextSchema.js";
16
+ import { AnyFileStreamSchema } from "../schemaTypes/FileStreamSchema.js";
17
+ import { AnyPlainTextSchema } from "../schemaTypes/PlainTextSchema.js";
18
+ import { AnyRichTextSchema } from "../schemaTypes/RichTextSchema.js";
17
19
  import { z } from "../zodReExport.js";
18
20
  import { InstanceOrPrimitiveOfSchema } from "./InstanceOrPrimitiveOfSchema.js";
19
21
 
20
22
  export type InstanceOfSchema<S extends CoValueClass | z.core.$ZodType> =
21
23
  S extends z.core.$ZodType
22
- ? S extends z.core.$ZodObject<infer Shape> & {
23
- collaborative: true;
24
- builtin: "Account";
25
- }
24
+ ? S extends AnyAccountSchema<infer Shape>
26
25
  ? {
27
26
  [key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;
28
27
  } & Account
29
- : S extends z.core.$ZodRecord<infer K, infer V> & {
30
- collaborative: true;
31
- }
28
+ : S extends AnyCoRecordSchema<infer K, infer V>
32
29
  ? {
33
30
  [key in z.output<K> & string]: InstanceOrPrimitiveOfSchema<V>;
34
31
  } & CoMap
@@ -45,11 +42,11 @@ export type InstanceOfSchema<S extends CoValueClass | z.core.$ZodType> =
45
42
  ? CoList<InstanceOrPrimitiveOfSchema<T>>
46
43
  : S extends AnyCoFeedSchema<infer T>
47
44
  ? CoFeed<InstanceOrPrimitiveOfSchema<T>>
48
- : S extends PlainTextSchema
45
+ : S extends AnyPlainTextSchema
49
46
  ? CoPlainText
50
- : S extends RichTextSchema
47
+ : S extends AnyRichTextSchema
51
48
  ? CoRichText
52
- : S extends FileStreamSchema
49
+ : S extends AnyFileStreamSchema
53
50
  ? FileStream
54
51
  : S extends z.core.$ZodOptional<infer Inner>
55
52
  ? InstanceOrPrimitiveOfSchema<Inner>
@@ -1,5 +1,12 @@
1
1
  import { RawAccount, RawCoList, RawCoMap } from "cojson";
2
- import { zodSchemaToCoSchema } from "./runtimeConverters/zodSchemaToCoSchema.js";
2
+ import { CoValueClass, CoValueFromRaw } from "../../internal.js";
3
+ import {
4
+ isAnyCoValueSchema,
5
+ zodSchemaToCoSchema,
6
+ } from "./runtimeConverters/zodSchemaToCoSchema.js";
7
+ import { AccountSchema } from "./schemaTypes/AccountSchema.js";
8
+ import { CoListSchema } from "./schemaTypes/CoListSchema.js";
9
+ import { CoMapSchema } from "./schemaTypes/CoMapSchema.js";
3
10
  import { z } from "./zodReExport.js";
4
11
 
5
12
  export function schemaUnionDiscriminatorFor(
@@ -8,7 +15,7 @@ export function schemaUnionDiscriminatorFor(
8
15
  if (isUnionOfCoMapsDeeply(schema)) {
9
16
  if (!schema._zod.disc || schema._zod.disc.size == 0) {
10
17
  throw new Error(
11
- "z.union() of collaborative types is not supported, use z.discriminatedUnion() instead",
18
+ "z.union() of collaborative types is not supported, use co.discriminatedUnion() instead",
12
19
  );
13
20
  }
14
21
 
@@ -17,14 +24,14 @@ export function schemaUnionDiscriminatorFor(
17
24
 
18
25
  if (!field) {
19
26
  throw new Error(
20
- "z.discriminatedUnion() of collaborative types with non-existent discriminator key is not supported",
27
+ "co.discriminatedUnion() of collaborative types with non-existent discriminator key is not supported",
21
28
  );
22
29
  }
23
30
 
24
31
  for (const value of field.values) {
25
32
  if (typeof value !== "string" && typeof value !== "number") {
26
33
  throw new Error(
27
- "z.discriminatedUnion() of collaborative types with non-string or non-number discriminator value is not supported",
34
+ "co.discriminatedUnion() of collaborative types with non-string or non-number discriminator value is not supported",
28
35
  );
29
36
  }
30
37
  }
@@ -42,7 +49,7 @@ export function schemaUnionDiscriminatorFor(
42
49
  }
43
50
  } else {
44
51
  throw new Error(
45
- "Unsupported zod type in z.discriminatedUnion() of collaborative types",
52
+ "Unsupported zod type in co.discriminatedUnion() of collaborative types",
46
53
  );
47
54
  }
48
55
  }
@@ -50,7 +57,7 @@ export function schemaUnionDiscriminatorFor(
50
57
  const determineSchema = (_raw: RawCoMap | RawAccount | RawCoList) => {
51
58
  if (_raw instanceof RawCoList) {
52
59
  throw new Error(
53
- "z.discriminatedUnion() of collaborative types is not supported for CoLists",
60
+ "co.discriminatedUnion() of collaborative types is not supported for CoLists",
54
61
  );
55
62
  }
56
63
 
@@ -90,12 +97,17 @@ export function schemaUnionDiscriminatorFor(
90
97
  }
91
98
 
92
99
  if (match) {
93
- return zodSchemaToCoSchema(option);
100
+ const coValueSchema = zodSchemaToCoSchema(option) as
101
+ | CoMapSchema<any>
102
+ | AccountSchema
103
+ | CoListSchema<any>;
104
+ return coValueSchema.getCoValueClass() as CoValueClass<any> &
105
+ CoValueFromRaw<any>;
94
106
  }
95
107
  }
96
108
 
97
109
  throw new Error(
98
- "z.discriminatedUnion() of collaborative types with no matching discriminator value found",
110
+ "co.discriminatedUnion() of collaborative types with no matching discriminator value found",
99
111
  );
100
112
  };
101
113
 
@@ -120,11 +132,7 @@ export function isUnionOfCoMapsDeeply(
120
132
  function isCoMapOrUnionOfCoMapsDeeply(
121
133
  schema: z.core.$ZodType,
122
134
  ): schema is z.core.$ZodDiscriminatedUnion {
123
- if (
124
- schema instanceof z.core.$ZodObject &&
125
- "collaborative" in schema &&
126
- schema.collaborative
127
- ) {
135
+ if (schema instanceof z.core.$ZodObject && isAnyCoValueSchema(schema)) {
128
136
  return true;
129
137
  } else if (schema instanceof z.core.$ZodUnion) {
130
138
  return schema._zod.def.options.every(isCoMapOrUnionOfCoMapsDeeply);
@@ -137,6 +145,6 @@ export function isUnionOfPrimitivesDeeply(schema: z.core.$ZodType) {
137
145
  if (schema instanceof z.core.$ZodUnion) {
138
146
  return schema._zod.def.options.every(isUnionOfPrimitivesDeeply);
139
147
  } else {
140
- return !("collaborative" in schema);
148
+ return !isAnyCoValueSchema(schema);
141
149
  }
142
150
  }