jazz-tools 0.15.15 → 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 (217) 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 -50
  7. package/CHANGELOG.md +35 -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-4CFNXQE7.js → chunk-MLCNE3TL.js} +791 -698
  15. package/dist/chunk-MLCNE3TL.js.map +1 -0
  16. package/dist/index.js +363 -11
  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 +2 -0
  21. package/dist/react/index.js.map +1 -1
  22. package/dist/react/testing.js +3 -1
  23. package/dist/react/testing.js.map +1 -1
  24. package/dist/react-core/hooks.d.ts +2 -2
  25. package/dist/react-core/hooks.d.ts.map +1 -1
  26. package/dist/react-core/index.js +3 -3
  27. package/dist/react-core/index.js.map +1 -1
  28. package/dist/react-native-core/hooks.d.ts +2 -2
  29. package/dist/react-native-core/hooks.d.ts.map +1 -1
  30. package/dist/react-native-core/index.js.map +1 -1
  31. package/dist/svelte/jazz.class.svelte.d.ts +2 -2
  32. package/dist/svelte/jazz.class.svelte.d.ts.map +1 -1
  33. package/dist/svelte/jazz.class.svelte.js +5 -5
  34. package/dist/svelte/jazz.svelte.d.ts +2 -2
  35. package/dist/svelte/jazz.svelte.d.ts.map +1 -1
  36. package/dist/testing.js +3 -3
  37. package/dist/testing.js.map +1 -1
  38. package/dist/tools/coValues/CoValueBase.d.ts +3 -13
  39. package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
  40. package/dist/tools/coValues/account.d.ts +2 -2
  41. package/dist/tools/coValues/account.d.ts.map +1 -1
  42. package/dist/tools/coValues/coFeed.d.ts.map +1 -1
  43. package/dist/tools/coValues/coList.d.ts.map +1 -1
  44. package/dist/tools/coValues/coMap.d.ts +5 -18
  45. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  46. package/dist/tools/coValues/deepLoading.d.ts +4 -1
  47. package/dist/tools/coValues/deepLoading.d.ts.map +1 -1
  48. package/dist/tools/coValues/extensions/imageDef.d.ts +4 -7
  49. package/dist/tools/coValues/extensions/imageDef.d.ts.map +1 -1
  50. package/dist/tools/coValues/group.d.ts +1 -0
  51. package/dist/tools/coValues/group.d.ts.map +1 -1
  52. package/dist/tools/coValues/inbox.d.ts +2 -2
  53. package/dist/tools/coValues/inbox.d.ts.map +1 -1
  54. package/dist/tools/coValues/interfaces.d.ts +58 -17
  55. package/dist/tools/coValues/interfaces.d.ts.map +1 -1
  56. package/dist/tools/coValues/request.d.ts +82 -0
  57. package/dist/tools/coValues/request.d.ts.map +1 -0
  58. package/dist/tools/coValues/schemaUnion.d.ts +5 -1
  59. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  60. package/dist/tools/exports.d.ts +4 -3
  61. package/dist/tools/exports.d.ts.map +1 -1
  62. package/dist/tools/implementation/createContext.d.ts +4 -4
  63. package/dist/tools/implementation/createContext.d.ts.map +1 -1
  64. package/dist/tools/implementation/invites.d.ts +2 -2
  65. package/dist/tools/implementation/invites.d.ts.map +1 -1
  66. package/dist/tools/implementation/schemaUtils.d.ts +8 -0
  67. package/dist/tools/implementation/schemaUtils.d.ts.map +1 -0
  68. package/dist/tools/implementation/zodSchema/coExport.d.ts +11 -1
  69. package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
  70. package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts +22 -0
  71. package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts.map +1 -0
  72. package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts +10 -0
  73. package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts.map +1 -0
  74. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +11 -11
  75. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  76. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +34 -24
  77. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -1
  78. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +16 -14
  79. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  80. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +23 -17
  81. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  82. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +24 -16
  83. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  84. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts +20 -9
  85. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts.map +1 -1
  86. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +18 -12
  87. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  88. package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts +18 -0
  89. package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts.map +1 -0
  90. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +14 -9
  91. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  92. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +14 -9
  93. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  94. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +14 -9
  95. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  96. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +15 -13
  97. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  98. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +12 -15
  99. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -1
  100. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +17 -20
  101. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -1
  102. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +17 -20
  103. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -1
  104. package/dist/tools/implementation/zodSchema/unionUtils.d.ts +3 -5
  105. package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
  106. package/dist/tools/implementation/zodSchema/zodCo.d.ts +10 -8
  107. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  108. package/dist/tools/implementation/zodSchema/zodReExport.d.ts +4 -8
  109. package/dist/tools/implementation/zodSchema/zodReExport.d.ts.map +1 -1
  110. package/dist/tools/implementation/zodSchema/zodSchema.d.ts +21 -30
  111. package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  112. package/dist/tools/internal.d.ts +3 -2
  113. package/dist/tools/internal.d.ts.map +1 -1
  114. package/dist/tools/lib/id.d.ts +2 -0
  115. package/dist/tools/lib/id.d.ts.map +1 -0
  116. package/dist/tools/lib/utilityTypes.d.ts +10 -0
  117. package/dist/tools/lib/utilityTypes.d.ts.map +1 -0
  118. package/dist/tools/subscribe/SubscriptionScope.d.ts +3 -2
  119. package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
  120. package/dist/tools/subscribe/utils.d.ts.map +1 -1
  121. package/dist/tools/testing.d.ts +2 -2
  122. package/dist/tools/testing.d.ts.map +1 -1
  123. package/dist/tools/tests/exportImport.test.d.ts +2 -0
  124. package/dist/tools/tests/exportImport.test.d.ts.map +1 -0
  125. package/dist/tools/tests/request.test.d.ts +2 -0
  126. package/dist/tools/tests/request.test.d.ts.map +1 -0
  127. package/dist/worker/index.d.ts.map +1 -1
  128. package/dist/worker/index.js +2 -2
  129. package/dist/worker/index.js.map +1 -1
  130. package/package.json +7 -6
  131. package/src/browser/index.ts +2 -4
  132. package/src/browser-media-images/index.ts +1 -1
  133. package/src/react/hooks.tsx +2 -2
  134. package/src/react-core/hooks.ts +6 -6
  135. package/src/react-core/tests/useAccount.test.ts +2 -2
  136. package/src/react-core/tests/useCoState.test.ts +3 -2
  137. package/src/react-native-core/hooks.tsx +2 -2
  138. package/src/svelte/jazz.class.svelte.ts +10 -7
  139. package/src/svelte/jazz.svelte.ts +2 -2
  140. package/src/tools/coValues/CoValueBase.ts +8 -20
  141. package/src/tools/coValues/account.ts +18 -14
  142. package/src/tools/coValues/coFeed.ts +0 -4
  143. package/src/tools/coValues/coList.ts +7 -9
  144. package/src/tools/coValues/coMap.ts +1 -6
  145. package/src/tools/coValues/coPlainText.ts +4 -4
  146. package/src/tools/coValues/deepLoading.ts +4 -1
  147. package/src/tools/coValues/extensions/imageDef.ts +3 -3
  148. package/src/tools/coValues/group.ts +1 -0
  149. package/src/tools/coValues/inbox.ts +8 -7
  150. package/src/tools/coValues/interfaces.ts +177 -69
  151. package/src/tools/coValues/request.ts +633 -0
  152. package/src/tools/coValues/schemaUnion.ts +8 -4
  153. package/src/tools/exports.ts +11 -14
  154. package/src/tools/implementation/createContext.ts +9 -9
  155. package/src/tools/implementation/invites.ts +2 -2
  156. package/src/tools/implementation/schemaUtils.ts +18 -0
  157. package/src/tools/implementation/zodSchema/coExport.ts +14 -0
  158. package/src/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.ts +156 -0
  159. package/src/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.ts +133 -0
  160. package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +36 -17
  161. package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +101 -52
  162. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +54 -50
  163. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +54 -46
  164. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +187 -137
  165. package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +29 -27
  166. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +28 -18
  167. package/src/tools/implementation/zodSchema/schemaTypes/CoValueSchema.ts +18 -0
  168. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +45 -36
  169. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +47 -35
  170. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +43 -30
  171. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +28 -23
  172. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +28 -25
  173. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +86 -78
  174. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +91 -82
  175. package/src/tools/implementation/zodSchema/unionUtils.ts +47 -51
  176. package/src/tools/implementation/zodSchema/zodCo.ts +42 -75
  177. package/src/tools/implementation/zodSchema/zodReExport.ts +44 -24
  178. package/src/tools/implementation/zodSchema/zodSchema.ts +64 -102
  179. package/src/tools/internal.ts +3 -2
  180. package/src/tools/lib/id.ts +3 -0
  181. package/src/tools/lib/utilityTypes.ts +7 -0
  182. package/src/tools/subscribe/SubscriptionScope.ts +10 -1
  183. package/src/tools/subscribe/utils.ts +4 -2
  184. package/src/tools/testing.ts +4 -4
  185. package/src/tools/tests/ContextManager.test.ts +8 -9
  186. package/src/tools/tests/account.test.ts +65 -3
  187. package/src/tools/tests/coDiscriminatedUnion.test-d.ts +38 -0
  188. package/src/tools/tests/coDiscriminatedUnion.test.ts +219 -1
  189. package/src/tools/tests/coFeed.test-d.ts +4 -3
  190. package/src/tools/tests/coList.test-d.ts +32 -3
  191. package/src/tools/tests/coList.test.ts +20 -2
  192. package/src/tools/tests/coMap.record.test-d.ts +31 -3
  193. package/src/tools/tests/coMap.record.test.ts +9 -9
  194. package/src/tools/tests/coMap.test-d.ts +8 -8
  195. package/src/tools/tests/coMap.test.ts +19 -5
  196. package/src/tools/tests/coOptional.test.ts +63 -1
  197. package/src/tools/tests/createContext.test.ts +7 -9
  198. package/src/tools/tests/deepLoading.test.ts +4 -10
  199. package/src/tools/tests/exportImport.test.ts +526 -0
  200. package/src/tools/tests/groupsAndAccounts.test.ts +5 -4
  201. package/src/tools/tests/inbox.test.ts +3 -2
  202. package/src/tools/tests/load.test.ts +3 -29
  203. package/src/tools/tests/request.test.ts +951 -0
  204. package/src/tools/tests/schemaUnion.test.ts +2 -2
  205. package/src/tools/tests/subscribe.test.ts +22 -114
  206. package/src/tools/tests/testing.test.ts +6 -6
  207. package/src/tools/tests/zod.test-d.ts +27 -0
  208. package/src/tools/tests/zod.test.ts +50 -45
  209. package/src/worker/index.ts +0 -1
  210. package/tsup.config.ts +0 -2
  211. package/dist/chunk-4CFNXQE7.js.map +0 -1
  212. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +0 -12
  213. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +0 -1
  214. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +0 -9
  215. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +0 -1
  216. package/src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +0 -172
  217. package/src/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +0 -218
