From 802cc57df9f3fb67704e0be412b44e8dcab1f30b Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Thu, 10 Oct 2024 08:40:23 +0200 Subject: [PATCH] feat(preview): throw TypeError if internationalizedArrayString fails type guard --- studio/schemas/documents/compensations.ts | 10 ++++++---- studio/schemas/fields/callToActionFields.ts | 10 ++++++---- .../objects/compensations/benefitsByLocation.ts | 10 ++++++---- studio/schemas/objects/link.ts | 10 ++++++---- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/studio/schemas/documents/compensations.ts b/studio/schemas/documents/compensations.ts index 82e9bc20e..7aa300b1b 100644 --- a/studio/schemas/documents/compensations.ts +++ b/studio/schemas/documents/compensations.ts @@ -49,11 +49,13 @@ const compensations = defineType({ title: title.name, }, prepare({ title }) { - const translatedTitle = isInternationalizedString(title) - ? firstTranslation(title) - : null; + if (!isInternationalizedString(title)) { + throw new TypeError( + `Expected 'title' to be InternationalizedString, was ${typeof title}`, + ); + } return { - title: translatedTitle ?? undefined, + title: firstTranslation(title) ?? undefined, }; }, }, diff --git a/studio/schemas/fields/callToActionFields.ts b/studio/schemas/fields/callToActionFields.ts index 1eaacc6bd..9edf177c7 100644 --- a/studio/schemas/fields/callToActionFields.ts +++ b/studio/schemas/fields/callToActionFields.ts @@ -18,11 +18,13 @@ const callToActionField = defineField({ title: "linkTitle", }, prepare({ title }) { - const translatedTitle = isInternationalizedString(title) - ? firstTranslation(title) - : null; + if (!isInternationalizedString(title)) { + throw new TypeError( + `Expected 'title' to be InternationalizedString, was ${typeof title}`, + ); + } return { - title: translatedTitle ?? undefined, + title: firstTranslation(title) ?? undefined, subtitle: "Call to Action", }; }, diff --git a/studio/schemas/objects/compensations/benefitsByLocation.ts b/studio/schemas/objects/compensations/benefitsByLocation.ts index 33238637f..3eefe6778 100644 --- a/studio/schemas/objects/compensations/benefitsByLocation.ts +++ b/studio/schemas/objects/compensations/benefitsByLocation.ts @@ -80,14 +80,16 @@ export const benefitsByLocation = defineField({ type: benefitType.name, }, prepare({ title, type }) { - const translatedTitle = isInternationalizedString(title) - ? firstTranslation(title) - : null; + if (!isInternationalizedString(title)) { + throw new TypeError( + `Expected 'title' to be InternationalizedString, was ${typeof title}`, + ); + } const subtitle = BENEFIT_TYPES.find((o) => o.value === type)?.title ?? "Unknown benefit type"; return { - title: translatedTitle ?? undefined, + title: firstTranslation(title) ?? undefined, subtitle, }; }, diff --git a/studio/schemas/objects/link.ts b/studio/schemas/objects/link.ts index 0d6107d70..abc752612 100644 --- a/studio/schemas/objects/link.ts +++ b/studio/schemas/objects/link.ts @@ -193,11 +193,13 @@ export const link = defineField({ type: "linkType", }, prepare({ title, type }) { - const translatedTitle = isInternationalizedString(title) - ? firstTranslation(title) - : null; + if (!isInternationalizedString(title)) { + throw new TypeError( + `Expected 'title' to be InternationalizedString, was ${typeof title}`, + ); + } return { - title: translatedTitle ?? undefined, + title: firstTranslation(title) ?? undefined, subtitle: type ? type.charAt(0).toUpperCase() + type.slice(1) : "", }; },