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
@@ -2,160 +2,184 @@ import { CoValueUniqueness } from "cojson";
2
2
  import {
3
3
  Account,
4
4
  CoMap,
5
+ DiscriminableCoValueSchemaDefinition,
6
+ DiscriminableCoreCoValueSchema,
5
7
  Group,
8
+ PartialOnUndefined,
6
9
  RefsToResolve,
7
10
  RefsToResolveStrict,
8
11
  Resolved,
9
12
  Simplify,
10
13
  SubscribeListenerOptions,
11
- zodSchemaToCoSchema,
14
+ coOptionalDefiner,
15
+ hydrateCoreCoValueSchema,
16
+ isAnyCoValueSchema,
12
17
  } from "../../../internal.js";
13
18
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
19
+ import { removeGetters } from "../../schemaUtils.js";
20
+ import { CoFieldInit } from "../typeConverters/CoFieldInit.js";
14
21
  import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
15
22
  import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
16
23
  import { z } from "../zodReExport.js";
17
- import { WithHelpers } from "../zodSchema.js";
24
+ import { AnyZodOrCoValueSchema } from "../zodSchema.js";
25
+ import { CoOptionalSchema } from "./CoOptionalSchema.js";
18
26
 
19
- export type CoMapSchema<
27
+ export interface CoMapSchema<
20
28
  Shape extends z.core.$ZodLooseShape,
21
- Config extends z.core.$ZodObjectConfig = z.core.$ZodObjectConfig,
29
+ CatchAll extends AnyZodOrCoValueSchema | unknown = unknown,
22
30
  Owner extends Account | Group = Account | Group,
