jazz-tools 0.13.31 → 0.14.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +8 -8
- package/CHANGELOG.md +22 -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-IJU4XPFS.js → chunk-WLOZKDOH.js} +3536 -3291
- package/dist/chunk-WLOZKDOH.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 +17 -21
- 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 +1 -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 +88 -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 +35 -43
- 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 +3 -1
- 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 +196 -179
- 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-IJU4XPFS.js.map +0 -1
- package/dist/tests/deepLoading.test-d.d.ts +0 -2
- package/dist/tests/deepLoading.test-d.d.ts.map +0 -1
- package/src/tests/deepLoading.test-d.ts +0 -393
@@ -0,0 +1,25 @@
|
|
1
|
+
import z from "zod/v4";
|
2
|
+
import { Account, CoRichText, Group } from "../../../internal.js";
|
3
|
+
import { AnonymousJazzAgent } from "../../anonymousJazzAgent.js";
|
4
|
+
|
5
|
+
export type RichTextSchema = z.core.$ZodCustom<CoRichText, unknown> & {
|
6
|
+
collaborative: true;
|
7
|
+
builtin: "CoRichText";
|
8
|
+
create(
|
9
|
+
text: string,
|
10
|
+
options?: { owner: Account | Group } | Account | Group,
|
11
|
+
): CoRichText;
|
12
|
+
load(
|
13
|
+
id: string,
|
14
|
+
options: { loadAs: Account | AnonymousJazzAgent },
|
15
|
+
): Promise<CoRichText>;
|
16
|
+
subscribe(
|
17
|
+
id: string,
|
18
|
+
options: { loadAs: Account | AnonymousJazzAgent },
|
19
|
+
listener: (value: CoRichText, unsubscribe: () => void) => void,
|
20
|
+
): () => void;
|
21
|
+
subscribe(
|
22
|
+
id: string,
|
23
|
+
listener: (value: CoRichText, unsubscribe: () => void) => void,
|
24
|
+
): () => void;
|
25
|
+
};
|
@@ -0,0 +1,61 @@
|
|
1
|
+
import z from "zod/v4";
|
2
|
+
import {
|
3
|
+
Account,
|
4
|
+
CoFeed,
|
5
|
+
CoList,
|
6
|
+
CoMap,
|
7
|
+
CoPlainText,
|
8
|
+
CoRichText,
|
9
|
+
CoValueClass,
|
10
|
+
FileStream,
|
11
|
+
} from "../../../internal.js";
|
12
|
+
import { AnyCoFeedSchema } from "../schemaTypes/CoFeedSchema.js";
|
13
|
+
import { AnyCoListSchema } from "../schemaTypes/CoListSchema.js";
|
14
|
+
import { AnyCoMapSchema } from "../schemaTypes/CoMapSchema.js";
|
15
|
+
import { FileStreamSchema } from "../schemaTypes/FileStreamSchema.js";
|
16
|
+
import { PlainTextSchema } from "../schemaTypes/PlainTextSchema.js";
|
17
|
+
import { RichTextSchema } from "../schemaTypes/RichTextSchema.js";
|
18
|
+
import { InstanceOrPrimitiveOfSchema } from "./InstanceOrPrimitiveOfSchema.js";
|
19
|
+
|
20
|
+
export type InstanceOfSchema<S extends CoValueClass | z.core.$ZodType> =
|
21
|
+
S extends z.core.$ZodType
|
22
|
+
? S extends z.core.$ZodObject<infer Shape> & {
|
23
|
+
collaborative: true;
|
24
|
+
builtin: "Account";
|
25
|
+
}
|
26
|
+
? {
|
27
|
+
[key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;
|
28
|
+
} & Account
|
29
|
+
: S extends z.core.$ZodRecord<infer K, infer V> & {
|
30
|
+
collaborative: true;
|
31
|
+
}
|
32
|
+
? {
|
33
|
+
[key in z.output<K> & string]: InstanceOrPrimitiveOfSchema<V>;
|
34
|
+
} & CoMap
|
35
|
+
: S extends AnyCoMapSchema<infer Shape, infer Config>
|
36
|
+
? {
|
37
|
+
[key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;
|
38
|
+
} & (unknown extends Config["out"][string]
|
39
|
+
? {}
|
40
|
+
: {
|
41
|
+
[key: string]: Config["out"][string];
|
42
|
+
}) &
|
43
|
+
CoMap
|
44
|
+
: S extends AnyCoListSchema<infer T>
|
45
|
+
? CoList<InstanceOrPrimitiveOfSchema<T>>
|
46
|
+
: S extends AnyCoFeedSchema<infer T>
|
47
|
+
? CoFeed<InstanceOrPrimitiveOfSchema<T>>
|
48
|
+
: S extends PlainTextSchema
|
49
|
+
? CoPlainText
|
50
|
+
: S extends RichTextSchema
|
51
|
+
? CoRichText
|
52
|
+
: S extends FileStreamSchema
|
53
|
+
? FileStream
|
54
|
+
: S extends z.core.$ZodOptional<infer Inner>
|
55
|
+
? InstanceOrPrimitiveOfSchema<Inner>
|
56
|
+
: S extends z.core.$ZodUnion<infer Members>
|
57
|
+
? InstanceOrPrimitiveOfSchema<Members[number]>
|
58
|
+
: never
|
59
|
+
: S extends CoValueClass
|
60
|
+
? InstanceType<S>
|
61
|
+
: never;
|
@@ -0,0 +1,77 @@
|
|
1
|
+
import z from "zod/v4";
|
2
|
+
import {
|
3
|
+
Account,
|
4
|
+
AnyCoFeedSchema,
|
5
|
+
AnyCoListSchema,
|
6
|
+
AnyCoMapSchema,
|
7
|
+
CoFeed,
|
8
|
+
CoList,
|
9
|
+
CoMap,
|
10
|
+
CoPlainText,
|
11
|
+
CoRichText,
|
12
|
+
CoValueClass,
|
13
|
+
FileStream,
|
14
|
+
} from "../../../internal.js";
|
15
|
+
import { FileStreamSchema } from "../schemaTypes/FileStreamSchema.js";
|
16
|
+
import { PlainTextSchema } from "../schemaTypes/PlainTextSchema.js";
|
17
|
+
import { RichTextSchema } from "../schemaTypes/RichTextSchema.js";
|
18
|
+
import { InstanceOrPrimitiveOfSchemaCoValuesNullable } from "./InstanceOrPrimitiveOfSchemaCoValuesNullable.js";
|
19
|
+
|
20
|
+
export type InstanceOfSchemaCoValuesNullable<
|
21
|
+
S extends CoValueClass | z.core.$ZodType,
|
22
|
+
> = S extends z.core.$ZodType
|
23
|
+
? S extends z.core.$ZodObject<infer Shape> & {
|
24
|
+
collaborative: true;
|
25
|
+
builtin: "Account";
|
26
|
+
}
|
27
|
+
?
|
28
|
+
| ({
|
29
|
+
[key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
|
30
|
+
Shape[key]
|
31
|
+
>;
|
32
|
+
} & Account)
|
33
|
+
| null
|
34
|
+
: S extends z.core.$ZodRecord<infer K, infer V> & {
|
35
|
+
collaborative: true;
|
36
|
+
}
|
37
|
+
?
|
38
|
+
| ({
|
39
|
+
[key in z.output<K> &
|
40
|
+
string]: InstanceOrPrimitiveOfSchemaCoValuesNullable<V>;
|
41
|
+
} & CoMap)
|
42
|
+
| null
|
43
|
+
: S extends AnyCoMapSchema<infer Shape, infer Config>
|
44
|
+
?
|
45
|
+
| ({
|
46
|
+
[key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
|
47
|
+
Shape[key]
|
48
|
+
>;
|
49
|
+
} & (unknown extends Config["out"][string]
|
50
|
+
? {}
|
51
|
+
: {
|
52
|
+
[key: string]: Config["out"][string];
|
53
|
+
}) &
|
54
|
+
CoMap)
|
55
|
+
| null
|
56
|
+
: S extends AnyCoListSchema<infer T>
|
57
|
+
? CoList<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>> | null
|
58
|
+
: S extends AnyCoFeedSchema<infer T>
|
59
|
+
? CoFeed<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>> | null
|
60
|
+
: S extends PlainTextSchema
|
61
|
+
? CoPlainText | null
|
62
|
+
: S extends RichTextSchema
|
63
|
+
? CoRichText | null
|
64
|
+
: S extends FileStreamSchema
|
65
|
+
? FileStream | null
|
66
|
+
: S extends z.core.$ZodOptional<infer Inner>
|
67
|
+
?
|
68
|
+
| InstanceOrPrimitiveOfSchemaCoValuesNullable<Inner>
|
69
|
+
| undefined
|
70
|
+
: S extends z.core.$ZodUnion<infer Members>
|
71
|
+
? InstanceOrPrimitiveOfSchemaCoValuesNullable<
|
72
|
+
Members[number]
|
73
|
+
>
|
74
|
+
: never
|
75
|
+
: S extends CoValueClass
|
76
|
+
? InstanceType<S> | null
|
77
|
+
: never;
|
@@ -0,0 +1,90 @@
|
|
1
|
+
import z from "zod/v4";
|
2
|
+
import {
|
3
|
+
Account,
|
4
|
+
CoFeed,
|
5
|
+
CoList,
|
6
|
+
CoMap,
|
7
|
+
CoPlainText,
|
8
|
+
CoRichText,
|
9
|
+
CoValueClass,
|
10
|
+
FileStream,
|
11
|
+
Profile,
|
12
|
+
} from "../../../internal.js";
|
13
|
+
import { AnyCoFeedSchema } from "../schemaTypes/CoFeedSchema.js";
|
14
|
+
import { AnyCoListSchema } from "../schemaTypes/CoListSchema.js";
|
15
|
+
import { AnyCoMapSchema } from "../schemaTypes/CoMapSchema.js";
|
16
|
+
import { FileStreamSchema } from "../schemaTypes/FileStreamSchema.js";
|
17
|
+
import { PlainTextSchema } from "../schemaTypes/PlainTextSchema.js";
|
18
|
+
import { RichTextSchema } from "../schemaTypes/RichTextSchema.js";
|
19
|
+
|
20
|
+
export type InstanceOrPrimitiveOfSchema<
|
21
|
+
S extends CoValueClass | z.core.$ZodType,
|
22
|
+
> = S extends z.core.$ZodType
|
23
|
+
? S extends z.core.$ZodObject<infer Shape> & {
|
24
|
+
collaborative: true;
|
25
|
+
builtin: "Account";
|
26
|
+
}
|
27
|
+
? {
|
28
|
+
-readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<Shape[key]>;
|
29
|
+
} & { profile: Profile } & Account
|
30
|
+
: S extends z.core.$ZodRecord<infer K, infer V> & {
|
31
|
+
collaborative: true;
|
32
|
+
}
|
33
|
+
? {
|
34
|
+
-readonly [key in z.output<K> &
|
35
|
+
string]: InstanceOrPrimitiveOfSchema<V>;
|
36
|
+
} & CoMap
|
37
|
+
: S extends AnyCoMapSchema<infer Shape, infer Config>
|
38
|
+
? {
|
39
|
+
-readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
|
40
|
+
Shape[key]
|
41
|
+
>;
|
42
|
+
} & (unknown extends Config["out"][string]
|
43
|
+
? {}
|
44
|
+
: {
|
45
|
+
[key: string]: Config["out"][string];
|
46
|
+
}) &
|
47
|
+
CoMap
|
48
|
+
: S extends AnyCoListSchema<infer T>
|
49
|
+
? CoList<InstanceOrPrimitiveOfSchema<T>>
|
50
|
+
: S extends AnyCoFeedSchema<infer T>
|
51
|
+
? CoFeed<InstanceOrPrimitiveOfSchema<T>>
|
52
|
+
: S extends PlainTextSchema
|
53
|
+
? CoPlainText
|
54
|
+
: S extends RichTextSchema
|
55
|
+
? CoRichText
|
56
|
+
: S extends FileStreamSchema
|
57
|
+
? FileStream
|
58
|
+
: S extends z.core.$ZodOptional<infer Inner>
|
59
|
+
? InstanceOrPrimitiveOfSchema<Inner> | undefined
|
60
|
+
: S extends z.core.$ZodUnion<infer Members>
|
61
|
+
? InstanceOrPrimitiveOfSchema<Members[number]>
|
62
|
+
: // primitives below here - we manually traverse to ensure we only allow what we can handle
|
63
|
+
S extends z.core.$ZodObject<infer Shape>
|
64
|
+
? {
|
65
|
+
-readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
|
66
|
+
Shape[key]
|
67
|
+
>;
|
68
|
+
}
|
69
|
+
: S extends z.core.$ZodArray<infer Item>
|
70
|
+
? InstanceOrPrimitiveOfSchema<Item>[]
|
71
|
+
: S extends z.core.$ZodTuple<infer Items>
|
72
|
+
? {
|
73
|
+
[key in keyof Items]: InstanceOrPrimitiveOfSchema<
|
74
|
+
Items[key]
|
75
|
+
>;
|
76
|
+
}
|
77
|
+
: S extends z.core.$ZodString
|
78
|
+
? string
|
79
|
+
: S extends z.core.$ZodNumber
|
80
|
+
? number
|
81
|
+
: S extends z.core.$ZodBoolean
|
82
|
+
? boolean
|
83
|
+
: S extends z.core.$ZodLiteral<infer Literal>
|
84
|
+
? Literal
|
85
|
+
: S extends z.core.$ZodDate
|
86
|
+
? Date
|
87
|
+
: never
|
88
|
+
: S extends CoValueClass
|
89
|
+
? InstanceType<S>
|
90
|
+
: never;
|
package/src/implementation/zodSchema/typeConverters/InstanceOrPrimitiveOfSchemaCoValuesNullable.ts
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
import z from "zod/v4";
|
2
|
+
import {
|
3
|
+
Account,
|
4
|
+
CoFeed,
|
5
|
+
CoList,
|
6
|
+
CoMap,
|
7
|
+
CoPlainText,
|
8
|
+
CoRichText,
|
9
|
+
CoValueClass,
|
10
|
+
FileStream,
|
11
|
+
InstanceOrPrimitiveOfSchema,
|
12
|
+
Profile,
|
13
|
+
} from "../../../internal.js";
|
14
|
+
import { AnyCoFeedSchema } from "../schemaTypes/CoFeedSchema.js";
|
15
|
+
import { AnyCoListSchema } from "../schemaTypes/CoListSchema.js";
|
16
|
+
import { AnyCoMapSchema } from "../schemaTypes/CoMapSchema.js";
|
17
|
+
import { FileStreamSchema } from "../schemaTypes/FileStreamSchema.js";
|
18
|
+
import { PlainTextSchema } from "../schemaTypes/PlainTextSchema.js";
|
19
|
+
import { RichTextSchema } from "../schemaTypes/RichTextSchema.js";
|
20
|
+
|
21
|
+
export type InstanceOrPrimitiveOfSchemaCoValuesNullable<
|
22
|
+
S extends CoValueClass | z.core.$ZodType,
|
23
|
+
> = S extends z.core.$ZodType
|
24
|
+
? S extends z.core.$ZodObject<infer Shape> & {
|
25
|
+
collaborative: true;
|
26
|
+
builtin: "Account";
|
27
|
+
}
|
28
|
+
?
|
29
|
+
| ({
|
30
|
+
-readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
|
31
|
+
Shape[key]
|
32
|
+
>;
|
33
|
+
} & { profile: Profile | null } & Account)
|
34
|
+
| null
|
35
|
+
: S extends z.core.$ZodRecord<infer K, infer V> & {
|
36
|
+
collaborative: true;
|
37
|
+
}
|
38
|
+
?
|
39
|
+
| ({
|
40
|
+
-readonly [key in z.output<K> &
|
41
|
+
string]: InstanceOrPrimitiveOfSchemaCoValuesNullable<V>;
|
42
|
+
} & CoMap)
|
43
|
+
| null
|
44
|
+
: S extends AnyCoMapSchema<infer Shape, infer Config>
|
45
|
+
?
|
46
|
+
| ({
|
47
|
+
-readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchemaCoValuesNullable<
|
48
|
+
Shape[key]
|
49
|
+
>;
|
50
|
+
} & (unknown extends Config["out"][string]
|
51
|
+
? {}
|
52
|
+
: {
|
53
|
+
[key: string]: Config["out"][string];
|
54
|
+
}) &
|
55
|
+
CoMap)
|
56
|
+
| null
|
57
|
+
: S extends AnyCoListSchema<infer T>
|
58
|
+
? CoList<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>> | null
|
59
|
+
: S extends AnyCoFeedSchema<infer T>
|
60
|
+
? CoFeed<InstanceOrPrimitiveOfSchemaCoValuesNullable<T>> | null
|
61
|
+
: S extends PlainTextSchema
|
62
|
+
? CoPlainText | null
|
63
|
+
: S extends RichTextSchema
|
64
|
+
? CoRichText | null
|
65
|
+
: S extends FileStreamSchema
|
66
|
+
? FileStream | null
|
67
|
+
: S extends z.core.$ZodOptional<infer Inner>
|
68
|
+
?
|
69
|
+
| InstanceOrPrimitiveOfSchemaCoValuesNullable<Inner>
|
70
|
+
| undefined
|
71
|
+
: S extends z.core.$ZodUnion<infer Members>
|
72
|
+
? InstanceOrPrimitiveOfSchemaCoValuesNullable<
|
73
|
+
Members[number]
|
74
|
+
>
|
75
|
+
: // primitives below here - we manually traverse to ensure we only allow what we can handle
|
76
|
+
S extends z.core.$ZodObject<infer Shape>
|
77
|
+
? {
|
78
|
+
-readonly [key in keyof Shape]: InstanceOrPrimitiveOfSchema<
|
79
|
+
Shape[key]
|
80
|
+
>;
|
81
|
+
}
|
82
|
+
: S extends z.core.$ZodArray<infer Item>
|
83
|
+
? InstanceOrPrimitiveOfSchema<Item>[]
|
84
|
+
: S extends z.core.$ZodTuple<infer Items>
|
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
|
+
: never
|
101
|
+
: S extends CoValueClass
|
102
|
+
? InstanceType<S> | null
|
103
|
+
: never;
|
@@ -0,0 +1,139 @@
|
|
1
|
+
import { RawAccount, RawCoList, RawCoMap } from "cojson";
|
2
|
+
import z from "zod/v4";
|
3
|
+
import { zodSchemaToCoSchema } from "./runtimeConverters/zodSchemaToCoSchema.js";
|
4
|
+
|
5
|
+
export function schemaUnionDiscriminatorFor(
|
6
|
+
schema: z.core.$ZodDiscriminatedUnion,
|
7
|
+
) {
|
8
|
+
if (isUnionOfCoMapsDeeply(schema)) {
|
9
|
+
if (!schema._zod.disc || schema._zod.disc.size == 0) {
|
10
|
+
throw new Error(
|
11
|
+
"z.union() of collaborative types is not supported, use z.discriminatedUnion() instead",
|
12
|
+
);
|
13
|
+
}
|
14
|
+
if (![...schema._zod.disc.keys()].every((key) => typeof key === "string")) {
|
15
|
+
throw new Error(
|
16
|
+
"z.discriminatedUnion() of collaborative types with non-string discriminator key is not supported",
|
17
|
+
);
|
18
|
+
}
|
19
|
+
if (
|
20
|
+
![...schema._zod.disc.values()].every((v) =>
|
21
|
+
[...v.values].every((value) => typeof value === "string"),
|
22
|
+
)
|
23
|
+
) {
|
24
|
+
throw new Error(
|
25
|
+
"z.discriminatedUnion() of collaborative types with non-string discriminator value is not supported",
|
26
|
+
);
|
27
|
+
}
|
28
|
+
|
29
|
+
const flattendOptions = [
|
30
|
+
...schema._zod.def.options.flatMap((option) => {
|
31
|
+
if (option._zod.def.type === "object") {
|
32
|
+
return [option];
|
33
|
+
} else if (option._zod.def.type === "union") {
|
34
|
+
return [...(option as z.core.$ZodUnion)._zod.def.options];
|
35
|
+
} else {
|
36
|
+
throw new Error(
|
37
|
+
"Unsupported zod type in z.discriminatedUnion() of collaborative types",
|
38
|
+
);
|
39
|
+
}
|
40
|
+
}),
|
41
|
+
];
|
42
|
+
|
43
|
+
const determineSchema = (_raw: RawCoMap | RawAccount | RawCoList) => {
|
44
|
+
if (_raw instanceof RawCoList) {
|
45
|
+
throw new Error(
|
46
|
+
"z.discriminatedUnion() of collaborative types is not supported for CoLists",
|
47
|
+
);
|
48
|
+
}
|
49
|
+
let remainingOptions = [...flattendOptions];
|
50
|
+
for (const discriminator of schema._zod.disc.keys()) {
|
51
|
+
const discriminatorValue = (_raw as RawCoMap).get(
|
52
|
+
discriminator as string,
|
53
|
+
);
|
54
|
+
if (typeof discriminatorValue !== "string") {
|
55
|
+
throw new Error("Discriminator must be a string");
|
56
|
+
}
|
57
|
+
remainingOptions = remainingOptions.filter((option) => {
|
58
|
+
if (option._zod.def.type !== "object") {
|
59
|
+
return false;
|
60
|
+
}
|
61
|
+
const discriminatorDef = (option as z.core.$ZodObject)._zod.def.shape[
|
62
|
+
discriminator as string
|
63
|
+
];
|
64
|
+
if (!discriminatorDef) {
|
65
|
+
return false;
|
66
|
+
}
|
67
|
+
|
68
|
+
console.log(discriminatorDef._zod.def);
|
69
|
+
|
70
|
+
if (discriminatorDef._zod.def.type !== "literal") {
|
71
|
+
console.warn(
|
72
|
+
"Non-literal discriminator found in z.discriminatedUnion() of collaborative types",
|
73
|
+
);
|
74
|
+
return false;
|
75
|
+
}
|
76
|
+
if (
|
77
|
+
(discriminatorDef._zod.def as z.core.$ZodLiteralDef).values
|
78
|
+
.length !== 1
|
79
|
+
) {
|
80
|
+
console.warn(
|
81
|
+
"Literal discriminator with more than one value found in z.discriminatedUnion() of collaborative types",
|
82
|
+
);
|
83
|
+
return false;
|
84
|
+
}
|
85
|
+
return (
|
86
|
+
(discriminatorDef._zod.def as z.core.$ZodLiteralDef).values[0] ===
|
87
|
+
discriminatorValue
|
88
|
+
);
|
89
|
+
});
|
90
|
+
if (remainingOptions.length === 1) {
|
91
|
+
return zodSchemaToCoSchema(remainingOptions[0]!);
|
92
|
+
}
|
93
|
+
}
|
94
|
+
throw new Error(
|
95
|
+
"z.discriminatedUnion() of collaborative types with no matching discriminator value found",
|
96
|
+
);
|
97
|
+
};
|
98
|
+
|
99
|
+
return determineSchema;
|
100
|
+
} else {
|
101
|
+
throw new Error(
|
102
|
+
"z.discriminatedUnion() of non-collaborative types is not supported",
|
103
|
+
);
|
104
|
+
}
|
105
|
+
}
|
106
|
+
|
107
|
+
export function isUnionOfCoMapsDeeply(
|
108
|
+
schema: z.core.$ZodType,
|
109
|
+
): schema is z.core.$ZodDiscriminatedUnion {
|
110
|
+
if (schema instanceof z.core.$ZodUnion) {
|
111
|
+
return schema._zod.def.options.every(isCoMapOrUnionOfCoMapsDeeply);
|
112
|
+
} else {
|
113
|
+
return false;
|
114
|
+
}
|
115
|
+
}
|
116
|
+
|
117
|
+
function isCoMapOrUnionOfCoMapsDeeply(
|
118
|
+
schema: z.core.$ZodType,
|
119
|
+
): schema is z.core.$ZodDiscriminatedUnion {
|
120
|
+
if (
|
121
|
+
schema instanceof z.core.$ZodObject &&
|
122
|
+
"collaborative" in schema &&
|
123
|
+
schema.collaborative
|
124
|
+
) {
|
125
|
+
return true;
|
126
|
+
} else if (schema instanceof z.core.$ZodUnion) {
|
127
|
+
return schema._zod.def.options.every(isCoMapOrUnionOfCoMapsDeeply);
|
128
|
+
} else {
|
129
|
+
return false;
|
130
|
+
}
|
131
|
+
}
|
132
|
+
|
133
|
+
export function isUnionOfPrimitivesDeeply(schema: z.core.$ZodType) {
|
134
|
+
if (schema instanceof z.core.$ZodUnion) {
|
135
|
+
return schema._zod.def.options.every(isUnionOfPrimitivesDeeply);
|
136
|
+
} else {
|
137
|
+
return !("collaborative" in schema);
|
138
|
+
}
|
139
|
+
}
|