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
@@ -1,8 +1,11 @@
1
1
  import {
2
- type Account,
3
- type AccountCreationProps,
4
2
  type AccountSchema,
5
- type AnyCoMapSchema,
3
+ AnyAccountSchema,
4
+ AnyCoFeedSchema,
5
+ AnyCoListSchema,
6
+ AnyCoSchema,
7
+ AnyFileStreamSchema,
8
+ AnyPlainTextSchema,
6
9
  BaseAccountShape,
7
10
  CoFeed,
8
11
  type CoFeedSchema,
@@ -20,121 +23,31 @@ import {
20
23
  type Simplify,
21
24
  zodSchemaToCoSchema,
22
25
  } from "../../internal.js";
23
- import { RichTextSchema } from "./schemaTypes/RichTextSchema.js";
26
+ import {
27
+ AnyDiscriminableCoSchema,
28
+ CoDiscriminatedUnionSchema,
29
+ } from "./schemaTypes/CoDiscriminatedUnionSchema.js";
30
+ import {
31
+ CoOptionalSchema,
32
+ createCoOptionalSchema,
33
+ } from "./schemaTypes/CoOptionalSchema.js";
34
+ import {
35
+ AnyRichTextSchema,
36
+ RichTextSchema,
37
+ } from "./schemaTypes/RichTextSchema.js";
24
38
  import { z } from "./zodReExport.js";
25
39
 
26
- function enrichCoMapSchema<Shape extends z.core.$ZodLooseShape>(
27
- schema: z.ZodObject<
28
- { -readonly [P in keyof Shape]: Shape[P] },
29
- z.core.$strip
30
- >,
31
- ) {
32
- const baseCatchall = schema.catchall;
33
-
34
- const enrichedSchema = Object.assign(schema, {
35
- collaborative: true,
36
- create: (...args: any[]) => {
37
- return coSchema.create(...args);
38
- },
39
- load: (...args: any[]) => {
40
- return coSchema.load(...args);
41
- },
42
- subscribe: (...args: any[]) => {
43
- return coSchema.subscribe(...args);
44
- },
45
- findUnique: (...args: any[]) => {
46
- return coSchema.findUnique(...args);
47
- },
48
- upsertUnique: (...args: any[]) => {
49
- return coSchema.upsertUnique(...args);
50
- },
51
- loadUnique: (...args: any[]) => {
52
- return coSchema.loadUnique(...args);
53
- },
54
- catchall: (index: z.core.$ZodType) => {
55
- return enrichCoMapSchema(baseCatchall(index));
56
- },
57
- withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
58
- return Object.assign(schema, helpers(schema));
59
- },
60
- withMigration: (migration: (value: any) => undefined) => {
61
- coSchema.prototype.migrate = migration;
62
-
63
- return enrichedSchema;
64
- },
65
- getCoSchema: () => {
66
- return coSchema;
67
- },
68
- }) as unknown as CoMapSchema<Shape>;
69
-
70
- // Needs to be derived from the enriched schema
71
- const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
72
-
73
- return enrichedSchema;
74
- }
75
-
76
40
  export const coMapDefiner = <Shape extends z.core.$ZodLooseShape>(
77
41
  shape: Shape,
78
42
  ): CoMapSchema<Shape> => {
79
43
  const objectSchema = z.object(shape).meta({
80
44
  collaborative: true,
81
45
  });
82
-
83
- return enrichCoMapSchema(objectSchema);
84
- };
85
-
86
- function enrichAccountSchema<Shape extends BaseAccountShape>(
87
- schema: z.ZodObject<Shape, z.core.$strip>,
88
- ) {
89
- const enrichedSchema = Object.assign(schema, {
46
+ const enrichedSchema = Object.assign(objectSchema, {
90
47
  collaborative: true,
91
- builtin: "Account",
92
- create: (...args: any[]) => {
93
- return coSchema.create(...args);
94
- },
95
- createAs: (...args: any[]) => {
96
- return coSchema.createAs(...args);
97
- },
98
- getMe: (...args: any[]) => {
99
- return coSchema.getMe(...args);
100
- },
101
- load: (...args: any[]) => {
102
- return coSchema.load(...args);
103
- },
104
- subscribe: (...args: any[]) => {
105
- return coSchema.subscribe(...args);
106
- },
107
- withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
108
- return Object.assign(schema, helpers(schema));
109
- },
110
- fromRaw: (...args: any[]) => {
111
- return coSchema.fromRaw(...args);
112
- },
113
- withMigration: (
114
- migration: (
115
- value: any,
116
- creationProps?: AccountCreationProps,
117
- ) => void | Promise<void>,
118
- ) => {
119
- (coSchema.prototype as Account).migrate = async function (
120
- this,
121
- creationProps,
122
- ) {
123
- await migration(this, creationProps);
124
- };
125
-
126
- return enrichedSchema;
127
- },
128
- getCoSchema: () => {
129
- return coSchema;
130
- },
131
- }) as unknown as AccountSchema<Shape>;
132
-
133
- // Needs to be derived from the enriched schema
134
- const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
135
-
136
- return enrichedSchema;
137
- }
48
+ });
49
+ return zodSchemaToCoSchema(enrichedSchema);
50
+ };
138
51
 
