jazz-tools 0.14.2 → 0.14.5

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 (43) hide show
  1. package/.turbo/turbo-build.log +6 -6
  2. package/CHANGELOG.md +13 -0
  3. package/dist/{chunk-WLOZKDOH.js → chunk-52PJ4QZ3.js} +29 -17
  4. package/dist/chunk-52PJ4QZ3.js.map +1 -0
  5. package/dist/coValues/extensions/imageDef.d.ts +2 -1
  6. package/dist/coValues/extensions/imageDef.d.ts.map +1 -1
  7. package/dist/implementation/zodSchema/coExport.d.ts +3 -0
  8. package/dist/implementation/zodSchema/coExport.d.ts.map +1 -0
  9. package/dist/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.d.ts.map +1 -1
  10. package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +11 -3
  11. package/dist/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts.map +1 -1
  12. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts +1 -1
  13. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.d.ts.map +1 -1
  14. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts +1 -1
  15. package/dist/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.d.ts.map +1 -1
  16. package/dist/implementation/zodSchema/zodCo.d.ts +21 -27
  17. package/dist/implementation/zodSchema/zodCo.d.ts.map +1 -1
  18. package/dist/implementation/zodSchema/zodSchema.d.ts +1 -4
  19. package/dist/implementation/zodSchema/zodSchema.d.ts.map +1 -1
  20. package/dist/index.js +3 -3
  21. package/dist/internal.d.ts +1 -0
  22. package/dist/internal.d.ts.map +1 -1
  23. package/dist/testing.js +1 -1
  24. package/dist/tests/coMap.record.test-d.d.ts +2 -0
  25. package/dist/tests/coMap.record.test-d.d.ts.map +1 -0
  26. package/dist/tests/coMap.test-d.d.ts +2 -0
  27. package/dist/tests/coMap.test-d.d.ts.map +1 -0
  28. package/package.json +1 -1
  29. package/src/coValues/extensions/imageDef.ts +7 -1
  30. package/src/implementation/zodSchema/coExport.ts +13 -0
  31. package/src/implementation/zodSchema/runtimeConverters/zodFieldToCoFieldDef.ts +3 -0
  32. package/src/implementation/zodSchema/schemaTypes/CoMapSchema.ts +20 -8
  33. package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +3 -1
  34. package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +3 -1
  35. package/src/implementation/zodSchema/zodCo.ts +9 -20
  36. package/src/implementation/zodSchema/zodSchema.ts +0 -5
  37. package/src/internal.ts +1 -0
  38. package/src/tests/coList.test.ts +9 -0
  39. package/src/tests/coMap.record.test-d.ts +149 -0
  40. package/src/tests/coMap.record.test.ts +11 -0
  41. package/src/tests/coMap.test-d.ts +421 -0
  42. package/src/tests/coMap.test.ts +8 -0
  43. package/dist/chunk-WLOZKDOH.js.map +0 -1
