jazz-tools 0.13.30 → 0.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +23 -3
- package/dist/auth/DemoAuth.d.ts.map +1 -1
- package/dist/auth/PassphraseAuth.d.ts +1 -3
- package/dist/auth/PassphraseAuth.d.ts.map +1 -1
- package/dist/{chunk-LMV6J7GN.js → chunk-2ASOGEYA.js} +3531 -3269
- package/dist/chunk-2ASOGEYA.js.map +1 -0
- package/dist/coValues/CoValueBase.d.ts +22 -0
- package/dist/coValues/CoValueBase.d.ts.map +1 -0
- package/dist/coValues/account.d.ts +12 -12
- package/dist/coValues/account.d.ts.map +1 -1
- package/dist/coValues/coFeed.d.ts +24 -25
- package/dist/coValues/coFeed.d.ts.map +1 -1
- package/dist/coValues/coList.d.ts +10 -13
- package/dist/coValues/coList.d.ts.map +1 -1
- package/dist/coValues/coMap.d.ts +32 -35
- package/dist/coValues/coMap.d.ts.map +1 -1
- package/dist/coValues/coPlainText.d.ts.map +1 -1
- package/dist/coValues/deepLoading.d.ts +28 -22
- package/dist/coValues/deepLoading.d.ts.map +1 -1
- package/dist/coValues/extensions/imageDef.d.ts +12 -11
- package/dist/coValues/extensions/imageDef.d.ts.map +1 -1
- package/dist/coValues/group.d.ts +5 -9
- package/dist/coValues/group.d.ts.map +1 -1
- package/dist/coValues/inbox.d.ts +2 -3
- package/dist/coValues/inbox.d.ts.map +1 -1
- package/dist/coValues/interfaces.d.ts +8 -34
- package/dist/coValues/interfaces.d.ts.map +1 -1
- package/dist/coValues/profile.d.ts +4 -14
- package/dist/coValues/profile.d.ts.map +1 -1
- package/dist/coValues/registeredSchemas.d.ts +1 -3
- package/dist/coValues/registeredSchemas.d.ts.map +1 -1
- package/dist/coValues/schemaUnion.d.ts +6 -6
- package/dist/exports.d.ts +12 -16
- package/dist/exports.d.ts.map +1 -1
- package/dist/implementation/ContextManager.d.ts +1 -1
- package/dist/implementation/ContextManager.d.ts.map +1 -1
- package/dist/implementation/activeAccountContext.d.ts +1 -1
- package/dist/implementation/activeAccountContext.d.ts.map +1 -1
- package/dist/implementation/createContext.d.ts +10 -10
- package/dist/implementation/createContext.d.ts.map +1 -1
- package/dist/implementation/invites.d.ts +6 -6
- package/dist/implementation/invites.d.ts.map +1 -1
- package/dist/implementation/refs.d.ts +2 -2
- package/dist/implementation/refs.d.ts.map +1 -1
- package/dist/implementation/schema.d.ts +21 -28
- package/dist/implementation/schema.d.ts.map +1 -1
- package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts +9 -0
- package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +1 -0
- package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts +28 -0
- package/dist/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts +65 -0
- package/dist/implementation/zodSchema/schemaTypes/AccountSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts +28 -0
- package/dist/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +24 -0
- package/dist/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +41 -0
- package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts +35 -0
- package/dist/implementation/zodSchema/schemaTypes/CoRecordSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts +9 -0
- package/dist/implementation/zodSchema/schemaTypes/FileStreamSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts +20 -0
- package/dist/implementation/zodSchema/schemaTypes/PlainTextSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts +18 -0
- package/dist/implementation/zodSchema/schemaTypes/RichTextSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts +24 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOfSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts +21 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.d.ts.map +1 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +29 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +29 -0
- package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -0
- package/dist/implementation/zodSchema/unionUtils.d.ts +6 -0
- package/dist/implementation/zodSchema/unionUtils.d.ts.map +1 -0
- package/dist/implementation/zodSchema/zodCo.d.ts +35 -0
- package/dist/implementation/zodSchema/zodCo.d.ts.map +1 -0
- package/dist/implementation/zodSchema/zodSchema.d.ts +38 -0
- package/dist/implementation/zodSchema/zodSchema.d.ts.map +1 -0
- package/dist/index.js +295 -10
- package/dist/index.js.map +1 -1
- package/dist/internal.d.ts +34 -0
- package/dist/internal.d.ts.map +1 -1
- package/dist/subscribe/SubscriptionScope.d.ts +2 -2
- package/dist/subscribe/SubscriptionScope.d.ts.map +1 -1
- package/dist/subscribe/utils.d.ts +2 -2
- package/dist/subscribe/utils.d.ts.map +1 -1
- package/dist/testing.d.ts +10 -8
- package/dist/testing.d.ts.map +1 -1
- package/dist/testing.js +1 -1
- package/dist/testing.js.map +1 -1
- package/dist/tests/utils.d.ts +6 -2
- package/dist/tests/utils.d.ts.map +1 -1
- package/dist/types.d.ts +1 -7
- package/dist/types.d.ts.map +1 -1
- package/package.json +3 -2
- package/src/auth/DemoAuth.ts +1 -2
- package/src/auth/PassphraseAuth.ts +1 -1
- package/src/coValues/CoValueBase.ts +83 -0
- package/src/coValues/account.ts +53 -43
- package/src/coValues/coFeed.ts +65 -83
- package/src/coValues/coList.ts +28 -21
- package/src/coValues/coMap.ts +54 -38
- package/src/coValues/coPlainText.ts +4 -1
- package/src/coValues/deepLoading.ts +46 -36
- package/src/coValues/extensions/imageDef.ts +21 -19
- package/src/coValues/group.ts +37 -38
- package/src/coValues/inbox.ts +24 -11
- package/src/coValues/interfaces.ts +29 -93
- package/src/coValues/profile.ts +12 -13
- package/src/coValues/registeredSchemas.ts +1 -3
- package/src/coValues/schemaUnion.ts +6 -6
- package/src/exports.ts +47 -25
- package/src/implementation/activeAccountContext.ts +1 -1
- package/src/implementation/createContext.ts +39 -24
- package/src/implementation/invites.ts +15 -12
- package/src/implementation/refs.ts +6 -4
- package/src/implementation/schema.ts +22 -34
- package/src/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +101 -0
- package/src/implementation/zodSchema/runtimeConverters/zodSchemaToCoSchema.ts +191 -0
- package/src/implementation/zodSchema/schemaTypes/AccountSchema.ts +102 -0
- package/src/implementation/zodSchema/schemaTypes/CoFeedSchema.ts +70 -0
- package/src/implementation/zodSchema/schemaTypes/CoListSchema.ts +59 -0
- package/src/implementation/zodSchema/schemaTypes/CoMapSchema.ts +126 -0
- package/src/implementation/zodSchema/schemaTypes/CoRecordSchema.ts +98 -0
- package/src/implementation/zodSchema/schemaTypes/FileStreamSchema.ts +9 -0
- package/src/implementation/zodSchema/schemaTypes/PlainTextSchema.ts +27 -0
- package/src/implementation/zodSchema/schemaTypes/RichTextSchema.ts +25 -0
- package/src/implementation/zodSchema/typeConverters/InstanceOfSchema.ts +61 -0
- package/src/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +77 -0
- package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +90 -0
- package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +103 -0
- package/src/implementation/zodSchema/unionUtils.ts +139 -0
- package/src/implementation/zodSchema/zodCo.ts +409 -0
- package/src/implementation/zodSchema/zodSchema.ts +116 -0
- package/src/internal.ts +38 -0
- package/src/subscribe/SubscriptionScope.ts +30 -3
- package/src/subscribe/utils.ts +7 -2
- package/src/testing.ts +14 -16
- package/src/tests/ContextManager.test.ts +73 -47
- package/src/tests/DemoAuth.test.ts +1 -1
- package/src/tests/account.test.ts +6 -9
- package/src/tests/coFeed.test.ts +102 -63
- package/src/tests/coList.test.ts +82 -95
- package/src/tests/coMap.record.test.ts +53 -87
- package/src/tests/coMap.test.ts +297 -312
- package/src/tests/coPlainText.test.ts +19 -39
- package/src/tests/createContext.test.ts +33 -15
- package/src/tests/deepLoading.test.ts +397 -131
- package/src/tests/groupsAndAccounts.test.ts +81 -72
- package/src/tests/imageDef.test.ts +22 -13
- package/src/tests/inbox.test.ts +36 -29
- package/src/tests/load.test.ts +10 -10
- package/src/tests/patterns/requestToJoin.test.ts +31 -31
- package/src/tests/schema.test.ts +37 -38
- package/src/tests/schemaUnion.test.ts +54 -64
- package/src/tests/subscribe.test.ts +118 -116
- package/src/tests/testing.test.ts +33 -33
- package/src/tests/utils.ts +3 -2
- package/src/types.ts +1 -8
- package/dist/chunk-LMV6J7GN.js.map +0 -1
@@ -1,27 +1,26 @@
|
|
1
1
|
import { assert, describe, expect, test } from "vitest";
|
2
|
-
import { Account, CoList, CoMap, Group, ID, co } from "../../exports";
|
2
|
+
import { Account, CoList, CoMap, Group, ID, co, z } from "../../exports";
|
3
3
|
import { createJazzTestAccount, linkAccounts } from "../../testing";
|
4
4
|
|
5
|
-
|
6
|
-
account
|
7
|
-
status
|
8
|
-
}
|
9
|
-
|
10
|
-
class ProjectsList extends CoList.Of(co.string) {}
|
5
|
+
const RequestToJoin = co.map({
|
6
|
+
account: Account,
|
7
|
+
status: z.literal(["pending", "approved", "rejected"]),
|
8
|
+
});
|
11
9
|
|
12
|
-
|
10
|
+
const RequestsMap = co.record(z.string(), RequestToJoin);
|
13
11
|
|
14
|
-
|
15
|
-
|
16
|
-
)
|
12
|
+
const RequestsStatus = co.record(
|
13
|
+
z.string(),
|
14
|
+
z.literal(["pending", "approved", "rejected"]),
|
15
|
+
);
|
17
16
|
|
18
|
-
|
19
|
-
name
|
20
|
-
requests
|
21
|
-
statuses
|
22
|
-
projects
|
23
|
-
mainGroup
|
24
|
-
}
|
17
|
+
const Organization = co.map({
|
18
|
+
name: z.string(),
|
19
|
+
requests: RequestsMap,
|
20
|
+
statuses: RequestsStatus,
|
21
|
+
projects: co.list(z.string()),
|
22
|
+
mainGroup: Group,
|
23
|
+
});
|
25
24
|
|
26
25
|
async function setup() {
|
27
26
|
const admin1 = await createJazzTestAccount();
|
@@ -57,7 +56,7 @@ async function setup() {
|
|
57
56
|
requests: RequestsMap.create({}, requestsGroup),
|
58
57
|
|
59
58
|
// To simulate the resource to share
|
60
|
-
projects:
|
59
|
+
projects: co.list(z.string()).create([], organizationGroup),
|
61
60
|
|
62
61
|
// Statuses are private to admins
|
63
62
|
// Used to make the requests statues readable only to them
|
@@ -83,10 +82,7 @@ async function setup() {
|
|
83
82
|
};
|
84
83
|
}
|
85
84
|
|
86
|
-
async function sendRequestToJoin(
|
87
|
-
organizationId: ID<Organization>,
|
88
|
-
account: Account,
|
89
|
-
) {
|
85
|
+
async function sendRequestToJoin(organizationId: string, account: Account) {
|
90
86
|
const organization = await Organization.load(organizationId, {
|
91
87
|
resolve: { requests: true },
|
92
88
|
loadAs: account,
|
@@ -112,7 +108,7 @@ async function sendRequestToJoin(
|
|
112
108
|
}
|
113
109
|
|
114
110
|
async function approveRequest(
|
115
|
-
organizationId:
|
111
|
+
organizationId: string,
|
116
112
|
admin: Account,
|
117
113
|
user: Account,
|
118
114
|
) {
|
@@ -147,7 +143,7 @@ async function approveRequest(
|
|
147
143
|
}
|
148
144
|
|
149
145
|
async function rejectRequest(
|
150
|
-
organizationId:
|
146
|
+
organizationId: string,
|
151
147
|
admin: Account,
|
152
148
|
user: Account,
|
153
149
|
) {
|
@@ -198,9 +194,11 @@ describe("Request to join", () => {
|
|
198
194
|
|
199
195
|
await approveRequest(organizationId, admin1, user1);
|
200
196
|
|
201
|
-
const projectsOnUser = await
|
202
|
-
|
203
|
-
|
197
|
+
const projectsOnUser = await co
|
198
|
+
.list(z.string())
|
199
|
+
.load(organization.projects.id, {
|
200
|
+
loadAs: user1,
|
201
|
+
});
|
204
202
|
|
205
203
|
assert(projectsOnUser);
|
206
204
|
|
@@ -226,9 +224,11 @@ describe("Request to join", () => {
|
|
226
224
|
await sendRequestToJoin(organizationId, user1);
|
227
225
|
await rejectRequest(organizationId, admin1, user1);
|
228
226
|
|
229
|
-
const projectsOnUser = await
|
230
|
-
|
231
|
-
|
227
|
+
const projectsOnUser = await co
|
228
|
+
.list(z.string())
|
229
|
+
.load(organization.projects.id, {
|
230
|
+
loadAs: user1,
|
231
|
+
});
|
232
232
|
|
233
233
|
expect(projectsOnUser).toBeNull();
|
234
234
|
});
|
package/src/tests/schema.test.ts
CHANGED
@@ -1,19 +1,18 @@
|
|
1
1
|
import { describe, expectTypeOf, it } from "vitest";
|
2
|
-
import { CoMap,
|
3
|
-
import { co as valueWithCoMarker } from "../internal.js";
|
2
|
+
import { CoMap, coField } from "../index.js";
|
4
3
|
|
5
|
-
describe("
|
4
|
+
describe("coField.json TypeScript validation", () => {
|
6
5
|
it("should accept serializable types", async () => {
|
7
6
|
type ValidType = { str: string; num: number; bool: boolean };
|
8
7
|
|
9
8
|
class ValidPrimitiveMap extends CoMap {
|
10
|
-
data =
|
9
|
+
data = coField.json<ValidType>();
|
11
10
|
}
|
12
11
|
|
13
12
|
expectTypeOf(ValidPrimitiveMap.create<ValidPrimitiveMap>)
|
14
13
|
.parameter(0)
|
15
14
|
.toEqualTypeOf<{
|
16
|
-
data:
|
15
|
+
data: ValidType;
|
17
16
|
}>();
|
18
17
|
});
|
19
18
|
|
@@ -27,13 +26,13 @@ describe("co.json TypeScript validation", () => {
|
|
27
26
|
};
|
28
27
|
|
29
28
|
class ValidNestedMap extends CoMap {
|
30
|
-
data =
|
29
|
+
data = coField.json<NestedType>();
|
31
30
|
}
|
32
31
|
|
33
32
|
expectTypeOf(ValidNestedMap.create<ValidNestedMap>)
|
34
33
|
.parameter(0)
|
35
34
|
.toEqualTypeOf<{
|
36
|
-
data:
|
35
|
+
data: NestedType;
|
37
36
|
}>();
|
38
37
|
});
|
39
38
|
|
@@ -44,13 +43,13 @@ describe("co.json TypeScript validation", () => {
|
|
44
43
|
};
|
45
44
|
|
46
45
|
class ValidMap extends CoMap {
|
47
|
-
data =
|
46
|
+
data = coField.json<TypeWithOptional>();
|
48
47
|
}
|
49
48
|
|
50
49
|
expectTypeOf(ValidMap.create<ValidMap>)
|
51
50
|
.parameter(0)
|
52
51
|
.toEqualTypeOf<{
|
53
|
-
data:
|
52
|
+
data: TypeWithOptional;
|
54
53
|
}>();
|
55
54
|
});
|
56
55
|
|
@@ -66,13 +65,13 @@ describe("co.json TypeScript validation", () => {
|
|
66
65
|
}
|
67
66
|
|
68
67
|
class ValidNestedMap extends CoMap {
|
69
|
-
data =
|
68
|
+
data = coField.json<NestedInterface>();
|
70
69
|
}
|
71
70
|
|
72
71
|
expectTypeOf(ValidNestedMap.create<ValidNestedMap>)
|
73
72
|
.parameter(0)
|
74
73
|
.toEqualTypeOf<{
|
75
|
-
data:
|
74
|
+
data: NestedInterface;
|
76
75
|
}>();
|
77
76
|
});
|
78
77
|
|
@@ -83,13 +82,13 @@ describe("co.json TypeScript validation", () => {
|
|
83
82
|
}
|
84
83
|
|
85
84
|
class ValidArrayMap extends CoMap {
|
86
|
-
data =
|
85
|
+
data = coField.json<ArrayInterface>();
|
87
86
|
}
|
88
87
|
|
89
88
|
expectTypeOf(ValidArrayMap.create<ValidArrayMap>)
|
90
89
|
.parameter(0)
|
91
90
|
.toEqualTypeOf<{
|
92
|
-
data:
|
91
|
+
data: ArrayInterface;
|
93
92
|
}>();
|
94
93
|
});
|
95
94
|
|
@@ -100,13 +99,13 @@ describe("co.json TypeScript validation", () => {
|
|
100
99
|
|
101
100
|
class InvalidFunctionMap extends CoMap {
|
102
101
|
// @ts-expect-error Should not be considered valid
|
103
|
-
data =
|
102
|
+
data = coField.json<InvalidInterface>();
|
104
103
|
}
|
105
104
|
|
106
105
|
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
107
106
|
.parameter(0)
|
108
107
|
.toEqualTypeOf<{
|
109
|
-
data:
|
108
|
+
data: InvalidInterface;
|
110
109
|
}>();
|
111
110
|
});
|
112
111
|
|
@@ -115,13 +114,13 @@ describe("co.json TypeScript validation", () => {
|
|
115
114
|
|
116
115
|
class InvalidFunctionMap extends CoMap {
|
117
116
|
// @ts-expect-error Should not be considered valid
|
118
|
-
data =
|
117
|
+
data = coField.json<InvalidType>();
|
119
118
|
}
|
120
119
|
|
121
120
|
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
122
121
|
.parameter(0)
|
123
122
|
.toEqualTypeOf<{
|
124
|
-
data:
|
123
|
+
data: InvalidType;
|
125
124
|
}>();
|
126
125
|
});
|
127
126
|
|
@@ -130,13 +129,13 @@ describe("co.json TypeScript validation", () => {
|
|
130
129
|
|
131
130
|
class InvalidFunctionMap extends CoMap {
|
132
131
|
// @ts-expect-error Should not be considered valid
|
133
|
-
data =
|
132
|
+
data = coField.json<InvalidType>();
|
134
133
|
}
|
135
134
|
|
136
135
|
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
137
136
|
.parameter(0)
|
138
137
|
.toEqualTypeOf<{
|
139
|
-
data:
|
138
|
+
data: InvalidType;
|
140
139
|
}>();
|
141
140
|
});
|
142
141
|
|
@@ -145,7 +144,7 @@ describe("co.json TypeScript validation", () => {
|
|
145
144
|
|
146
145
|
class InvalidFunctionMap extends CoMap {
|
147
146
|
// @ts-expect-error Should not be considered valid
|
148
|
-
data =
|
147
|
+
data = coField.json<InvalidType>();
|
149
148
|
}
|
150
149
|
});
|
151
150
|
|
@@ -159,16 +158,16 @@ describe("co.json TypeScript validation", () => {
|
|
159
158
|
};
|
160
159
|
|
161
160
|
class MapWithOptionalJSON extends CoMap {
|
162
|
-
data =
|
161
|
+
data = coField.optional.json<ValidType>();
|
163
162
|
// @ts-expect-error Should not be considered valid
|
164
|
-
data2 =
|
163
|
+
data2 = coField.optional.json<InvalidType>();
|
165
164
|
}
|
166
165
|
|
167
166
|
expectTypeOf(MapWithOptionalJSON.create<MapWithOptionalJSON>)
|
168
167
|
.parameter(0)
|
169
168
|
.toEqualTypeOf<{
|
170
|
-
data?:
|
171
|
-
data2?:
|
169
|
+
data?: ValidType | null;
|
170
|
+
data2?: InvalidType | null;
|
172
171
|
}>();
|
173
172
|
});
|
174
173
|
|
@@ -188,58 +187,58 @@ describe("co.json TypeScript validation", () => {
|
|
188
187
|
}
|
189
188
|
|
190
189
|
class MapWithOptionalJSON extends CoMap {
|
191
|
-
data1 =
|
192
|
-
data2 =
|
193
|
-
data3 =
|
190
|
+
data1 = coField.optional.json<ValidInterface0>();
|
191
|
+
data2 = coField.optional.json<ValidInterface1>();
|
192
|
+
data3 = coField.optional.json<InterfaceWithOptionalTypes>();
|
194
193
|
}
|
195
194
|
|
196
195
|
expectTypeOf(MapWithOptionalJSON.create<MapWithOptionalJSON>)
|
197
196
|
.parameter(0)
|
198
197
|
.toEqualTypeOf<{
|
199
|
-
data1?:
|
200
|
-
data2?:
|
201
|
-
data3?:
|
198
|
+
data1?: ValidInterface0 | null;
|
199
|
+
data2?: ValidInterface1 | null;
|
200
|
+
data3?: InterfaceWithOptionalTypes | null;
|
202
201
|
}>();
|
203
202
|
});
|
204
203
|
|
205
204
|
it("should not accept functions", async () => {
|
206
205
|
class InvalidFunctionMap extends CoMap {
|
207
206
|
// @ts-expect-error Should not be considered valid
|
208
|
-
data =
|
207
|
+
data = coField.json<() => void>();
|
209
208
|
}
|
210
209
|
});
|
211
210
|
|
212
211
|
it("should not accept functions in nested properties", async () => {
|
213
212
|
class InvalidFunctionMap extends CoMap {
|
214
213
|
// @ts-expect-error Should not be considered valid
|
215
|
-
data =
|
214
|
+
data = coField.json<{ func: () => void }>();
|
216
215
|
}
|
217
216
|
});
|
218
217
|
|
219
218
|
it("should not accept RegExp", async () => {
|
220
219
|
class InvalidFunctionMap extends CoMap {
|
221
220
|
// @ts-expect-error Should not be considered valid
|
222
|
-
data =
|
221
|
+
data = coField.json<RegExp>();
|
223
222
|
}
|
224
223
|
|
225
224
|
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
226
225
|
.parameter(0)
|
227
226
|
.toEqualTypeOf<{
|
228
|
-
data:
|
227
|
+
data: RegExp;
|
229
228
|
}>();
|
230
229
|
});
|
231
230
|
|
232
231
|
it("should accept strings and numbers", async () => {
|
233
232
|
class InvalidFunctionMap extends CoMap {
|
234
|
-
str =
|
235
|
-
num =
|
233
|
+
str = coField.json<string>();
|
234
|
+
num = coField.json<number>();
|
236
235
|
}
|
237
236
|
|
238
237
|
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
239
238
|
.parameter(0)
|
240
239
|
.toEqualTypeOf<{
|
241
|
-
str:
|
242
|
-
num:
|
240
|
+
str: string;
|
241
|
+
num: number;
|
243
242
|
}>();
|
244
243
|
});
|
245
244
|
});
|
@@ -1,67 +1,47 @@
|
|
1
1
|
import { WasmCrypto } from "cojson/crypto/WasmCrypto";
|
2
|
-
import { beforeAll, describe, expect, it } from "vitest";
|
3
|
-
import { SchemaUnion } from "../coValues/schemaUnion.js";
|
2
|
+
import { assert, beforeAll, describe, expect, it } from "vitest";
|
4
3
|
import {
|
5
4
|
Account,
|
6
|
-
CoMap,
|
7
5
|
CryptoProvider,
|
6
|
+
Loaded,
|
8
7
|
co,
|
9
8
|
loadCoValue,
|
10
9
|
subscribeToCoValue,
|
10
|
+
z,
|
11
|
+
zodSchemaToCoSchema,
|
11
12
|
} from "../exports.js";
|
12
13
|
|
13
|
-
|
14
|
-
type
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
type = co.literal("button");
|
19
|
-
color = co.literal("red");
|
20
|
-
label = co.string;
|
21
|
-
}
|
22
|
-
|
23
|
-
class BlueButtonWidget extends BaseWidget {
|
24
|
-
type = co.literal("button");
|
25
|
-
color = co.literal("blue");
|
26
|
-
label = co.string;
|
27
|
-
}
|
14
|
+
const RedButtonWidget = co.map({
|
15
|
+
type: z.literal("button"),
|
16
|
+
color: z.literal("red"),
|
17
|
+
label: z.string(),
|
18
|
+
});
|
28
19
|
|
29
|
-
const
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
return BlueButtonWidget;
|
35
|
-
default:
|
36
|
-
throw new Error(`Unknown button color: ${raw.get("color")}`);
|
37
|
-
}
|
20
|
+
const BlueButtonWidget = co.map({
|
21
|
+
type: z.literal("button"),
|
22
|
+
color: z.literal("blue"),
|
23
|
+
label: z.string(),
|
24
|
+
blueness: z.number(),
|
38
25
|
});
|
39
26
|
|
40
|
-
|
41
|
-
type = co.literal("slider");
|
42
|
-
min = co.number;
|
43
|
-
max = co.number;
|
44
|
-
}
|
27
|
+
const ButtonWidget = z.discriminatedUnion([RedButtonWidget, BlueButtonWidget]);
|
45
28
|
|
46
|
-
|
47
|
-
type
|
48
|
-
|
49
|
-
|
29
|
+
const SliderWidget = co.map({
|
30
|
+
type: z.literal("slider"),
|
31
|
+
min: z.number(),
|
32
|
+
max: z.number(),
|
33
|
+
});
|
50
34
|
|
51
|
-
const
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
case "slider":
|
56
|
-
return SliderWidget;
|
57
|
-
case "checkbox":
|
58
|
-
return CheckboxWidget;
|
59
|
-
default:
|
60
|
-
throw new Error(`Unknown widget type: ${raw.get("type")}`);
|
61
|
-
}
|
62
|
-
};
|
35
|
+
const CheckboxWidget = co.map({
|
36
|
+
type: z.literal("checkbox"),
|
37
|
+
checked: z.boolean(),
|
38
|
+
});
|
63
39
|
|
64
|
-
|
40
|
+
const WidgetUnion = z.discriminatedUnion([
|
41
|
+
ButtonWidget,
|
42
|
+
SliderWidget,
|
43
|
+
CheckboxWidget,
|
44
|
+
]);
|
65
45
|
|
66
46
|
describe("SchemaUnion", () => {
|
67
47
|
let Crypto: CryptoProvider;
|
@@ -75,7 +55,7 @@ describe("SchemaUnion", () => {
|
|
75
55
|
});
|
76
56
|
});
|
77
57
|
|
78
|
-
it("should instantiate the correct
|
58
|
+
it("should instantiate the correct schema based on schema and provided data", async () => {
|
79
59
|
const buttonWidget = RedButtonWidget.create(
|
80
60
|
{ type: "button", color: "red", label: "Submit" },
|
81
61
|
{ owner: me },
|
@@ -89,36 +69,46 @@ describe("SchemaUnion", () => {
|
|
89
69
|
{ owner: me },
|
90
70
|
);
|
91
71
|
|
92
|
-
const loadedButtonWidget = await loadCoValue(
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
72
|
+
const loadedButtonWidget = await loadCoValue(
|
73
|
+
zodSchemaToCoSchema(WidgetUnion),
|
74
|
+
buttonWidget.id,
|
75
|
+
{
|
76
|
+
loadAs: me,
|
77
|
+
},
|
78
|
+
);
|
79
|
+
const loadedSliderWidget = await loadCoValue(
|
80
|
+
zodSchemaToCoSchema(WidgetUnion),
|
81
|
+
sliderWidget.id,
|
82
|
+
{
|
83
|
+
loadAs: me,
|
84
|
+
},
|
85
|
+
);
|
98
86
|
const loadedCheckboxWidget = await loadCoValue(
|
99
|
-
WidgetUnion,
|
87
|
+
zodSchemaToCoSchema(WidgetUnion),
|
100
88
|
checkboxWidget.id,
|
101
89
|
{ loadAs: me },
|
102
90
|
);
|
103
91
|
|
104
|
-
expect(loadedButtonWidget).
|
105
|
-
expect(loadedSliderWidget).
|
106
|
-
expect(loadedCheckboxWidget).
|
92
|
+
expect(loadedButtonWidget?.type).toBe("button");
|
93
|
+
expect(loadedSliderWidget?.type).toBe("slider");
|
94
|
+
expect(loadedCheckboxWidget?.type).toBe("checkbox");
|
107
95
|
});
|
108
96
|
|
109
97
|
it("should integrate with subscribeToCoValue correctly", async () => {
|
110
98
|
const buttonWidget = BlueButtonWidget.create(
|
111
|
-
{ type: "button", color: "blue", label: "Submit" },
|
99
|
+
{ type: "button", color: "blue", label: "Submit", blueness: 100 },
|
112
100
|
{ owner: me },
|
113
101
|
);
|
114
102
|
let currentValue = "Submit";
|
115
103
|
const unsubscribe = subscribeToCoValue(
|
116
|
-
WidgetUnion,
|
104
|
+
zodSchemaToCoSchema(WidgetUnion),
|
117
105
|
buttonWidget.id,
|
118
106
|
{ loadAs: me, syncResolution: true },
|
119
|
-
(value:
|
120
|
-
if (value
|
107
|
+
(value: Loaded<typeof WidgetUnion>) => {
|
108
|
+
if (value.type === "button") {
|
121
109
|
expect(value.label).toBe(currentValue);
|
110
|
+
assert(value.color === "blue");
|
111
|
+
expect(value.blueness).toBe(100);
|
122
112
|
} else {
|
123
113
|
throw new Error("Unexpected widget type");
|
124
114
|
}
|