jazz-tools 0.16.2 → 0.16.4

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 (76) hide show
  1. package/.turbo/turbo-build.log +38 -38
  2. package/CHANGELOG.md +22 -0
  3. package/dist/{chunk-CL3ROOZM.js → chunk-74LZG2M3.js} +112 -36
  4. package/dist/chunk-74LZG2M3.js.map +1 -0
  5. package/dist/index.js +1 -1
  6. package/dist/react-core/hooks.d.ts +150 -2
  7. package/dist/react-core/hooks.d.ts.map +1 -1
  8. package/dist/react-core/index.js.map +1 -1
  9. package/dist/testing.js +1 -1
  10. package/dist/tools/coValues/coFeed.d.ts.map +1 -1
  11. package/dist/tools/coValues/coList.d.ts.map +1 -1
  12. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  13. package/dist/tools/coValues/interfaces.d.ts.map +1 -1
  14. package/dist/tools/coValues/schemaUnion.d.ts +7 -2
  15. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  16. package/dist/tools/exports.d.ts +1 -1
  17. package/dist/tools/exports.d.ts.map +1 -1
  18. package/dist/tools/implementation/schema.d.ts +12 -2
  19. package/dist/tools/implementation/schema.d.ts.map +1 -1
  20. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +2 -4
  21. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  22. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +2 -4
  23. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  24. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +23 -7
  25. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  26. package/dist/tools/implementation/zodSchema/typeConverters/CoFieldInit.d.ts +20 -6
  27. package/dist/tools/implementation/zodSchema/typeConverters/CoFieldInit.d.ts.map +1 -1
  28. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +4 -4
  29. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  30. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +3 -3
  31. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -1
  32. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +3 -25
  33. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -1
  34. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +3 -25
  35. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -1
  36. package/dist/tools/implementation/zodSchema/typeConverters/TypeOfZodSchema.d.ts +24 -0
  37. package/dist/tools/implementation/zodSchema/typeConverters/TypeOfZodSchema.d.ts.map +1 -0
  38. package/dist/tools/implementation/zodSchema/unionUtils.d.ts +2 -3
  39. package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
  40. package/dist/tools/implementation/zodSchema/zodSchema.d.ts +4 -5
  41. package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  42. package/dist/tools/internal.d.ts +1 -0
  43. package/dist/tools/internal.d.ts.map +1 -1
  44. package/dist/tools/subscribe/SubscriptionScope.d.ts.map +1 -1
  45. package/package.json +4 -4
  46. package/src/react-core/hooks.ts +143 -0
  47. package/src/tools/coValues/coFeed.ts +12 -4
  48. package/src/tools/coValues/coList.ts +41 -19
  49. package/src/tools/coValues/coMap.ts +12 -2
  50. package/src/tools/coValues/inbox.ts +1 -1
  51. package/src/tools/coValues/interfaces.ts +2 -3
  52. package/src/tools/coValues/schemaUnion.ts +28 -3
  53. package/src/tools/exports.ts +0 -1
  54. package/src/tools/implementation/schema.ts +28 -2
  55. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +2 -7
  56. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +2 -7
  57. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +61 -12
  58. package/src/tools/implementation/zodSchema/typeConverters/CoFieldInit.ts +74 -9
  59. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +9 -4
  60. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +3 -3
  61. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +3 -114
  62. package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +5 -129
  63. package/src/tools/implementation/zodSchema/typeConverters/TypeOfZodSchema.ts +79 -0
  64. package/src/tools/implementation/zodSchema/unionUtils.ts +6 -9
  65. package/src/tools/implementation/zodSchema/zodSchema.ts +4 -14
  66. package/src/tools/internal.ts +1 -0
  67. package/src/tools/subscribe/SubscriptionScope.ts +4 -2
  68. package/src/tools/subscribe/utils.ts +2 -2
  69. package/src/tools/tests/coFeed.test.ts +40 -0
  70. package/src/tools/tests/coList.test.ts +42 -0
  71. package/src/tools/tests/coMap.test-d.ts +184 -1
  72. package/src/tools/tests/coMap.test.ts +182 -58
  73. package/src/tools/tests/groupsAndAccounts.test.ts +54 -0
  74. package/src/tools/tests/load.test.ts +19 -0
  75. package/src/tools/tests/zod.test.ts +16 -0
  76. package/dist/chunk-CL3ROOZM.js.map +0 -1