23
- > = AnyCoMapSchema<Shape, Config> &
24
- z.$ZodTypeDiscriminable & {
25
- create: (
26
- init: Simplify<CoMapInitZod<Shape>>,
27
- options?:
28
- | {
29
- owner: Owner;
30
- unique?: CoValueUniqueness["uniqueness"];
31
- }
32
- | Owner,
33
- ) => (Shape extends Record<string, never>
34
- ? {}
35
- : {
36
- -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
37
- Shape[key]
38
- >;
39
- }) &
40
- (unknown extends Config["out"][string]
41
- ? {}
42
- : {
43
- [key: string]: Config["out"][string];
44
- }) &
45
- CoMap;
46
-
47
- load<
48
- const R extends RefsToResolve<
49
- Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap
50
- > = true,
51
- >(
52
- id: string,
53
- options?: {
54
- resolve?: RefsToResolveStrict<
55
- Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
56
- R
57
- >;
58
- loadAs?: Account | AnonymousJazzAgent;
59
- skipRetry?: boolean;
60
- },
61
- ): Promise<Resolved<
31
+ > extends CoreCoMapSchema<Shape, CatchAll> {
32
+ create: (
33
+ init: Simplify<CoMapSchemaInit<Shape>>,
34
+ options?:
35
+ | {
36
+ owner: Owner;
37
+ unique?: CoValueUniqueness["uniqueness"];
38
+ }
39
+ | Owner,
40
+ ) => CoMapInstanceShape<Shape, CatchAll> & CoMap;
41
+
42
+ load<
43
+ const R extends RefsToResolve<
44
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap
45
+ > = true,
46
+ >(
47
+ id: string,
48
+ options?: {
49
+ resolve?: RefsToResolveStrict<
50
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
51
+ R
52
+ >;
53
+ loadAs?: Account | AnonymousJazzAgent;
54
+ skipRetry?: boolean;
55
+ },
56
+ ): Promise<Resolved<
57
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
58
+ R
59
+ > | null>;
60
+
61
+ subscribe<
62
+ const R extends RefsToResolve<
63
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap
64
+ > = true,
65
+ >(
66
+ id: string,
67
+ options: SubscribeListenerOptions<
62
68
  Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
63
69
  R
64
- > | null>;
65
-
66
- subscribe<
67
- const R extends RefsToResolve<
68
- Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap
69
- > = true,
70
- >(
71
- id: string,
72
- options: SubscribeListenerOptions<
70
+ >,
71
+ listener: (
72
+ value: Resolved<
73
73
  Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
74
74
  R
75
75
  >,
76
- listener: (
77
- value: Resolved<
78
- Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
79
- R
80
- >,
81
- unsubscribe: () => void,
82
- ) => void,
83
- ): () => void;
84
-
85
- /** @deprecated Use `CoMap.upsertUnique` and `CoMap.loadUnique` instead. */
86
- findUnique(
87
- unique: CoValueUniqueness["uniqueness"],
88
- ownerID: string,
89
- as?: Account | Group | AnonymousJazzAgent,
90
- ): string;
91
-
92
- upsertUnique: <
93
- const R extends RefsToResolve<
94
- Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap
95
- > = true,
96
- >(options: {
97
- value: Simplify<CoMapInitZod<Shape>>;
98
- unique: CoValueUniqueness["uniqueness"];
99
- owner: Owner;
76
+ unsubscribe: () => void,
77
+ ) => void,
78
+ ): () => void;
79
+
80
+ /** @deprecated Use `CoMap.upsertUnique` and `CoMap.loadUnique` instead. */
81
+ findUnique(
82
+ unique: CoValueUniqueness["uniqueness"],
83
+ ownerID: string,
84
+ as?: Account | Group | AnonymousJazzAgent,
85
+ ): string;
86
+
87
+ upsertUnique: <
88
+ const R extends RefsToResolve<
89
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap
90
+ > = true,
91
+ >(options: {
92
+ value: Simplify<CoMapSchemaInit<Shape>>;
93
+ unique: CoValueUniqueness["uniqueness"];
94
+ owner: Owner;
95
+ resolve?: RefsToResolveStrict<
96
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
97
+ R
98
+ >;
99
+ }) => Promise<Resolved<
100
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
101
+ R
102
+ > | null>;
103
+
104
+ loadUnique<
105
+ const R extends RefsToResolve<
106
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap
107
+ > = true,
108
+ >(
109
+ unique: CoValueUniqueness["uniqueness"],
110
+ ownerID: string,
111
+ options?: {
100
112
  resolve?: RefsToResolveStrict<
101
113
  Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
102
114
  R
103
115
  >;
104
- }) => Promise<Resolved<
105
- Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
106
- R
107
- > | null>;
108
-
109
- loadUnique<
110
- const R extends RefsToResolve<
111
- Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap
112
- > = true,
113
- >(
114
- unique: CoValueUniqueness["uniqueness"],
115
- ownerID: string,
116
- options?: {
117
- resolve?: RefsToResolveStrict<
118
- Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
119
- R
120
- >;
121
- loadAs?: Account | AnonymousJazzAgent;
116
+ loadAs?: Account | AnonymousJazzAgent;
117
+ },
118
+ ): Promise<Resolved<
119
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
120
+ R
121
+ > | null>;
122
+
123
+ catchall<T extends AnyZodOrCoValueSchema>(schema: T): CoMapSchema<Shape, T>;
124
+
125
+ withMigration(
126
+ migration: (
127
+ value: Resolved<
128
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
129
+ true
130
+ >,
131
+ ) => undefined,
132
+ ): CoMapSchema<Shape, CatchAll, Owner>;
133
+
134
+ getCoValueClass: () => typeof CoMap;
135
+
136
+ optional(): CoOptionalSchema<this>;
137
+ }
138
+
139
+ export function createCoreCoMapSchema<
140
+ Shape extends z.core.$ZodLooseShape,
141
+ CatchAll extends AnyZodOrCoValueSchema | unknown = unknown,
142
+ >(shape: Shape, catchAll?: CatchAll): CoreCoMapSchema<Shape, CatchAll> {
143
+ return {
144
+ collaborative: true as const,
145
+ builtin: "CoMap" as const,
146
+ shape,
147
+ catchAll,
148
+ getDefinition: () => ({
149
+ get shape() {
150
+ return shape;
122
151
  },
123
- ): Promise<Resolved<
124
- Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
125
- R
126
- > | null>;
127
-
128
- catchall<T extends z.core.$ZodType>(
129
- schema: T,
130
- ): CoMapSchema<Shape, z.core.$catchall<T>>;
131
-
132
- /** @deprecated Define your helper methods separately, in standalone functions. */
133
- withHelpers<S extends z.core.$ZodType, T extends object>(
134
- this: S,
135
- helpers: (Self: S) => T,
136
- ): WithHelpers<S, T>;
137
-
138
- withMigration(
139
- migration: (
140
- value: Resolved<
141
- Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
142
- true
143
- >,
144
- ) => undefined,
145
- ): CoMapSchema<Shape, Config, Owner>;
146
-
147
- getCoValueClass: () => typeof CoMap;
152
+ get catchall() {
153
+ return catchAll;
154
+ },
155
+ get discriminatorMap() {
156
+ const propValues: DiscriminableCoValueSchemaDefinition["discriminatorMap"] =
157
+ {};
158
+ // remove getters to avoid circularity issues. Getters are not used as discriminators
159
+ for (const key in removeGetters(shape)) {
160
+ if (isAnyCoValueSchema(shape[key])) {
161
+ // CoValues cannot be used as discriminators either
162
+ continue;
163
+ }
164
+ const field = shape[key]._zod;
165
+ if (field.values) {
166
+ propValues[key] ??= new Set();
167
+ for (const v of field.values) propValues[key].add(v);
168
+ }
169
+ }
170
+ return propValues;
171
+ },
172
+ }),
148
173
  };
174
+ }
149
175
 
150
176
  export function enrichCoMapSchema<
151
177
  Shape extends z.core.$ZodLooseShape,
152
- Config extends z.core.$ZodObjectConfig,
178
+ CatchAll extends AnyZodOrCoValueSchema | unknown,
153
179
  >(
154
- schema: AnyCoMapSchema<Shape, Config>,
180
+ schema: CoreCoMapSchema<Shape, CatchAll>,
155
181
  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;
182
+ ): CoMapSchema<Shape, CatchAll> {
159
183
  const coValueSchema = Object.assign(schema, {
160
184
  create: (...args: [any, ...any[]]) => {
161
185
  return coValueClass.create(...args);
@@ -179,16 +203,12 @@ export function enrichCoMapSchema<
179
203
  // @ts-expect-error
180
204
  return coValueClass.loadUnique(...args);
181
205
  },
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));
206
+ catchall: (catchAll: AnyZodOrCoValueSchema) => {
207
+ const schemaWithCatchAll = createCoreCoMapSchema(
208
+ coValueSchema.getDefinition().shape,
209
+ catchAll,
210
+ );
211
+ return hydrateCoreCoValueSchema(schemaWithCatchAll);
192
212
  },
193
213
  withMigration: (migration: (value: any) => undefined) => {
194
214
  // @ts-expect-error TODO check
@@ -199,41 +219,50 @@ export function enrichCoMapSchema<
199
219
  getCoValueClass: () => {
200
220
  return coValueClass;
201
221
  },
202
- }) as unknown as CoMapSchema<Shape, Config>;
222
+
223
+ optional: () => {
224
+ return coOptionalDefiner(coValueSchema);
225
+ },
226
+ }) as unknown as CoMapSchema<Shape, CatchAll>;
203
227
  return coValueSchema;
