Skip to content

Commit

Permalink
fix(federation): 🐛 Fix extensions schema being incorrectly non-optional
Browse files Browse the repository at this point in the history
  • Loading branch information
CPlusPatch committed May 15, 2024
1 parent f6b3741 commit 071a149
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
15 changes: 15 additions & 0 deletions federation/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
CustomEmojiExtension,
DislikeSchema,
EntitySchema,
ExtensionPropertySchema,
ExtensionSchema,
FollowAcceptSchema,
FollowRejectSchema,
Expand Down Expand Up @@ -83,6 +84,9 @@ class EntityValidator {
declare static $ActorPublicKeyData: InferType<
typeof ActorPublicKeyDataSchema
>;
declare static $ExtensionProperty: InferType<
typeof ExtensionPropertySchema
>;
declare static $VanityExtension: InferType<typeof VanityExtensionSchema>;
declare static $User: InferType<typeof UserSchema>;
declare static $Action: InferType<typeof ActionSchema>;
Expand Down Expand Up @@ -302,6 +306,17 @@ class EntityValidator {
public Entity(data: unknown): Promise<InferType<typeof EntitySchema>> {
return this.validate(EntitySchema, data);
}

/**
* Validates an ExtensionProperty.
* @param data - The data to validate
* @returns A promise that resolves to the validated data.
*/
public ExtensionProperty(
data: unknown,
): Promise<InferType<typeof ExtensionPropertySchema>> {
return this.validate(ExtensionPropertySchema, data);
}
}

export {
Expand Down
14 changes: 7 additions & 7 deletions federation/schemas/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { z } from "zod";
import { ContentFormatSchema } from "./content_format";
import { ExtensionPropertySchema } from "./extensions";
import { CustomEmojiExtension } from "./extensions/custom_emojis";
import { VanityExtensionSchema } from "./extensions/vanity";
import { extensionTypeRegex } from "./regex";
Expand All @@ -9,9 +10,7 @@ const EntitySchema = z.object({
created_at: z.string(),
uri: z.string().url(),
type: z.string(),
extensions: z.object({
"org.lysand:custom_emojis": CustomEmojiExtension.optional(),
}),
extensions: ExtensionPropertySchema.optional(),
});

const VisibilitySchema = z.enum(["public", "unlisted", "private", "direct"]);
Expand All @@ -27,7 +26,7 @@ const PublicationSchema = EntitySchema.extend({
subject: z.string().optional(),
is_sensitive: z.boolean().optional(),
visibility: VisibilitySchema,
extensions: EntitySchema.shape.extensions.extend({
extensions: ExtensionPropertySchema.extend({
"org.lysand:reactions": z
.object({
reactions: z.string(),
Expand All @@ -43,7 +42,7 @@ const PublicationSchema = EntitySchema.extend({
}),
})
.optional(),
}),
}).optional(),
});

const NoteSchema = PublicationSchema.extend({
Expand Down Expand Up @@ -85,9 +84,9 @@ const UserSchema = EntitySchema.extend({
dislikes: z.string().url(),
inbox: z.string().url(),
outbox: z.string().url(),
extensions: EntitySchema.shape.extensions.extend({
extensions: ExtensionPropertySchema.extend({
"org.lysand:vanity": VanityExtensionSchema.optional(),
}),
}).optional(),
});

const ActionSchema = EntitySchema.extend({
Expand Down Expand Up @@ -185,4 +184,5 @@ export {
ServerMetadataSchema,
ContentFormatSchema,
CustomEmojiExtension,
ExtensionPropertySchema,
};
6 changes: 6 additions & 0 deletions federation/schemas/extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { z } from "zod";
import { CustomEmojiExtension } from "./extensions/custom_emojis";

export const ExtensionPropertySchema = z.object({
"org.lysand:custom_emojis": CustomEmojiExtension.optional(),
});

0 comments on commit 071a149

Please sign in to comment.