@@ -0,0 +1,149 @@
1
+ import { assert, describe, expectTypeOf, test } from "vitest";
2
+ import { Group, co, z } from "../exports.js";
3
+ import { Account } from "../index.js";
4
+ import { Loaded } from "../internal.js";
5
+
6
+ describe("CoMap.Record", () => {
7
+ describe("init", () => {
8
+ test("create a Record with basic property access", () => {
9
+ const Person = co.record(z.string(), z.string());
10
+
11
+ const person = Person.create({
12
+ name: "John",
13
+ age: "20",
14
+ });
15
+
16
+ type ExpectedType = {
17
+ [key: string]: string;
18
+ };
19
+
20
+ function matches(value: ExpectedType) {
21
+ return value;
22
+ }
23
+
24
+ matches(person);
25
+ });
26
+
27
+ test("has the _owner property", () => {
28
+ const Person = co.record(z.string(), z.string());
29
+
30
+ const person = Person.create({ name: "John" }, Account.getMe());
31
+
32
+ expectTypeOf(person._owner).toEqualTypeOf<Account | Group>();
33
+ });
34
+
35
+ test("Record with reference", () => {
36
+ const Dog = co.map({
37
+ name: z.string(),
38
+ breed: z.string(),
39
+ });
40
+
41
+ const Person = co.record(z.string(), Dog);
42
+
43
+ const person = Person.create({
44
+ pet1: Dog.create({ name: "Rex", breed: "Labrador" }),
45
+ pet2: Dog.create({ name: "Fido", breed: "Poodle" }),
46
+ });
47
+
48
+ type ExpectedType = {
49
+ [key: string]: Loaded<typeof Dog>;
50
+ };
51
+
52
+ function matches(value: ExpectedType) {
53
+ return value;
54
+ }
55
+
56
+ matches(person);
57
+ });
58
+
59
+ test("Record with optional reference", () => {
60
+ const Dog = co.map({
61
+ name: z.string(),
62
+ breed: z.string(),
63
+ });
64
+
65
+ const Person = co.record(z.string(), Dog.optional());
66
+
67
+ const person = Person.create({
68
+ pet1: Dog.create({ name: "Rex", breed: "Labrador" }),
69
+ pet2: undefined,
70
+ });
71
+
72
+ type ExpectedType = {
73
+ [key: string]: Loaded<typeof Dog> | undefined;
74
+ };
75
+
76
+ function matches(value: ExpectedType) {
77
+ return value;
78
+ }
79
+
80
+ matches(person);
81
+ });
82
+ });
83
+
84
+ describe("Record resolution", () => {
85
+ test("loading a record with deep resolve", async () => {
86
+ const Dog = co.map({
87
+ name: z.string(),
88
+ breed: z.string(),
89
+ });
90
+
91
+ const Person = co.record(z.string(), Dog);
92
+
93
+ const person = Person.create({
94
+ pet1: Dog.create({ name: "Rex", breed: "Labrador" }),
95
+ pet2: Dog.create({ name: "Fido", breed: "Poodle" }),
96
+ });
97
+
98
+ const loadedPerson = await Person.load(person.id, {
99
+ resolve: {
100
+ $each: true,
101
+ },
102
+ });
103
+
104
+ type ExpectedType = {
105
+ [key: string]: Loaded<typeof Dog>;
106
+ } | null;
107
+
108
+ function matches(value: ExpectedType) {
109
+ return value;
110
+ }
111
+
112
+ matches(loadedPerson);
113
+ });
114
+
115
+ test("loading a record with $onError", async () => {
116
+ const Dog = co.map({
117
+ name: z.string(),
118
+ breed: z.string(),
119
+ });
120
+
121
+ const Person = co.record(z.string(), Dog);
122
+
123
+ const person = Person.create({
124
+ pet1: Dog.create({ name: "Rex", breed: "Labrador" }),
125
+ pet2: Dog.create({ name: "Fido", breed: "Poodle" }),
126
+ });
127
+
128
+ const loadedPerson = await Person.load(person.id, {
129
+ resolve: {
130
+ $each: { $onError: null },
131
+ },
132
+ });
133
+
134
+ type ExpectedType = {
135
+ [key: string]:
136
+ | (Loaded<typeof Dog> & {
137
+ $onError: never;
138
+ })
139
+ | null;
140
+ } | null;
141
+
142
+ function matches(value: ExpectedType) {
143
+ return value;
144
+ }
145
+
146
+ matches(loadedPerson);
147
+ });
148
+ });
149
+ });
@@ -41,6 +41,17 @@ describe("CoMap.Record", async () => {
41
41
  expect(Object.keys(person)).toEqual(["name", "age"]);
42
42
  });
43
43
 