139
52
  /**
140
53
  * Defines a collaborative account schema for Jazz applications.
@@ -183,11 +96,14 @@ export const coAccountDefiner = <Shape extends BaseAccountShape>(
183
96
  root: coMapDefiner({}),
184
97
  } as unknown as Shape,
185
98
  ): AccountSchema<Shape> => {
186
- const objectSchema = z.object(shape).meta({
99
+ const schema = z.object(shape).meta({
187
100
  collaborative: true,
188
101
  });
189
-
190
- return enrichAccountSchema(objectSchema) as unknown as AccountSchema<Shape>;
102
+ const enrichedSchema = Object.assign(schema, {
103
+ collaborative: true,
104
+ builtin: "Account",
105
+ }) as AnyAccountSchema<Shape>;
106
+ return zodSchemaToCoSchema(enrichedSchema);
191
107
  };
192
108
 
193
109
  export const coRecordDefiner = <
@@ -203,190 +119,86 @@ export const coRecordDefiner = <
203
119
  >;
204
120
  };
205
121
 
206
- function enrichCoListSchema<T extends z.core.$ZodType>(schema: z.ZodArray<T>) {
207
- const enrichedSchema = Object.assign(schema, {
208
- collaborative: true,
209
- create: (...args: any[]) => {
210
- return coSchema.create(...args);
211
- },
212
- load: (...args: any[]) => {
213
- return coSchema.load(...args);
214
- },
215
- subscribe: (...args: any[]) => {
216
- return coSchema.subscribe(...args);
217
- },
218
- withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
219
- return Object.assign(schema, helpers(schema));
220
- },
221
- getCoSchema: () => {
222
- return coSchema;
223
- },
224
- }) as unknown as CoListSchema<T>;
225
-
226
- // Needs to be derived from the enriched schema
227
- const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
228
-
229
- return enrichedSchema;
230
- }
231
-
232
122
  export const coListDefiner = <T extends z.core.$ZodType>(
233
123
  element: T,
234
124
  ): CoListSchema<T> => {
235
- const arraySchema = z.array(element).meta({
125
+ const schema = z.array(element).meta({
236
126
  collaborative: true,
237
127
  });
238
-
239
- return enrichCoListSchema(arraySchema);
128
+ const enrichedSchema = Object.assign(schema, {
129
+ collaborative: true,
130
+ }) as AnyCoListSchema<T>;
131
+ return zodSchemaToCoSchema(enrichedSchema) as unknown as CoListSchema<T>;
240
132
  };
241
133
 
242
134
  export const coProfileDefiner = <
243
135
  Shape extends z.core.$ZodLooseShape = Simplify<DefaultProfileShape>,
244
136
  >(
245
- shape: Shape & {
246
- name?: z.core.$ZodString<string>;
247
- inbox?: z.core.$ZodOptional<z.core.$ZodString>;
248
- inboxInvite?: z.core.$ZodOptional<z.core.$ZodString>;
249
- } = {} as any,
137
+ shape: Shape & Partial<DefaultProfileShape> = {} as any,
250
138
  ): CoProfileSchema<Shape> => {
251
- const ehnancedShape = Object.assign(shape ?? {}, {
139
+ const ehnancedShape = Object.assign(shape, {
252
140
  name: z.string(),
253
141
  inbox: z.optional(z.string()),
254
142
  inboxInvite: z.optional(z.string()),
255
143
  });
256
-
257
144
  return coMapDefiner(ehnancedShape) as CoProfileSchema<Shape>;
258
145
  };
259
146
 
260
- function enrichCoFeedSchema<T extends z.core.$ZodType>(
261
- schema: z.ZodCustom<CoFeed<unknown>, unknown>,
147
+ export const coFeedDefiner = <T extends z.core.$ZodType>(
262
148
  element: T,
263
- ) {
149
+ ): CoFeedSchema<T> => {
150
+ const schema = z.instanceof(CoFeed);
264
151
  const enrichedSchema = Object.assign(schema, {
265
152
  collaborative: true,
266
153
  builtin: "CoFeed",
267
154
  element,
268
- create: (...args: any[]) => {
269
- return coSchema.create(...args);
270
- },
271
- load: (...args: any[]) => {
272
- return coSchema.load(...args);
273
- },
274
- subscribe: (...args: any[]) => {
275
- return coSchema.subscribe(...args);
276
- },
277
- withHelpers: (helpers: (Self: z.core.$ZodType) => object) => {
278
- return Object.assign(schema, helpers(schema));
279
- },
280
- getCoSchema: () => {
281
- return coSchema;
282
- },
283
- }) as unknown as CoFeedSchema<T>;
284
-
285
- // Needs to be derived from the enriched schema
286
- const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
287
-
288
- return enrichedSchema;
289
- }
290
-
291
- export const coFeedDefiner = <T extends z.core.$ZodType>(
292
- element: T,
293
- ): CoFeedSchema<T> => {
294
- return enrichCoFeedSchema(z.instanceof(CoFeed), element);
155
+ }) as AnyCoFeedSchema<T>;
156
+ return zodSchemaToCoSchema(enrichedSchema);
295
157
  };
296
158
 
297
- function enrichFileStreamSchema(schema: z.ZodCustom<FileStream, unknown>) {
159
+ export const coFileStreamDefiner = (): FileStreamSchema => {
160
+ const schema = z.instanceof(FileStream);
298
161
  const enrichedSchema = Object.assign(schema, {
299
162
  collaborative: true,
300
163
  builtin: "FileStream",
301
- create: (...args: any[]) => {
302
- return coSchema.create(...args);
303
- },
304
- createFromBlob: (...args: any[]) => {
305
- return coSchema.createFromBlob(...args);
306
- },
307
- load: (...args: any[]) => {
308
- return coSchema.load(...args);
309
- },
310
- loadAsBlob: (...args: any[]) => {
311
- return coSchema.loadAsBlob(...args);
312
- },
313
- subscribe: (...args: any[]) => {
314
- return coSchema.subscribe(...args);
315
- },
316
- getCoSchema: () => {
317
- return coSchema;
318
- },
319
- }) as unknown as FileStreamSchema;
320
-
321
- // Needs to be derived from the enriched schema
322
- const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
323
-
324
- return enrichedSchema;
325
- }
326
-
327
- export const coFileStreamDefiner = (): FileStreamSchema => {
328
- return enrichFileStreamSchema(z.instanceof(FileStream));
164
+ }) as AnyFileStreamSchema;
165
+ return zodSchemaToCoSchema(enrichedSchema);
329
166
  };
330
167
 
331
- function enrichPlainTextSchema(schema: z.ZodCustom<CoPlainText, unknown>) {
168
+ export const coPlainTextDefiner = (): PlainTextSchema => {
169
+ const schema = z.instanceof(CoPlainText);
332
170
  const enrichedSchema = Object.assign(schema, {
333
171
  collaborative: true,
334
172
  builtin: "CoPlainText",
335
- create: (...args: any[]) => {
336
- return coSchema.create(...args);
337
- },
338
- load: (...args: any[]) => {
339
- return coSchema.load(...args);
340
- },
341
- subscribe: (...args: any[]) => {
342
- return coSchema.subscribe(...args);
343
- },
344
- fromRaw: (...args: any[]) => {
345
- return coSchema.fromRaw(...args);
346
- },
347
- getCoSchema: () => {
348
- return coSchema;
349
- },
350
- }) as unknown as PlainTextSchema;
351
-
352
- // Needs to be derived from the enriched schema
353
- const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
354
-
355
- return enrichedSchema;
356
- }
357
-
358
- export const coPlainTextDefiner = (): PlainTextSchema => {
359
- return enrichPlainTextSchema(z.instanceof(CoPlainText));
173
+ }) as AnyPlainTextSchema;
174
+ return zodSchemaToCoSchema(enrichedSchema);
360
175
  };
361
176
 
362
- function enrichRichTextSchema(schema: z.ZodCustom<CoRichText, unknown>) {
177
+ export const coRichTextDefiner = (): RichTextSchema => {
178
+ const schema = z.instanceof(CoRichText);
363
179
  const enrichedSchema = Object.assign(schema, {
364
180
  collaborative: true,
365
181
  builtin: "CoRichText",
366
- create: (...args: any[]) => {
367
- return coSchema.create(...args);
368
- },
369
- load: (...args: any[]) => {
370
- return coSchema.load(...args);
371
- },
372
- subscribe: (...args: any[]) => {
373
- return coSchema.subscribe(...args);
374
- },
375
- getCoSchema: () => {
376
- return coSchema;
377
- },
378
- }) as unknown as RichTextSchema;
379
-
380
- // Needs to be derived from the enriched schema
381
- const coSchema = zodSchemaToCoSchema(enrichedSchema) as any;
382
-
383
- return enrichedSchema;
384
- }
385
-
386
- export const coRichTextDefiner = (): RichTextSchema => {
387
- return enrichRichTextSchema(z.instanceof(CoRichText));
182
+ }) as AnyRichTextSchema;
183
+ return zodSchemaToCoSchema(enrichedSchema);
388
184
  };
389
185
 
390
186
  export const coImageDefiner = (): typeof ImageDefinition => {
391
187
  return ImageDefinition;
392
188
  };
189
+
190
+ export const coOptionalDefiner = <T extends AnyCoSchema>(
191
+ schema: T,
192
+ ): CoOptionalSchema<T> => {
193
+ return createCoOptionalSchema(schema);
194
+ };
195
+
196
+ export const coDiscriminatedUnionDefiner = <
197
+ T extends readonly [AnyDiscriminableCoSchema, ...AnyDiscriminableCoSchema[]],
198
+ >(
199
+ discriminator: string,
200
+ schemas: T,
201
+ ): CoDiscriminatedUnionSchema<T> => {
202
+ const schema = z.discriminatedUnion(discriminator, schemas as any);
203
+ return zodSchemaToCoSchema(schema);
204
+ };
@@ -1,13 +1,19 @@
1
+ import {
2
+ ZodArray,
3
+ ZodTuple,
4
+ ZodUnion,
5
+ core,
6
+ array as zodArray,
7
+ tuple as zodTuple,
8
+ union as zodUnion,
9
+ } from "zod/v4";
1
10
  export {
2
11
  string,
3
12
  number,
4
13
  boolean,
5
- union,
6
14
  object,
7
- array,
8
15
  templateLiteral,
9
16
  json,
10
- tuple,
11
17
  date,
12
18
  emoji,
13
19
  base64,
@@ -29,8 +35,8 @@ export {
29
35
  int32,
30
36
  strictObject,
31
37
  discriminatedUnion,
32
- // intersection,
33
- // record,
38
+ // record,
39
+ // intersection,
34
40
  int,
35
41
  optional,
36
42
  type ZodOptional,
@@ -39,5 +45,29 @@ export {
39
45
  type ZodDefault,
40
46
  type ZodCatch,
41
47
  type output as infer,
48
+ type ZodDiscriminatedUnion,
42
49
  z,
43
50
  } from "zod/v4";
51
+
52
+ type NonCoZodType = core.$ZodType & { collaborative?: false };
53
+
54
+ export function union<const T extends readonly NonCoZodType[]>(
55
+ options: T,
56
+ params?: string | core.$ZodUnionParams,
57
+ ): ZodUnion<T> {
58
+ return zodUnion(options, params);
59
+ }
60
+
61
+ export function array<T extends NonCoZodType>(
62
+ element: T,
63
+ params?: string | core.$ZodArrayParams,
64
+ ): ZodArray<T> {
65
+ return zodArray(element, params);
66
+ }
67
+
68
+ export function tuple<T extends readonly [NonCoZodType, ...NonCoZodType[]]>(
69
+ options: T,
70
+ params?: string | core.$ZodTupleParams,
71
+ ): ZodTuple<T> {
72
+ return zodTuple(options, params);
73
+ }
@@ -2,23 +2,47 @@ import { LocalNode, RawAccount } from "cojson";
2
2
  import {
3
3
  Account,
4
4
  AccountClass,
5
- CoValue,
6
5
  CoValueClass,
7
6
  CoValueFromRaw,
7
+ InstanceOfSchema,
8
8
  RefsToResolve,
9
9
  RefsToResolveStrict,
10
10
  Resolved,
11
11
  Simplify,
12
12
  } from "../../internal.js";
13
- import { AnyAccountSchema } from "./schemaTypes/AccountSchema.js";
14
- import { AnyCoFeedSchema } from "./schemaTypes/CoFeedSchema.js";
15
- import { AnyCoListSchema } from "./schemaTypes/CoListSchema.js";
16
- import { AnyCoMapSchema, CoMapInitZod } from "./schemaTypes/CoMapSchema.js";
17
- import { AnyCoRecordSchema } from "./schemaTypes/CoRecordSchema.js";
18
- import { FileStreamSchema } from "./schemaTypes/FileStreamSchema.js";
19
- import { PlainTextSchema } from "./schemaTypes/PlainTextSchema.js";
20
- import { RichTextSchema } from "./schemaTypes/RichTextSchema.js";
21
- import { InstanceOfSchema } from "./typeConverters/InstanceOfSchema.js";
13
+ import {
14
+ AccountSchema,
15
+ AnyAccountSchema,
16
+ BaseAccountShape,
17
+ } from "./schemaTypes/AccountSchema.js";
18
+ import {
19
+ AnyDiscriminableCoSchema,
20
+ CoDiscriminatedUnionSchema,
21
+ } from "./schemaTypes/CoDiscriminatedUnionSchema.js";
22
+ import { AnyCoFeedSchema, CoFeedSchema } from "./schemaTypes/CoFeedSchema.js";
23
+ import { AnyCoListSchema, CoListSchema } from "./schemaTypes/CoListSchema.js";
24
+ import {
25
+ AnyCoMapSchema,
26
+ CoMapInitZod,
27
+ CoMapSchema,
28
+ } from "./schemaTypes/CoMapSchema.js";
29
+ import { AnyCoOptionalSchema } from "./schemaTypes/CoOptionalSchema.js";
30
+ import {
31
+ AnyCoRecordSchema,
32
+ CoRecordSchema,
33
+ } from "./schemaTypes/CoRecordSchema.js";
34
+ import {
35
+ AnyFileStreamSchema,
36
+ FileStreamSchema,
37
+ } from "./schemaTypes/FileStreamSchema.js";
38
+ import {
39
+ AnyPlainTextSchema,
40
+ PlainTextSchema,
41
+ } from "./schemaTypes/PlainTextSchema.js";
42
+ import {
43
+ AnyRichTextSchema,
44
+ RichTextSchema,
45
+ } from "./schemaTypes/RichTextSchema.js";
22
46
  import { InstanceOfSchemaCoValuesNullable } from "./typeConverters/InstanceOfSchemaCoValuesNullable.js";
23
47
  import { z } from "./zodReExport.js";
24
48
 
@@ -47,7 +71,7 @@ export type AnyCoUnionSchema = z.core.$ZodDiscriminatedUnion<
47
71
  // this is a series of hacks to work around z4 removing _zod at runtime from z.core.$ZodType
48
72
  export function isZodObject(
49
73
  schema: z.core.$ZodType,
50
- ): schema is z.core.$ZodObject<any, any> {
74
+ ): schema is z.ZodObject<any, any> {
51
75
  return (schema as any).def?.type === "object";
52
76
  }
53
77
 
@@ -67,19 +91,53 @@ export function getDef<S extends z.core.$ZodType>(schema: S): S["_zod"]["def"] {
67
91
  return (schema as any).def;
68
92
  }
69
93
 
94
+ // TODO rename. This represents a CoValue class or a CoValue schema
70
95
  export type CoValueOrZodSchema = CoValueClass | AnyCoSchema;
71
96
 
72
- export type CoValueClassFromZodSchema<S extends z.core.$ZodType> = CoValueClass<
73
- InstanceOfSchema<S>
74
- > &
75
- CoValueFromRaw<InstanceOfSchema<S>> &
76
- (S extends AnyAccountSchema ? AccountClassEssentials : {});
97
+ // TODO rename to CoValueSchemaFromCoProtoSchema
98
+ export type CoValueSchemaFromZodSchema<S extends z.core.$ZodType> =
99
+ S extends z.core.$ZodType
100
+ ? S extends AnyAccountSchema<infer Shape extends BaseAccountShape>
101
+ ? AccountSchema<Shape>
102
+ : S extends AnyCoRecordSchema<infer K, infer V>
103
+ ? CoRecordSchema<K, V>
104
+ : S extends AnyCoMapSchema<infer Shape, infer Config>
105
+ ? CoMapSchema<Shape, Config>
106
+ : S extends AnyCoListSchema<infer T>
107
+ ? CoListSchema<T>
108
+ : S extends AnyCoFeedSchema<infer T>
109
+ ? CoFeedSchema<T>
110
+ : S extends AnyPlainTextSchema
111
+ ? PlainTextSchema
112
+ : S extends AnyRichTextSchema
113
+ ? RichTextSchema
114
+ : S extends AnyFileStreamSchema
115
+ ? FileStreamSchema
116
+ : S extends z.core.$ZodOptional<infer Inner>
117
+ ? CoValueSchemaFromZodSchema<Inner>
118
+ : S extends z.core.$ZodUnion<
119
+ infer Members extends readonly [
120
+ AnyDiscriminableCoSchema,
121
+ ...AnyDiscriminableCoSchema[],
122
+ ]
123
+ >
124
+ ? CoDiscriminatedUnionSchema<Members>
125
+ : never
126
+ : never;
127
+
128
+ export type CoValueClassFromAnySchema<S extends CoValueOrZodSchema> =
129
+ S extends CoValueClass<any>
130
+ ? S
131
+ : CoValueClass<InstanceOfSchema<S>> &
132
+ CoValueFromRaw<InstanceOfSchema<S>> &
133
+ (S extends AnyAccountSchema ? AccountClassEssentials : {});
77
134
 
78
135
  type AccountClassEssentials = {
79
136
  fromRaw: <A extends Account>(this: AccountClass<A>, raw: RawAccount) => A;
80
137
  fromNode: <A extends Account>(this: AccountClass<A>, node: LocalNode) => A;
81
138
  };
82
139
 
140
+ // TODO rename to ProtoCoSchema?
83
141
  export type AnyCoSchema =
84
142
  | AnyCoMapSchema
85
143
  | AnyAccountSchema
@@ -87,9 +145,10 @@ export type AnyCoSchema =
87
145
  | AnyCoListSchema
88
146
  | AnyCoFeedSchema
89
147
  | AnyCoUnionSchema
90
- | PlainTextSchema
91
- | RichTextSchema
92
- | FileStreamSchema;
148
+ | AnyCoOptionalSchema
149
+ | AnyPlainTextSchema
150
+ | AnyRichTextSchema
151
+ | AnyFileStreamSchema;
93
152
 
94
153
  export type Loaded<
95
154
  T extends CoValueClass | AnyCoSchema,
@@ -33,6 +33,7 @@ export * from "./implementation/zodSchema/zodSchema.js";
33
33
  export * from "./implementation/zodSchema/zodCo.js";
34
34
  export * as co from "./implementation/zodSchema/coExport.js";
35
35
  export * from "./implementation/zodSchema/schemaTypes/CoMapSchema.js";
36
+ export * from "./implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.js";
36
37
  export * from "./implementation/zodSchema/schemaTypes/CoRecordSchema.js";
37
38
  export * from "./implementation/zodSchema/schemaTypes/CoListSchema.js";
38
39
  export * from "./implementation/zodSchema/schemaTypes/CoFeedSchema.js";
@@ -126,8 +126,7 @@ export class SubscriptionScope<D extends CoValue> {
126
126
  new JazzError(this.id, "unauthorized", [
127
127
  {
128
128
  code: "unauthorized",
129
- message:
130
- "The current user is not authorized to access this value",
129
+ message: `The current user (${this.node.getCurrentAgent().id}) is not authorized to access this value`,
131
130
  params: {
132
131
  id: this.id,
133
132
  },
@@ -4,13 +4,11 @@ import {
4
4
  Account,
5
5
  AccountClass,
6
6
  AuthSecretStorage,
7
- CoMap,
8
7
  Group,
9
8
  InMemoryKVStore,
10
9
  JazzAuthContext,
11
10
  KvStoreContext,
12
11
  co,
13
- coField,
14
12
  z,
15
13
  } from "../exports";
16
14
  import {
@@ -26,6 +24,7 @@ import {
26
24
  CoValueFromRaw,
27
25
  InstanceOfSchema,
28
26
  Loaded,
27
+ anySchemaToCoSchema,
29
28
  zodSchemaToCoSchema,
30
29
  } from "../internal";
31
30
  import {
@@ -258,7 +257,7 @@ describe("ContextManager", () => {
258
257
 
259
258
  // Create initial anonymous context
260
259
  await customManager.createContext({
261
- AccountSchema: zodSchemaToCoSchema(CustomAccount),
260
+ AccountSchema: anySchemaToCoSchema(CustomAccount),
262
261
  });
263
262
 
264
263
  const account = (
@@ -313,7 +312,7 @@ describe("ContextManager", () => {
313
312
 
314
313
  // Create initial anonymous context
315
314
  await customManager.createContext({
316
- AccountSchema: zodSchemaToCoSchema(CustomAccount),
315
+ AccountSchema: anySchemaToCoSchema(CustomAccount),
317
316
  });
318
317
 
319
318
  const account = (
@@ -339,7 +338,7 @@ describe("ContextManager", () => {
339
338
  const AccountRoot = co.map({
340
339
  value: z.string(),
341
340
  get transferredRoot(): z.ZodOptional<typeof AccountRoot> {
342
- return z.optional(AccountRoot);
341
+ return co.optional(AccountRoot);
343
342
  },
344
343
  });
345
344
 
@@ -385,7 +384,7 @@ describe("ContextManager", () => {
385
384
  // Create initial anonymous context
386
385
  await customManager.createContext({
387
386
  onAnonymousAccountDiscarded,
388
- AccountSchema: zodSchemaToCoSchema(CustomAccount),
387
+ AccountSchema: anySchemaToCoSchema(CustomAccount),
389
388
  });
390
389
 
391
390
  const account = await createJazzTestAccount({