jazz-tools 0.15.16 → 0.16.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/.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 +46 -46
- package/CHANGELOG.md +25 -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-MLCNE3TL.js} +594 -598
- package/dist/chunk-MLCNE3TL.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 +5 -18
- 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 +2 -2
- 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 +16 -14
- package/dist/tools/implementation/zodSchema/schemaTypes/CoFeedSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts +23 -17
- package/dist/tools/implementation/zodSchema/schemaTypes/CoListSchema.d.ts.map +1 -1
- package/dist/tools/implementation/zodSchema/schemaTypes/CoMapSchema.d.ts +24 -16
- 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 +18 -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/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 +10 -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 +1 -6
- 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 +15 -13
- 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 +133 -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 +187 -137
- 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/InstanceOfSchema.ts +28 -23
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOfSchemaCoValuesNullable.ts +28 -25
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchema.ts +86 -78
- package/src/tools/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts +91 -82
- package/src/tools/implementation/zodSchema/unionUtils.ts +47 -51
- package/src/tools/implementation/zodSchema/zodCo.ts +42 -75
- package/src/tools/implementation/zodSchema/zodReExport.ts +44 -24
- package/src/tools/implementation/zodSchema/zodSchema.ts +64 -102
- package/src/tools/internal.ts +3 -2
- package/src/tools/lib/utilityTypes.ts +7 -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/coList.test-d.ts +32 -3
- package/src/tools/tests/coList.test.ts +20 -2
- package/src/tools/tests/coMap.record.test-d.ts +31 -3
- package/src/tools/tests/coMap.record.test.ts +9 -9
- package/src/tools/tests/coMap.test-d.ts +8 -8
- package/src/tools/tests/coMap.test.ts +19 -5
- 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 +50 -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
@@ -1,113 +1,121 @@
|
|
1
1
|
import { JsonValue } from "cojson";
|
2
2
|
import {
|
3
3
|
Account,
|
4
|
+
AnyZodOrCoValueSchema,
|
5
|
+
CoDiscriminatedUnionSchema,
|
4
6
|
CoFeed,
|
5
7
|
CoList,
|
6
8
|
CoMap,
|
7
9
|
CoPlainText,
|
8
10
|
CoRichText,
|
9
11
|
CoValueClass,
|
12
|
+
CoreAccountSchema,
|
13
|
+
CoreCoRecordSchema,
|
10
14
|
FileStream,
|
11
15
|
Profile,
|
12
16
|
} from "../../../internal.js";
|
13
|
-
import {
|
14
|
-
import {
|
15
|
-
import {
|
16
|
-
import {
|
17
|
-
import {
|
18
|
-
import {
|
17
|
+
import { CoreCoFeedSchema } from "../schemaTypes/CoFeedSchema.js";
|
18
|
+
import { CoreCoListSchema } from "../schemaTypes/CoListSchema.js";
|
19
|
+
import { CoreCoMapSchema } from "../schemaTypes/CoMapSchema.js";
|
20
|
+
import { CoreCoOptionalSchema } from "../schemaTypes/CoOptionalSchema.js";
|
21
|
+
import { CoreCoValueSchema } from "../schemaTypes/CoValueSchema.js";
|
22
|
+
import { CoreFileStreamSchema } from "../schemaTypes/FileStreamSchema.js";
|
23
|
+
import { CorePlainTextSchema } from "../schemaTypes/PlainTextSchema.js";
|
24
|
+
import { CoreRichTextSchema } from "../schemaTypes/RichTextSchema.js";
|
19
25
|
import { z } from "../zodReExport.js";
|
20
26
|
|
21
27
|
export type InstanceOrPrimitiveOfSchema<
|
22
|
-
S extends CoValueClass |
|
23
|
-
> = S extends
|
24
|
-
? S extends
|
25
|
-
collaborative: true;
|
26
|
-
builtin: "Account";
|
27
|
-
}
|
28
|
+
S extends CoValueClass | AnyZodOrCoValueSchema,
|
29
|
+
> = S extends CoreCoValueSchema
|
30
|
+
? S extends CoreAccountSchema<infer Shape>
|
28
31
|
? {
|
29
32
|
-readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;
|
30
33
|
} & { profile: Profile } & Account
|
31
|
-
: S extends
|
32
|
-
collaborative: true;
|
33
|
-
}
|
34
|
+
: S extends CoreCoRecordSchema<infer K, infer V>
|
34
35
|
? {
|
35
36
|
-readonly [key in z.output<K> &
|
36
37
|
string]: InstanceOrPrimitiveOfSchema<V>;
|
37
38
|
} & CoMap
|
38
|
-
: S extends
|
39
|
+
: S extends CoreCoMapSchema<infer Shape, infer CatchAll>
|
39
40
|
? {
|
40
41
|
-readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
|
41
42
|
Shape[key]
|
42
43
|
>;
|
43
|
-
} & (unknown extends
|
44
|
+
} & (unknown extends CatchAll
|
44
45
|
? {}
|
45
46
|
: {
|
46
|
-
|
47
|
+
// @ts-expect-error
|
48
|
+
[key: string]: InstanceOrPrimitiveOfSchema<CatchAll>;
|
47
49
|
}) &
|
48
50
|
CoMap
|
49
|
-
: S extends
|
51
|
+
: S extends CoreCoListSchema<infer T>
|
50
52
|
? CoList<InstanceOrPrimitiveOfSchema<T>>
|
51
|
-
: S extends
|
53
|
+
: S extends CoreCoFeedSchema<infer T>
|
52
54
|
? CoFeed<InstanceOrPrimitiveOfSchema<T>>
|
53
|
-
: S extends
|
55
|
+
: S extends CorePlainTextSchema
|
54
56
|
? CoPlainText
|
55
|
-
: S extends
|
57
|
+
: S extends CoreRichTextSchema
|
56
58
|
? CoRichText
|
57
|
-
: S extends
|
59
|
+
: S extends CoreFileStreamSchema
|
58
60
|
? FileStream
|
59
|
-
: S extends
|
60
|
-
? InstanceOrPrimitiveOfSchema<
|
61
|
-
: S extends
|
62
|
-
?
|
63
|
-
:
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
61
|
+
: S extends CoreCoOptionalSchema<infer T>
|
62
|
+
? InstanceOrPrimitiveOfSchema<T> | undefined
|
63
|
+
: S extends CoDiscriminatedUnionSchema<infer Members>
|
64
|
+
? InstanceOrPrimitiveOfSchema<Members[number]>
|
65
|
+
: never
|
66
|
+
: S extends z.core.$ZodType
|
67
|
+
? S extends z.core.$ZodOptional<infer Inner extends z.core.$ZodType>
|
68
|
+
? InstanceOrPrimitiveOfSchema<Inner> | undefined
|
69
|
+
: S extends z.ZodJSONSchema
|
70
|
+
? JsonValue
|
71
|
+
: S extends z.core.$ZodUnion<infer Members extends z.core.$ZodType[]>
|
72
|
+
? InstanceOrPrimitiveOfSchema<Members[number]>
|
73
|
+
: // primitives below here - we manually traverse to ensure we only allow what we can handle
|
74
|
+
S extends z.core.$ZodObject<infer Shape>
|
75
|
+
? {
|
76
|
+
-readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
|
77
|
+
Shape[key]
|
78
|
+
>;
|
79
|
+
}
|
80
|
+
: S extends z.core.$ZodArray<infer Item extends z.core.$ZodType>
|
81
|
+
? InstanceOrPrimitiveOfSchema<Item>[]
|
82
|
+
: S extends z.core.$ZodTuple<
|
83
|
+
infer Items extends readonly z.core.$ZodType[]
|
84
|
+
>
|
85
|
+
? {
|
86
|
+
[key in keyof Items]: InstanceOrPrimitiveOfSchema<
|
87
|
+
Items[key]
|
88
|
+
>;
|
89
|
+
}
|
90
|
+
: S extends z.core.$ZodString
|
91
|
+
? string
|
92
|
+
: S extends z.core.$ZodNumber
|
93
|
+
? number
|
94
|
+
: S extends z.core.$ZodBoolean
|
95
|
+
? boolean
|
96
|
+
: S extends z.core.$ZodLiteral<infer Literal>
|
97
|
+
? Literal
|
98
|
+
: S extends z.core.$ZodDate
|
99
|
+
? Date
|
100
|
+
: S extends z.core.$ZodEnum<infer Enum>
|
101
|
+
? Enum[keyof Enum]
|
102
|
+
: S extends z.core.$ZodTemplateLiteral<
|
103
|
+
infer pattern
|
104
|
+
>
|
105
|
+
? pattern
|
106
|
+
: S extends z.core.$ZodReadonly<
|
107
|
+
infer Inner extends z.core.$ZodType
|
108
|
+
>
|
109
|
+
? InstanceOrPrimitiveOfSchema<Inner>
|
110
|
+
: S extends z.core.$ZodDefault<
|
111
|
+
infer Default extends z.core.$ZodType
|
112
|
+
>
|
113
|
+
? InstanceOrPrimitiveOfSchema<Default>
|
114
|
+
: S extends z.core.$ZodCatch<
|
115
|
+
infer Catch extends z.core.$ZodType
|
116
|
+
>
|
117
|
+
? InstanceOrPrimitiveOfSchema<Catch>
|
118
|
+
: never
|
119
|
+
: S extends CoValueClass
|
120
|
+
? InstanceType<S>
|
121
|
+
: never;
|
@@ -1,31 +1,34 @@
|
|
1
1
|
import { JsonValue } from "cojson";
|
2
2
|
import {
|
3
3
|
Account,
|
4
|
+
AnyZodOrCoValueSchema,
|
5
|
+
CoDiscriminatedUnionSchema,
|
4
6
|
CoFeed,
|
5
7
|
CoList,
|
6
8
|
CoMap,
|
7
9
|
CoPlainText,
|
8
10
|
CoRichText,
|
9
11
|
CoValueClass,
|
12
|
+
CoreAccountSchema,
|
13
|
+
CoreCoRecordSchema,
|
10
14
|
FileStream,
|
11
15
|
InstanceOrPrimitiveOfSchema,
|
12
16
|
Profile,
|
13
17
|
} from "../../../internal.js";
|
14
|
-
import {
|
15
|
-
import {
|
16
|
-
import {
|
17
|
-
import {
|
18
|
-
import {
|
19
|
-
import {
|
18
|
+
import { CoreCoFeedSchema } from "../schemaTypes/CoFeedSchema.js";
|
19
|
+
import { CoreCoListSchema } from "../schemaTypes/CoListSchema.js";
|
20
|
+
import { CoreCoMapSchema } from "../schemaTypes/CoMapSchema.js";
|
21
|
+
import { CoreCoOptionalSchema } from "../schemaTypes/CoOptionalSchema.js";
|
22
|
+
import { CoreCoValueSchema } from "../schemaTypes/CoValueSchema.js";
|
23
|
+
import { CoreFileStreamSchema } from "../schemaTypes/FileStreamSchema.js";
|
24
|
+
import { CorePlainTextSchema } from "../schemaTypes/PlainTextSchema.js";
|
25
|
+
import { CoreRichTextSchema } from "../schemaTypes/RichTextSchema.js";
|
20
26
|
import { z } from "../zodReExport.js";
|
21
27
|
|
22
28
|
export type InstanceOrPrimitiveOfSchemaCoValuesNullable<
|
23
|
-
S extends CoValueClass |
|
24
|
-
> = S extends
|
25
|
-
? S extends
|
26
|
-
collaborative: true;
|
27
|
-
builtin: "Account";
|
28
|
-
}
|
29
|
+
S extends CoValueClass | AnyZodOrCoValueSchema,
|
30
|
+
> = S extends CoreCoValueSchema
|
31
|
+
? S extends CoreAccountSchema<infer Shape>
|
29
32
|
?
|
30
33
|
| ({
|
31
34
|
-readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
|
@@ -33,94 +36,100 @@ export type InstanceOrPrimitiveOfSchemaCoValuesNullable<
|
|
33
36
|
>;
|
34
37
|
} & { profile: Profile | null } & Account)
|
35
38
|
| null
|
36
|
-
: S extends
|
37
|
-
collaborative: true;
|
38
|
-
}
|
39
|
+
: S extends CoreCoRecordSchema<infer K, infer V>
|
39
40
|
?
|
40
41
|
| ({
|
41
42
|
-readonly [key in z.output<K> &
|
42
43
|
string]: InstanceOrPrimitiveOfSchemaCoValuesNullable<V>;
|
43
44
|
} & CoMap)
|
44
45
|
| null
|
45
|
-
: S extends
|
46
|
+
: S extends CoreCoMapSchema<infer Shape, infer CatchAll>
|
46
47
|
?
|
47
48
|
| ({
|
48
49
|
-readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
|
49
50
|
Shape[key]
|
50
51
|
>;
|
51
|
-
} & (unknown extends
|
52
|
+
} & (unknown extends CatchAll
|
52
53
|
? {}
|
53
54
|
: {
|
54
|
-
[
|
55
|
+
[
|
56
|
+
key: string // @ts-expect-error
|
57
|
+
]: InstanceOrPrimitiveOfSchemaCoValuesNullable<CatchAll>;
|
55
58
|
}) &
|
56
59
|
CoMap)
|
57
60
|
| null
|
58
|
-
: S extends
|
61
|
+
: S extends CoreCoListSchema<infer T>
|
59
62
|
? CoList<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>> | null
|
60
|
-
: S extends
|
63
|
+
: S extends CoreCoFeedSchema<infer T>
|
61
64
|
? CoFeed<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>> | null
|
62
|
-
: S extends
|
65
|
+
: S extends CorePlainTextSchema
|
63
66
|
? CoPlainText | null
|
64
|
-
: S extends
|
67
|
+
: S extends CoreRichTextSchema
|
65
68
|
? CoRichText | null
|
66
|
-
: S extends
|
69
|
+
: S extends CoreFileStreamSchema
|
67
70
|
? FileStream | null
|
68
|
-
: S extends
|
69
|
-
?
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
:
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
71
|
+
: S extends CoreCoOptionalSchema<infer T>
|
72
|
+
? InstanceOrPrimitiveOfSchemaCoValuesNullable<T> | undefined
|
73
|
+
: S extends CoDiscriminatedUnionSchema<infer Members>
|
74
|
+
? InstanceOrPrimitiveOfSchemaCoValuesNullable<
|
75
|
+
Members[number]
|
76
|
+
>
|
77
|
+
: never
|
78
|
+
: S extends z.core.$ZodType
|
79
|
+
? S extends z.core.$ZodOptional<infer Inner extends z.core.$ZodType>
|
80
|
+
? InstanceOrPrimitiveOfSchemaCoValuesNullable<Inner> | undefined
|
81
|
+
: S extends z.ZodJSONSchema
|
82
|
+
? JsonValue
|
83
|
+
: S extends z.core.$ZodUnion<
|
84
|
+
infer Members extends readonly z.core.$ZodType[]
|
85
|
+
>
|
86
|
+
? InstanceOrPrimitiveOfSchemaCoValuesNullable<Members[number]>
|
87
|
+
: // primitives below here - we manually traverse to ensure we only allow what we can handle
|
88
|
+
S extends z.core.$ZodObject<infer Shape>
|
89
|
+
? {
|
90
|
+
-readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
|
91
|
+
Shape[key]
|
92
|
+
>;
|
93
|
+
}
|
94
|
+
: S extends z.core.$ZodArray<infer Item extends z.core.$ZodType>
|
95
|
+
? InstanceOrPrimitiveOfSchema<Item>[]
|
96
|
+
: S extends z.core.$ZodTuple<
|
97
|
+
infer Items extends z.core.$ZodType[]
|
98
|
+
>
|
99
|
+
? {
|
100
|
+
[key in keyof Items]: InstanceOrPrimitiveOfSchema<
|
101
|
+
Items[key]
|
102
|
+
>;
|
103
|
+
}
|
104
|
+
: S extends z.core.$ZodString
|
105
|
+
? string
|
106
|
+
: S extends z.core.$ZodNumber
|
107
|
+
? number
|
108
|
+
: S extends z.core.$ZodBoolean
|
109
|
+
? boolean
|
110
|
+
: S extends z.core.$ZodLiteral<infer Literal>
|
111
|
+
? Literal
|
112
|
+
: S extends z.core.$ZodDate
|
113
|
+
? Date
|
114
|
+
: S extends z.core.$ZodEnum<infer Enum>
|
115
|
+
? Enum[keyof Enum]
|
116
|
+
: S extends z.core.$ZodTemplateLiteral<
|
117
|
+
infer pattern
|
118
|
+
>
|
119
|
+
? pattern
|
120
|
+
: S extends z.core.$ZodReadonly<
|
121
|
+
infer Inner extends z.core.$ZodType
|
122
|
+
>
|
123
|
+
? InstanceOrPrimitiveOfSchema<Inner>
|
124
|
+
: S extends z.core.$ZodDefault<
|
125
|
+
infer Default extends z.core.$ZodType
|
126
|
+
>
|
127
|
+
? InstanceOrPrimitiveOfSchema<Default>
|
128
|
+
: S extends z.core.$ZodCatch<
|
129
|
+
infer Catch extends z.core.$ZodType
|
130
|
+
>
|
131
|
+
? InstanceOrPrimitiveOfSchema<Catch>
|
132
|
+
: never
|
133
|
+
: S extends CoValueClass
|
134
|
+
? InstanceType<S> | null
|
135
|
+
: never;
|
@@ -1,34 +1,34 @@
|
|
1
1
|
import { RawAccount, RawCoList, RawCoMap } from "cojson";
|
2
|
-
import { CoValueClass, CoValueFromRaw } from "../../internal.js";
|
3
2
|
import {
|
3
|
+
AnyZodOrCoValueSchema,
|
4
|
+
CoDiscriminatedUnionSchema,
|
5
|
+
CoMap,
|
6
|
+
CoreCoDiscriminatedUnionSchema,
|
7
|
+
CoreCoMapSchema,
|
8
|
+
DiscriminableCoValueSchemas,
|
9
|
+
DiscriminableCoreCoValueSchema,
|
10
|
+
} from "../../internal.js";
|
11
|
+
import {
|
12
|
+
hydrateCoreCoValueSchema,
|
4
13
|
isAnyCoValueSchema,
|
5
|
-
|
6
|
-
} from "./runtimeConverters/zodSchemaToCoSchema.js";
|
7
|
-
import { AccountSchema } from "./schemaTypes/AccountSchema.js";
|
8
|
-
import { CoListSchema } from "./schemaTypes/CoListSchema.js";
|
9
|
-
import { CoMapSchema } from "./schemaTypes/CoMapSchema.js";
|
14
|
+
} from "./runtimeConverters/coValueSchemaTransformation.js";
|
10
15
|
import { z } from "./zodReExport.js";
|
11
16
|
|
12
17
|
export function schemaUnionDiscriminatorFor(
|
13
|
-
schema:
|
18
|
+
schema: CoreCoDiscriminatedUnionSchema<DiscriminableCoValueSchemas>,
|
14
19
|
) {
|
15
20
|
if (isUnionOfCoMapsDeeply(schema)) {
|
16
|
-
|
17
|
-
|
18
|
-
"z.union() of collaborative types is not supported, use co.discriminatedUnion() instead",
|
19
|
-
);
|
20
|
-
}
|
21
|
-
|
22
|
-
const discriminator = schema._zod.def.discriminator;
|
23
|
-
const field = schema._zod.disc.get(discriminator);
|
21
|
+
const definition = schema.getDefinition();
|
22
|
+
const { discriminatorMap, discriminator, options } = definition;
|
24
23
|
|
24
|
+
const field = discriminatorMap[discriminator];
|
25
25
|
if (!field) {
|
26
26
|
throw new Error(
|
27
27
|
"co.discriminatedUnion() of collaborative types with non-existent discriminator key is not supported",
|
28
28
|
);
|
29
29
|
}
|
30
30
|
|
31
|
-
for (const value of field
|
31
|
+
for (const value of field) {
|
32
32
|
if (typeof value !== "string" && typeof value !== "number") {
|
33
33
|
throw new Error(
|
34
34
|
"co.discriminatedUnion() of collaborative types with non-string or non-number discriminator value is not supported",
|
@@ -36,15 +36,17 @@ export function schemaUnionDiscriminatorFor(
|
|
36
36
|
}
|
37
37
|
}
|
38
38
|
|
39
|
-
const availableOptions:
|
40
|
-
|
41
|
-
for (const option of
|
42
|
-
if (option.
|
43
|
-
availableOptions.push(option
|
44
|
-
} else if (option.
|
45
|
-
for (const subOption of (
|
46
|
-
|
47
|
-
|
39
|
+
const availableOptions: DiscriminableCoreCoValueSchema[] = [];
|
40
|
+
|
41
|
+
for (const option of options) {
|
42
|
+
if (option.builtin === "CoMap") {
|
43
|
+
availableOptions.push(option);
|
44
|
+
} else if (option.builtin === "CoDiscriminatedUnion") {
|
45
|
+
for (const subOption of (
|
46
|
+
option as CoDiscriminatedUnionSchema<any>
|
47
|
+
).getDefinition().options) {
|
48
|
+
if (!options.includes(subOption)) {
|
49
|
+
options.push(subOption);
|
48
50
|
}
|
49
51
|
}
|
50
52
|
} else {
|
@@ -64,10 +66,9 @@ export function schemaUnionDiscriminatorFor(
|
|
64
66
|
for (const option of availableOptions) {
|
65
67
|
let match = true;
|
66
68
|
|
67
|
-
for (const key of
|
68
|
-
const discriminatorDef = (option as
|
69
|
-
key as string
|
70
|
-
];
|
69
|
+
for (const key of Object.keys(discriminatorMap)) {
|
70
|
+
const discriminatorDef = (option as CoreCoMapSchema).getDefinition()
|
71
|
+
.shape[key as string];
|
71
72
|
|
72
73
|
const discriminatorValue = (_raw as RawCoMap).get(key as string);
|
73
74
|
|
@@ -88,7 +89,8 @@ export function schemaUnionDiscriminatorFor(
|
|
88
89
|
break;
|
89
90
|
}
|
90
91
|
|
91
|
-
const literalDef = discriminatorDef._zod
|
92
|
+
const literalDef = discriminatorDef._zod
|
93
|
+
.def as z.core.$ZodLiteralDef<any>;
|
92
94
|
|
93
95
|
if (!Array.from(literalDef.values).includes(discriminatorValue)) {
|
94
96
|
match = false;
|
@@ -97,12 +99,8 @@ export function schemaUnionDiscriminatorFor(
|
|
97
99
|
}
|
98
100
|
|
99
101
|
if (match) {
|
100
|
-
const coValueSchema =
|
101
|
-
|
102
|
-
| AccountSchema
|
103
|
-
| CoListSchema<any>;
|
104
|
-
return coValueSchema.getCoValueClass() as CoValueClass<any> &
|
105
|
-
CoValueFromRaw<any>;
|
102
|
+
const coValueSchema = hydrateCoreCoValueSchema(option as any);
|
103
|
+
return coValueSchema.getCoValueClass() as typeof CoMap;
|
106
104
|
}
|
107
105
|
}
|
108
106
|
|
@@ -114,34 +112,32 @@ export function schemaUnionDiscriminatorFor(
|
|
114
112
|
return determineSchema;
|
115
113
|
} else {
|
116
114
|
throw new Error(
|
117
|
-
"
|
115
|
+
"co.discriminatedUnion() of non-collaborative types is not supported",
|
118
116
|
);
|
119
117
|
}
|
120
118
|
}
|
121
119
|
|
122
|
-
|
123
|
-
schema:
|
124
|
-
):
|
125
|
-
|
126
|
-
return schema._zod.def.options.every(isCoMapOrUnionOfCoMapsDeeply);
|
127
|
-
} else {
|
128
|
-
return false;
|
129
|
-
}
|
120
|
+
function isUnionOfCoMapsDeeply(
|
121
|
+
schema: CoreCoDiscriminatedUnionSchema<DiscriminableCoValueSchemas>,
|
122
|
+
): boolean {
|
123
|
+
return schema.getDefinition().options.every(isCoMapOrUnionOfCoMapsDeeply);
|
130
124
|
}
|
131
125
|
|
132
126
|
function isCoMapOrUnionOfCoMapsDeeply(
|
133
|
-
schema:
|
134
|
-
):
|
135
|
-
if (schema
|
127
|
+
schema: DiscriminableCoreCoValueSchema,
|
128
|
+
): boolean {
|
129
|
+
if (schema.builtin === "CoMap") {
|
136
130
|
return true;
|
137
|
-
} else if (schema
|
138
|
-
return schema
|
131
|
+
} else if (schema.builtin === "CoDiscriminatedUnion") {
|
132
|
+
return (schema as CoDiscriminatedUnionSchema<any>)
|
133
|
+
.getDefinition()
|
134
|
+
.options.every(isCoMapOrUnionOfCoMapsDeeply);
|
139
135
|
} else {
|
140
136
|
return false;
|
141
137
|
}
|
142
138
|
}
|
143
139
|
|
144
|
-
export function isUnionOfPrimitivesDeeply(schema:
|
140
|
+
export function isUnionOfPrimitivesDeeply(schema: AnyZodOrCoValueSchema) {
|
145
141
|
if (schema instanceof z.core.$ZodUnion) {
|
146
142
|
return schema._zod.def.options.every(isUnionOfPrimitivesDeeply);
|
147
143
|
} else {
|