jazz-tools 0.8.15 → 0.8.17
Sign up to get free protection for your applications and to get access to all the features.
- package/CHANGELOG.md +283 -269
- package/dist/native/coValues/account.js +3 -6
- package/dist/native/coValues/account.js.map +1 -1
- package/dist/native/coValues/coList.js +3 -7
- package/dist/native/coValues/coList.js.map +1 -1
- package/dist/native/coValues/coMap.js +8 -14
- package/dist/native/coValues/coMap.js.map +1 -1
- package/dist/native/coValues/coStream.js +7 -12
- package/dist/native/coValues/coStream.js.map +1 -1
- package/dist/native/coValues/deepLoading.js +6 -3
- package/dist/native/coValues/deepLoading.js.map +1 -1
- package/dist/native/coValues/extensions/imageDef.js.map +1 -1
- package/dist/native/coValues/group.js +3 -6
- package/dist/native/coValues/group.js.map +1 -1
- package/dist/native/coValues/interfaces.js +4 -3
- package/dist/native/coValues/interfaces.js.map +1 -1
- package/dist/native/exports.js +3 -9
- package/dist/native/exports.js.map +1 -1
- package/dist/native/implementation/createContext.js +1 -2
- package/dist/native/implementation/createContext.js.map +1 -1
- package/dist/native/implementation/devtoolsFormatters.js +5 -25
- package/dist/native/implementation/devtoolsFormatters.js.map +1 -1
- package/dist/native/implementation/refs.js +1 -2
- package/dist/native/implementation/refs.js.map +1 -1
- package/dist/native/implementation/schema.js +1 -1
- package/dist/native/implementation/schema.js.map +1 -1
- package/dist/native/implementation/subscriptionScope.js +2 -4
- package/dist/native/implementation/subscriptionScope.js.map +1 -1
- package/dist/native/index.native.js +1 -1
- package/dist/native/index.native.js.map +1 -1
- package/dist/native/lib/cache.js.map +1 -1
- package/dist/native/lib/cache.test.js +1 -1
- package/dist/native/lib/cache.test.js.map +1 -1
- package/dist/web/coValues/account.js +3 -6
- package/dist/web/coValues/account.js.map +1 -1
- package/dist/web/coValues/coList.js +3 -7
- package/dist/web/coValues/coList.js.map +1 -1
- package/dist/web/coValues/coMap.js +8 -14
- package/dist/web/coValues/coMap.js.map +1 -1
- package/dist/web/coValues/coStream.js +7 -12
- package/dist/web/coValues/coStream.js.map +1 -1
- package/dist/web/coValues/deepLoading.js +6 -3
- package/dist/web/coValues/deepLoading.js.map +1 -1
- package/dist/web/coValues/extensions/imageDef.js.map +1 -1
- package/dist/web/coValues/group.js +3 -6
- package/dist/web/coValues/group.js.map +1 -1
- package/dist/web/coValues/interfaces.js +4 -3
- package/dist/web/coValues/interfaces.js.map +1 -1
- package/dist/web/exports.js +3 -9
- package/dist/web/exports.js.map +1 -1
- package/dist/web/implementation/createContext.js +1 -2
- package/dist/web/implementation/createContext.js.map +1 -1
- package/dist/web/implementation/devtoolsFormatters.js +5 -25
- package/dist/web/implementation/devtoolsFormatters.js.map +1 -1
- package/dist/web/implementation/refs.js +1 -2
- package/dist/web/implementation/refs.js.map +1 -1
- package/dist/web/implementation/schema.js +1 -1
- package/dist/web/implementation/schema.js.map +1 -1
- package/dist/web/implementation/subscriptionScope.js +2 -4
- package/dist/web/implementation/subscriptionScope.js.map +1 -1
- package/dist/web/lib/cache.js.map +1 -1
- package/dist/web/lib/cache.test.js +1 -1
- package/dist/web/lib/cache.test.js.map +1 -1
- package/package.json +5 -9
- package/src/coValues/account.ts +330 -339
- package/src/coValues/coList.ts +474 -495
- package/src/coValues/coMap.ts +584 -604
- package/src/coValues/coStream.ts +624 -650
- package/src/coValues/deepLoading.ts +184 -200
- package/src/coValues/extensions/imageDef.ts +44 -44
- package/src/coValues/group.ts +196 -210
- package/src/coValues/interfaces.ts +197 -199
- package/src/exports.ts +38 -26
- package/src/implementation/createContext.ts +206 -213
- package/src/implementation/devtoolsFormatters.ts +80 -100
- package/src/implementation/refs.ts +127 -139
- package/src/implementation/schema.ts +124 -128
- package/src/implementation/subscriptionScope.ts +111 -121
- package/src/index.native.ts +3 -3
- package/src/lib/cache.test.ts +48 -48
- package/src/lib/cache.ts +9 -9
- package/src/tests/coList.test.ts +264 -283
- package/src/tests/coMap.test.ts +741 -761
- package/src/tests/coStream.test.ts +405 -438
- package/src/tests/deepLoading.test.ts +251 -256
- package/src/tests/groupsAndAccounts.test.ts +70 -74
- package/src/tests/schema.test.ts +198 -198
- package/src/tests/subscribe.test.ts +312 -299
- package/tsconfig.json +2 -4
- package/tsconfig.native.json +4 -10
- package/tsconfig.web.json +4 -10
- package/.eslintrc.cjs +0 -24
- package/.prettierrc.js +0 -9
@@ -1,91 +1,87 @@
|
|
1
|
-
import {
|
2
|
-
import { Account, CoMap,
|
1
|
+
import { describe, expect, test } from "vitest";
|
2
|
+
import { Account, CoMap, Group, WasmCrypto, co } from "../index.web.js";
|
3
3
|
|
4
4
|
const Crypto = await WasmCrypto.create();
|
5
5
|
|
6
6
|
describe("Custom accounts and groups", async () => {
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
class CustomProfile extends CoMap {
|
8
|
+
name = co.string;
|
9
|
+
color = co.string;
|
10
|
+
}
|
11
|
+
|
12
|
+
class CustomAccount extends Account {
|
13
|
+
profile = co.ref(CustomProfile);
|
14
|
+
root = co.ref(CoMap);
|
15
|
+
|
16
|
+
migrate(this: CustomAccount, creationProps?: { name: string }) {
|
17
|
+
if (creationProps) {
|
18
|
+
const profileGroup = Group.create({ owner: this });
|
19
|
+
profileGroup.addMember("everyone", "reader");
|
20
|
+
this.profile = CustomProfile.create(
|
21
|
+
{ name: creationProps.name, color: "blue" },
|
22
|
+
{ owner: this },
|
23
|
+
);
|
24
|
+
}
|
10
25
|
}
|
26
|
+
}
|
11
27
|
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
profileGroup.addMember("everyone", "reader");
|
20
|
-
this.profile = CustomProfile.create(
|
21
|
-
{ name: creationProps.name, color: "blue" },
|
22
|
-
{ owner: this },
|
23
|
-
);
|
24
|
-
}
|
25
|
-
}
|
28
|
+
class CustomGroup extends Group {
|
29
|
+
profile = co.null;
|
30
|
+
root = co.null;
|
31
|
+
[co.members] = co.ref(CustomAccount);
|
32
|
+
|
33
|
+
get nMembers() {
|
34
|
+
return this.members.length;
|
26
35
|
}
|
36
|
+
}
|
27
37
|
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
38
|
+
test("Custom account and group", async () => {
|
39
|
+
const me = await CustomAccount.create({
|
40
|
+
creationProps: { name: "Hermes Puggington" },
|
41
|
+
crypto: Crypto,
|
42
|
+
});
|
32
43
|
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
}
|
44
|
+
expect(me.profile).toBeDefined();
|
45
|
+
expect(me.profile?.name).toBe("Hermes Puggington");
|
46
|
+
expect(me.profile?.color).toBe("blue");
|
37
47
|
|
38
|
-
|
39
|
-
|
40
|
-
creationProps: { name: "Hermes Puggington" },
|
41
|
-
crypto: Crypto,
|
42
|
-
});
|
48
|
+
const group = new CustomGroup({ owner: me });
|
49
|
+
group.addMember("everyone", "reader");
|
43
50
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
const group = new CustomGroup({ owner: me });
|
49
|
-
group.addMember("everyone", "reader");
|
50
|
-
|
51
|
-
expect(group.members).toMatchObject([
|
52
|
-
{ id: me.id, role: "admin" },
|
53
|
-
{ id: "everyone", role: "reader" },
|
54
|
-
]);
|
55
|
-
|
56
|
-
expect(group.nMembers).toBe(2);
|
57
|
-
|
58
|
-
await new Promise<void>((resolve) => {
|
59
|
-
group.subscribe({}, (update) => {
|
60
|
-
const meAsMember = update.members.find((member) => {
|
61
|
-
return member.id === me.id && member.account?.profile;
|
62
|
-
});
|
63
|
-
if (meAsMember) {
|
64
|
-
expect(meAsMember.account?.profile?.name).toBe(
|
65
|
-
"Hermes Puggington",
|
66
|
-
);
|
67
|
-
expect(meAsMember.account?.profile?.color).toBe("blue");
|
68
|
-
resolve();
|
69
|
-
}
|
70
|
-
});
|
71
|
-
});
|
51
|
+
expect(group.members).toMatchObject([
|
52
|
+
{ id: me.id, role: "admin" },
|
53
|
+
{ id: "everyone", role: "reader" },
|
54
|
+
]);
|
72
55
|
|
73
|
-
|
74
|
-
|
56
|
+
expect(group.nMembers).toBe(2);
|
57
|
+
|
58
|
+
await new Promise<void>((resolve) => {
|
59
|
+
group.subscribe({}, (update) => {
|
60
|
+
const meAsMember = update.members.find((member) => {
|
61
|
+
return member.id === me.id && member.account?.profile;
|
62
|
+
});
|
63
|
+
if (meAsMember) {
|
64
|
+
expect(meAsMember.account?.profile?.name).toBe("Hermes Puggington");
|
65
|
+
expect(meAsMember.account?.profile?.color).toBe("blue");
|
66
|
+
resolve();
|
75
67
|
}
|
68
|
+
});
|
69
|
+
});
|
76
70
|
|
77
|
-
|
71
|
+
class MyMap extends CoMap {
|
72
|
+
name = co.string;
|
73
|
+
}
|
78
74
|
|
79
|
-
|
80
|
-
.castAs(CustomGroup)
|
81
|
-
.members.find((member) => member.id === me.id);
|
82
|
-
expect(meAsCastMember?.account?.profile?.name).toBe(
|
83
|
-
"Hermes Puggington",
|
84
|
-
);
|
85
|
-
expect(meAsCastMember?.account?.profile?.color).toBe("blue");
|
75
|
+
const map = MyMap.create({ name: "test" }, { owner: group });
|
86
76
|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
77
|
+
const meAsCastMember = map._owner
|
78
|
+
.castAs(CustomGroup)
|
79
|
+
.members.find((member) => member.id === me.id);
|
80
|
+
expect(meAsCastMember?.account?.profile?.name).toBe("Hermes Puggington");
|
81
|
+
expect(meAsCastMember?.account?.profile?.color).toBe("blue");
|
82
|
+
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
84
|
+
expect((map._owner as any).nMembers).toBeUndefined();
|
85
|
+
expect(map._owner.castAs(CustomGroup).nMembers).toBe(2);
|
86
|
+
});
|
91
87
|
});
|
package/src/tests/schema.test.ts
CHANGED
@@ -3,203 +3,203 @@ import { CoMap, co } from "../index.web.js";
|
|
3
3
|
import { co as valueWithCoMarker } from "../internal.js";
|
4
4
|
|
5
5
|
describe("co.json TypeScript validation", () => {
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
};
|
26
|
-
};
|
27
|
-
}
|
28
|
-
|
29
|
-
class ValidNestedMap extends CoMap {
|
30
|
-
data = co.json<NestedType>();
|
31
|
-
}
|
32
|
-
|
33
|
-
expectTypeOf(ValidNestedMap.create<ValidNestedMap>)
|
34
|
-
.parameter(0)
|
35
|
-
.toEqualTypeOf<{
|
36
|
-
data: valueWithCoMarker<NestedType>;
|
37
|
-
}>();
|
38
|
-
});
|
39
|
-
|
40
|
-
it("should accept types with optional attributes", async () => {
|
41
|
-
type TypeWithOptional = {
|
42
|
-
value: string;
|
43
|
-
optional?: string | null;
|
44
|
-
}
|
45
|
-
|
46
|
-
class ValidMap extends CoMap {
|
47
|
-
data = co.json<TypeWithOptional>();
|
48
|
-
}
|
49
|
-
|
50
|
-
expectTypeOf(ValidMap.create<ValidMap>)
|
51
|
-
.parameter(0)
|
52
|
-
.toEqualTypeOf<{
|
53
|
-
data: valueWithCoMarker<TypeWithOptional>;
|
54
|
-
}>();
|
55
|
-
});
|
56
|
-
|
57
|
-
it("should accept nested serializable interfaces", async () => {
|
58
|
-
interface InnerInterface {
|
59
|
-
value: string;
|
60
|
-
}
|
61
|
-
|
62
|
-
interface NestedInterface {
|
63
|
-
outer: {
|
64
|
-
inner: InnerInterface;
|
65
|
-
};
|
66
|
-
}
|
67
|
-
|
68
|
-
class ValidNestedMap extends CoMap {
|
69
|
-
data = co.json<NestedInterface>();
|
70
|
-
}
|
71
|
-
|
72
|
-
expectTypeOf(ValidNestedMap.create<ValidNestedMap>)
|
73
|
-
.parameter(0)
|
74
|
-
.toEqualTypeOf<{
|
75
|
-
data: valueWithCoMarker<NestedInterface>;
|
76
|
-
}>();
|
77
|
-
});
|
78
|
-
|
79
|
-
it("should accept arrays of serializable types", async () => {
|
80
|
-
interface ArrayInterface {
|
81
|
-
numbers: number[];
|
82
|
-
objects: { id: number; name: string }[];
|
83
|
-
}
|
84
|
-
|
85
|
-
class ValidArrayMap extends CoMap {
|
86
|
-
data = co.json<ArrayInterface>();
|
87
|
-
}
|
88
|
-
|
89
|
-
expectTypeOf(ValidArrayMap.create<ValidArrayMap>)
|
90
|
-
.parameter(0)
|
91
|
-
.toEqualTypeOf<{
|
92
|
-
data: valueWithCoMarker<ArrayInterface>;
|
93
|
-
}>();
|
94
|
-
});
|
95
|
-
|
96
|
-
it("should flag interfaces with functions as invalid", async () => {
|
97
|
-
interface InvalidInterface {
|
98
|
-
func: () => void;
|
99
|
-
}
|
100
|
-
|
101
|
-
class InvalidFunctionMap extends CoMap {
|
102
|
-
// @ts-expect-error Should not be considered valid
|
103
|
-
data = co.json<InvalidInterface>();
|
104
|
-
}
|
105
|
-
|
106
|
-
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
107
|
-
.parameter(0)
|
108
|
-
.toEqualTypeOf<{
|
109
|
-
data: valueWithCoMarker<InvalidInterface>;
|
110
|
-
}>();
|
111
|
-
});
|
112
|
-
|
113
|
-
it("should flag types with functions as invalid", async () => {
|
114
|
-
type InvalidType = { func: () => void };
|
115
|
-
|
116
|
-
class InvalidFunctionMap extends CoMap {
|
117
|
-
// @ts-expect-error Should not be considered valid
|
118
|
-
data = co.json<InvalidType>();
|
119
|
-
}
|
120
|
-
|
121
|
-
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
122
|
-
.parameter(0)
|
123
|
-
.toEqualTypeOf<{
|
124
|
-
data: valueWithCoMarker<InvalidType>;
|
125
|
-
}>();
|
126
|
-
});
|
127
|
-
|
128
|
-
it("should flag types with non-serializable constructors as invalid", async () => {
|
129
|
-
type InvalidType = { date: Date; regexp: RegExp; symbol: symbol };
|
130
|
-
|
131
|
-
class InvalidFunctionMap extends CoMap {
|
132
|
-
// @ts-expect-error Should not be considered valid
|
133
|
-
data = co.json<InvalidType>();
|
134
|
-
}
|
135
|
-
|
136
|
-
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
137
|
-
.parameter(0)
|
138
|
-
.toEqualTypeOf<{
|
139
|
-
data: valueWithCoMarker<InvalidType>;
|
140
|
-
}>();
|
141
|
-
});
|
142
|
-
|
143
|
-
it("should apply the same validation to optional json", async () => {
|
144
|
-
type ValidType = {
|
145
|
-
value: string;
|
6
|
+
it("should accept serializable types", async () => {
|
7
|
+
type ValidType = { str: string; num: number; bool: boolean };
|
8
|
+
|
9
|
+
class ValidPrimitiveMap extends CoMap {
|
10
|
+
data = co.json<ValidType>();
|
11
|
+
}
|
12
|
+
|
13
|
+
expectTypeOf(ValidPrimitiveMap.create<ValidPrimitiveMap>)
|
14
|
+
.parameter(0)
|
15
|
+
.toEqualTypeOf<{
|
16
|
+
data: valueWithCoMarker<ValidType>;
|
17
|
+
}>();
|
18
|
+
});
|
19
|
+
|
20
|
+
it("should accept nested serializable types", async () => {
|
21
|
+
type NestedType = {
|
22
|
+
outer: {
|
23
|
+
inner: {
|
24
|
+
value: string;
|
146
25
|
};
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
203
|
-
|
204
|
-
}
|
26
|
+
};
|
27
|
+
};
|
28
|
+
|
29
|
+
class ValidNestedMap extends CoMap {
|
30
|
+
data = co.json<NestedType>();
|
31
|
+
}
|
32
|
+
|
33
|
+
expectTypeOf(ValidNestedMap.create<ValidNestedMap>)
|
34
|
+
.parameter(0)
|
35
|
+
.toEqualTypeOf<{
|
36
|
+
data: valueWithCoMarker<NestedType>;
|
37
|
+
}>();
|
38
|
+
});
|
39
|
+
|
40
|
+
it("should accept types with optional attributes", async () => {
|
41
|
+
type TypeWithOptional = {
|
42
|
+
value: string;
|
43
|
+
optional?: string | null;
|
44
|
+
};
|
45
|
+
|
46
|
+
class ValidMap extends CoMap {
|
47
|
+
data = co.json<TypeWithOptional>();
|
48
|
+
}
|
49
|
+
|
50
|
+
expectTypeOf(ValidMap.create<ValidMap>)
|
51
|
+
.parameter(0)
|
52
|
+
.toEqualTypeOf<{
|
53
|
+
data: valueWithCoMarker<TypeWithOptional>;
|
54
|
+
}>();
|
55
|
+
});
|
56
|
+
|
57
|
+
it("should accept nested serializable interfaces", async () => {
|
58
|
+
interface InnerInterface {
|
59
|
+
value: string;
|
60
|
+
}
|
61
|
+
|
62
|
+
interface NestedInterface {
|
63
|
+
outer: {
|
64
|
+
inner: InnerInterface;
|
65
|
+
};
|
66
|
+
}
|
67
|
+
|
68
|
+
class ValidNestedMap extends CoMap {
|
69
|
+
data = co.json<NestedInterface>();
|
70
|
+
}
|
71
|
+
|
72
|
+
expectTypeOf(ValidNestedMap.create<ValidNestedMap>)
|
73
|
+
.parameter(0)
|
74
|
+
.toEqualTypeOf<{
|
75
|
+
data: valueWithCoMarker<NestedInterface>;
|
76
|
+
}>();
|
77
|
+
});
|
78
|
+
|
79
|
+
it("should accept arrays of serializable types", async () => {
|
80
|
+
interface ArrayInterface {
|
81
|
+
numbers: number[];
|
82
|
+
objects: { id: number; name: string }[];
|
83
|
+
}
|
84
|
+
|
85
|
+
class ValidArrayMap extends CoMap {
|
86
|
+
data = co.json<ArrayInterface>();
|
87
|
+
}
|
88
|
+
|
89
|
+
expectTypeOf(ValidArrayMap.create<ValidArrayMap>)
|
90
|
+
.parameter(0)
|
91
|
+
.toEqualTypeOf<{
|
92
|
+
data: valueWithCoMarker<ArrayInterface>;
|
93
|
+
}>();
|
94
|
+
});
|
95
|
+
|
96
|
+
it("should flag interfaces with functions as invalid", async () => {
|
97
|
+
interface InvalidInterface {
|
98
|
+
func: () => void;
|
99
|
+
}
|
100
|
+
|
101
|
+
class InvalidFunctionMap extends CoMap {
|
102
|
+
// @ts-expect-error Should not be considered valid
|
103
|
+
data = co.json<InvalidInterface>();
|
104
|
+
}
|
105
|
+
|
106
|
+
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
107
|
+
.parameter(0)
|
108
|
+
.toEqualTypeOf<{
|
109
|
+
data: valueWithCoMarker<InvalidInterface>;
|
110
|
+
}>();
|
111
|
+
});
|
112
|
+
|
113
|
+
it("should flag types with functions as invalid", async () => {
|
114
|
+
type InvalidType = { func: () => void };
|
115
|
+
|
116
|
+
class InvalidFunctionMap extends CoMap {
|
117
|
+
// @ts-expect-error Should not be considered valid
|
118
|
+
data = co.json<InvalidType>();
|
119
|
+
}
|
120
|
+
|
121
|
+
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
122
|
+
.parameter(0)
|
123
|
+
.toEqualTypeOf<{
|
124
|
+
data: valueWithCoMarker<InvalidType>;
|
125
|
+
}>();
|
126
|
+
});
|
127
|
+
|
128
|
+
it("should flag types with non-serializable constructors as invalid", async () => {
|
129
|
+
type InvalidType = { date: Date; regexp: RegExp; symbol: symbol };
|
130
|
+
|
131
|
+
class InvalidFunctionMap extends CoMap {
|
132
|
+
// @ts-expect-error Should not be considered valid
|
133
|
+
data = co.json<InvalidType>();
|
134
|
+
}
|
135
|
+
|
136
|
+
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
137
|
+
.parameter(0)
|
138
|
+
.toEqualTypeOf<{
|
139
|
+
data: valueWithCoMarker<InvalidType>;
|
140
|
+
}>();
|
141
|
+
});
|
142
|
+
|
143
|
+
it("should apply the same validation to optional json", async () => {
|
144
|
+
type ValidType = {
|
145
|
+
value: string;
|
146
|
+
};
|
147
|
+
|
148
|
+
type InvalidType = {
|
149
|
+
value: () => string;
|
150
|
+
};
|
151
|
+
|
152
|
+
class MapWithOptionalJSON extends CoMap {
|
153
|
+
data = co.optional.json<ValidType>();
|
154
|
+
// @ts-expect-error Should not be considered valid
|
155
|
+
data2 = co.optional.json<InvalidType>();
|
156
|
+
}
|
157
|
+
|
158
|
+
expectTypeOf(MapWithOptionalJSON.create<MapWithOptionalJSON>)
|
159
|
+
.parameter(0)
|
160
|
+
.toEqualTypeOf<{
|
161
|
+
data?: valueWithCoMarker<ValidType> | null;
|
162
|
+
data2?: valueWithCoMarker<InvalidType> | null;
|
163
|
+
}>();
|
164
|
+
});
|
165
|
+
|
166
|
+
it("should not accept functions", async () => {
|
167
|
+
class InvalidFunctionMap extends CoMap {
|
168
|
+
// @ts-expect-error Should not be considered valid
|
169
|
+
data = co.json<() => void>();
|
170
|
+
}
|
171
|
+
|
172
|
+
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
173
|
+
.parameter(0)
|
174
|
+
.toEqualTypeOf<{
|
175
|
+
data: valueWithCoMarker<() => void>;
|
176
|
+
}>();
|
177
|
+
});
|
178
|
+
|
179
|
+
it("should not accept RegExp", async () => {
|
180
|
+
class InvalidFunctionMap extends CoMap {
|
181
|
+
// @ts-expect-error Should not be considered valid
|
182
|
+
data = co.json<RegExp>();
|
183
|
+
}
|
184
|
+
|
185
|
+
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
186
|
+
.parameter(0)
|
187
|
+
.toEqualTypeOf<{
|
188
|
+
data: valueWithCoMarker<RegExp>;
|
189
|
+
}>();
|
190
|
+
});
|
191
|
+
|
192
|
+
it("should accept strings and numbers", async () => {
|
193
|
+
class InvalidFunctionMap extends CoMap {
|
194
|
+
str = co.json<string>();
|
195
|
+
num = co.json<number>();
|
196
|
+
}
|
197
|
+
|
198
|
+
expectTypeOf(InvalidFunctionMap.create<InvalidFunctionMap>)
|
199
|
+
.parameter(0)
|
200
|
+
.toEqualTypeOf<{
|
201
|
+
str: valueWithCoMarker<string>;
|
202
|
+
num: valueWithCoMarker<number>;
|
203
|
+
}>();
|
204
|
+
});
|
205
205
|
});
|