refactor(federation): 🚚 Rename validators back to schemas, fix barrel file

This commit is contained in:
Jesse Wierzbinski 2024-09-23 09:44:20 +02:00
parent d43775ec6e
commit ce9be7e857
No known key found for this signature in database
20 changed files with 58 additions and 81 deletions

View file

@ -0,0 +1,52 @@
/**
* Custom emojis extension.
* @module federation/schemas/extensions/custom_emojis
* @see module:federation/schemas/base
* @see https://versia.pub/extensions/custom-emojis
*/
import { z } from "zod";
import { ImageOnlyContentFormatSchema } from "../content_format.ts";
import { emojiRegex } from "../regex.ts";
/**
* @description Used to validate the properties the extension's custom field
* @see https://versia.pub/extensions/custom-emojis
* @example
* {
* // ...
* "extensions": {
* "pub.versia:custom_emojis": {
* "emojis": [
* {
* "name": ":happy_face:",
* "url": {
* "image/png": {
* "content": "https://cdn.example.com/emojis/happy_face.png",
* "remote": true
* }
* }
* },
* // ...
* ]
* }
* }
* // ...
* }
*/
export const CustomEmojiExtensionSchema = z.object({
emojis: z.array(
z
.object({
name: z
.string()
.min(1)
.max(256)
.regex(
emojiRegex,
"Emoji name must be alphanumeric, underscores, or dashes, and surrounded by identifiers",
),
url: ImageOnlyContentFormatSchema,
})
.strict(),
),
});

View file

@ -0,0 +1,40 @@
import { z } from "zod";
import { EntitySchema } from "../base.ts";
/**
* @description Like entity
* @see https://versia.pub/extensions/likes
* @example
* {
* "id": "3e7e4750-afd4-4d99-a256-02f0710a0520",
* "type": "pub.versia:likes/Like",
* "created_at": "2021-01-01T00:00:00.000Z",
* "author": "https://example.com/users/6e0204a2-746c-4972-8602-c4f37fc63bbe",
* "uri": "https://example.com/likes/3e7e4750-afd4-4d99-a256-02f0710a0520",
* "liked": "https://otherexample.org/notes/fmKZ763jzIU8"
* }
*/
export const LikeSchema = EntitySchema.extend({
type: z.literal("pub.versia:likes/Like"),
author: z.string().url(),
liked: z.string().url(),
});
/**
* @description Dislike entity
* @see https://versia.pub/extensions/likes
* @example
* {
* "id": "3e7e4750-afd4-4d99-a256-02f0710a0520",
* "type": "pub.versia:likes/Dislike",
* "created_at": "2021-01-01T00:00:00.000Z",
* "author": "https://example.com/users/6e0204a2-746c-4972-8602-c4f37fc63bbe",
* "uri": "https://example.com/dislikes/3e7e4750-afd4-4d99-a256-02f0710a0520",
* "disliked": "https://otherexample.org/notes/fmKZ763jzIU8"
* }
*/
export const DislikeSchema = EntitySchema.extend({
type: z.literal("pub.versia:likes/Dislike"),
author: z.string().url(),
disliked: z.string().url(),
});

View file

@ -0,0 +1,33 @@
/**
* Polls extension
* @module federation/schemas/extensions/polls
* @see module:federation/schemas/base
* @see https://versia.pub/extensions/polls
*/
import { z } from "zod";
import { EntitySchema } from "../base.ts";
/**
* @description Vote extension entity
* @see https://versia.pub/extensions/polls
* @example
* {
* "id": "6f27bc77-58ee-4c9b-b804-8cc1c1182fa9",
* "type": "pub.versia:polls/Vote",
* "uri": "https://example.com/actions/6f27bc77-58ee-4c9b-b804-8cc1c1182fa9",
* "created_at": "2021-01-01T00:00:00.000Z",
* "author": "https://example.com/users/6e0204a2-746c-4972-8602-c4f37fc63bbe",
* "poll": "https://example.com/notes/f08a124e-fe90-439e-8be4-15a428a72a19",
* "option": 1
* }
*/
export const VoteSchema = EntitySchema.extend({
type: z.literal("pub.versia:polls/Vote"),
author: z.string().url(),
poll: z.string().url(),
option: z
.number()
.int()
.nonnegative()
.max(2 ** 64 - 1),
});

View file

