From b806824fde141938851b4e1a05969c5a28f2c2b2 Mon Sep 17 00:00:00 2001 From: Chuma McPhoy Date: Thu, 29 Aug 2024 11:16:41 +0200 Subject: [PATCH] fix(SW-285): fix validation --- .../routers/contentstack/contentPage/query.ts | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/server/routers/contentstack/contentPage/query.ts b/server/routers/contentstack/contentPage/query.ts index 73de1479e..24099a24e 100644 --- a/server/routers/contentstack/contentPage/query.ts +++ b/server/routers/contentstack/contentPage/query.ts @@ -9,6 +9,7 @@ import { makeImageVaultImage } from "@/utils/imageVault" import { validateContentPageSchema } from "./output" +import { ImageVaultAsset } from "@/types/components/imageVault" import { TrackingChannelEnum, TrackingSDKPageData, @@ -27,45 +28,45 @@ export const contentPageQueryRouter = router({ const response = await request( GetContentPage, - { - locale: lang, - uid, - }, - { - cache: "force-cache", - next: { - tags: [generateTag(lang, uid)], - }, - } + { locale: lang, uid }, + { cache: "force-cache", next: { tags: [generateTag(lang, uid)] } } ) - if (!response.data) { + const { content_page } = response.data + if (!content_page) { throw notFound(response) } - const validatedContentPage = validateContentPageSchema.safeParse( - response.data - ) + const validatedContentPage = validateContentPageSchema.safeParse({ + content_page: { + ...content_page, + hero_image: makeImageVaultImage(content_page.hero_image), + }, + }) if (!validatedContentPage.success) { console.error( `Failed to validate Contentpage Data - (lang: ${lang}, uid: ${uid})` ) - console.error(validatedContentPage.error) + console.error(validatedContentPage.error?.format()) return null } - const contentPageData = validatedContentPage.data.content_page + // Destructure hero_image and rename it to heroImage + const { hero_image: heroImage, ...restContentPage } = + validatedContentPage.data.content_page + + // Construct the contentPage object with the correct structure const contentPage: ContentPage = { - ...contentPageData, - heroImage: makeImageVaultImage(contentPageData.hero_image), + ...restContentPage, + heroImage: heroImage as ImageVaultAsset | undefined, } const tracking: TrackingSDKPageData = { - pageId: contentPageData.system.uid, - lang: contentPageData.system.locale as Lang, - publishedDate: contentPageData.system.updated_at, - createdDate: contentPageData.system.created_at, + pageId: contentPage.system.uid, + lang: contentPage.system.locale as Lang, + publishedDate: contentPage.system.updated_at, + createdDate: contentPage.system.created_at, channel: TrackingChannelEnum["static-content-page"], pageType: "staticcontentpage", }