204
228
  }
205
229
 
206
- export type optionalKeys<Shape extends z.core.$ZodLooseShape> = {
207
- [key in keyof Shape]: Shape[key] extends z.core.$ZodOptional<any>
208
- ? key
209
- : never;
210
- }[keyof Shape];
211
-
212
- export type requiredKeys<Shape extends z.core.$ZodLooseShape> = {
213
- [key in keyof Shape]: Shape[key] extends z.core.$ZodOptional<any>
214
- ? never
215
- : key;
216
- }[keyof Shape];
217
-
218
- export type CoMapInitZod<Shape extends z.core.$ZodLooseShape> = {
219
- [key in optionalKeys<Shape>]?: NonNullable<
220
- InstanceOrPrimitiveOfSchemaCoValuesNullable<Shape[key]>
221
- >;
222
- } & {
223
- [key in requiredKeys<Shape>]: NonNullable<
224
- InstanceOrPrimitiveOfSchemaCoValuesNullable<Shape[key]>
225
- >;
226
- } & { [key in keyof Shape]?: unknown };
230
+ // Due to a TS limitation with types that contain known properties and
231
+ // an index signature, we cannot accept catchall properties on creation
232
+ export type CoMapSchemaInit<Shape extends z.core.$ZodLooseShape> =
233
+ PartialOnUndefined<{
234
+ [key in keyof Shape]: CoFieldInit<Shape[key]>;
235
+ }>;
236
+
237
+ export interface CoMapSchemaDefinition<
238
+ Shape extends z.core.$ZodLooseShape = z.core.$ZodLooseShape,
239
+ CatchAll extends AnyZodOrCoValueSchema | unknown = unknown,
240
+ > extends DiscriminableCoValueSchemaDefinition {
241
+ shape: Shape;
242
+ catchall?: CatchAll;
243
+ }
227
244
 