@@ -5,24 +5,25 @@ import {
5
5
  DiscriminableCoValueSchemaDefinition,
6
6
  DiscriminableCoreCoValueSchema,
7
7
  Group,
8
- PartialOnUndefined,
9
8
  RefsToResolve,
10
9
  RefsToResolveStrict,
11
10
  Resolved,
12
11
  Simplify,
13
12
  SubscribeListenerOptions,
13
+ coMapDefiner,
14
14
  coOptionalDefiner,
15
15
  hydrateCoreCoValueSchema,
16
16
  isAnyCoValueSchema,
17
17
  } from "../../../internal.js";
18
18
  import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
19
19
  import { removeGetters } from "../../schemaUtils.js";
20
- import { CoFieldInit } from "../typeConverters/CoFieldInit.js";
20
+ import { CoMapSchemaInit } from "../typeConverters/CoFieldInit.js";
21
21
  import { InstanceOrPrimitiveOfSchema } from "../typeConverters/InstanceOrPrimitiveOfSchema.js";
22
22
  import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "../typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
23
23
  import { z } from "../zodReExport.js";
24
- import { AnyZodOrCoValueSchema } from "../zodSchema.js";
24
+ import { AnyZodOrCoValueSchema, AnyZodSchema } from "../zodSchema.js";
25
25
  import { CoOptionalSchema } from "./CoOptionalSchema.js";
26
+ import { CoreCoValueSchema } from "./CoValueSchema.js";
26
27
 
27
28
  export interface CoMapSchema<
28
29
  Shape extends z.core.$ZodLooseShape,
@@ -30,7 +31,7 @@ export interface CoMapSchema<
30
31
  Owner extends Account | Group = Account | Group,
31
32
  > extends CoreCoMapSchema<Shape, CatchAll> {
32
33
  create: (
33
- init: Simplify<CoMapSchemaInit<Shape>>,
34
+ init: CoMapSchemaInit<Shape>,
34
35
  options?:
35
36
  | {
36
37
  owner: Owner;
@@ -134,6 +135,23 @@ export interface CoMapSchema<
134
135
  getCoValueClass: () => typeof CoMap;
135
136
 
136
137
  optional(): CoOptionalSchema<this>;
138
+
139
+ /**
140
+ * Creates a new CoMap schema by picking the specified keys from the original schema.
141
+ *
142
+ * @param keys - The keys to pick from the original schema.
143
+ * @returns A new CoMap schema with the picked keys.
144
+ */
145
+ pick<Keys extends keyof Shape>(
146
+ keys: { [key in Keys]: true },
147
+ ): CoMapSchema<Simplify<Pick<Shape, Keys>>, unknown, Owner>;
148
+
149
+ /**
150
+ * Creates a new CoMap schema by making all fields optional.
151
+ *
152
+ * @returns A new CoMap schema with all fields optional.
153
+ */
154
+ partial(): CoMapSchema<PartialShape<Shape>, CatchAll, Owner>;
137
155
  }
138
156
 
139
157
  export function createCoreCoMapSchema<
@@ -219,21 +237,44 @@ export function enrichCoMapSchema<
219
237
  getCoValueClass: () => {
220
238
  return coValueClass;
221
239
  },
222
-
223
240
  optional: () => {
224
241
  return coOptionalDefiner(coValueSchema);
225
242
  },
243
+ pick: <Keys extends keyof Shape>(keys: { [key in Keys]: true }) => {
244
+ const keysSet = new Set(Object.keys(keys));
245
+ const pickedShape: Record<string, AnyZodOrCoValueSchema> = {};
246
+
247
+ for (const [key, value] of Object.entries(coValueSchema.shape)) {
248
+ if (keysSet.has(key)) {
249
+ pickedShape[key] = value;
250
+ }
251
+ }
252
+
253
+ return coMapDefiner(pickedShape);
254
+ },
255
+ partial: () => {
256
+ const partialShape: Record<string, AnyZodOrCoValueSchema> = {};
257
+
258
+ for (const [key, value] of Object.entries(coValueSchema.shape)) {
259
+ if (isAnyCoValueSchema(value)) {
260
+ partialShape[key] = coOptionalDefiner(value);
261
+ } else {
262
+ partialShape[key] = z.optional(coValueSchema.shape[key]);
263
+ }
264
+ }
265
+
266
+ const partialCoMapSchema = coMapDefiner(partialShape);
267
+ if (coValueSchema.catchAll) {
268
+ return partialCoMapSchema.catchall(
269
+ coValueSchema.catchAll as unknown as AnyZodOrCoValueSchema,
270
+ );
271
+ }
272
+ return partialCoMapSchema;
273
+ },
226
274
  }) as unknown as CoMapSchema<Shape, CatchAll>;
