Add contentSchema helper
This commit is contained in:
parent
a7e1350a65
commit
1dec42cd9f
3 changed files with 8 additions and 5 deletions
|
@ -5,7 +5,7 @@ import emojify from 'soapbox/features/emoji';
|
||||||
|
|
||||||
import { customEmojiSchema } from './custom-emoji';
|
import { customEmojiSchema } from './custom-emoji';
|
||||||
import { relationshipSchema } from './relationship';
|
import { relationshipSchema } from './relationship';
|
||||||
import { filteredArray, makeCustomEmojiMap } from './utils';
|
import { contentSchema, filteredArray, makeCustomEmojiMap } from './utils';
|
||||||
|
|
||||||
const avatarMissing = require('assets/images/avatar-missing.png');
|
const avatarMissing = require('assets/images/avatar-missing.png');
|
||||||
const headerMissing = require('assets/images/header-missing.png');
|
const headerMissing = require('assets/images/header-missing.png');
|
||||||
|
@ -39,7 +39,7 @@ const accountSchema = z.object({
|
||||||
z.string(),
|
z.string(),
|
||||||
z.null(),
|
z.null(),
|
||||||
]).catch(null),
|
]).catch(null),
|
||||||
note: z.string().catch(''),
|
note: contentSchema,
|
||||||
pleroma: z.any(), // TODO
|
pleroma: z.any(), // TODO
|
||||||
source: z.any(), // TODO
|
source: z.any(), // TODO
|
||||||
statuses_count: z.number().catch(0),
|
statuses_count: z.number().catch(0),
|
||||||
|
|
|
@ -3,14 +3,14 @@ import { z } from 'zod';
|
||||||
import { attachmentSchema } from './attachment';
|
import { attachmentSchema } from './attachment';
|
||||||
import { cardSchema } from './card';
|
import { cardSchema } from './card';
|
||||||
import { customEmojiSchema } from './custom-emoji';
|
import { customEmojiSchema } from './custom-emoji';
|
||||||
import { emojiSchema, filteredArray } from './utils';
|
import { contentSchema, emojiSchema, filteredArray } from './utils';
|
||||||
|
|
||||||
const chatMessageSchema = z.object({
|
const chatMessageSchema = z.object({
|
||||||
account_id: z.string(),
|
account_id: z.string(),
|
||||||
media_attachments: filteredArray(attachmentSchema),
|
media_attachments: filteredArray(attachmentSchema),
|
||||||
card: cardSchema.nullable().catch(null),
|
card: cardSchema.nullable().catch(null),
|
||||||
chat_id: z.string(),
|
chat_id: z.string(),
|
||||||
content: z.string().catch(''),
|
content: contentSchema,
|
||||||
created_at: z.string().datetime().catch(new Date().toUTCString()),
|
created_at: z.string().datetime().catch(new Date().toUTCString()),
|
||||||
emojis: filteredArray(customEmojiSchema),
|
emojis: filteredArray(customEmojiSchema),
|
||||||
expiration: z.number().optional().catch(undefined),
|
expiration: z.number().optional().catch(undefined),
|
||||||
|
|
|
@ -2,6 +2,9 @@ import z from 'zod';
|
||||||
|
|
||||||
import type { CustomEmoji } from './custom-emoji';
|
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. */
|
/** Validates individual items in an array, dropping any that aren't valid. */
|
||||||
function filteredArray<T extends z.ZodTypeAny>(schema: T) {
|
function filteredArray<T extends z.ZodTypeAny>(schema: T) {
|
||||||
return z.any().array().catch([])
|
return z.any().array().catch([])
|
||||||
|
@ -24,4 +27,4 @@ function makeCustomEmojiMap(customEmojis: CustomEmoji[]) {
|
||||||
}, {});
|
}, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
export { filteredArray, makeCustomEmojiMap, emojiSchema };
|
export { filteredArray, makeCustomEmojiMap, emojiSchema, contentSchema };
|
Loading…
Reference in a new issue