jazz-tools 0.15.16 → 0.16.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.svelte-kit/__package__/jazz.class.svelte.d.ts +2 -2
- package/.svelte-kit/__package__/jazz.class.svelte.d.ts.map +1 -1
- package/.svelte-kit/__package__/jazz.class.svelte.js +5 -5
- package/.svelte-kit/__package__/jazz.svelte.d.ts +2 -2
- package/.svelte-kit/__package__/jazz.svelte.d.ts.map +1 -1
- package/.turbo/turbo-build.log +44 -44
- package/CHANGELOG.md +34 -0
- package/dist/browser/index.d.ts +2 -2
- package/dist/browser/index.d.ts.map +1 -1
- package/dist/browser/index.js.map +1 -1
- package/dist/browser-media-images/index.d.ts +3 -1
- package/dist/browser-media-images/index.d.ts.map +1 -1
- package/dist/browser-media-images/index.js.map +1 -1
- package/dist/{chunk-OSVAAVWQ.js → chunk-CL3ROOZM.js} +598 -598
- package/dist/chunk-CL3ROOZM.js.map +1 -0
- package/dist/index.js +8 -10
- package/dist/index.js.map +1 -1
- package/dist/react/hooks.d.ts +2 -2
- package/dist/react/hooks.d.ts.map +1 -1
- package/dist/react/index.js.map +1 -1
- package/dist/react-core/hooks.d.ts +2 -2
- package/dist/react-core/hooks.d.ts.map +1 -1
- package/dist/react-core/index.js +3 -3
- package/dist/react-core/index.js.map +1 -1
- package/dist/react-native-core/hooks.d.ts +2 -2
- package/dist/react-native-core/hooks.d.ts.map +1 -1
- package/dist/react-native-core/index.js.map +1 -1
- package/dist/svelte/jazz.class.svelte.d.ts +2 -2
- package/dist/svelte/jazz.class.svelte.d.ts.map +1 -1
- package/dist/svelte/jazz.class.svelte.js +5 -5
- package/dist/svelte/jazz.svelte.d.ts +2 -2
- package/dist/svelte/jazz.svelte.d.ts.map +1 -1
- package/dist/testing.js +3 -3
- package/dist/testing.js.map +1 -1
- package/dist/tools/coValues/CoValueBase.d.ts +3 -13
- package/dist/tools/coValues/CoValueBase.d.ts.map +1 -1
- package/dist/tools/coValues/account.d.ts +2 -2
- package/dist/tools/coValues/account.d.ts.map +1 -1
- package/dist/tools/coValues/coFeed.d.ts.map +1 -1
- package/dist/tools/coValues/coList.d.ts.map +1 -1
- package/dist/tools/coValues/coMap.d.ts +8 -23
- package/dist/tools/coValues/coMap.d.ts.map +1 -1
- package/dist/tools/coValues/deepLoading.d.ts +4 -1
- package/dist/tools/coValues/deepLoading.d.ts.map +1 -1
- package/dist/tools/coValues/extensions/imageDef.d.ts +4 -7
- package/dist/tools/coValues/extensions/imageDef.d.ts.map +1 -1
- package/dist/tools/coValues/inbox.d.ts +2 -2
- package/dist/tools/coValues/inbox.d.ts.map +1 -1
- package/dist/tools/coValues/interfaces.d.ts +2 -17
- package/dist/tools/coValues/interfaces.d.ts.map +1 -1
- package/dist/tools/coValues/request.d.ts +3 -3
- package/dist/tools/coValues/request.d.ts.map +1 -1
- package/dist/tools/coValues/schemaUnion.d.ts +5 -1
- package/dist/tools/coValues/schemaUnion.d.ts.map +1 -1
- package/dist/tools/exports.d.ts +3 -3
- package/dist/tools/exports.d.ts.map +1 -1
- package/dist/tools/implementation/createContext.d.ts +4 -4
- package/dist/tools/implementation/createContext.d.ts.map +1 -1
- package/dist/tools/implementation/invites.d.ts +2 -2
- package/dist/tools/implementation/invites.d.ts.map +1 -1
- package/dist/tools/implementation/schemaUtils.d.ts +8 -0
- package/dist/tools/implementation/schemaUtils.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/coExport.d.ts +11 -1
- package/dist/tools/implementation/zodSchema/coExport.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts +22 -0
- package/dist/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts +10 -0
- package/dist/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +11 -11
- package/dist/tools/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts +34 -24
- package/dist/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +17 -14
- package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +24 -17
- package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +31 -34
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts +20 -9
- package/dist/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +19 -12
- package/dist/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts +18 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/CoValueSchema.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +14 -9
- package/dist/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +14 -9
- package/dist/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +14 -9
- package/dist/tools/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/CoFieldInit.d.ts +10 -0
- package/dist/tools/implementation/zodSchema/typeConverters/CoFieldInit.d.ts.map +1 -0
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +15 -13
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +12 -15
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +17 -20
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +17 -20
- package/dist/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/unionUtils.d.ts +3 -5
- package/dist/tools/implementation/zodSchema/unionUtils.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodCo.d.ts +10 -8
- package/dist/tools/implementation/zodSchema/zodCo.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodReExport.d.ts +4 -8
- package/dist/tools/implementation/zodSchema/zodReExport.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/zodSchema.d.ts +21 -30
- package/dist/tools/implementation/zodSchema/zodSchema.d.ts.map +1 -1
- package/dist/tools/internal.d.ts +3 -2
- package/dist/tools/internal.d.ts.map +1 -1
- package/dist/tools/lib/utilityTypes.d.ts +18 -0
- package/dist/tools/lib/utilityTypes.d.ts.map +1 -0
- package/dist/tools/subscribe/utils.d.ts.map +1 -1
- package/dist/tools/testing.d.ts +2 -2
- package/dist/tools/testing.d.ts.map +1 -1
- package/dist/worker/index.d.ts.map +1 -1
- package/dist/worker/index.js +2 -2
- package/dist/worker/index.js.map +1 -1
- package/package.json +5 -5
- package/src/browser/index.ts +2 -4
- package/src/browser-media-images/index.ts +1 -1
- package/src/react/hooks.tsx +2 -2
- package/src/react-core/hooks.ts +6 -6
- package/src/react-core/tests/useAccount.test.ts +2 -2
- package/src/react-core/tests/useCoState.test.ts +3 -2
- package/src/react-native-core/hooks.tsx +2 -2
- package/src/svelte/jazz.class.svelte.ts +10 -7
- package/src/svelte/jazz.svelte.ts +2 -2
- package/src/tools/coValues/CoValueBase.ts +8 -20
- package/src/tools/coValues/account.ts +18 -14
- package/src/tools/coValues/coFeed.ts +0 -4
- package/src/tools/coValues/coList.ts +7 -9
- package/src/tools/coValues/coMap.ts +5 -13
- package/src/tools/coValues/coPlainText.ts +4 -4
- package/src/tools/coValues/deepLoading.ts +4 -1
- package/src/tools/coValues/extensions/imageDef.ts +3 -3
- package/src/tools/coValues/inbox.ts +4 -4
- package/src/tools/coValues/interfaces.ts +4 -71
- package/src/tools/coValues/request.ts +17 -15
- package/src/tools/coValues/schemaUnion.ts +8 -4
- package/src/tools/exports.ts +3 -14
- package/src/tools/implementation/createContext.ts +9 -9
- package/src/tools/implementation/invites.ts +2 -2
- package/src/tools/implementation/schemaUtils.ts +18 -0
- package/src/tools/implementation/zodSchema/coExport.ts +14 -0
- package/src/tools/implementation/zodSchema/runtimeConverters/coValueSchemaTransformation.ts +156 -0
- package/src/tools/implementation/zodSchema/runtimeConverters/schemaFieldToCoFieldDef.ts +149 -0
- package/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts +36 -17
- package/src/tools/implementation/zodSchema/schemaTypes/CoDiscriminatedUnionSchema.ts +101 -52
- package/src/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +54 -50
- package/src/tools/implementation/zodSchema/schemaTypes/CoListSchema.ts +54 -46
- package/src/tools/implementation/zodSchema/schemaTypes/CoMapSchema.ts +190 -161
- package/src/tools/implementation/zodSchema/schemaTypes/CoOptionalSchema.ts +29 -27
- package/src/tools/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +28 -18
- package/src/tools/implementation/zodSchema/schemaTypes/CoValueSchema.ts +18 -0
- package/src/tools/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +45 -36
- package/src/tools/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +47 -35
- package/src/tools/implementation/zodSchema/schemaTypes/RichTextSchema.ts +43 -30
- package/src/tools/implementation/zodSchema/typeConverters/CoFieldInit.ts +13 -0
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +30 -26
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +31 -28
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +89 -80
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +95 -84
- package/src/tools/implementation/zodSchema/unionUtils.ts +47 -51
- package/src/tools/implementation/zodSchema/zodCo.ts +43 -76
- package/src/tools/implementation/zodSchema/zodReExport.ts +44 -24
- package/src/tools/implementation/zodSchema/zodSchema.ts +65 -103
- package/src/tools/internal.ts +3 -2
- package/src/tools/lib/utilityTypes.ts +16 -0
- package/src/tools/subscribe/utils.ts +4 -2
- package/src/tools/testing.ts +4 -4
- package/src/tools/tests/ContextManager.test.ts +8 -9
- package/src/tools/tests/account.test.ts +65 -3
- package/src/tools/tests/coDiscriminatedUnion.test-d.ts +38 -0
- package/src/tools/tests/coDiscriminatedUnion.test.ts +219 -1
- package/src/tools/tests/coFeed.test-d.ts +4 -3
- package/src/tools/tests/coFeed.test.ts +8 -6
- package/src/tools/tests/coList.test-d.ts +32 -3
- package/src/tools/tests/coList.test.ts +30 -2
- package/src/tools/tests/coMap.record.test-d.ts +31 -3
- package/src/tools/tests/coMap.record.test.ts +18 -9
- package/src/tools/tests/coMap.test-d.ts +36 -8
- package/src/tools/tests/coMap.test.ts +87 -6
- package/src/tools/tests/coOptional.test.ts +63 -1
- package/src/tools/tests/createContext.test.ts +7 -9
- package/src/tools/tests/deepLoading.test.ts +4 -10
- package/src/tools/tests/exportImport.test.ts +2 -2
- package/src/tools/tests/groupsAndAccounts.test.ts +5 -4
- package/src/tools/tests/inbox.test.ts +3 -2
- package/src/tools/tests/load.test.ts +3 -29
- package/src/tools/tests/schemaUnion.test.ts +2 -2
- package/src/tools/tests/subscribe.test.ts +22 -114
- package/src/tools/tests/testing.test.ts +6 -6
- package/src/tools/tests/zod.test-d.ts +27 -0
- package/src/tools/tests/zod.test.ts +88 -45
- package/src/worker/index.ts +0 -1
- package/dist/chunk-OSVAAVWQ.js.map +0 -1
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +0 -12
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +0 -1
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +0 -9
- package/dist/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +0 -1
- package/src/tools/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +0 -172
- package/src/tools/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +0 -218
@@ -52,6 +52,15 @@ describe("CoMap.Record", async () => {
|
|
52
52
|
expect(Object.keys(person)).toEqual(["age"]);
|
53
53
|
});
|
54
54
|
|
55
|
+
test("create a Record with nullable values", () => {
|
56
|
+
const Person = co.record(z.string(), z.string().nullable());
|
57
|
+
const person = Person.create({ name: "John", age: null });
|
58
|
+
person.bio = null;
|
59
|
+
expect(person.name).toEqual("John");
|
60
|
+
expect(person.age).toEqual(null);
|
61
|
+
expect(person.bio).toEqual(null);
|
62
|
+
});
|
63
|
+
|
55
64
|
test("property existence", () => {
|
56
65
|
const Person = co.record(z.string(), z.string());
|
57
66
|
|
@@ -236,9 +245,6 @@ describe("CoMap.Record", async () => {
|
|
236
245
|
pet1: Dog.create({ name: "Rex", breed: "Labrador" }),
|
237
246
|
});
|
238
247
|
|
239
|
-
type V = (typeof Person)["_zod"]["def"]["valueType"];
|
240
|
-
type T = InstanceOrPrimitiveOfSchema<typeof Person>;
|
241
|
-
|
242
248
|
const updates: Loaded<typeof Person, { $each: true }>[] = [];
|
243
249
|
const spy = vi.fn((person) => updates.push(person));
|
244
250
|
|
@@ -301,7 +307,7 @@ describe("CoMap.Record", async () => {
|
|
301
307
|
});
|
302
308
|
|
303
309
|
// Covers https://github.com/garden-co/jazz/issues/2385
|
304
|
-
test("create a Record with a discriminated union containing a co.
|
310
|
+
test("create a Record with a discriminated union containing a co.image", () => {
|
305
311
|
const Base = co.map({
|
306
312
|
type: z.literal("base"),
|
307
313
|
name: z.string(),
|
@@ -309,8 +315,8 @@ describe("CoMap.Record", async () => {
|
|
309
315
|
|
310
316
|
const IssueRepro = co.map({
|
311
317
|
type: z.literal("repro"),
|
312
|
-
catchall: co.map({}).withHelpers((self) => self),
|
313
318
|
name: z.string(),
|
319
|
+
image: co.image(),
|
314
320
|
});
|
315
321
|
|
316
322
|
const PersonRecord = co.record(
|
@@ -320,8 +326,10 @@ describe("CoMap.Record", async () => {
|
|
320
326
|
|
321
327
|
const person = IssueRepro.create({
|
322
328
|
type: "repro",
|
323
|
-
catchall: IssueRepro.def.shape.catchall.create({}),
|
324
329
|
name: "John",
|
330
|
+
image: co.image().create({
|
331
|
+
originalSize: [1920, 1080],
|
332
|
+
}),
|
325
333
|
});
|
326
334
|
|
327
335
|
const record = PersonRecord.create({
|
@@ -329,7 +337,7 @@ describe("CoMap.Record", async () => {
|
|
329
337
|
});
|
330
338
|
|
331
339
|
if (record.john?.type === "repro") {
|
332
|
-
expect(record.john.
|
340
|
+
expect(record.john.image.originalSize).toEqual([1920, 1080]);
|
333
341
|
expect(record.john.name).toEqual("John");
|
334
342
|
expect(record.john.type).toEqual("repro");
|
335
343
|
}
|
@@ -342,9 +350,10 @@ describe("CoMap.Record", async () => {
|
|
342
350
|
name: z.string(),
|
343
351
|
});
|
344
352
|
|
353
|
+
const Catchall = co.map({}).catchall(z.string());
|
345
354
|
const IssueRepro = co.map({
|
346
355
|
type: z.literal("repro"),
|
347
|
-
catchall:
|
356
|
+
catchall: Catchall,
|
348
357
|
name: z.string(),
|
349
358
|
});
|
350
359
|
|
@@ -355,7 +364,7 @@ describe("CoMap.Record", async () => {
|
|
355
364
|
|
356
365
|
const person = IssueRepro.create({
|
357
366
|
type: "repro",
|
358
|
-
catchall:
|
367
|
+
catchall: Catchall.create({}),
|
359
368
|
name: "John",
|
360
369
|
});
|
361
370
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { assert, describe, expectTypeOf, test } from "vitest";
|
2
2
|
import { Group, co, z } from "../exports.js";
|
3
3
|
import { Account } from "../index.js";
|
4
|
-
import {
|
4
|
+
import { CoMap, Loaded } from "../internal.js";
|
5
5
|
|
6
6
|
describe("CoMap", async () => {
|
7
7
|
describe("init", () => {
|
@@ -117,9 +117,10 @@ describe("CoMap", async () => {
|
|
117
117
|
});
|
118
118
|
|
119
119
|
test("CoMap create with partially loaded, reference and optional", () => {
|
120
|
+
const Breed = co.map({ type: z.literal("labrador"), value: z.string() });
|
120
121
|
const Dog = co.map({
|
121
122
|
name: z.string(),
|
122
|
-
breed:
|
123
|
+
breed: Breed,
|
123
124
|
});
|
124
125
|
type Dog = co.loaded<typeof Dog>;
|
125
126
|
|
@@ -131,7 +132,7 @@ describe("CoMap", async () => {
|
|
131
132
|
|
132
133
|
const dog = Dog.create({
|
133
134
|
name: "Rex",
|
134
|
-
breed:
|
135
|
+
breed: Breed.create({
|
135
136
|
type: "labrador",
|
136
137
|
value: "Labrador",
|
137
138
|
}),
|
@@ -158,8 +159,8 @@ describe("CoMap", async () => {
|
|
158
159
|
|
159
160
|
test("Comap with recursive optional reference", () => {
|
160
161
|
const Recursive = co.map({
|
161
|
-
get child()
|
162
|
-
return
|
162
|
+
get child() {
|
163
|
+
return Recursive.optional();
|
163
164
|
},
|
164
165
|
});
|
165
166
|
|
@@ -183,8 +184,7 @@ describe("CoMap", async () => {
|
|
183
184
|
const Person = co.map({
|
184
185
|
name: z.string(),
|
185
186
|
age: z.number(),
|
186
|
-
|
187
|
-
get friend(): z.ZodOptional<typeof Person> {
|
187
|
+
get friend() {
|
188
188
|
return co.optional(Person);
|
189
189
|
},
|
190
190
|
});
|
@@ -243,7 +243,7 @@ describe("CoMap", async () => {
|
|
243
243
|
test("update a reference on a loaded value", () => {
|
244
244
|
const Dog = co.map({
|
245
245
|
name: z.string(),
|
246
|
-
get siblings()
|
246
|
+
get siblings() {
|
247
247
|
return co.list(Dog);
|
248
248
|
},
|
249
249
|
});
|
@@ -408,4 +408,32 @@ describe("CoMap resolution", async () => {
|
|
408
408
|
| null
|
409
409
|
>();
|
410
410
|
});
|
411
|
+
|
412
|
+
test("loading a map with a nullable field", async () => {
|
413
|
+
const Dog = co.map({
|
414
|
+
name: z.string(),
|
415
|
+
breed: z.string(),
|
416
|
+
});
|
417
|
+
const Person = co.map({
|
418
|
+
name: z.string(),
|
419
|
+
age: z.number().nullable(),
|
420
|
+
dog: Dog,
|
421
|
+
});
|
422
|
+
|
423
|
+
const person = Person.create({
|
424
|
+
name: "John",
|
425
|
+
age: 20,
|
426
|
+
dog: Dog.create({ name: "Rex", breed: "Labrador" }),
|
427
|
+
});
|
428
|
+
|
429
|
+
const loadedPerson = await Person.load(person.id);
|
430
|
+
|
431
|
+
expectTypeOf(loadedPerson!).toEqualTypeOf<
|
432
|
+
{
|
433
|
+
name: string;
|
434
|
+
age: number | null;
|
435
|
+
dog: Loaded<typeof Dog> | null;
|
436
|
+
} & CoMap
|
437
|
+
>();
|
438
|
+
});
|
411
439
|
});
|
@@ -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 { Loaded,
|
15
|
+
import { Loaded, coValueClassFromCoValueClassOrSchema } from "../internal.js";
|
16
16
|
import {
|
17
17
|
createJazzTestAccount,
|
18
18
|
getPeerConnectedToTestSyncServer,
|
@@ -143,8 +143,7 @@ describe("CoMap", async () => {
|
|
143
143
|
const Person = co.map({
|
144
144
|
name: z.string(),
|
145
145
|
age: z.number(),
|
146
|
-
|
147
|
-
get friend(): z.ZodOptional<typeof Person> {
|
146
|
+
get friend() {
|
148
147
|
return co.optional(Person);
|
149
148
|
},
|
150
149
|
});
|
@@ -181,7 +180,7 @@ describe("CoMap", async () => {
|
|
181
180
|
const Person = co.map({
|
182
181
|
name: z.string(),
|
183
182
|
age: z.number(),
|
184
|
-
get friend():
|
183
|
+
get friend(): co.Optional<typeof Person> {
|
185
184
|
return co.optional(Person);
|
186
185
|
},
|
187
186
|
});
|
@@ -210,7 +209,7 @@ describe("CoMap", async () => {
|
|
210
209
|
const Person = co.map({
|
211
210
|
name: z.string(),
|
212
211
|
age: z.number(),
|
213
|
-
get friend():
|
212
|
+
get friend(): co.Optional<typeof Person> {
|
214
213
|
return co.optional(Person);
|
215
214
|
},
|
216
215
|
});
|
@@ -291,6 +290,74 @@ describe("CoMap", async () => {
|
|
291
290
|
age: 30,
|
292
291
|
});
|
293
292
|
});
|
293
|
+
|
294
|
+
it("should allow extra properties when catchall is provided", () => {
|
295
|
+
const Person = co
|
296
|
+
.map({
|
297
|
+
name: z.string(),
|
298
|
+
age: z.number(),
|
299
|
+
})
|
300
|
+
.catchall(z.string());
|
301
|
+
|
302
|
+
const person = Person.create({ name: "John", age: 20 });
|
303
|
+
expect(person.name).toEqual("John");
|
304
|
+
expect(person.age).toEqual(20);
|
305
|
+
expect(person.extra).toBeUndefined();
|
306
|
+
|
307
|
+
person.name = "Jane";
|
308
|
+
person.age = 28;
|
309
|
+
person.extra = "extra";
|
310
|
+
|
311
|
+
expect(person.name).toEqual("Jane");
|
312
|
+
expect(person.age).toEqual(28);
|
313
|
+
expect(person.extra).toEqual("extra");
|
314
|
+
});
|
315
|
+
|
316
|
+
test("CoMap with reference can be created with a shallowly resolved reference", async () => {
|
317
|
+
const Dog = co.map({
|
318
|
+
name: z.string(),
|
319
|
+
breed: z.string(),
|
320
|
+
});
|
321
|
+
const Person = co.map({
|
322
|
+
name: z.string(),
|
323
|
+
age: z.number(),
|
324
|
+
pet: Dog,
|
325
|
+
get friend() {
|
326
|
+
return Person.optional();
|
327
|
+
},
|
328
|
+
});
|
329
|
+
|
330
|
+
const group = Group.create();
|
331
|
+
group.addMember("everyone", "writer");
|
332
|
+
|
333
|
+
const pet = Dog.create({ name: "Rex", breed: "Labrador" }, group);
|
334
|
+
const personA = Person.create(
|
335
|
+
{
|
336
|
+
name: "John",
|
337
|
+
age: 20,
|
338
|
+
pet,
|
339
|
+
},
|
340
|
+
{ owner: group },
|
341
|
+
);
|
342
|
+
|
343
|
+
const userB = await createJazzTestAccount();
|
344
|
+
const loadedPersonA = await Person.load(personA.id, {
|
345
|
+
resolve: true,
|
346
|
+
loadAs: userB,
|
347
|
+
});
|
348
|
+
|
349
|
+
expect(loadedPersonA).not.toBeNull();
|
350
|
+
assert(loadedPersonA);
|
351
|
+
|
352
|
+
const personB = Person.create({
|
353
|
+
name: "Jane",
|
354
|
+
age: 28,
|
355
|
+
pet,
|
356
|
+
friend: loadedPersonA,
|
357
|
+
});
|
358
|
+
|
359
|
+
expect(personB.friend?.pet.name).toEqual("Rex");
|
360
|
+
});
|
294
361
|
});
|
295
362
|
|
296
363
|
describe("Mutation", () => {
|
@@ -801,7 +868,7 @@ describe("CoMap resolution", async () => {
|
|
801
868
|
const spy = vi.fn((person) => updates.push(person));
|
802
869
|
|
803
870
|
subscribeToCoValue(
|
804
|
-
|
871
|
+
coValueClassFromCoValueClassOrSchema(Person), // TODO: we should get rid of the conversion in the future
|
805
872
|
person.id,
|
806
873
|
{
|
807
874
|
syncResolution: true,
|
@@ -2104,6 +2171,20 @@ describe("CoMap migration", () => {
|
|
2104
2171
|
});
|
2105
2172
|
});
|
2106
2173
|
|
2174
|
+
describe("co.map schema", () => {
|
2175
|
+
test("can access the inner schemas of a co.map", () => {
|
2176
|
+
const Person = co.map({
|
2177
|
+
name: co.plainText(),
|
2178
|
+
});
|
2179
|
+
|
2180
|
+
const person = Person.create({
|
2181
|
+
name: Person.shape["name"].create("John"),
|
2182
|
+
});
|
2183
|
+
|
2184
|
+
expect(person.name.toString()).toEqual("John");
|
2185
|
+
});
|
2186
|
+
});
|
2187
|
+
|
2107
2188
|
describe("Updating a nested reference", () => {
|
2108
2189
|
test("should assign a resolved optional reference and expect value is not null", async () => {
|
2109
2190
|
// Define the schema similar to the server-worker-http example
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import { beforeEach, describe, test } from "vitest";
|
1
|
+
import { beforeEach, describe, expect, test } from "vitest";
|
2
2
|
import { CoPlainText, co, z } from "../exports.js";
|
3
3
|
import { createJazzTestAccount, setupJazzTestSync } from "../testing.js";
|
4
4
|
|
@@ -36,4 +36,66 @@ describe("co.optional", () => {
|
|
36
36
|
preferredName: co.optional(z.string()),
|
37
37
|
});
|
38
38
|
});
|
39
|
+
|
40
|
+
test("can use schem.optional() on all CoValue schemas but co.optional()", () => {
|
41
|
+
const Option1 = co.map({ type: z.literal("1") });
|
42
|
+
const Option2 = co.map({ type: z.literal("2") });
|
43
|
+
const Schema = co.map({
|
44
|
+
plainText: co.plainText().optional(),
|
45
|
+
richText: co.richText().optional(),
|
46
|
+
fileStream: co.fileStream().optional(),
|
47
|
+
image: co.image().optional(),
|
48
|
+
record: co.record(z.string(), z.string()).optional(),
|
49
|
+
map: co.map({ field: z.string() }).optional(),
|
50
|
+
list: co.list(z.string()).optional(),
|
51
|
+
feed: co.feed(z.string()).optional(),
|
52
|
+
union: co.discriminatedUnion("type", [Option1, Option2]).optional(),
|
53
|
+
});
|
54
|
+
|
55
|
+
const schema = Schema.create({});
|
56
|
+
|
57
|
+
expect(schema.plainText).toBeUndefined();
|
58
|
+
expect(schema.richText).toBeUndefined();
|
59
|
+
expect(schema.fileStream).toBeUndefined();
|
60
|
+
expect(schema.image).toBeUndefined();
|
61
|
+
expect(schema.record).toBeUndefined();
|
62
|
+
expect(schema.map).toBeUndefined();
|
63
|
+
expect(schema.list).toBeUndefined();
|
64
|
+
expect(schema.feed).toBeUndefined();
|
65
|
+
expect(schema.union).toBeUndefined();
|
66
|
+
|
67
|
+
schema.plainText = Schema.shape.plainText.innerType.create("Hello");
|
68
|
+
schema.richText = Schema.shape.richText.innerType.create("Hello");
|
69
|
+
schema.fileStream = Schema.shape.fileStream.innerType.create();
|
70
|
+
schema.image = Schema.shape.image.innerType.create({
|
71
|
+
originalSize: [1920, 1080],
|
72
|
+
});
|
73
|
+
schema.record = Schema.shape.record.innerType.create({ field: "hello" });
|
74
|
+
schema.map = Schema.shape.map.innerType.create({ field: "hello" });
|
75
|
+
schema.list = Schema.shape.list.innerType.create([]);
|
76
|
+
schema.feed = Schema.shape.feed.innerType.create([]);
|
77
|
+
schema.union = Option1.create({ type: "1" });
|
78
|
+
|
79
|
+
expect(schema.plainText?.toString()).toEqual("Hello");
|
80
|
+
expect(schema.richText?.toString()).toEqual("Hello");
|
81
|
+
expect(schema.fileStream).not.toBeUndefined();
|
82
|
+
expect(schema.image?.originalSize).toEqual([1920, 1080]);
|
83
|
+
expect(schema.record?.field).toEqual("hello");
|
84
|
+
expect(schema.map?.field).toEqual("hello");
|
85
|
+
expect(schema.list).toEqual([]);
|
86
|
+
expect(schema.feed).not.toBeUndefined();
|
87
|
+
expect(schema.union?.type).toEqual("1");
|
88
|
+
});
|
89
|
+
|
90
|
+
test("can access the inner schema of a co.optional", () => {
|
91
|
+
const Person = co.map({
|
92
|
+
preferredName: co.optional(co.plainText()),
|
93
|
+
});
|
94
|
+
|
95
|
+
const person = Person.create({
|
96
|
+
preferredName: Person.shape["preferredName"].innerType.create("John"),
|
97
|
+
});
|
98
|
+
|
99
|
+
expect(person?.preferredName?.toString()).toEqual("John");
|
100
|
+
});
|
39
101
|
});
|
@@ -9,15 +9,13 @@ import {
|
|
9
9
|
ID,
|
10
10
|
InMemoryKVStore,
|
11
11
|
KvStoreContext,
|
12
|
-
anySchemaToCoSchema,
|
13
12
|
co,
|
13
|
+
coValueClassFromCoValueClassOrSchema,
|
14
14
|
createAnonymousJazzContext,
|
15
15
|
createJazzContext,
|
16
16
|
createJazzContextForNewAccount,
|
17
17
|
createJazzContextFromExistingCredentials,
|
18
18
|
randomSessionProvider,
|
19
|
-
z,
|
20
|
-
zodSchemaToCoSchema,
|
21
19
|
} from "../exports";
|
22
20
|
import { activeAccountContext } from "../implementation/activeAccountContext";
|
23
21
|
import {
|
@@ -86,12 +84,12 @@ describe("createContext methods", () => {
|
|
86
84
|
credentials,
|
87
85
|
peersToLoadFrom: [getPeerConnectedToTestSyncServer()],
|
88
86
|
crypto: Crypto,
|
89
|
-
AccountSchema:
|
87
|
+
AccountSchema: CustomAccount,
|
90
88
|
sessionProvider: randomSessionProvider,
|
91
89
|
});
|
92
90
|
|
93
91
|
expect(context.account).toBeInstanceOf(
|
94
|
-
|
92
|
+
coValueClassFromCoValueClassOrSchema(CustomAccount),
|
95
93
|
);
|
96
94
|
});
|
97
95
|
|
@@ -196,11 +194,11 @@ describe("createContext methods", () => {
|
|
196
194
|
creationProps: { name: "New User" },
|
197
195
|
peersToLoadFrom: [],
|
198
196
|
crypto: Crypto,
|
199
|
-
AccountSchema:
|
197
|
+
AccountSchema: CustomAccount,
|
200
198
|
});
|
201
199
|
|
202
200
|
expect(context.account).toBeInstanceOf(
|
203
|
-
|
201
|
+
coValueClassFromCoValueClassOrSchema(CustomAccount),
|
204
202
|
);
|
205
203
|
});
|
206
204
|
|
@@ -347,11 +345,11 @@ describe("createContext methods", () => {
|
|
347
345
|
crypto: Crypto,
|
348
346
|
authSecretStorage,
|
349
347
|
sessionProvider: randomSessionProvider,
|
350
|
-
AccountSchema:
|
348
|
+
AccountSchema: CustomAccount,
|
351
349
|
});
|
352
350
|
|
353
351
|
expect(context.account).toBeInstanceOf(
|
354
|
-
|
352
|
+
coValueClassFromCoValueClassOrSchema(CustomAccount),
|
355
353
|
);
|
356
354
|
});
|
357
355
|
});
|
@@ -1,4 +1,4 @@
|
|
1
|
-
import {
|
1
|
+
import { cojsonInternals } from "cojson";
|
2
2
|
import { WasmCrypto } from "cojson/crypto/WasmCrypto";
|
3
3
|
import { assert, describe, expect, expectTypeOf, test, vi } from "vitest";
|
4
4
|
import {
|
@@ -9,13 +9,7 @@ import {
|
|
9
9
|
isControlledAccount,
|
10
10
|
z,
|
11
11
|
} from "../index.js";
|
12
|
-
import {
|
13
|
-
Account,
|
14
|
-
CoListSchema,
|
15
|
-
Loaded,
|
16
|
-
co,
|
17
|
-
randomSessionProvider,
|
18
|
-
} from "../internal.js";
|
12
|
+
import { Account, Loaded, co, randomSessionProvider } from "../internal.js";
|
19
13
|
import { createJazzTestAccount, linkAccounts } from "../testing.js";
|
20
14
|
import { waitFor } from "./utils.js";
|
21
15
|
|
@@ -794,11 +788,11 @@ describe("Deep loading with unauthorized account", async () => {
|
|
794
788
|
test("unaccessible list element with $onError and $each with depth", async () => {
|
795
789
|
const Person = co.map({
|
796
790
|
name: z.string(),
|
797
|
-
get friends():
|
791
|
+
get friends(): co.Optional<typeof Friends> {
|
798
792
|
return co.optional(Friends);
|
799
793
|
},
|
800
794
|
});
|
801
|
-
const Friends:
|
795
|
+
const Friends: co.List<typeof Person> = co.list(Person); // TODO: annoying that we have to annotate
|
802
796
|
|
803
797
|
const list = Friends.create(
|
804
798
|
[
|
@@ -464,13 +464,13 @@ describe("importContentPieces", () => {
|
|
464
464
|
{
|
465
465
|
title: "My First Post",
|
466
466
|
content: "Hello World",
|
467
|
-
comments: Post.
|
467
|
+
comments: Post.shape.comments.create([comment1, comment2], group),
|
468
468
|
},
|
469
469
|
group,
|
470
470
|
);
|
471
471
|
|
472
472
|
const blog = Blog.create(
|
473
|
-
{ name: "My Blog", posts: Blog.
|
473
|
+
{ name: "My Blog", posts: Blog.shape.posts.create([post], group) },
|
474
474
|
group,
|
475
475
|
);
|
476
476
|
|
@@ -1,7 +1,7 @@
|
|
1
1
|
import { WasmCrypto } from "cojson/crypto/WasmCrypto";
|
2
2
|
import { assert, beforeEach, describe, expect, test } from "vitest";
|
3
3
|
import { CoMap, Group, z } from "../exports.js";
|
4
|
-
import { Loaded, Ref, co
|
4
|
+
import { Loaded, Ref, co } from "../internal.js";
|
5
5
|
import { createJazzTestAccount, setupJazzTestSync } from "../testing.js";
|
6
6
|
import { setupTwoNodes, waitFor } from "./utils.js";
|
7
7
|
|
@@ -22,15 +22,16 @@ describe("Custom accounts and groups", async () => {
|
|
22
22
|
color: z.string(),
|
23
23
|
});
|
24
24
|
|
25
|
+
const Root = co.map({});
|
25
26
|
const CustomAccount = co
|
26
27
|
.account({
|
27
28
|
profile: CustomProfile,
|
28
|
-
root:
|
29
|
+
root: Root,
|
29
30
|
})
|
30
31
|
.withMigration((account, creationProps?: { name: string }) => {
|
31
32
|
// making sure that the inferred type of account.root & account.profile considers the root/profile not being loaded
|
32
33
|
type R = typeof account.root;
|
33
|
-
const _r: R = {} as Loaded<typeof
|
34
|
+
const _r: R = {} as Loaded<typeof Root> | null;
|
34
35
|
type P = typeof account.profile;
|
35
36
|
const _p: P = {} as Loaded<typeof CustomProfile> | null;
|
36
37
|
if (creationProps) {
|
@@ -47,7 +48,7 @@ describe("Custom accounts and groups", async () => {
|
|
47
48
|
const me = await createJazzTestAccount({
|
48
49
|
creationProps: { name: "Hermes Puggington" },
|
49
50
|
isCurrentActiveAccount: true,
|
50
|
-
AccountSchema:
|
51
|
+
AccountSchema: CustomAccount,
|
51
52
|
});
|
52
53
|
|
53
54
|
expect(me.profile).toBeDefined();
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { describe, expect, it, vi } from "vitest";
|
2
2
|
import { Group, Inbox, InboxSender, z } from "../exports";
|
3
|
-
import { Loaded,
|
3
|
+
import { Loaded, co, coValueClassFromCoValueClassOrSchema } from "../internal";
|
4
4
|
import { setupTwoNodes, waitFor } from "./utils";
|
5
5
|
|
6
6
|
const Message = co.map({
|
@@ -18,7 +18,8 @@ describe("Inbox", () => {
|
|
18
18
|
|
19
19
|
const { clientAccount: sender, serverAccount: receiver } =
|
20
20
|
await setupTwoNodes({
|
21
|
-
ServerAccountSchema:
|
21
|
+
ServerAccountSchema:
|
22
|
+
coValueClassFromCoValueClassOrSchema(WorkerAccount),
|
22
23
|
});
|
23
24
|
|
24
25
|
await expect(() => InboxSender.load(receiver.id, sender)).rejects.toThrow(
|
@@ -69,33 +69,6 @@ test("load a missing optional value (co.optional)", async () => {
|
|
69
69
|
expect(john.dog).toBeUndefined();
|
70
70
|
});
|
71
71
|
|
72
|
-
test("load a missing optional value (z.optional)", async () => {
|
73
|
-
const Dog = co.map({
|
74
|
-
name: z.string(),
|
75
|
-
});
|
76
|
-
|
77
|
-
const Person = co.map({
|
78
|
-
name: z.string(),
|
79
|
-
dog: z.optional(Dog),
|
80
|
-
});
|
81
|
-
|
82
|
-
const group = Group.create();
|
83
|
-
const map = Person.create({ name: "John" }, group);
|
84
|
-
group.addMember("everyone", "reader");
|
85
|
-
|
86
|
-
const alice = await createJazzTestAccount();
|
87
|
-
|
88
|
-
const john = await Person.load(map.id, {
|
89
|
-
loadAs: alice,
|
90
|
-
resolve: { dog: true },
|
91
|
-
});
|
92
|
-
|
93
|
-
assert(john);
|
94
|
-
|
95
|
-
expect(john.name).toBe("John");
|
96
|
-
expect(john.dog).toBeUndefined();
|
97
|
-
});
|
98
|
-
|
99
72
|
test("load a missing optional value (Schema.optional)", async () => {
|
100
73
|
const Dog = co.map({
|
101
74
|
name: z.string(),
|
@@ -214,13 +187,14 @@ test("returns null if the value is unavailable after retries", async () => {
|
|
214
187
|
test("load a large coValue", async () => {
|
215
188
|
const syncServer = await setupJazzTestSync({ asyncPeers: true });
|
216
189
|
|
190
|
+
const Data = co.list(z.string());
|
217
191
|
const LargeDataset = co.map({
|
218
192
|
metadata: z.object({
|
219
193
|
name: z.string(),
|
220
194
|
description: z.string(),
|
221
195
|
createdAt: z.number(),
|
222
196
|
}),
|
223
|
-
data:
|
197
|
+
data: Data,
|
224
198
|
});
|
225
199
|
|
226
200
|
const group = Group.create(syncServer);
|
@@ -232,7 +206,7 @@ test("load a large coValue", async () => {
|
|
232
206
|
"A dataset with many entries for testing large coValue loading",
|
233
207
|
createdAt: Date.now(),
|
234
208
|
},
|
235
|
-
data:
|
209
|
+
data: Data.create([], group),
|
236
210
|
},
|
237
211
|
group,
|
238
212
|
);
|
@@ -4,8 +4,8 @@ import {
|
|
4
4
|
Account,
|
5
5
|
CryptoProvider,
|
6
6
|
Loaded,
|
7
|
-
anySchemaToCoSchema,
|
8
7
|
co,
|
8
|
+
coValueClassFromCoValueClassOrSchema,
|
9
9
|
subscribeToCoValue,
|
10
10
|
z,
|
11
11
|
} from "../exports.js";
|
@@ -93,7 +93,7 @@ describe("SchemaUnion", () => {
|
|
93
93
|
);
|
94
94
|
let currentValue = "Submit";
|
95
95
|
const unsubscribe = subscribeToCoValue(
|
96
|
-
|
96
|
+
coValueClassFromCoValueClassOrSchema(WidgetUnion),
|
97
97
|
buttonWidget.id,
|
98
98
|
{ loadAs: me, syncResolution: true },
|
99
99
|
(value: Loaded<typeof WidgetUnion>) => {
|