227
275
  return coValueSchema;
228
276
  }
229
277
 
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
278
  export interface CoMapSchemaDefinition<
238
279
  Shape extends z.core.$ZodLooseShape = z.core.$ZodLooseShape,
239
280
  CatchAll extends AnyZodOrCoValueSchema | unknown = unknown,
@@ -270,3 +311,11 @@ export type CoMapInstanceCoValuesNullable<Shape extends z.core.$ZodLooseShape> =
270
311
  Shape[key]
271
312
  >;
272
313
  };
314
+
315
+ export type PartialShape<Shape extends z.core.$ZodLooseShape> = Simplify<{
316
+ -readonly [key in keyof Shape]: Shape[key] extends AnyZodSchema
317
+ ? z.ZodOptional<Shape[key]>
318
+ : Shape[key] extends CoreCoValueSchema
319
+ ? CoOptionalSchema<Shape[key]>
320
+ : never;
321
+ }>;
@@ -1,13 +1,78 @@
1
- import { NotNull } from "../../../internal.js";
1
+ import {
2
+ CoDiscriminatedUnionSchema,
3
+ CoValueClass,
4
+ CoreCoFeedSchema,
5
+ CoreCoListSchema,
6
+ CoreCoMapSchema,
7
+ CoreCoRecordSchema,
8
+ CorePlainTextSchema,
9
+ PartialOnUndefined,
10
+ Simplify,
11
+ } from "../../../internal.js";
12
+ import { CoreCoOptionalSchema } from "../schemaTypes/CoOptionalSchema.js";
13
+ import { CoreCoValueSchema } from "../schemaTypes/CoValueSchema.js";
14
+ import { CoreRichTextSchema } from "../schemaTypes/RichTextSchema.js";
2
15
  import { z } from "../zodReExport.js";
3
- import { AnyZodOrCoValueSchema } from "../zodSchema.js";
4
- import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "./InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
16
+ import { AnyZodOrCoValueSchema, Loaded } from "../zodSchema.js";
17
+ import { TypeOfZodSchema } from "./TypeOfZodSchema.js";
5
18
 
6
19
  /**
7
- * Returns the type of the value that should be used to initialize a coField
8
- * of the given schema.
20
+ * The type of value that can be used to initialize a CoField of the given schema.
21
+ *
22
+ * For CoValue fields, this can be either a shallowly-loaded CoValue instance
23
+ * or a JSON object that will be used to create the CoValue.
9
24
  */