@@ -2,160 +2,193 @@ import { CoValueUniqueness } from "cojson";
2
2
  import {
3
3
  Account,
4
4
  CoMap,
5
+ DiscriminableCoValueSchemaDefinition,
6
+ DiscriminableCoreCoValueSchema,
5
7
  Group,
6
8
  RefsToResolve,
7
9
  RefsToResolveStrict,
8
10
  Resolved,
9
11
  Simplify,
10
12
  SubscribeListenerOptions,
11
- zodSchemaToCoSchema,
13
+ coOptionalDefiner,
14
+ hydrateCoreCoValueSchema,
15
+ isAnyCoValueSchema,
12
16
  } from "../../../internal.js";
13
17
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
18
+ import { removeGetters } from "../../schemaUtils.js";
14
19
  import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
15
20
  import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
16
21
  import { z } from "../zodReExport.js";
17
- import { WithHelpers } from "../zodSchema.js";
22
+ import { AnyZodOrCoValueSchema } from "../zodSchema.js";
23
+ import { CoOptionalSchema, CoreCoOptionalSchema } from "./CoOptionalSchema.js";
18
24
 
19
- export type CoMapSchema<
25
+ export interface CoMapSchema<
20
26
  Shape extends z.core.$ZodLooseShape,
21
- Config extends z.core.$ZodObjectConfig = z.core.$ZodObjectConfig,
27
+ CatchAll extends AnyZodOrCoValueSchema | unknown = unknown,
22
28
  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>