@ -0,0 +1,29 @@
/**
* Reactions extension
* @module federation/schemas/extensions/reactions
* @see module:federation/schemas/base
* @see https://versia.pub/extensions/reactions
*/
import { z } from "zod";
import { EntitySchema } from "../base.ts";
/**
* @description Reaction extension entity
* @see https://versia.pub/extensions/reactions
* @example
* {
* "id": "6f27bc77-58ee-4c9b-b804-8cc1c1182fa9",
* "type": "pub.versia:reactions/Reaction",
* "uri": "https://example.com/actions/6f27bc77-58ee-4c9b-b804-8cc1c1182fa9",
* "created_at": "2021-01-01T00:00:00.000Z",
* "author": "https://example.com/users/6e0204a2-746c-4972-8602-c4f37fc63bbe",
* "object": "https://example.com/publications/f08a124e-fe90-439e-8be4-15a428a72a19",
* "content": "😀",
* }
*/
export const ReactionSchema = EntitySchema.extend({
type: z.literal("pub.versia:reactions/Reaction"),
author: z.string().url(),
object: z.string().url(),
content: z.string().min(1).max(256),
});

View file

@ -0,0 +1,21 @@
import { z } from "zod";
import { EntitySchema } from "../base.ts";
/**
* @description Share entity
* @see https://versia.pub/extensions/share
* @example
* {
* "id": "3e7e4750-afd4-4d99-a256-02f0710a0520",
* "type": "pub.versia:share/Share",
* "created_at": "2021-01-01T00:00:00.000Z",
* "author": "https://example.com/users/6e0204a2-746c-4972-8602-c4f37fc63bbe",
* "uri": "https://example.com/shares/3e7e4750-afd4-4d99-a256-02f0710a0520",
* "shared": "https://otherexample.org/notes/fmKZ763jzIU8"
* }
*/
export const ShareSchema = EntitySchema.extend({
type: z.literal("pub.versia:share/Share"),
author: z.string().url(),
shared: z.string().url(),
});

View file

@ -0,0 +1,107 @@
/**
* Vanity extension schema.
* @module federation/schemas/extensions/vanity
* @see module:federation/schemas/base
* @see https://versia.pub/extensions/vanity
*/
import { z } from "zod";
import {
AudioOnlyContentFormatSchema,
ImageOnlyContentFormatSchema,
} from "../content_format.ts";
import { isISOString } from "../regex.ts";
/**
* @description Vanity extension entity
* @see https://versia.pub/extensions/vanity
* @example
* {
* // ...
* "type": "User",
* // ...
* "extensions": {
* "pub.versia:vanity": {
* "avatar_overlays": [
* {
* "image/png": {
* "content": "https://cdn.example.com/ab5081cf-b11f-408f-92c2-7c246f290593/cat_ears.png",
* "remote": true,
* }
* }
* ],
* "avatar_mask": {
* "image/png": {
* "content": "https://cdn.example.com/d8c42be1-d0f7-43ef-b4ab-5f614e1beba4/rounded_square.jpeg",
* "remote": true,
* }
* },
* "background": {
* "image/png": {
* "content": "https://cdn.example.com/6492ddcd-311e-4921-9567-41b497762b09/untitled-file-0019822.png",
* "remote": true,
* }
* },
* "audio": {
* "audio/mpeg": {
* "content": "https://cdn.example.com/4da2f0d4-4728-4819-83e4-d614e4c5bebc/michael-jackson-thriller.mp3",
* "remote": true,
* }
* },
* "pronouns": {
* "en-us": [
* "he/him",
* {
* "subject": "they",
* "object": "them",
* "dependent_possessive": "their",
* "independent_possessive": "theirs",
* "reflexive": "themself"
* },
* ]
* },
* "birthday": "1998-04-12",
* "location": "+40.6894-074.0447/",
* "aliases": [
* "https://burger.social/accounts/349ee237-c672-41c1-aadc-677e185f795a",
* "https://versia.social/users/f565ef02-035d-4974-ba5e-f62a8558331d"
* ]
* }
* }
* }
*/
export const VanityExtensionSchema = z
.object({
avatar_overlays: z
.array(ImageOnlyContentFormatSchema)
.optional()
.nullable(),
avatar_mask: ImageOnlyContentFormatSchema.optional().nullable(),
background: ImageOnlyContentFormatSchema.optional().nullable(),
audio: AudioOnlyContentFormatSchema.optional().nullable(),
pronouns: z.record(
z.string(),
z.array(
z.union([
z
.object({
subject: z.string(),
object: z.string(),
dependent_possessive: z.string(),
independent_possessive: z.string(),
reflexive: z.string(),
})
.strict(),
z.string(),
]),
),
),
birthday: z
.string()
.refine((v) => isISOString(v), "must be a valid ISO8601 datetime")
.optional()
.nullable(),
location: z.string().optional().nullable(),
aliases: z.array(z.string().url()).optional().nullable(),
})
.strict();