44
+ test("create a Record with enum value", () => {
45
+ const Person = co.record(z.string(), z.enum(["a", "b", "c"]));
46
+
47
+ const person = Person.create({
48
+ age: "a",
49
+ });
50
+
51
+ expect(person.age).toEqual("a");
52
+ expect(Object.keys(person)).toEqual(["age"]);
53
+ });
54
+
44
55
  test("property existence", () => {
45
56
  const Person = co.record(z.string(), z.string());
46
57
 
@@ -0,0 +1,421 @@
1
+ import { assert, describe, expectTypeOf, test } from "vitest";
2
+ import { Group, co, z } from "../exports.js";
3
+ import { Account } from "../index.js";
4
+ import {
5
+ CoListSchema,
6
+ CoMapInitZod,
7
+ Loaded,
8
+ optionalKeys,
9
+ requiredKeys,
10
+ } from "../internal.js";
11
+
12
+ describe("CoMap", async () => {
13
+ describe("init", () => {
14
+ test("create a CoMap with basic property access", () => {
15
+ const Person = co.map({
16
+ color: z.string(),
17
+ _height: z.number(),
18
+ birthday: z.date(),
19
+ name: z.string(),
20
+ enum: z.enum(["a", "b", "c"]),
21
+ enumMap: z.enum({ a: 1, b: 2, c: 3 }),
22
+ optionalDate: z.date().optional(),
23
+ });
24
+
25
+ const birthday = new Date("1989-11-27");
26
+
27
+ const john = Person.create({
28
+ color: "red",
29
+ _height: 10,
30
+ birthday,
31
+ enum: "a",
32
+ enumMap: 1,
33
+ name: "John",
34
+ });
35
+
36
+ type ExpectedType = {
37
+ color: string;
38
+ _height: number;
39
+ birthday: Date;
40
+ name: string;
41
+ enum: "a" | "b" | "c";
42
+ enumMap: 1 | 2 | 3;
43
+ optionalDate: Date | undefined;
44
+ };
45
+
46
+ function matches(value: ExpectedType) {
47
+ return value;
48
+ }
49
+
50
+ matches(john);
51
+ });
52
+
53
+ test("has the _owner property", () => {
54
+ const Person = co.map({
55
+ name: z.string(),
56
+ });
57
+
58
+ const john = Person.create({ name: "John" }, Account.getMe());
59
+
60
+ expectTypeOf(john._owner).toEqualTypeOf<Account | Group>();
61
+ });
62
+
63
+ test("CoMap with reference", () => {
64
+ const Dog = co.map({
65
+ name: z.string(),
66
+ breed: z.string(),
67
+ });
68
+
69
+ const Person = co.map({
70
+ name: z.string(),
71
+ age: z.number(),
72
+ dog: Dog,
73
+ });
74
+
75
+ const person = Person.create({
76
+ name: "John",
77
+ age: 20,
78
+ dog: Dog.create({ name: "Rex", breed: "Labrador" }),
79
+ });
80
+
81
+ type ExpectedType = {
82
+ name: string;
83
+ age: number;
84
+ dog: Loaded<typeof Dog>;
85
+ };
86
+
87
+ function matches(value: ExpectedType) {
88
+ return value;
89
+ }
90
+
91
+ matches(person);
92
+ });
93
+
94
+ test("CoMap with optional reference", () => {
95
+ const Dog = co.map({
96
+ name: z.string(),
97
+ breed: z.string(),
98
+ });
99
+
100
+ const Person = co.map({
101
+ name: z.string(),
102
+ age: z.number(),
103
+ dog: Dog.optional(),
104
+ });
105
+
106
+ const person = Person.create({
107
+ name: "John",
108
+ age: 20,
109
+ dog: Dog.create({ name: "Rex", breed: "Labrador" }),
110
+ });
111
+
112
+ type ExpectedType = {
113
+ name: string;
114
+ age: number;
115
+ dog: Loaded<typeof Dog> | undefined;
116
+ };
117
+
118
+ function matches(value: ExpectedType) {
119
+ return value;
120
+ }
121
+
122
+ matches(person);
123
+ });
124
+
125
+ test("CoMap create with partially loaded, reference and optional", () => {
126
+ const Dog = co.map({
127
+ name: z.string(),
128
+ breed: co.map({ type: z.literal("labrador"), value: z.string() }),
129
+ });
130
+ type Dog = co.loaded<typeof Dog>;
131
+
132
+ const Person = co.map({
133
+ name: z.string(),
134
+ age: z.number(),
135
+ dog: Dog.optional(),
136
+ });
137
+
138
+ const dog = Dog.create({
139
+ name: "Rex",
140
+ breed: Dog.def.shape.breed.create({
141
+ type: "labrador",
142
+ value: "Labrador",
143
+ }),
144
+ }) as Dog;
145
+
146
+ type R = requiredKeys<typeof Person.def.shape>;
147
+ type O = optionalKeys<typeof Person.def.shape>;
148
+ type I = CoMapInitZod<typeof Person.def.shape>;
149
+
150
+ const person = Person.create({
151
+ name: "John",
152
+ age: 20,
153
+ dog,
154
+ });
155
+
156
+ type ExpectedType = {
157
+ name: string;
158
+ age: number;
159
+ dog: Loaded<typeof Dog> | undefined;
160
+ };
161
+
162
+ function matches(value: ExpectedType) {
163
+ return value;
164
+ }
165
+
166
+ matches(person);
167
+ });
168
+
169
+ test("Comap with recursive optional reference", () => {
170
+ const Recursive = co.map({
171
+ get child(): z.ZodOptional<typeof Recursive> {
172
+ return z.optional(Recursive);
173
+ },
174
+ });
175
+
176
+ const child: Loaded<typeof Recursive> = Recursive.create({});
177
+ const parent = Recursive.create({
178
+ child: child,
179
+ });
180
+
181
+ type ExpectedType = {
182
+ child: Loaded<typeof Recursive> | undefined;
183
+ };
184
+
185
+ function matches(value: ExpectedType) {
186
+ return value;
187
+ }
188
+
189
+ matches(parent);
190
+ });
191
+
192
+ test("CoMap with self reference", () => {
193
+ const Person = co.map({
194
+ name: z.string(),
195
+ age: z.number(),
196
+ // TODO: would be nice if this didn't need a type annotation
197
+ get friend(): z.ZodOptional<typeof Person> {
198
+ return z.optional(Person);
199
+ },
200
+ });
201
+
202
+ const person = Person.create({
203
+ name: "John",
204
+ age: 20,
205
+ friend: Person.create({ name: "Jane", age: 21 }),
206
+ });
207
+
208
+ type ExpectedType = {
209
+ name: string;
210
+ age: number;
211
+ friend: Loaded<typeof Person> | undefined;
212
+ };
213
+
214
+ function matches(value: ExpectedType) {
215
+ return value;
216
+ }
217
+
218
+ matches(person);
219
+ });
220
+
221
+ test("should disallow extra properties", () => {
222
+ const Person = co.map({
223
+ name: z.string(),
224
+ age: z.number(),
225
+ });
226
+
227
+ // @ts-expect-error - x is not a valid property
228
+ Person.create({ name: "John", age: 30, xtra: 1 });
229
+ });
230
+ });
231
+
232
+ describe("Mutation", () => {
233
+ test("update a reference", () => {
234
+ const Dog = co.map({
235
+ name: z.string(),
236
+ });
237
+
238
+ const Person = co.map({
239
+ name: z.string(),
240
+ age: z.number(),
241
+ dog: Dog,
242
+ });
243
+
244
+ const john = Person.create({
245
+ name: "John",
246
+ age: 20,
247
+ dog: Dog.create({ name: "Rex" }),
248
+ });
249
+
250
+ john.dog = Dog.create({ name: "Fido" });
251
+ });
252
+
253
+ test("update a reference on a loaded value", () => {
254
+ const Dog = co.map({
255
+ name: z.string(),
256
+ get siblings(): CoListSchema<typeof Dog> {
257
+ return co.list(Dog);
258
+ },
259
+ });
260
+
261
+ const Person = co.map({
262
+ name: z.string(),
263
+ age: z.number(),
264
+ dog: Dog,
265
+ });
266
+
267
+ const john = Person.create({
268
+ name: "John",
269
+ age: 20,
270
+ dog: Dog.create({ name: "Rex", siblings: co.list(Dog).create([]) }),
271
+ }) as Loaded<typeof Person, { dog: { siblings: true } }>;
272
+
273
+ john.dog = Dog.create({
274
+ name: "Fido",
275
+ siblings: co.list(Dog).create([]),
276
+ });
277
+ });
278
+ });
279
+
280
+ test("Enum of maps", () => {
281
+ const ChildA = co.map({
282
+ type: z.literal("a"),
283
+ value: z.number(),
284
+ });
285
+
286
+ const ChildB = co.map({
287
+ type: z.literal("b"),
288
+ value: z.string(),
289
+ });
290
+
291
+ const MapWithEnumOfMaps = co.map({
292
+ name: z.string(),
293
+ child: z.discriminatedUnion([ChildA, ChildB]),
294
+ });
295
+
296
+ const mapWithEnum = MapWithEnumOfMaps.create({
297
+ name: "enum",
298
+ child: ChildA.create({
299
+ type: "a",
300
+ value: 5,
301
+ }),
302
+ });
303
+
304
+ type ExpectedType = {
305
+ name: string;
306
+ child: Loaded<typeof ChildA> | Loaded<typeof ChildB>;
307
+ };
308
+
309
+ function matches(value: ExpectedType) {
310
+ return value;
311
+ }
312
+
313
+ matches(mapWithEnum);
314
+
315
+ function matchesNarrowed(value: Loaded<typeof ChildA>) {
316
+ return value;
317
+ }
318
+
319
+ if (mapWithEnum.child.type === "a") {
320
+ matchesNarrowed(mapWithEnum.child);
321
+ }
322
+ });
323
+ });
324
+
325
+ describe("CoMap resolution", async () => {
326
+ test("partial loading a map with deep resolve", async () => {
327
+ const Dog = co.map({
328
+ name: z.string(),
329
+ breed: z.string(),
330
+ });
331
+
332
+ const Person = co.map({
333
+ name: z.string(),
334
+ age: z.number(),
335
+ dog1: Dog,
336
+ dog2: Dog,
337
+ });
338
+
339
+ const person = Person.create({
340
+ name: "John",
341
+ age: 20,
342
+ dog1: Dog.create({ name: "Rex", breed: "Labrador" }),
343
+ dog2: Dog.create({ name: "Fido", breed: "Poodle" }),
344
+ });
345
+
346
+ const loadedPerson = await Person.load(person.id, {
347
+ resolve: {
348
+ dog1: true,
349
+ },
350
+ });
351
+
352
+ type ExpectedType = {
353
+ name: string;
354
+ age: number;
355
+ dog1: Loaded<typeof Dog>;
356
+ dog2: Loaded<typeof Dog> | null;
357
+ } | null;
358
+
359
+ function matches(value: ExpectedType) {
360
+ return value;
361
+ }
362
+
363
+ matches(loadedPerson);
364
+
365
+ assert(loadedPerson);
366
+ expectTypeOf<typeof loadedPerson.dog1.name>().toEqualTypeOf<string>();
367
+ expectTypeOf<typeof loadedPerson.dog2>().toEqualTypeOf<Loaded<
368
+ typeof Dog
369
+ > | null>();
370
+ });
371
+
372
+ test("loading a map with deep resolve and $onError", async () => {
373
+ const Dog = co.map({
374
+ name: z.string(),
375
+ breed: z.string(),
376
+ });
377
+
378
+ const Person = co.map({
379
+ name: z.string(),
380
+ age: z.number(),
381
+ dog1: Dog,
382
+ dog2: Dog,
383
+ });
384
+
385
+ const person = Person.create({
386
+ name: "John",
387
+ age: 20,
388
+ dog1: Dog.create({ name: "Rex", breed: "Labrador" }),
389
+ dog2: Dog.create({ name: "Fido", breed: "Poodle" }),
390
+ });
391
+
392
+ const loadedPerson = await Person.load(person.id, {
393
+ resolve: {
394
+ dog1: true,
395
+ dog2: { $onError: null },
396
+ },
397
+ });
398
+
399
+ type ExpectedType = {
400
+ name: string;
401
+ age: number;
402
+ dog1: Loaded<typeof Dog>;
403
+ dog2: Loaded<typeof Dog> | null;
404
+ } | null;
405
+
406
+ function matches(value: ExpectedType) {
407
+ return value;
408
+ }
409
+
410
+ matches(loadedPerson);
411
+
412
+ assert(loadedPerson);
413
+ expectTypeOf<typeof loadedPerson.dog1.name>().toEqualTypeOf<string>();
414
+ expectTypeOf<typeof loadedPerson.dog2>().toEqualTypeOf<
415
+ | (Loaded<typeof Dog> & {
416
+ $onError: never; // TODO: Clean the $onError from the type
417
+ })
418
+ | null
419
+ >();
420
+ });
421
+ });
@@ -34,6 +34,8 @@ describe("CoMap", async () => {
34
34
  _height: z.number(),
35
35
  birthday: z.date(),
36
36
  name: z.string(),
37
+ enum: z.enum(["a", "b", "c"]),
38
+ enumMap: z.enum({ a: 1, b: 2, c: 3 }),
37
39
  // nullable: z.optional.encoded<string | undefined>({
38
40
  // encode: (value: string | undefined) => value || null,
39
41
  // decode: (value: unknown) => (value as string) || undefined,
@@ -48,6 +50,8 @@ describe("CoMap", async () => {
48
50
  _height: 10,
49
51
  birthday,
50
52
  name: "John",
53
+ enum: "a",
54
+ enumMap: 1,
51
55
  });
52
56
 
53
57
  expect(john.color).toEqual("red");
@@ -59,7 +63,11 @@ describe("CoMap", async () => {
59
63
  "_height",
60
64
  "birthday",
61
65
  "name",
66
+ "enum",
67
+ "enumMap",
62
68
  ]);
69
+ expect(john.enum).toEqual("a");
70
+ expect(john.enumMap).toEqual(1);
63
71
  });
64
72
 
65
73
  test("property existence", () => {