29
+ > extends CoreCoMapSchema<Shape, CatchAll> {
30
+ create: (
31
+ init: Simplify<CoMapInitZod<Shape>>,
32
+ options?:
33
+ | {
34
+ owner: Owner;
35
+ unique?: CoValueUniqueness["uniqueness"];
36
+ }
37
+ | Owner,
38
+ ) => (Shape extends Record<string, never>
39
+ ? {}
40
+ : {
41
+ -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;
42
+ }) &
43
+ (unknown extends CatchAll
34
44
  ? {}
35
45
  : {
36
- -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
37
- Shape[key]
38
- >;
46
+ // @ts-expect-error
47
+ [key: string]: InstanceOrPrimitiveOfSchema<CatchAll>;
39
48
  }) &
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<
49
+ CoMap;
50
+
51
+ load<
52
+ const R extends RefsToResolve<
53
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap
54
+ > = true,
55
+ >(
56
+ id: string,
57
+ options?: {
58
+ resolve?: RefsToResolveStrict<
59
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
60
+ R
61
+ >;
62
+ loadAs?: Account | AnonymousJazzAgent;
63
+ skipRetry?: boolean;
64
+ },
65
+ ): Promise<Resolved<
66
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
67
+ R
68
+ > | null>;
69
+
70
+ subscribe<
71
+ const R extends RefsToResolve<
72
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap
73
+ > = true,
74
+ >(
75
+ id: string,
76
+ options: SubscribeListenerOptions<
62
77
  Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
63
78
  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<
79
+ >,
80
+ listener: (
81
+ value: Resolved<
73
82
  Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
74
83
  R
75
84
  >,
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;
85
+ unsubscribe: () => void,
86
+ ) => void,
87
+ ): () => void;
88
+
89
+ /** @deprecated Use `CoMap.upsertUnique` and `CoMap.loadUnique` instead. */
90
+ findUnique(
91
+ unique: CoValueUniqueness["uniqueness"],
92
+ ownerID: string,
93
+ as?: Account | Group | AnonymousJazzAgent,
94
+ ): string;
95
+
96
+ upsertUnique: <
97
+ const R extends RefsToResolve<
98
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap
99
+ > = true,
100
+ >(options: {
101
+ value: Simplify<CoMapInitZod<Shape>>;
102
+ unique: CoValueUniqueness["uniqueness"];
103
+ owner: Owner;
104
+ resolve?: RefsToResolveStrict<
105
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
106
+ R
107
+ >;
108
+ }) => Promise<Resolved<
109
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
110
+ R
111
+ > | null>;
112
+
113
+ loadUnique<
114
+ const R extends RefsToResolve<
115
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap
116
+ > = true,
117
+ >(
118
+ unique: CoValueUniqueness["uniqueness"],
119
+ ownerID: string,
120
+ options?: {
100
121
  resolve?: RefsToResolveStrict<
101
122
  Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
102
123
  R
103
124
  >;
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;
125
+ loadAs?: Account | AnonymousJazzAgent;
126
+ },
127
+ ): Promise<Resolved<
128
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
129
+ R
130
+ > | null>;
131
+
132
+ catchall<T extends AnyZodOrCoValueSchema>(schema: T): CoMapSchema<Shape, T>;
133
+
134
+ withMigration(
135
+ migration: (
136
+ value: Resolved<
137
+ Simplify<CoMapInstanceCoValuesNullable<Shape>> & CoMap,
138
+ true
139
+ >,
140
+ ) => undefined,
141
+ ): CoMapSchema<Shape, CatchAll, Owner>;
142
+
143
+ getCoValueClass: () => typeof CoMap;
144
+
145
+ optional(): CoOptionalSchema<this>;
146
+ }
147
+
148
+ export function createCoreCoMapSchema<
149
+ Shape extends z.core.$ZodLooseShape,
150
+ CatchAll extends AnyZodOrCoValueSchema | unknown = unknown,
151
+ >(shape: Shape, catchAll?: CatchAll): CoreCoMapSchema<Shape, CatchAll> {
152
+ return {
153
+ collaborative: true as const,
154
+ builtin: "CoMap" as const,
155
+ shape,
156
+ catchAll,
157
+ getDefinition: () => ({
158
+ get shape() {
159
+ return shape;
122
160
  },
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;
161
+ get catchall() {
162
+ return catchAll;
163
+ },
164
+ get discriminatorMap() {
165
+ const propValues: DiscriminableCoValueSchemaDefinition["discriminatorMap"] =
166
+ {};
167
+ // remove getters to avoid circularity issues. Getters are not used as discriminators
168
+ for (const key in removeGetters(shape)) {
169
+ if (isAnyCoValueSchema(shape[key])) {
170
+ // CoValues cannot be used as discriminators either
171
+ continue;
172
+ }
173
+ const field = shape[key]._zod;
174
+ if (field.values) {
175
+ propValues[key] ??= new Set();
176
+ for (const v of field.values) propValues[key].add(v);
177
+ }
178
+ }
179
+ return propValues;
180
+ },
181
+ }),
148
182
  };
183
+ }
149
184
 