10
- export type CoFieldInit<T extends AnyZodOrCoValueSchema> =
11
- T extends z.core.$ZodNullable
12
- ? InstanceOrPrimitiveOfSchemaCoValuesNullable<T>
13
- : NotNull<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>>;
25
+ export type CoFieldInit<S extends CoValueClass | AnyZodOrCoValueSchema> =
26
+ S extends CoreCoValueSchema
27
+ ?
28
+ | Loaded<S>
29
+ | (S extends CoreCoRecordSchema<infer K, infer V>
30
+ ? CoMapSchemaInit<{ [key in z.output<K> & string]: V }>
31
+ : S extends CoreCoMapSchema<infer Shape>
32
+ ? CoMapSchemaInit<Shape>
33
+ : S extends CoreCoListSchema<infer T>
34
+ ? CoListInit<T>
35
+ : S extends CoreCoFeedSchema<infer T>
36
+ ? CoFeedInit<T>
37
+ : S extends CorePlainTextSchema | CoreRichTextSchema
38
+ ? string
39
+ : S extends CoreCoOptionalSchema<infer T>
40
+ ? CoFieldInit<T> | undefined
41
+ : S extends CoDiscriminatedUnionSchema<infer Members>
42
+ ? CoFieldInit<Members[number]>
43
+ : never)
44
+ : S extends z.core.$ZodType
45
+ ? TypeOfZodSchema<S>
46
+ : S extends CoValueClass
47
+ ? InstanceType<S>
48
+ : never;
49
+
50
+ // Due to a TS limitation with types that contain known properties and
51
+ // an index signature, we cannot accept catchall properties on creation
52
+ export type CoMapSchemaInit<Shape extends z.core.$ZodLooseShape> = Simplify<
53
+ {
54
+ /**
55
+ * Cannot use {@link PartialOnUndefined} because evaluating CoFieldInit<Shape[Key]>
56
+ * to know if the value can be undefined does not work with recursive types.
57
+ */
58
+ [Key in keyof Shape as Shape[Key] extends
59
+ | CoreCoOptionalSchema
60
+ | z.core.$ZodOptional
61
+ ? never
62
+ : Key]: CoFieldInit<Shape[Key]>;
63
+ } & {
64
+ [Key in keyof Shape as Shape[Key] extends
65
+ | CoreCoOptionalSchema
66
+ | z.core.$ZodOptional
67
+ ? Key
68
+ : never]?: CoFieldInit<Shape[Key]>;
69
+ }
70
+ >;
71
+
72
+ export type CoListInit<T extends AnyZodOrCoValueSchema> = Simplify<
73
+ ReadonlyArray<CoFieldInit<T>>
74
+ >;
75
+
76
+ export type CoFeedInit<T extends AnyZodOrCoValueSchema> = Simplify<
77
+ ReadonlyArray<CoFieldInit<T>>
78
+ >;
@@ -27,15 +27,20 @@ export type InstanceOfSchema<S extends CoValueClass | AnyZodOrCoValueSchema> =
27
27
  S extends CoreCoValueSchema
28
28
  ? S extends CoreAccountSchema<infer Shape>
