Skip to content

Commit c3912fd

Browse files
authored
Merge pull request #2618 from garden-co/fix/inspector-element
fix: simplify definition of the AccountSchema type
2 parents 11dcfd7 + 356bfa4 commit c3912fd

File tree

4 files changed

+78
-47
lines changed

4 files changed

+78
-47
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"jazz-tools": patch
3+
---
4+
5+
Refactor AccountSchema types to solve "This is likely not portable. A type annotation is necessary" issue when using co.account()

packages/jazz-tools/src/tools/exports.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ export {
102102
type InstanceOfSchemaCoValuesNullable,
103103
type CoValueOrZodSchema,
104104
type Loaded,
105+
type BaseAccountShape,
106+
type DefaultAccountShape,
105107
type AccountSchema,
106108
type AnyAccountSchema,
107109
type CoListSchema,

packages/jazz-tools/src/tools/implementation/zodSchema/schemaTypes/AccountSchema.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,24 @@ import { z } from "../zodReExport.js";
77
import { Loaded, ResolveQuery } from "../zodSchema.js";
88
import { AnyCoMapSchema, CoMapSchema } from "./CoMapSchema.js";
99

10+
export type BaseProfileShape = {
11+
name: z.core.$ZodString<string>;
12+
inbox?: z.core.$ZodOptional<z.core.$ZodString>;
13+
inboxInvite?: z.core.$ZodOptional<z.core.$ZodString>;
14+
};
15+
16+
export type BaseAccountShape = {
17+
profile: AnyCoMapSchema<BaseProfileShape>;
18+
root: AnyCoMapSchema;
19+
};
20+
21+
export type DefaultAccountShape = {
22+
profile: CoMapSchema<BaseProfileShape>;
23+
root: CoMapSchema<{}>;
24+
};
25+
1026
export type AccountSchema<
11-
Shape extends {
12-
profile: AnyCoMapSchema<{
13-
name: z.core.$ZodString<string>;
14-
inbox?: z.core.$ZodOptional<z.core.$ZodString>;
15-
inboxInvite?: z.core.$ZodOptional<z.core.$ZodString>;
16-
}>;
17-
root: AnyCoMapSchema;
18-
} = {
19-
profile: CoMapSchema<{
20-
name: z.core.$ZodString<string>;
21-
inbox?: z.core.$ZodOptional<z.core.$ZodString>;
22-
inboxInvite?: z.core.$ZodOptional<z.core.$ZodString>;
23-
}>;
24-
root: CoMapSchema<{}>;
25-
},
27+
Shape extends BaseAccountShape = DefaultAccountShape,
2628
> = Omit<CoMapSchema<Shape>, "create" | "load" | "withMigration"> & {
2729
builtin: "Account";
2830

packages/jazz-tools/src/tools/implementation/zodSchema/zodCo.ts

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import {
22
type Account,
3-
AccountCreationProps,
4-
AccountSchema,
5-
AnyCoMapSchema,
3+
type AccountCreationProps,
4+
type AccountSchema,
5+
type AnyCoMapSchema,
6+
BaseAccountShape,
67
CoFeed,
7-
CoFeedSchema,
8-
CoListSchema,
9-
CoMapSchema,
8+
type CoFeedSchema,
9+
type CoListSchema,
10+
type CoMapSchema,
1011
CoPlainText,
11-
CoProfileSchema,
12-
CoRecordSchema,
12+
type CoProfileSchema,
13+
type CoRecordSchema,
1314
CoRichText,
14-
DefaultProfileShape,
15+
type DefaultProfileShape,
1516
FileStream,
16-
FileStreamSchema,
17+
type FileStreamSchema,
1718
ImageDefinition,
18-
PlainTextSchema,
19-
Simplify,
19+
type PlainTextSchema,
20+
type Simplify,
2021
zodSchemaToCoSchema,
2122
} from "../../internal.js";
2223
import { RichTextSchema } from "./schemaTypes/RichTextSchema.js";
@@ -82,16 +83,9 @@ export const coMapDefiner = <Shape extends z.core.$ZodLooseShape>(
8283
return enrichCoMapSchema(objectSchema);
8384
};
8485

85-
function enrichAccountSchema<
86-
Shape extends {
87-
profile: AnyCoMapSchema<{
88-
name: z.core.$ZodString<string>;
89-
inbox?: z.core.$ZodOptional<z.core.$ZodString>;
90-
inboxInvite?: z.core.$ZodOptional<z.core.$ZodString>;
91-
}>;
92-
root: AnyCoMapSchema;
93-
},
94-
>(schema: z.ZodObject<Shape, z.core.$strip>) {
86+
function enrichAccountSchema<Shape extends BaseAccountShape>(
87+
schema: z.ZodObject<Shape, z.core.$strip>,
88+
) {
9589
const enrichedSchema = Object.assign(schema, {
9690
collaborative: true,
9791
builtin: "Account",
@@ -142,16 +136,44 @@ function enrichAccountSchema<
142136
return enrichedSchema;
143137
}
144138

145-
export const coAccountDefiner = <
146-
Shape extends {
147-
profile: AnyCoMapSchema<{
148-
name: z.core.$ZodString<string>;
149-
inbox?: z.core.$ZodOptional<z.core.$ZodString>;
150-
inboxInvite?: z.core.$ZodOptional<z.core.$ZodString>;
151-
}>;
152-
root: AnyCoMapSchema;
153-
},
154-
>(
139+
/**
140+
* Defines a collaborative account schema for Jazz applications.
141+
*
142+
* Creates an account schema that represents a user account with profile and root data.
143+
* Accounts are the primary way to identify and manage users in Jazz applications.
144+
*
145+
* @template Shape - The shape of the account schema extending BaseAccountShape
146+
* @param shape - The account schema shape. Defaults to a basic profile with name, inbox, and inboxInvite fields, plus an empty root object.
147+
*
148+
* @example
149+
* ```typescript
150+
* // Basic account with default profile
151+
* const BasicAccount = co.account();
152+
*
153+
* // Custom account with specific profile and root structure
154+
* const JazzAccount = co.account({
155+
* profile: co.profile({
156+
* name: z.string(),
157+
* avatar: z.optional(z.string()),
158+
* }),
159+
* root: co.map({
160+
* organizations: co.list(Organization),
161+
* draftOrganization: DraftOrganization,
162+
* }),
163+
* }).withMigration(async (account) => {
164+
* // Migration logic for existing accounts
165+
* if (account.profile === undefined) {
166+
* const group = Group.create();
167+
* account.profile = co.profile().create(
168+
* { name: getRandomUsername() },
169+
* group
170+
* );
171+
* group.addMember("everyone", "reader");
172+
* }
173+
* });
174+
* ```
175+
*/
176+
export const coAccountDefiner = <Shape extends BaseAccountShape>(
155177
shape: Shape = {
156178
profile: coMapDefiner({
157179
name: z.string(),

0 commit comments

Comments
 (0)