150
185
  export function enrichCoMapSchema<
151
186
  Shape extends z.core.$ZodLooseShape,
152
- Config extends z.core.$ZodObjectConfig,
187
+ CatchAll extends AnyZodOrCoValueSchema | unknown,
153
188
  >(
154
- schema: AnyCoMapSchema<Shape, Config>,
189
+ schema: CoreCoMapSchema<Shape, CatchAll>,
155
190
  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;
191
+ ): CoMapSchema<Shape, CatchAll> {
159
192
  const coValueSchema = Object.assign(schema, {
160
193
  create: (...args: [any, ...any[]]) => {
161
194
  return coValueClass.create(...args);
@@ -179,16 +212,12 @@ export function enrichCoMapSchema<
179
212
  // @ts-expect-error
180
213
  return coValueClass.loadUnique(...args);
181
214
  },
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));
215
+ catchall: (catchAll: AnyZodOrCoValueSchema) => {
216
+ const schemaWithCatchAll = createCoreCoMapSchema(
217
+ coValueSchema.getDefinition().shape,
218
+ catchAll,
219
+ );
220
+ return hydrateCoreCoValueSchema(schemaWithCatchAll);
192
221
  },
193
222
  withMigration: (migration: (value: any) => undefined) => {
194
223
  // @ts-expect-error TODO check
@@ -199,18 +228,26 @@ export function enrichCoMapSchema<
199
228
  getCoValueClass: () => {
200
229
  return coValueClass;
201
230
  },
202
- }) as unknown as CoMapSchema<Shape, Config>;
231
+
232
+ optional: () => {
233
+ return coOptionalDefiner(coValueSchema);
234
+ },
235
+ }) as unknown as CoMapSchema<Shape, CatchAll>;
203
236
  return coValueSchema;