29
29
  ? {
30
- [key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;
30
+ -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
31
+ Shape[key]
32
+ >;
31
33
  } & Account
32
34
  : S extends CoreCoRecordSchema<infer K, infer V>
33
35
  ? {
34
- [key in z.output<K> & string]: InstanceOrPrimitiveOfSchema<V>;
36
+ -readonly [key in z.output<K> &
37
+ string]: InstanceOrPrimitiveOfSchema<V>;
35
38
  } & CoMap
36
39
  : S extends CoreCoMapSchema<infer Shape, infer CatchAll>
37
40
  ? {
38
- [key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;
41
+ -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
42
+ Shape[key]
43
+ >;
39
44
  } & (CatchAll extends AnyZodOrCoValueSchema
40
45
  ? {
41
46
  [key: string]: InstanceOrPrimitiveOfSchema<CatchAll>;
@@ -53,7 +58,7 @@ export type InstanceOfSchema<S extends CoValueClass | AnyZodOrCoValueSchema> =
53
58
  : S extends CoreFileStreamSchema
54
59
  ? FileStream
55
60
  : S extends CoreCoOptionalSchema<infer T>
56
- ? InstanceOrPrimitiveOfSchema<T>
61
+ ? InstanceOrPrimitiveOfSchema<T> | undefined
57
62
  : S extends CoDiscriminatedUnionSchema<infer Members>
58
63
  ? InstanceOrPrimitiveOfSchema<Members[number]>
59
64
  : never
@@ -29,7 +29,7 @@ export type InstanceOfSchemaCoValuesNullable<
29
29
  ? S extends CoreAccountSchema<infer Shape>
30
30
  ?
31
31
  | ({
32
- [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
32
+ -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
33
33
  Shape[key]
34
34
  >;
35
35
  } & Account)
@@ -37,14 +37,14 @@ export type InstanceOfSchemaCoValuesNullable<
37
37
  : S extends CoreCoRecordSchema<infer K, infer V>
38
38
  ?
39
39
  | ({
40
- [key in z.output<K> &
40
+ -readonly [key in z.output<K> &
41
41
  string]: InstanceOrPrimitiveOfSchemaCoValuesNullable<V>;
42
42
  } & CoMap)
43
43
  | null
44
44
  : S extends CoreCoMapSchema<infer Shape, infer CatchAll>
45
45
  ?
46
46
  | ({
47
- [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
47
+ -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
48
48
  Shape[key]
49
49
  >;
50
50
  } & (CatchAll extends AnyZodOrCoValueSchema
@@ -1,122 +1,11 @@
1
- import { JsonValue } from "cojson";
2
1
  import {
3
- Account,
4
2
  AnyZodOrCoValueSchema,
5
- CoDiscriminatedUnionSchema,
6
- CoFeed,
7
- CoList,
8
- CoMap,
9
- CoPlainText,
10
- CoRichText,
11
3
  CoValueClass,
12
- CoreAccountSchema,
13
- CoreCoRecordSchema,
14
- FileStream,
15
- Profile,
4
+ InstanceOfSchema,
16
5
  } from "../../../internal.js";
17
- import { CoreCoFeedSchema } from "../schemaTypes/CoFeedSchema.js";
18
- import { CoreCoListSchema } from "../schemaTypes/CoListSchema.js";
19
- import { CoreCoMapSchema } from "../schemaTypes/CoMapSchema.js";
20
- import { CoreCoOptionalSchema } from "../schemaTypes/CoOptionalSchema.js";
21
- import { CoreCoValueSchema } from "../schemaTypes/CoValueSchema.js";
22
- import { CoreFileStreamSchema } from "../schemaTypes/FileStreamSchema.js";
23
- import { CorePlainTextSchema } from "../schemaTypes/PlainTextSchema.js";
24
- import { CoreRichTextSchema } from "../schemaTypes/RichTextSchema.js";
25
6
  import { z } from "../zodReExport.js";
7
+ import { TypeOfZodSchema } from "./TypeOfZodSchema.js";
26
8
 
27
9
  export type InstanceOrPrimitiveOfSchema<
28
10
  S extends CoValueClass | AnyZodOrCoValueSchema,
29
- > = S extends CoreCoValueSchema
30
- ? S extends CoreAccountSchema<infer Shape>
31
- ? {
32
- -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;
33
- } & { profile: Profile } & Account
34
- : S extends CoreCoRecordSchema<infer K, infer V>
35
- ? {
36
- -readonly [key in z.output<K> &
37
- string]: InstanceOrPrimitiveOfSchema<V>;
38
- } & CoMap
39
- : S extends CoreCoMapSchema<infer Shape, infer CatchAll>
40
- ? {
41
- -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
42
- Shape[key]
43
- >;
44
- } & (CatchAll extends AnyZodOrCoValueSchema
45
- ? {
46
- [key: string]: InstanceOrPrimitiveOfSchema<CatchAll>;
47
- }
48
- : {}) &
49
- CoMap
50
- : S extends CoreCoListSchema<infer T>
51
- ? CoList<InstanceOrPrimitiveOfSchema<T>>
52
- : S extends CoreCoFeedSchema<infer T>
53
- ? CoFeed<InstanceOrPrimitiveOfSchema<T>>
54
- : S extends CorePlainTextSchema
55
- ? CoPlainText
56
- : S extends CoreRichTextSchema
57
- ? CoRichText
58
- : S extends CoreFileStreamSchema
59
- ? FileStream
60
- : S extends CoreCoOptionalSchema<infer T>
61
- ? InstanceOrPrimitiveOfSchema<T> | undefined
62
- : S extends CoDiscriminatedUnionSchema<infer Members>
63
- ? InstanceOrPrimitiveOfSchema<Members[number]>
64
- : never
65
- : S extends z.core.$ZodType
66
- ? S extends z.core.$ZodOptional<infer Inner extends z.core.$ZodType>
67
- ? InstanceOrPrimitiveOfSchema<Inner> | undefined
68
- : S extends z.core.$ZodNullable<infer Inner extends z.core.$ZodType>
69
- ? InstanceOrPrimitiveOfSchema<Inner> | null
70
- : S extends z.ZodJSONSchema
71
- ? JsonValue
72
- : S extends z.core.$ZodUnion<infer Members extends z.core.$ZodType[]>
73
- ? InstanceOrPrimitiveOfSchema<Members[number]>
74
- : // primitives below here - we manually traverse to ensure we only allow what we can handle
75
- S extends z.core.$ZodObject<infer Shape>
76
- ? {
77
- -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
78
- Shape[key]
79
- >;
80
- }
81
- : S extends z.core.$ZodArray<infer Item extends z.core.$ZodType>
82
- ? InstanceOrPrimitiveOfSchema<Item>[]
83
- : S extends z.core.$ZodTuple<
84
- infer Items extends readonly z.core.$ZodType[]
85
- >
86
- ? {
87
- [key in keyof Items]: InstanceOrPrimitiveOfSchema<
88
- Items[key]
89
- >;
90
- }
91
- : S extends z.core.$ZodString
92
- ? string
93
- : S extends z.core.$ZodNumber
94
- ? number
95
- : S extends z.core.$ZodBoolean
96
- ? boolean
97
- : S extends z.core.$ZodLiteral<infer Literal>
98
- ? Literal
99
- : S extends z.core.$ZodDate
100
- ? Date
101
- : S extends z.core.$ZodEnum<infer Enum>
102
- ? Enum[keyof Enum]
103
- : S extends z.core.$ZodTemplateLiteral<
104
- infer pattern
105
- >
106
- ? pattern
107
- : S extends z.core.$ZodReadonly<
108
- infer Inner extends z.core.$ZodType
109
- >
110
- ? InstanceOrPrimitiveOfSchema<Inner>
111
- : S extends z.core.$ZodDefault<
112
- infer Default extends z.core.$ZodType
113
- >
114
- ? InstanceOrPrimitiveOfSchema<Default>
115
- : S extends z.core.$ZodCatch<
116
- infer Catch extends z.core.$ZodType
117
- >
118
- ? InstanceOrPrimitiveOfSchema<Catch>
119
- : never
120
- : S extends CoValueClass
121
- ? InstanceType<S>
122
- : never;
11
+ > = S extends z.core.$ZodType ? TypeOfZodSchema<S> : InstanceOfSchema<S>;
@@ -1,137 +1,13 @@
1
- import { JsonValue } from "cojson";
2
1
  import {
3
- Account,
4
2
  AnyZodOrCoValueSchema,
5
- CoDiscriminatedUnionSchema,
6
- CoFeed,
7
- CoList,
8
- CoMap,
9
- CoPlainText,
10
- CoRichText,
11
3
  CoValueClass,
12
- CoreAccountSchema,
13
- CoreCoRecordSchema,
14
- FileStream,
15
- InstanceOrPrimitiveOfSchema,
16
- Profile,
4
+ InstanceOfSchemaCoValuesNullable,
17
5
  } from "../../../internal.js";
18
- import { CoreCoFeedSchema } from "../schemaTypes/CoFeedSchema.js";
19
- import { CoreCoListSchema } from "../schemaTypes/CoListSchema.js";
20
- import { CoreCoMapSchema } from "../schemaTypes/CoMapSchema.js";
21
- import { CoreCoOptionalSchema } from "../schemaTypes/CoOptionalSchema.js";
22
- import { CoreCoValueSchema } from "../schemaTypes/CoValueSchema.js";
23
- import { CoreFileStreamSchema } from "../schemaTypes/FileStreamSchema.js";
24
- import { CorePlainTextSchema } from "../schemaTypes/PlainTextSchema.js";
25
- import { CoreRichTextSchema } from "../schemaTypes/RichTextSchema.js";
26
6
  import { z } from "../zodReExport.js";
7
+ import { TypeOfZodSchema } from "./TypeOfZodSchema.js";
27
8
 
28
9
  export type InstanceOrPrimitiveOfSchemaCoValuesNullable<
29
10
  S extends CoValueClass | AnyZodOrCoValueSchema,
30
- > = S extends CoreCoValueSchema
31
- ? S extends CoreAccountSchema<infer Shape>
32
- ?
33
- | ({
34
- -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
35
- Shape[key]
36
- >;
37
- } & { profile: Profile | null } & Account)
38
- | null
39
- : S extends CoreCoRecordSchema<infer K, infer V>
40
- ?
41
- | ({
42
- -readonly [key in z.output<K> &
43
- string]: InstanceOrPrimitiveOfSchemaCoValuesNullable<V>;
44
- } & CoMap)
45
- | null
46
- : S extends CoreCoMapSchema<infer Shape, infer CatchAll>
47
- ?
48
- | ({
49
- -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
50
- Shape[key]
51
- >;
52
- } & (CatchAll extends AnyZodOrCoValueSchema
53
- ? {
54
- [
55
- key: string
56
- ]: InstanceOrPrimitiveOfSchemaCoValuesNullable<CatchAll>;
57
- }
58
- : {}) &
59
- CoMap)
60
- | null
61
- : S extends CoreCoListSchema<infer T>
62
- ? CoList<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>> | null
63
- : S extends CoreCoFeedSchema<infer T>
64
- ? CoFeed<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>> | null
65
- : S extends CorePlainTextSchema
66
- ? CoPlainText | null
67
- : S extends CoreRichTextSchema
68
- ? CoRichText | null
69
- : S extends CoreFileStreamSchema
70
- ? FileStream | null
71
- : S extends CoreCoOptionalSchema<infer T>
72
- ? InstanceOrPrimitiveOfSchemaCoValuesNullable<T> | undefined
73
- : S extends CoDiscriminatedUnionSchema<infer Members>
74
- ? InstanceOrPrimitiveOfSchemaCoValuesNullable<
75
- Members[number]
76
- >
77
- : never
78
- : S extends z.core.$ZodType
79
- ? S extends z.core.$ZodOptional<infer Inner extends z.core.$ZodType>
80
- ? InstanceOrPrimitiveOfSchemaCoValuesNullable<Inner> | undefined
81
- : S extends z.core.$ZodNullable<infer Inner extends z.core.$ZodType>
82
- ? InstanceOrPrimitiveOfSchemaCoValuesNullable<Inner> | null
83
- : S extends z.ZodJSONSchema
84
- ? JsonValue
85
- : S extends z.core.$ZodUnion<
86
- infer Members extends readonly z.core.$ZodType[]
87
- >
88
- ? InstanceOrPrimitiveOfSchemaCoValuesNullable<Members[number]>
89
- : // primitives below here - we manually traverse to ensure we only allow what we can handle
90
- S extends z.core.$ZodObject<infer Shape>
91
- ? {
92
- -readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
93
- Shape[key]
94
- >;
95
- }
96
- : S extends z.core.$ZodArray<infer Item extends z.core.$ZodType>
97
- ? InstanceOrPrimitiveOfSchema<Item>[]
98
- : S extends z.core.$ZodTuple<
99
- infer Items extends z.core.$ZodType[]
100
- >
101
- ? {
102
- [key in keyof Items]: InstanceOrPrimitiveOfSchema<
103
- Items[key]
104
- >;
105
- }
106
- : S extends z.core.$ZodString
107
- ? string
108
- : S extends z.core.$ZodNumber
109
- ? number
110
- : S extends z.core.$ZodBoolean
111
- ? boolean
112
- : S extends z.core.$ZodLiteral<infer Literal>
113
- ? Literal
114
- : S extends z.core.$ZodDate
115
- ? Date
116
- : S extends z.core.$ZodEnum<infer Enum>
117
- ? Enum[keyof Enum]
118
- : S extends z.core.$ZodTemplateLiteral<
119
- infer pattern
120
- >
121
- ? pattern
122
- : S extends z.core.$ZodReadonly<
123
- infer Inner extends z.core.$ZodType
124
- >
125
- ? InstanceOrPrimitiveOfSchema<Inner>
126
- : S extends z.core.$ZodDefault<
127
- infer Default extends z.core.$ZodType
128
- >
129
- ? InstanceOrPrimitiveOfSchema<Default>
130
- : S extends z.core.$ZodCatch<
131
- infer Catch extends z.core.$ZodType
132
- >
133
- ? InstanceOrPrimitiveOfSchema<Catch>
134
- : never
135
- : S extends CoValueClass
136
- ? InstanceType<S> | null
137
- : never;
11
+ > = S extends z.core.$ZodType
12
+ ? TypeOfZodSchema<S>
13
+ : InstanceOfSchemaCoValuesNullable<S>;
@@ -0,0 +1,79 @@
1
+ import { JsonValue } from "cojson";
2
+ import { PartialOnUndefined } from "../../../internal.js";
3
+ import { z } from "../zodReExport.js";
4
+
5
+ // Copied from https://github.com/colinhacks/zod/blob/7e7e3461aceecf3633e158df50d6bc852e7cdf45/packages/zod/src/v4/core/schemas.ts#L1591,
6
+ // since this type is not exported by Zod
7
+ type OptionalInSchema = {
8
+ _zod: {
9
+ optin: "optional";
10
+ };
11
+ };
12
+
13
+ /**
14
+ * Get type from Zod schema definition.
15
+ *
16
+ * Similar to `z.infer`, but we manually traverse Zod types to ensure we only allow what we can handle
17
+ */
18
+ export type TypeOfZodSchema<S extends z.core.$ZodType> =
19
+ S extends z.core.$ZodOptional<infer Inner extends z.core.$ZodType>
20
+ ? TypeOfZodSchema<Inner> | undefined
21
+ : S extends z.core.$ZodNullable<infer Inner extends z.core.$ZodType>
22
+ ? TypeOfZodSchema<Inner> | null
23
+ : S extends z.ZodJSONSchema
24
+ ? JsonValue
25
+ : S extends z.core.$ZodUnion<
26
+ infer Members extends readonly z.core.$ZodType[]
27
+ >
28
+ ? TypeOfZodSchema<Members[number]>
29
+ : S extends z.core.$ZodObject<infer Shape>
30
+ ? /**
31
+ * Cannot use {@link PartialOnUndefined} because evaluating TypeOfZodSchema<Shape[key]>
32
+ * to know if the value can be undefined does not work with recursive types.
33
+ */
34
+ {
35
+ -readonly [key in keyof Shape as Shape[key] extends OptionalInSchema
36
+ ? never
37
+ : key]: TypeOfZodSchema<Shape[key]>;
38
+ } & {
39
+ -readonly [key in keyof Shape as Shape[key] extends OptionalInSchema
40
+ ? key
41
+ : never]?: TypeOfZodSchema<Shape[key]>;
42
+ }
43
+ : S extends z.core.$ZodArray<infer Item extends z.core.$ZodType>
44
+ ? TypeOfZodSchema<Item>[]
45
+ : S extends z.core.$ZodTuple<
46
+ infer Items extends readonly z.core.$ZodType[]
47
+ >
48
+ ? {
49
+ [key in keyof Items]: TypeOfZodSchema<Items[key]>;
50
+ }
51
+ : S extends z.core.$ZodString
52
+ ? string
53
+ : S extends z.core.$ZodNumber
54
+ ? number
55
+ : S extends z.core.$ZodBoolean
56
+ ? boolean
57
+ : S extends z.core.$ZodLiteral<infer Literal>
58
+ ? Literal
59
+ : S extends z.core.$ZodDate
60
+ ? Date
61
+ : S extends z.core.$ZodEnum<infer Enum>
62
+ ? Enum[keyof Enum]
63
+ : S extends z.core.$ZodTemplateLiteral<
64
+ infer pattern
65
+ >
66
+ ? pattern
67
+ : S extends z.core.$ZodReadonly<
68
+ infer Inner extends z.core.$ZodType
69
+ >
70
+ ? TypeOfZodSchema<Inner>
71
+ : S extends z.core.$ZodDefault<
72
+ infer Default extends z.core.$ZodType
73
+ >
74
+ ? TypeOfZodSchema<Default>
75
+ : S extends z.core.$ZodCatch<
76
+ infer Catch extends z.core.$ZodType
77
+ >
78
+ ? TypeOfZodSchema<Catch>
79
+ : never;
@@ -7,6 +7,7 @@ import {
7
7
  CoreCoMapSchema,
8
8
  DiscriminableCoValueSchemas,
9
9
  DiscriminableCoreCoValueSchema,
10
+ SchemaUnionDiscriminator,
10
11
  } from "../../internal.js";
11
12
  import {
12
13
  hydrateCoreCoValueSchema,
@@ -56,21 +57,17 @@ export function schemaUnionDiscriminatorFor(
56
57
  }
57
58
  }
58
59
 
59
- const determineSchema = (_raw: RawCoMap | RawAccount | RawCoList) => {
60
- if (_raw instanceof RawCoList) {
61
- throw new Error(
62
- "co.discriminatedUnion() of collaborative types is not supported for CoLists",
63
- );
64
- }
65
-
60
+ const determineSchema: SchemaUnionDiscriminator<CoMap> = (
61
+ discriminable,
62
+ ) => {
66
63
  for (const option of availableOptions) {
67
64
  let match = true;
68
65
 
69
66
  for (const key of Object.keys(discriminatorMap)) {
70
67
  const discriminatorDef = (option as CoreCoMapSchema).getDefinition()
71
- .shape[key as string];
68
+ .shape[key];
72
69
 
73
- const discriminatorValue = (_raw as RawCoMap).get(key as string);
70
+ const discriminatorValue = discriminable.get(key);
74
71
 
75
72
  if (discriminatorValue && typeof discriminatorValue === "object") {
76
73
  throw new Error("Discriminator must be a primitive value");