diff --git a/sanityv3/schemas/objects/promotion/promoteTopic.tsx b/sanityv3/schemas/objects/promotion/promoteTopic.tsx index d237f9822..d82da868e 100644 --- a/sanityv3/schemas/objects/promotion/promoteTopic.tsx +++ b/sanityv3/schemas/objects/promotion/promoteTopic.tsx @@ -62,7 +62,7 @@ export default { description: 'A short and catchy introduction text for this topic content card (max. 215 chars)', type: 'array', of: [introBlockContentType], - validation: (Rule: Rule) => Rule.custom((value: any) => validateCharCounterEditor(value, 215)), + validation: (Rule: Rule) => Rule.custom((value: any) => validateCharCounterEditor(value, 215, true)), }, ], preview: { diff --git a/sanityv3/schemas/validations/validateCharCounterEditor.ts b/sanityv3/schemas/validations/validateCharCounterEditor.ts index e6b7540e2..a4def83e7 100644 --- a/sanityv3/schemas/validations/validateCharCounterEditor.ts +++ b/sanityv3/schemas/validations/validateCharCounterEditor.ts @@ -1,14 +1,18 @@ -export const validateCharCounterEditor = (value: PortableTextBlock[], charLimit: number) => { - if (!value || value.length === 0) { - return 'Required' +import type { PortableTextBlock } from 'sanity' + +export const validateCharCounterEditor = (value: PortableTextBlock[], charLimit: number, allowZeroLength = false) => { + if (!value || (value.length === 0 && !allowZeroLength)) { + return allowZeroLength ? true : 'Required' } - const count = value[0].children.reduce( - (total: any, current: { text: string | any[] }) => total + current.text.length, - 0, - ) + const count = value[0]?.children + ? (value[0].children as { text: string }[]).reduce( + (total: number, current: { text: string }) => total + current.text.length, + 0, + ) + : null - if (count > charLimit) { + if (count !== null && count > charLimit) { return `The introduction should be no longer than ${charLimit} characters. Currently ${count} characters long.` }