fix(SW-285): fix validation

This commit is contained in:
Chuma McPhoy
2024-08-29 11:16:41 +02:00
parent fd9d6d5a1c
commit b806824fde

View File

@@ -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<ContentPageDataRaw>(
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",
}