jazz-tools 0.15.10 → 0.15.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. package/.turbo/turbo-build.log +42 -42
  2. package/CHANGELOG.md +10 -0
  3. package/dist/{chunk-V54IPL5M.js → chunk-MLLG3GBR.js} +421 -302
  4. package/dist/chunk-MLLG3GBR.js.map +1 -0
  5. package/dist/index.js +1 -1
  6. package/dist/testing.js +1 -1
  7. package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
  8. package/dist/tools/coValues/coMap.d.ts.map +1 -1
  9. package/dist/tools/coValues/inbox.d.ts.map +1 -1
  10. package/dist/tools/coValues/schemaUnion.d.ts +27 -1
  11. package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
  12. package/dist/tools/implementation/zodSchema/coExport.d.ts +1 -1
  13. package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
  14. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +6 -3
  15. package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +1 -1
  16. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +6 -25
  17. package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +1 -1
  18. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +2 -1
  19. package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
  20. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +30 -0
  21. package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -0
  22. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +2 -1
  23. package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
  24. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +3 -3
  25. package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
  26. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +3 -3
  27. package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  28. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts +11 -0
  29. package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts.map +1 -0
  30. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +2 -3
  31. package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
  32. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +5 -2
  33. package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
  34. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +5 -2
  35. package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
  36. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +5 -2
  37. package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
  38. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +7 -12
  39. package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
  40. package/dist/tools/implementation/zodSchema/unionUtils.d.ts +2 -3
  41. package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
  42. package/dist/tools/implementation/zodSchema/zodCo.d.ts +6 -6
  43. package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
  44. package/dist/tools/implementation/zodSchema/zodReExport.d.ts +8 -1
  45. package/dist/tools/implementation/zodSchema/zodReExport.d.ts.map +1 -1
  46. package/dist/tools/implementation/zodSchema/zodSchema.d.ts +18 -13
  47. package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  48. package/dist/tools/internal.d.ts +1 -0
  49. package/dist/tools/internal.d.ts.map +1 -1
  50. package/dist/tools/tests/coDiscriminatedUnion.test-d.d.ts +2 -0
  51. package/dist/tools/tests/coDiscriminatedUnion.test-d.d.ts.map +1 -0
  52. package/dist/tools/tests/coDiscriminatedUnion.test.d.ts +2 -0
  53. package/dist/tools/tests/coDiscriminatedUnion.test.d.ts.map +1 -0
  54. package/dist/tools/tests/coOptional.test.d.ts +2 -0
  55. package/dist/tools/tests/coOptional.test.d.ts.map +1 -0
  56. package/dist/tools/tests/zod.test-d.d.ts +2 -0
  57. package/dist/tools/tests/zod.test-d.d.ts.map +1 -0
  58. package/package.json +5 -4
  59. package/src/react-core/tests/useAccount.test.ts +1 -11
  60. package/src/tools/coValues/CoValueBase.ts +2 -2
  61. package/src/tools/coValues/inbox.ts +0 -2
  62. package/src/tools/coValues/schemaUnion.ts +62 -0
  63. package/src/tools/implementation/zodSchema/coExport.ts +2 -0
  64. package/src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +29 -12
  65. package/src/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +93 -63
  66. package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +62 -3
  67. package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +90 -0
  68. package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +27 -2
  69. package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +27 -5
  70. package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +60 -5
  71. package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +33 -0
  72. package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +3 -5
  73. package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +32 -2
  74. package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +30 -2
  75. package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +26 -2
  76. package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +10 -13
  77. package/src/tools/implementation/zodSchema/unionUtils.ts +22 -14
  78. package/src/tools/implementation/zodSchema/zodCo.ts +68 -256
  79. package/src/tools/implementation/zodSchema/zodReExport.ts +34 -5
  80. package/src/tools/implementation/zodSchema/zodSchema.ts +78 -19
  81. package/src/tools/internal.ts +1 -0
  82. package/src/tools/tests/ContextManager.test.ts +5 -6
  83. package/src/tools/tests/coDiscriminatedUnion.test-d.ts +57 -0
  84. package/src/tools/tests/coDiscriminatedUnion.test.ts +93 -0
  85. package/src/tools/tests/coList.test.ts +4 -12
  86. package/src/tools/tests/coMap.record.test.ts +4 -4
  87. package/src/tools/tests/coMap.test-d.ts +3 -3
  88. package/src/tools/tests/coMap.test.ts +10 -10
  89. package/src/tools/tests/coOptional.test.ts +39 -0
  90. package/src/tools/tests/createContext.test.ts +5 -4
  91. package/src/tools/tests/deepLoading.test.ts +3 -3
  92. package/src/tools/tests/inbox.test.ts +3 -11
  93. package/src/tools/tests/patterns/requestToJoin.test.ts +1 -1
  94. package/src/tools/tests/schemaUnion.test.ts +11 -22
  95. package/src/tools/tests/subscribe.test.ts +25 -39
  96. package/src/tools/tests/zod.test-d.ts +70 -0
  97. package/src/tools/tests/zod.test.ts +64 -5
  98. package/dist/chunk-V54IPL5M.js.map +0 -1
