Add contentSchema helper

This commit is contained in:
Alex Gleason 2023-05-04 11:42:20 -05:00
parent a7e1350a65
commit 1dec42cd9f
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
3 changed files with 8 additions and 5 deletions

View file

@ -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),

View file

@ -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),

View file

@ -2,6 +2,9 @@ import z from 'zod';
import type { CustomEmoji } from './custom-emoji';
/** Ensure HTML content is a string, and drop empty `<p>` tags. */
const contentSchema = z.string().catch('').transform((value) => value === '<p></p>' ? '' : value);
/** Validates individual items in an array, dropping any that aren't valid. */
function filteredArray<T extends z.ZodTypeAny>(schema: T) {
return z.any().array().catch([])
@ -24,4 +27,4 @@ function makeCustomEmojiMap(customEmojis: CustomEmoji[]) {
}, {});
}
export { filteredArray, makeCustomEmojiMap, emojiSchema };
export { filteredArray, makeCustomEmojiMap, emojiSchema, contentSchema };