228
245
  // less precise version to avoid circularity issues and allow matching against
229
- export type AnyCoMapSchema<
246
+ export interface CoreCoMapSchema<
230
247
  Shape extends z.core.$ZodLooseShape = z.core.$ZodLooseShape,
231
- Config extends z.core.$ZodObjectConfig = z.core.$ZodObjectConfig,
232
- > = z.core.$ZodObject<Shape, Config> & { collaborative: true };
248
+ CatchAll extends AnyZodOrCoValueSchema | unknown = unknown,
249
+ > extends DiscriminableCoreCoValueSchema {
250
+ builtin: "CoMap";
251
+ shape: Shape;
252
+ catchAll?: CatchAll;
253
+ getDefinition: () => CoMapSchemaDefinition;
254
+ }
233
255
 
234
- export type CoMapInstance<Shape extends z.core.$ZodLooseShape> = {
256
+ export type CoMapInstanceShape<
257
+ Shape extends z.core.$ZodLooseShape,
258
+ CatchAll extends AnyZodOrCoValueSchema | unknown = unknown,
259
+ > = {
235
260
  -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;
236
- } & CoMap;
261
+ } & (CatchAll extends AnyZodOrCoValueSchema
262
+ ? {
263
+ [key: string]: InstanceOrPrimitiveOfSchema<CatchAll>;
264
+ }
265
+ : {});
237
266
 
238
267
  export type CoMapInstanceCoValuesNullable<Shape extends z.core.$ZodLooseShape> =
