From 1dec42cd9fcb2e56c3dbb383a5d4c3bd453a09c2 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 4 May 2023 11:42:20 -0500 Subject: [PATCH] Add contentSchema helper --- app/soapbox/schemas/account.ts | 4 ++-- app/soapbox/schemas/chat-message.ts | 4 ++-- app/soapbox/schemas/utils.ts | 5 ++++- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app/soapbox/schemas/account.ts b/app/soapbox/schemas/account.ts index 9381edbbd..51202d2e6 100644 --- a/app/soapbox/schemas/account.ts +++ b/app/soapbox/schemas/account.ts @@ -5,7 +5,7 @@ import emojify from 'soapbox/features/emoji'; import { customEmojiSchema } from './custom-emoji'; import { relationshipSchema } from './relationship'; -import { filteredArray, makeCustomEmojiMap } from './utils'; +import { contentSchema, filteredArray, makeCustomEmojiMap } from './utils'; const avatarMissing = require('assets/images/avatar-missing.png'); const headerMissing = require('assets/images/header-missing.png'); @@ -39,7 +39,7 @@ const accountSchema = z.object({ z.string(), z.null(), ]).catch(null), - note: z.string().catch(''), + note: contentSchema, pleroma: z.any(), // TODO source: z.any(), // TODO statuses_count: z.number().catch(0), diff --git a/app/soapbox/schemas/chat-message.ts b/app/soapbox/schemas/chat-message.ts index 121ef9d5f..fe0ff0f6b 100644 --- a/app/soapbox/schemas/chat-message.ts +++ b/app/soapbox/schemas/chat-message.ts @@ -3,14 +3,14 @@ import { z } from 'zod'; import { attachmentSchema } from './attachment'; import { cardSchema } from './card'; import { customEmojiSchema } from './custom-emoji'; -import { emojiSchema, filteredArray } from './utils'; +import { contentSchema, emojiSchema, filteredArray } from './utils'; const chatMessageSchema = z.object({ account_id: z.string(), media_attachments: filteredArray(attachmentSchema), card: cardSchema.nullable().catch(null), chat_id: z.string(), - content: z.string().catch(''), + content: contentSchema, created_at: z.string().datetime().catch(new Date().toUTCString()), emojis: filteredArray(customEmojiSchema), expiration: z.number().optional().catch(undefined), diff --git a/app/soapbox/schemas/utils.ts b/app/soapbox/schemas/utils.ts index 1e53e11aa..b64185800 100644 --- a/app/soapbox/schemas/utils.ts +++ b/app/soapbox/schemas/utils.ts @@ -2,6 +2,9 @@ import z from 'zod'; import type { CustomEmoji } from './custom-emoji'; +/** Ensure HTML content is a string, and drop empty `

` tags. */ +const contentSchema = z.string().catch('').transform((value) => value === '

' ? '' : value); + /** Validates individual items in an array, dropping any that aren't valid. */ function filteredArray(schema: T) { return z.any().array().catch([]) @@ -24,4 +27,4 @@ function makeCustomEmojiMap(customEmojis: CustomEmoji[]) { }, {}); } -export { filteredArray, makeCustomEmojiMap, emojiSchema }; \ No newline at end of file +export { filteredArray, makeCustomEmojiMap, emojiSchema, contentSchema }; \ No newline at end of file