204
237
  }
205
238
 
206
239
  export type optionalKeys<Shape extends z.core.$ZodLooseShape> = {
207
- [key in keyof Shape]: Shape[key] extends z.core.$ZodOptional<any>
240
+ [key in keyof Shape]: Shape[key] extends
241
+ | z.core.$ZodOptional<any>
242
+ | CoreCoOptionalSchema<any>
208
243
  ? key
209
244
  : never;
210
245
  }[keyof Shape];
211
246
 
212
247
  export type requiredKeys<Shape extends z.core.$ZodLooseShape> = {
213
- [key in keyof Shape]: Shape[key] extends z.core.$ZodOptional<any>
248
+ [key in keyof Shape]: Shape[key] extends
249
+ | z.core.$ZodOptional<any>
250
+ | CoreCoOptionalSchema<any>
214
251
  ? never
215
252
  : key;
216
253
  }[keyof Shape];
@@ -225,11 +262,24 @@ export type CoMapInitZod<Shape extends z.core.$ZodLooseShape> = {
225
262
  >;
226
263
  } & { [key in keyof Shape]?: unknown };
227
264
 
265
+ export interface CoMapSchemaDefinition<
266
+ Shape extends z.core.$ZodLooseShape = z.core.$ZodLooseShape,
267
+ CatchAll extends AnyZodOrCoValueSchema | unknown = unknown,
268
+ > extends DiscriminableCoValueSchemaDefinition {
269
+ shape: Shape;
270
+ catchall?: CatchAll;
271
+ }
272
+
228
273
  // less precise version to avoid circularity issues and allow matching against
229
- export type AnyCoMapSchema<
274
+ export interface CoreCoMapSchema<
230
275
  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 };
276
+ CatchAll extends AnyZodOrCoValueSchema | unknown = unknown,
277
+ > extends DiscriminableCoreCoValueSchema {
278
+ builtin: "CoMap";
279
+ shape: Shape;
280
+ catchAll?: CatchAll;
281
+ getDefinition: () => CoMapSchemaDefinition;
282
+ }
233
283
 
234
284
  export type CoMapInstance<Shape extends z.core.$ZodLooseShape> = {
235
285
  -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;
@@ -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,8 +2,10 @@ import { CoValueUniqueness } from "cojson";
2
2
  import {
3
3
  Account,
4
4
  type CoMap,
5
+ CoMapSchemaDefinition,
5
6
  Group,
6
7
  ID,
8
+ NotNull,
7
9
  RefsToResolve,
8
10
  RefsToResolveStrict,
9
11
  Resolved,
@@ -14,21 +16,21 @@ import { AnonymousJazzAgent } from "../../anonymousJazzAgent.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>]: NotNull<InstanceOrPrimitiveOfSchemaCoValuesNullable<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
+ }