239
268
  {
@@ -1,33 +1,35 @@
1
- import { isAnyCoValueSchema } from "../runtimeConverters/zodSchemaToCoSchema.js";
2
- import { z } from "../zodReExport.js";
3
- import { AnyCoSchema, CoValueSchemaFromZodSchema } from "../zodSchema.js";
1
+ import { CoValueSchemaFromCoreSchema } from "../zodSchema.js";
2
+ import { CoreCoValueSchema } from "./CoValueSchema.js";
4
3
 
5
- export type AnyCoOptionalSchema<
6
- Shape extends z.core.$ZodType = z.core.$ZodType,
7
- > = z.ZodOptional<Shape> & {
8
- collaborative: true;
4
+ type CoOptionalSchemaDefinition<
5
+ Shape extends CoreCoValueSchema = CoreCoValueSchema,
6
+ > = {
7
+ innerType: Shape;
9
8
  };
10
9
 
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>;
10
+ export interface CoreCoOptionalSchema<
11
+ Shape extends CoreCoValueSchema = CoreCoValueSchema,
12
+ > extends CoreCoValueSchema {
13
+ builtin: "CoOptional";
14
+ innerType: Shape;
15
+ getDefinition: () => CoOptionalSchemaDefinition<Shape>;
27
16
  }
28
17
 
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";
18
+ export class CoOptionalSchema<
19
+ Shape extends CoreCoValueSchema = CoreCoValueSchema,
20
+ > implements CoreCoOptionalSchema<Shape>
21
+ {
22
+ readonly collaborative = true as const;
23
+ readonly builtin = "CoOptional" as const;
24
+ readonly getDefinition = () => ({
25
+ innerType: this.innerType,
26
+ });
27
+
28
+ constructor(public readonly innerType: Shape) {}
29
+
30
+ getCoValueClass(): ReturnType<
31
+ CoValueSchemaFromCoreSchema<Shape>["getCoValueClass"]
32
+ > {
33
+ return (this.innerType as any).getCoValueClass();
34
+ }
33
35
  }
@@ -2,6 +2,7 @@ import { CoValueUniqueness } from "cojson";
2
2
  import {
3
3
  Account,
4
4
  type CoMap,
5
+ CoMapSchemaDefinition,
5
6
  Group,
6
7
  ID,
7
8
  RefsToResolve,
@@ -11,24 +12,25 @@ import {
11
12
  SubscribeListenerOptions,
12
13
  } from "../../../internal.js";
13
14
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
15
+ import { CoFieldInit } from "../typeConverters/CoFieldInit.js";
14
16
  import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
15
17
  import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
16
18
  import { z } from "../zodReExport.js";
17
- import { WithHelpers } from "../zodSchema.js";
19
+ import { AnyZodOrCoValueSchema } from "../zodSchema.js";
20
+ import { CoOptionalSchema } from "./CoOptionalSchema.js";
21
+ import { CoreCoValueSchema } from "./CoValueSchema.js";
18
22
 
19
23
  type CoRecordInit<
20
24
  K extends z.core.$ZodString<string>,
21
- V extends z.core.$ZodType,
25
+ V extends AnyZodOrCoValueSchema,
22
26
  > = {
23
- [key in z.output<K>]: V extends z.core.$ZodOptional<any>
24
- ? InstanceOrPrimitiveOfSchemaCoValuesNullable<V>
25
- : NonNullable<InstanceOrPrimitiveOfSchemaCoValuesNullable<V>>;
27
+ [key in z.output<K>]: CoFieldInit<V>;
26
28
  };
27
29
 
28
- export type CoRecordSchema<
30
+ export interface CoRecordSchema<
29
31
  K extends z.core.$ZodString<string>,
30
- V extends z.core.$ZodType,
31
- > = AnyCoRecordSchema<K, V> & {
32
+ V extends AnyZodOrCoValueSchema,
33
+ > extends CoreCoRecordSchema<K, V> {
32
34
  create: (
33
35
  init: Simplify<CoRecordInit<K, V>>,
34
36
  options?:
@@ -76,30 +78,38 @@ export type CoRecordSchema<
76
78
  as?: Account | Group | AnonymousJazzAgent,
77
79
  ): ID<CoRecordInstanceCoValuesNullable<K, V>>;
78
80
 
79
- /** @deprecated Define your helper methods separately, in standalone functions. */
80
- withHelpers<S extends z.core.$ZodType, T extends object>(
81
- this: S,
82
- helpers: (Self: S) => T,
83
- ): WithHelpers<S, T>;
84
81
  getCoValueClass: () => typeof CoMap;
82
+
83
+ optional(): CoOptionalSchema<this>;
84
+ }
85
+
86
+ type CoRecordSchemaDefinition<
87
+ K extends z.core.$ZodString<string>,
88
+ V extends AnyZodOrCoValueSchema,
89
+ > = CoMapSchemaDefinition & {
90
+ keyType: K;
91
+ valueType: V;
85
92
  };
86
93
 
87
94
  // less precise version to avoid circularity issues and allow matching against
88
- export type AnyCoRecordSchema<
95
+ export interface CoreCoRecordSchema<
89
96
  K extends z.core.$ZodString<string> = z.core.$ZodString<string>,
90
- V extends z.core.$ZodType = z.core.$ZodType,
91
- > = z.core.$ZodRecord<K, V> & { collaborative: true };
97
+ V extends AnyZodOrCoValueSchema = AnyZodOrCoValueSchema,
98
+ > extends CoreCoValueSchema {
99
+ builtin: "CoMap";
100
+ getDefinition: () => CoRecordSchemaDefinition<K, V>;
101
+ }
92
102
 
93
103
  export type CoRecordInstance<
94
104
  K extends z.core.$ZodString<string>,
95
- V extends z.core.$ZodType,
105
+ V extends AnyZodOrCoValueSchema,
96
106
  > = {
97
107
  [key in z.output<K>]: InstanceOrPrimitiveOfSchema<V>;
98
108
  } & CoMap;
99
109
 
100
110
  export type CoRecordInstanceCoValuesNullable<
101
111
  K extends z.core.$ZodString<string>,
102
- V extends z.core.$ZodType,
112
+ V extends AnyZodOrCoValueSchema,
103
113
  > = {
104
114
  [key in z.output<K>]: InstanceOrPrimitiveOfSchemaCoValuesNullable<V>;
105
115
  } & CoMap;
@@ -0,0 +1,18 @@
1
+ /**
2
+ * "Core" CoValue schemas contain all data necessary to represent a CoValue schema.
3
+ * Behavior is provided by CoValue schemas that extend "core" CoValue schema data structures.
4
+ *
5
+ * "Core" CoValue schemas are necessary to avoid circularity issues when defining schemas.
6
+ * This is similar to how Zod's "core" schemas are used.
7
+ *
8
+ * They are not meant to be used directly outside of the `jazz-tools` package,
9
+ * use concrete CoValue schemas instead.
10
+ */
11
+ export interface CoreCoValueSchema {
12
+ collaborative: true;
13
+
14
+ /**
15
+ * Used for discriminating between different CoValue schemas.
16
+ */
17
+ builtin: string;
18
+ }