@@ -1,13 +1,19 @@
1
+ import {
2
+ ZodArray,
3
+ ZodTuple,
4
+ ZodUnion,
5
+ core,
6
+ array as zodArray,
7
+ tuple as zodTuple,
8
+ union as zodUnion,
9
+ } from "zod/v4";
1
10
  export {
2
11
  string,
3
12
  number,
4
13
  boolean,
5
- union,
6
14
  object,
7
- array,
8
15
  templateLiteral,
9
16
  json,
10
- tuple,
11
17
  date,
12
18
  emoji,
13
19
  base64,
@@ -29,8 +35,8 @@ export {
29
35
  int32,
30
36
  strictObject,
31
37
  discriminatedUnion,
32
- // intersection,
33
- // record,
38
+ // record,
39
+ // intersection,
34
40
  int,
35
41
  optional,
36
42
  type ZodOptional,
@@ -42,3 +48,26 @@ export {
42
48
  type ZodDiscriminatedUnion,
43
49
  z,
44
50
  } from "zod/v4";
51
+
52
+ type NonCoZodType = core.$ZodType & { collaborative?: false };
53
+
54
+ export function union<const T extends readonly NonCoZodType[]>(
55
+ options: T,
56
+ params?: string | core.$ZodUnionParams,
57
+ ): ZodUnion<T> {
58
+ return zodUnion(options, params);
59
+ }
60
+
61
+ export function array<T extends NonCoZodType>(
62
+ element: T,
63
+ params?: string | core.$ZodArrayParams,
64
+ ): ZodArray<T> {
65
+ return zodArray(element, params);
66
+ }
67
+
68
+ export function tuple<T extends readonly [NonCoZodType, ...NonCoZodType[]]>(
69
+ options: T,
70
+ params?: string | core.$ZodTupleParams,
71
+ ): ZodTuple<T> {
72
+ return zodTuple(options, params);
73
+ }
@@ -2,23 +2,47 @@ import { LocalNode, RawAccount } from "cojson";
2
2
  import {
3
3
  Account,
4
4
  AccountClass,
5
- CoValue,
6
5
  CoValueClass,
7
6
  CoValueFromRaw,
7
+ InstanceOfSchema,
8
8
  RefsToResolve,
9
9
  RefsToResolveStrict,
10
10
  Resolved,
11
11
  Simplify,
12
12
  } from "../../internal.js";
13
- import { AnyAccountSchema } from "./schemaTypes/AccountSchema.js";
14
- import { AnyCoFeedSchema } from "./schemaTypes/CoFeedSchema.js";
15
- import { AnyCoListSchema } from "./schemaTypes/CoListSchema.js";
16
- import { AnyCoMapSchema, CoMapInitZod } from "./schemaTypes/CoMapSchema.js";
17
- import { AnyCoRecordSchema } from "./schemaTypes/CoRecordSchema.js";
18
- import { FileStreamSchema } from "./schemaTypes/FileStreamSchema.js";
19
- import { PlainTextSchema } from "./schemaTypes/PlainTextSchema.js";
20
- import { RichTextSchema } from "./schemaTypes/RichTextSchema.js";
21
- import { InstanceOfSchema } from "./typeConverters/InstanceOfSchema.js";
13
+ import {
14
+ AccountSchema,
15
+ AnyAccountSchema,
16
+ BaseAccountShape,
17
+ } from "./schemaTypes/AccountSchema.js";
18
+ import {
19
+ AnyDiscriminableCoSchema,
20
+ CoDiscriminatedUnionSchema,
21
+ } from "./schemaTypes/CoDiscriminatedUnionSchema.js";
22
+ import { AnyCoFeedSchema, CoFeedSchema } from "./schemaTypes/CoFeedSchema.js";
23
+ import { AnyCoListSchema, CoListSchema } from "./schemaTypes/CoListSchema.js";
24
+ import {
25
+ AnyCoMapSchema,
26
+ CoMapInitZod,
27
+ CoMapSchema,
28
+ } from "./schemaTypes/CoMapSchema.js";
29
+ import { AnyCoOptionalSchema } from "./schemaTypes/CoOptionalSchema.js";
30
+ import {
31
+ AnyCoRecordSchema,
32
+ CoRecordSchema,
33
+ } from "./schemaTypes/CoRecordSchema.js";
34
+ import {
35
+ AnyFileStreamSchema,
36
+ FileStreamSchema,
37
+ } from "./schemaTypes/FileStreamSchema.js";
38
+ import {
39
+ AnyPlainTextSchema,
40
+ PlainTextSchema,
41
+ } from "./schemaTypes/PlainTextSchema.js";
42
+ import {
43
+ AnyRichTextSchema,
44
+ RichTextSchema,
45
+ } from "./schemaTypes/RichTextSchema.js";
22
46
  import { InstanceOfSchemaCoValuesNullable } from "./typeConverters/InstanceOfSchemaCoValuesNullable.js";
23
47
  import { z } from "./zodReExport.js";
24
48
 
@@ -47,7 +71,7 @@ export type AnyCoUnionSchema = z.core.$ZodDiscriminatedUnion<
47
71
  // this is a series of hacks to work around z4 removing _zod at runtime from z.core.$ZodType
48
72
  export function isZodObject(
49
73
  schema: z.core.$ZodType,
50
- ): schema is z.core.$ZodObject<any, any> {
74
+ ): schema is z.ZodObject<any, any> {
51
75
  return (schema as any).def?.type === "object";
52
76
  }
53
77
 
@@ -67,19 +91,53 @@ export function getDef<S extends z.core.$ZodType>(schema: S): S["_zod"]["def"] {
67
91
  return (schema as any).def;
68
92
  }
69
93
 
94
+ // TODO rename. This represents a CoValue class or a CoValue schema
70
95
  export type CoValueOrZodSchema = CoValueClass | AnyCoSchema;
71
96
 
72
- export type CoValueClassFromZodSchema<S extends z.core.$ZodType> = CoValueClass<
73
- InstanceOfSchema<S>
74
- > &
75
- CoValueFromRaw<InstanceOfSchema<S>> &
76
- (S extends AnyAccountSchema ? AccountClassEssentials : {});
97
+ // TODO rename to CoValueSchemaFromCoProtoSchema
98
+ export type CoValueSchemaFromZodSchema<S extends z.core.$ZodType> =
99
+ S extends z.core.$ZodType
100
+ ? S extends AnyAccountSchema<infer Shape extends BaseAccountShape>
101
+ ? AccountSchema<Shape>
102
+ : S extends AnyCoRecordSchema<infer K, infer V>
103
+ ? CoRecordSchema<K, V>
104
+ : S extends AnyCoMapSchema<infer Shape, infer Config>
105
+ ? CoMapSchema<Shape, Config>
106
+ : S extends AnyCoListSchema<infer T>
107
+ ? CoListSchema<T>
108
+ : S extends AnyCoFeedSchema<infer T>
109
+ ? CoFeedSchema<T>
110
+ : S extends AnyPlainTextSchema
111
+ ? PlainTextSchema
112
+ : S extends AnyRichTextSchema
113
+ ? RichTextSchema
114
+ : S extends AnyFileStreamSchema
115
+ ? FileStreamSchema
116
+ : S extends z.core.$ZodOptional<infer Inner>
117
+ ? CoValueSchemaFromZodSchema<Inner>
118
+ : S extends z.core.$ZodUnion<
119
+ infer Members extends readonly [
120
+ AnyDiscriminableCoSchema,
121
+ ...AnyDiscriminableCoSchema[],
122
+ ]
123
+ >
124
+ ? CoDiscriminatedUnionSchema<Members>
125
+ : never
126
+ : never;
127
+
128
+ export type CoValueClassFromAnySchema<S extends CoValueOrZodSchema> =
129
+ S extends CoValueClass<any>
130
+ ? S
131
+ : CoValueClass<InstanceOfSchema<S>> &
132
+ CoValueFromRaw<InstanceOfSchema<S>> &
133
+ (S extends AnyAccountSchema ? AccountClassEssentials : {});
77
134
 
78
135
  type AccountClassEssentials = {
79
136
  fromRaw: <A extends Account>(this: AccountClass<A>, raw: RawAccount) => A;
80
137
  fromNode: <A extends Account>(this: AccountClass<A>, node: LocalNode) => A;
81
138
  };
82
139
 
140
+ // TODO rename to ProtoCoSchema?
83
141
  export type AnyCoSchema =
84
142
  | AnyCoMapSchema
85
143
  | AnyAccountSchema
@@ -87,9 +145,10 @@ export type AnyCoSchema =
87
145
  | AnyCoListSchema
88
146
  | AnyCoFeedSchema
89
147
  | AnyCoUnionSchema
90
- | PlainTextSchema
91
- | RichTextSchema
92
- | FileStreamSchema;
148
+ | AnyCoOptionalSchema
149
+ | AnyPlainTextSchema
150
+ | AnyRichTextSchema
151
+ | AnyFileStreamSchema;
93
152
 
94
153
  export type Loaded<
95
154
  T extends CoValueClass | AnyCoSchema,
@@ -33,6 +33,7 @@ export * from "./implementation/zodSchema/zodSchema.js";
33
33
  export * from "./implementation/zodSchema/zodCo.js";
34
34
  export * as co from "./implementation/zodSchema/coExport.js";
35
35
  export * from "./implementation/zodSchema/schemaTypes/CoMapSchema.js";
36
+ export * from "./implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.js";
36
37
  export * from "./implementation/zodSchema/schemaTypes/CoRecordSchema.js";
37
38
  export * from "./implementation/zodSchema/schemaTypes/CoListSchema.js";
38
39
  export * from "./implementation/zodSchema/schemaTypes/CoFeedSchema.js";
@@ -4,13 +4,11 @@ import {
4
4
  Account,
5
5
  AccountClass,
6
6
  AuthSecretStorage,
7
- CoMap,
8
7
  Group,
9
8
  InMemoryKVStore,
10
9
  JazzAuthContext,
11
10
  KvStoreContext,
12
11
  co,
13
- coField,
14
12
  z,
15
13
  } from "../exports";
16
14
  import {
@@ -26,6 +24,7 @@ import {
26
24
  CoValueFromRaw,
27
25
  InstanceOfSchema,
28
26
  Loaded,
27
+ anySchemaToCoSchema,
29
28
  zodSchemaToCoSchema,
30
29
  } from "../internal";
31
30
  import {
@@ -258,7 +257,7 @@ describe("ContextManager", () => {
258
257
 
259
258
  // Create initial anonymous context
260
259
  await customManager.createContext({
261
- AccountSchema: zodSchemaToCoSchema(CustomAccount),
260
+ AccountSchema: anySchemaToCoSchema(CustomAccount),
262
261
  });
263
262
 
264
263
  const account = (
@@ -313,7 +312,7 @@ describe("ContextManager", () => {
313
312
 
314
313
  // Create initial anonymous context
315
314
  await customManager.createContext({
316
- AccountSchema: zodSchemaToCoSchema(CustomAccount),
315
+ AccountSchema: anySchemaToCoSchema(CustomAccount),
317
316
  });
318
317
 
319
318
  const account = (
@@ -339,7 +338,7 @@ describe("ContextManager", () => {
339
338
  const AccountRoot = co.map({
340
339
  value: z.string(),
341
340
  get transferredRoot(): z.ZodOptional<typeof AccountRoot> {
342
- return z.optional(AccountRoot);
341
+ return co.optional(AccountRoot);
343
342
  },
344
343
  });
345
344
 
@@ -385,7 +384,7 @@ describe("ContextManager", () => {
385
384
  // Create initial anonymous context
386
385
  await customManager.createContext({
387
386
  onAnonymousAccountDiscarded,
388
- AccountSchema: zodSchemaToCoSchema(CustomAccount),
387
+ AccountSchema: anySchemaToCoSchema(CustomAccount),
389
388
  });
390
389
 
391
390
  const account = await createJazzTestAccount({
@@ -0,0 +1,57 @@
1
+ import { beforeEach, describe, test } from "vitest";
2
+ import { Loaded, co, z } from "../exports.js";
3
+ import { createJazzTestAccount, setupJazzTestSync } from "../testing.js";
4
+
5
+ describe("co.discriminatedUnion", () => {
6
+ beforeEach(async () => {
7
+ await setupJazzTestSync();
8
+
9
+ await createJazzTestAccount({
10
+ isCurrentActiveAccount: true,
11
+ creationProps: { name: "Hermes Puggington" },
12
+ });
13
+ });
14
+
15
+ test("can use co.discriminatedUnion with CoValue schemas as values", () => {
16
+ const Dog = co.map({
17
+ type: z.literal("dog"),
18
+ });
19
+ const Cat = co.map({
20
+ type: z.literal("cat"),
21
+ });
22
+ const Person = co.map({
23
+ pet: co.discriminatedUnion("type", [Dog, Cat]),
24
+ });
25
+
26
+ const person = Person.create({
27
+ pet: Dog.create({
28
+ type: "dog",
29
+ }),
30
+ });
31
+
32
+ person.pet = Cat.create({
33
+ type: "cat",
34
+ });
35
+
36
+ type ExpectedType = {
37
+ pet: Loaded<typeof Dog> | Loaded<typeof Cat>;
38
+ };
39
+
40
+ function matches(value: ExpectedType) {
41
+ return value;
42
+ }
43
+
44
+ matches(person);
45
+ });
46
+
47
+ test("cannot use co.discriminatedUnion with zod schemas as values", () => {
48
+ const Person = co.map({
49
+ pet: co.discriminatedUnion("type", [
50
+ // @ts-expect-error: cannot use co.discriminatedUnion with a Zod schema
51
+ z.object({
52
+ type: z.string(),
53
+ }),
54
+ ]),
55
+ });
56
+ });
57
+ });
@@ -0,0 +1,93 @@
1
+ import { beforeEach, describe, expect, test, vi } from "vitest";
2
+ import { Loaded, co, z } from "../exports.js";
3
+ import { createJazzTestAccount, setupJazzTestSync } from "../testing.js";
4
+ import { waitFor } from "./utils.js";
5
+
6
+ describe("co.discriminatedUnion", () => {
7
+ beforeEach(async () => {
8
+ await setupJazzTestSync();
9
+
10
+ await createJazzTestAccount({
11
+ isCurrentActiveAccount: true,
12
+ creationProps: { name: "Hermes Puggington" },
13
+ });
14
+ });
15
+
16
+ test("use co.discriminatedUnion with CoValue schemas as values", () => {
17
+ const Dog = co.map({
18
+ type: z.literal("dog"),
19
+ });
20
+ const Cat = co.map({
21
+ type: z.literal("cat"),
22
+ });
23
+ const Person = co.map({
24
+ pet: co.discriminatedUnion("type", [Dog, Cat]),
25
+ });
26
+
27
+ const person = Person.create({
28
+ pet: Dog.create({
29
+ type: "dog",
30
+ }),
31
+ });
32
+
33
+ expect(person.pet.type).toEqual("dog");
34
+
35
+ person.pet = Cat.create({
36
+ type: "cat",
37
+ });
38
+
39
+ expect(person.pet.type).toEqual("cat");
40
+ });
41
+
42
+ test("load CoValue instances using the DiscriminatedUnion schema", async () => {
43
+ const Dog = co.map({
44
+ type: z.literal("dog"),
45
+ });
46
+ const Cat = co.map({
47
+ type: z.literal("cat"),
48
+ });
49
+ const Pet = co.discriminatedUnion("type", [Dog, Cat]);
50
+
51
+ const dog = Dog.create({ type: "dog" });
52
+ const loadedPet = await Pet.load(dog.id);
53
+ expect(loadedPet?.type).toEqual("dog");
54
+ });
55
+
56
+ test("subscribe to CoValue instances using the DiscriminatedUnion schema", async () => {
57
+ const Person = co.map({
58
+ name: z.string(),
59
+ });
60
+ const Dog = co.map({
61
+ type: z.literal("dog"),
62
+ name: z.string(),
63
+ owner: Person,
64
+ });
65
+ const Cat = co.map({
66
+ type: z.literal("cat"),
67
+ name: z.string(),
68
+ });
69
+ const Pet = co.discriminatedUnion("type", [Dog, Cat]);
70
+
71
+ const dog = Dog.create({
72
+ type: "dog",
73
+ name: "Rex",
74
+ owner: Person.create({ name: "John Doe" }),
75
+ });
76
+
77
+ const updates: Loaded<typeof Pet>[] = [];
78
+ const spy = vi.fn((pet) => updates.push(pet));
79
+
80
+ Pet.subscribe(dog.id, {}, (pet) => {
81
+ expect(pet.type).toEqual("dog");
82
+ spy(pet);
83
+ });
84
+
85
+ expect(spy).not.toHaveBeenCalled();
86
+
87
+ await waitFor(() => expect(spy).toHaveBeenCalled());
88
+
89
+ expect(spy).toHaveBeenCalledTimes(1);
90
+
91
+ expect(updates[0]?.name).toEqual("Rex");
92
+ });
93
+ });
@@ -1,15 +1,7 @@
1
1
  import { WasmCrypto } from "cojson/crypto/WasmCrypto";
2
2
  import { beforeEach, describe, expect, test, vi } from "vitest";
3
- import {
4
- Account,
5
- CoList,
6
- CoMap,
7
- Group,
8
- coField,
9
- subscribeToCoValue,
10
- z,
11
- } from "../index.js";
12
- import { Loaded, co, zodSchemaToCoSchema } from "../internal.js";
3
+ import { Account, Group, subscribeToCoValue, z } from "../index.js";
4
+ import { Loaded, anySchemaToCoSchema, co } from "../internal.js";
13
5
  import { createJazzTestAccount, setupJazzTestSync } from "../testing.js";
14
6
  import { waitFor } from "./utils.js";
15
7
 
@@ -127,7 +119,7 @@ describe("Simple CoList operations", async () => {
127
119
  name: z.string(),
128
120
  });
129
121
 
130
- const Recipe = co.list(z.optional(Ingredient));
122
+ const Recipe = co.list(co.optional(Ingredient));
131
123
 
132
124
  const recipe = Recipe.create(
133
125
  [
@@ -588,7 +580,7 @@ describe("CoList subscription", async () => {
588
580
  const spy = vi.fn((list) => updates.push(list));
589
581
 
590
582
  subscribeToCoValue(
591
- zodSchemaToCoSchema(TestList),
583
+ anySchemaToCoSchema(TestList),
592
584
  list.id,
593
585
  {
594
586
  syncResolution: true,
@@ -281,7 +281,7 @@ describe("CoMap.Record", async () => {
281
281
  });
282
282
 
283
283
  test("Is ok to omit optional fields", () => {
284
- const TestRecord = co.record(z.string(), z.optional(NestedRecord));
284
+ const TestRecord = co.record(z.string(), co.optional(NestedRecord));
285
285
 
286
286
  expectTypeOf<typeof TestRecord.create>().toBeCallableWith(
287
287
  {
@@ -315,7 +315,7 @@ describe("CoMap.Record", async () => {
315
315
 
316
316
  const PersonRecord = co.record(
317
317
  z.string(),
318
- z.discriminatedUnion("type", [Base, IssueRepro]),
318
+ co.discriminatedUnion("type", [Base, IssueRepro]),
319
319
  );
320
320
 
321
321
  const person = IssueRepro.create({
@@ -350,7 +350,7 @@ describe("CoMap.Record", async () => {
350
350
 
351
351
  const PersonRecord = co.record(
352
352
  z.string(),
353
- z.discriminatedUnion("type", [Base, IssueRepro]),
353
+ co.discriminatedUnion("type", [Base, IssueRepro]),
354
354
  );
355
355
 
356
356
  const person = IssueRepro.create({
@@ -385,7 +385,7 @@ describe("CoMap.Record", async () => {
385
385
 
386
386
  const PersonRecord = co.record(
387
387
  z.string(),
388
- z.discriminatedUnion("type", [Base, IssueRepro]),
388
+ co.discriminatedUnion("type", [Base, IssueRepro]),
389
389
  );
390
390
 
391
391
  const person = IssueRepro.create({
@@ -159,7 +159,7 @@ describe("CoMap", async () => {
159
159
  test("Comap with recursive optional reference", () => {
160
160
  const Recursive = co.map({
161
161
  get child(): z.ZodOptional<typeof Recursive> {
162
- return z.optional(Recursive);
162
+ return co.optional(Recursive);
163
163
  },
164
164
  });
165
165
 
@@ -185,7 +185,7 @@ describe("CoMap", async () => {
185
185
  age: z.number(),
186
186
  // TODO: would be nice if this didn't need a type annotation
187
187
  get friend(): z.ZodOptional<typeof Person> {
188
- return z.optional(Person);
188
+ return co.optional(Person);
189
189
  },
190
190
  });
191
191
 
@@ -280,7 +280,7 @@ describe("CoMap", async () => {
280
280
 
281
281
  const MapWithEnumOfMaps = co.map({
282
282
  name: z.string(),
283
- child: z.discriminatedUnion("type", [ChildA, ChildB]),
283
+ child: co.discriminatedUnion("type", [ChildA, ChildB]),
284
284
  });
285
285
 
286
286
  const mapWithEnum = MapWithEnumOfMaps.create({
@@ -12,7 +12,7 @@ import {
12
12
  } from "vitest";
13
13
  import { Group, co, subscribeToCoValue, z } from "../exports.js";
14
14
  import { Account } from "../index.js";
15
- import { ID, Loaded, zodSchemaToCoSchema } from "../internal.js";
15
+ import { Loaded, anySchemaToCoSchema } from "../internal.js";
16
16
  import {
17
17
  createJazzTestAccount,
18
18
  getPeerConnectedToTestSyncServer,
@@ -145,7 +145,7 @@ describe("CoMap", async () => {
145
145
  age: z.number(),
146
146
  // TODO: would be nice if this didn't need a type annotation
147
147
  get friend(): z.ZodOptional<typeof Person> {
148
- return z.optional(Person);
148
+ return co.optional(Person);
149
149
  },
150
150
  });
151
151
 
@@ -182,7 +182,7 @@ describe("CoMap", async () => {
182
182
  name: z.string(),
183
183
  age: z.number(),
184
184
  get friend(): z.ZodOptional<typeof Person> {
185
- return z.optional(Person);
185
+ return co.optional(Person);
186
186
  },
187
187
  });
188
188
 
@@ -211,7 +211,7 @@ describe("CoMap", async () => {
211
211
  name: z.string(),
212
212
  age: z.number(),
213
213
  get friend(): z.ZodOptional<typeof Person> {
214
- return z.optional(Person);
214
+ return co.optional(Person);
215
215
  },
216
216
  });
217
217
 
@@ -400,7 +400,7 @@ describe("CoMap", async () => {
400
400
 
401
401
  const MapWithEnumOfMaps = co.map({
402
402
  name: z.string(),
403
- child: z.discriminatedUnion("type", [ChildA, ChildB]),
403
+ child: co.discriminatedUnion("type", [ChildA, ChildB]),
404
404
  });
405
405
 
406
406
  const mapWithEnum = MapWithEnumOfMaps.create({
@@ -801,7 +801,7 @@ describe("CoMap resolution", async () => {
801
801
  const spy = vi.fn((person) => updates.push(person));
802
802
 
803
803
  subscribeToCoValue(
804
- zodSchemaToCoSchema(Person), // TODO: we should get rid of the conversion in the future
804
+ anySchemaToCoSchema(Person), // TODO: we should get rid of the conversion in the future
805
805
  person.id,
806
806
  {
807
807
  syncResolution: true,
@@ -1003,7 +1003,7 @@ describe("CoMap applyDiff", async () => {
1003
1003
  birthday: z.date(),
1004
1004
  nested: NestedMap,
1005
1005
  optionalField: z.string().optional(),
1006
- optionalNested: z.optional(NestedMap),
1006
+ optionalNested: co.optional(NestedMap),
1007
1007
  });
1008
1008
 
1009
1009
  test("Basic applyDiff", () => {
@@ -1698,7 +1698,7 @@ describe("Creating and finding unique CoMaps", async () => {
1698
1698
  dateValue: z.date(),
1699
1699
  });
1700
1700
 
1701
- const AttributeValue = z.discriminatedUnion("type", [
1701
+ const AttributeValue = co.discriminatedUnion("type", [
1702
1702
  StringAttributeValue,
1703
1703
  NumberAttributeValue,
1704
1704
  DateAttributeValue,
@@ -1714,7 +1714,7 @@ describe("Creating and finding unique CoMaps", async () => {
1714
1714
  attributeValue: AttributeValue,
1715
1715
  });
1716
1716
 
1717
- const Tag = z.discriminatedUnion("type", [
1717
+ const Tag = co.discriminatedUnion("type", [
1718
1718
  AttributeTag,
1719
1719
  StringTag,
1720
1720
  DateTag,
@@ -1764,7 +1764,7 @@ describe("Creating and finding unique CoMaps", async () => {
1764
1764
  error: HttpError,
1765
1765
  });
1766
1766
 
1767
- const ErrorResponse = z.discriminatedUnion("type", [
1767
+ const ErrorResponse = co.discriminatedUnion("type", [
1768
1768
  ClientError,
1769
1769
  ServerError,
1770
1770
  NetworkError,
@@ -0,0 +1,39 @@
1
+ import { beforeEach, describe, test } from "vitest";
2
+ import { CoPlainText, co, z } from "../exports.js";
3
+ import { createJazzTestAccount, setupJazzTestSync } from "../testing.js";
4
+
5
+ describe("co.optional", () => {
6
+ beforeEach(async () => {
7
+ await setupJazzTestSync();
8
+
9
+ await createJazzTestAccount({
10
+ isCurrentActiveAccount: true,
11
+ creationProps: { name: "Hermes Puggington" },
12
+ });
13
+ });
14
+
15
+ test("can use co.optional with CoValue schemas as values", () => {
16
+ const Person = co.map({
17
+ preferredName: co.optional(co.plainText()),
18
+ });
19
+
20
+ const person = Person.create({});
21
+
22
+ type ExpectedType = {
23
+ preferredName?: CoPlainText;
24
+ };
25
+
26
+ function matches(value: ExpectedType) {
27
+ return value;
28
+ }
29
+
30
+ matches(person);
31
+ });
32
+
33
+ test("cannot use co.optional with zod schemas as values", () => {
34
+ const Person = co.map({
35
+ // @ts-expect-error: cannot use co.optional with a Zod schema
36
+ preferredName: co.optional(z.string()),
37
+ });
38
+ });
39
+ });
@@ -1,4 +1,4 @@
1
- import { AgentSecret, RawCoMap } from "cojson";
1
+ import { AgentSecret } from "cojson";
2
2
  import { WasmCrypto } from "cojson/crypto/WasmCrypto";
3
3
  import { beforeEach, describe, expect, test, vi } from "vitest";
4
4
  import {
@@ -9,6 +9,7 @@ import {
9
9
  ID,
10
10
  InMemoryKVStore,
11
11
  KvStoreContext,
12
+ anySchemaToCoSchema,
12
13
  co,
13
14
  createAnonymousJazzContext,
14
15
  createJazzContext,
@@ -90,7 +91,7 @@ describe("createContext methods", () => {
90
91
  });
91
92
 
92
93
  expect(context.account).toBeInstanceOf(
93
- zodSchemaToCoSchema(CustomAccount),
94
+ anySchemaToCoSchema(CustomAccount),
94
95
  );
95
96
  });
96
97
 
@@ -199,7 +200,7 @@ describe("createContext methods", () => {
199
200
  });
200
201
 
201
202
  expect(context.account).toBeInstanceOf(
202
- zodSchemaToCoSchema(CustomAccount),
203
+ anySchemaToCoSchema(CustomAccount),
203
204
  );
204
205
  });
205
206
 
@@ -350,7 +351,7 @@ describe("createContext methods", () => {
350
351
  });
351
352
 
352
353
  expect(context.account).toBeInstanceOf(
353
- zodSchemaToCoSchema(CustomAccount),
354
+ anySchemaToCoSchema(CustomAccount),
354
355
  );
355
356
  });
356
357
  });