fix: validation for header
This commit is contained in:
@@ -1,10 +1,17 @@
|
||||
import { z } from "zod"
|
||||
import { z, ZodError, ZodIssueCode } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
import { discriminatedUnion } from "@/lib/discriminatedUnion"
|
||||
import {
|
||||
cardBlockRefsSchema,
|
||||
cardBlockSchema,
|
||||
transformCardBlock,
|
||||
transformCardBlockRefs,
|
||||
} from "@/server/routers/contentstack/schemas/blocks/cardsGrid"
|
||||
import * as pageLinks from "@/server/routers/contentstack/schemas/pageLinks"
|
||||
|
||||
import { removeMultipleSlashes } from "@/utils/url"
|
||||
|
||||
import { imageVaultAssetTransformedSchema } from "../schemas/imageVault"
|
||||
import { systemSchema } from "../schemas/system"
|
||||
|
||||
import { Image } from "@/types/image"
|
||||
@@ -430,228 +437,219 @@ export const validateFooterRefConfigSchema = z.object({
|
||||
}),
|
||||
})
|
||||
|
||||
const linkConnectionNodeSchema = z
|
||||
/**
|
||||
* New Header Validation
|
||||
*/
|
||||
|
||||
const linkRefsUnionSchema = z.discriminatedUnion("__typename", [
|
||||
pageLinks.contentPageRefSchema,
|
||||
pageLinks.hotelPageRefSchema,
|
||||
pageLinks.loyaltyPageRefSchema,
|
||||
])
|
||||
|
||||
const linkRefsSchema = z
|
||||
.object({
|
||||
edges: z
|
||||
.array(
|
||||
linkConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
system: systemSchema,
|
||||
url: z.string(),
|
||||
title: z.string(),
|
||||
web: z.object({
|
||||
original_url: z.string(),
|
||||
}),
|
||||
}),
|
||||
node: linkRefsUnionSchema,
|
||||
})
|
||||
)
|
||||
.max(1),
|
||||
})
|
||||
.transform((data) => {
|
||||
const node = data.edges[0]?.node
|
||||
if (!node) {
|
||||
return null
|
||||
}
|
||||
const url = node.url
|
||||
const originalUrl = node.web?.original_url
|
||||
const lang = node.system.locale
|
||||
|
||||
return {
|
||||
href: originalUrl || removeMultipleSlashes(`/${lang}/${url}`),
|
||||
isExternal: !!originalUrl,
|
||||
}
|
||||
})
|
||||
|
||||
const linkWithTitleSchema = z
|
||||
.object({
|
||||
title: z.string(),
|
||||
linkConnection: linkConnectionNodeSchema,
|
||||
})
|
||||
.transform((rawData) => {
|
||||
return rawData.linkConnection && rawData.title
|
||||
? {
|
||||
...rawData.linkConnection,
|
||||
title: rawData.title,
|
||||
}
|
||||
: null
|
||||
})
|
||||
|
||||
const cardButtonSchema = z
|
||||
.object({
|
||||
cta_text: z.string(),
|
||||
external_link: z.object({
|
||||
href: z.string(),
|
||||
title: z.string(),
|
||||
),
|
||||
}),
|
||||
is_contentstack_link: z.boolean(),
|
||||
linkConnection: linkConnectionNodeSchema,
|
||||
open_in_new_tab: z.boolean(),
|
||||
})
|
||||
.transform((data) => {
|
||||
const linkConnectionData = data.linkConnection
|
||||
const isContentstackLink = data.is_contentstack_link
|
||||
const externalLink = data.external_link
|
||||
const href =
|
||||
isContentstackLink && externalLink.href
|
||||
? externalLink.href
|
||||
: linkConnectionData?.href ?? ""
|
||||
|
||||
return {
|
||||
openInNewTab: data.open_in_new_tab,
|
||||
title: data.cta_text,
|
||||
href,
|
||||
isExternal: !isContentstackLink || linkConnectionData?.isExternal,
|
||||
if (data.linkConnection.edges.length) {
|
||||
const link = pageLinks.transformRef(data.linkConnection.edges[0].node)
|
||||
if (link) {
|
||||
return {
|
||||
link,
|
||||
}
|
||||
}
|
||||
}
|
||||
return { link: null }
|
||||
})
|
||||
|
||||
const cardConnectionSchema = z
|
||||
.object({
|
||||
edges: z
|
||||
.array(
|
||||
const menuItemsRefsSchema = z.intersection(
|
||||
linkRefsSchema,
|
||||
z
|
||||
.object({
|
||||
cardConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: cardBlockRefsSchema,
|
||||
})
|
||||
),
|
||||
}),
|
||||
see_all_link: linkRefsSchema,
|
||||
submenu: z.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
heading: z.string(),
|
||||
body_text: z.string(),
|
||||
background_image: imageVaultAssetTransformedSchema,
|
||||
has_primary_button: z.boolean(),
|
||||
has_secondary_button: z.boolean(),
|
||||
scripted_top_title: z.string(),
|
||||
primary_button: cardButtonSchema.nullable(),
|
||||
secondary_button: cardButtonSchema.nullable(),
|
||||
}),
|
||||
links: z.array(linkRefsSchema),
|
||||
})
|
||||
)
|
||||
.max(1),
|
||||
})
|
||||
.transform((data) => {
|
||||
const node = data.edges[0]?.node
|
||||
if (!node) {
|
||||
return null
|
||||
}
|
||||
),
|
||||
})
|
||||
.transform((data) => {
|
||||
let card = null
|
||||
if (data.cardConnection.edges.length) {
|
||||
card = transformCardBlockRefs(data.cardConnection.edges[0].node)
|
||||
}
|
||||
|
||||
return {
|
||||
scriptedTopTitle: node.scripted_top_title,
|
||||
heading: node.heading,
|
||||
bodyText: node.body_text,
|
||||
backgroundImage: node.background_image,
|
||||
primaryButton: node.has_primary_button ? node.primary_button : null,
|
||||
secondaryButton: node.has_secondary_button ? node.secondary_button : null,
|
||||
}
|
||||
})
|
||||
return {
|
||||
card,
|
||||
see_all_link: data.see_all_link,
|
||||
submenu: data.submenu,
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
export const menuItemSchema = z
|
||||
.object({
|
||||
title: z.string(),
|
||||
linkConnection: linkConnectionNodeSchema,
|
||||
submenu: z.array(
|
||||
z.object({
|
||||
title: z.string(),
|
||||
links: z.array(linkWithTitleSchema),
|
||||
})
|
||||
),
|
||||
see_all_link: linkWithTitleSchema,
|
||||
cardConnection: cardConnectionSchema,
|
||||
})
|
||||
.transform((data) => {
|
||||
const { submenu, linkConnection, cardConnection, see_all_link, title } =
|
||||
data
|
||||
return {
|
||||
title,
|
||||
link: submenu.length ? null : linkConnection,
|
||||
seeAllLink: submenu.length ? see_all_link : null,
|
||||
submenu,
|
||||
card: cardConnection,
|
||||
}
|
||||
})
|
||||
|
||||
export const getHeaderSchema = z
|
||||
export const headerRefsSchema = z
|
||||
.object({
|
||||
all_header: z.object({
|
||||
items: z
|
||||
.array(
|
||||
z.object({
|
||||
top_link: linkWithTitleSchema,
|
||||
menu_items: z.array(menuItemSchema),
|
||||
menu_items: z.array(menuItemsRefsSchema),
|
||||
system: systemSchema,
|
||||
top_link: linkRefsSchema,
|
||||
})
|
||||
)
|
||||
.length(1),
|
||||
.max(1),
|
||||
}),
|
||||
})
|
||||
.transform((data) => {
|
||||
const { top_link, menu_items } = data.all_header.items[0]
|
||||
if (!data.all_header.items.length) {
|
||||
console.info(`Zod Error - No header returned in refs request`)
|
||||
throw new ZodError([
|
||||
{
|
||||
code: ZodIssueCode.custom,
|
||||
fatal: true,
|
||||
message: "No header returned (Refs)",
|
||||
path: ["all_header.items"],
|
||||
},
|
||||
])
|
||||
}
|
||||
|
||||
return {
|
||||
topLink: top_link,
|
||||
menuItems: menu_items,
|
||||
header: data.all_header.items[0],
|
||||
}
|
||||
})
|
||||
|
||||
const linkConnectionRefs = z.object({
|
||||
edges: z
|
||||
.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
system: systemSchema,
|
||||
}),
|
||||
})
|
||||
)
|
||||
.max(1),
|
||||
})
|
||||
const linkUnionSchema = z.discriminatedUnion("__typename", [
|
||||
pageLinks.contentPageSchema,
|
||||
pageLinks.hotelPageSchema,
|
||||
pageLinks.loyaltyPageSchema,
|
||||
])
|
||||
|
||||
const cardConnectionRefs = z.object({
|
||||
primary_button: z
|
||||
.object({
|
||||
linkConnection: linkConnectionRefs,
|
||||
})
|
||||
.nullable(),
|
||||
secondary_button: z
|
||||
.object({
|
||||
linkConnection: linkConnectionRefs,
|
||||
})
|
||||
.nullable(),
|
||||
system: z.object({
|
||||
content_type_uid: z.string(),
|
||||
uid: z.string(),
|
||||
}),
|
||||
})
|
||||
|
||||
export const getHeaderRefSchema = z.object({
|
||||
all_header: z.object({
|
||||
items: z
|
||||
.array(
|
||||
const linkSchema = z
|
||||
.object({
|
||||
linkConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
top_link: z
|
||||
.object({
|
||||
linkConnection: linkConnectionRefs,
|
||||
})
|
||||
.nullable(),
|
||||
menu_items: z.array(
|
||||
node: discriminatedUnion(linkUnionSchema.options),
|
||||
})
|
||||
),
|
||||
}),
|
||||
})
|
||||
.transform((data) => {
|
||||
if (data.linkConnection.edges.length) {
|
||||
const link = pageLinks.transform(data.linkConnection.edges[0].node)
|
||||
if (link) {
|
||||
return {
|
||||
link,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
link: null,
|
||||
}
|
||||
})
|
||||
|
||||
const titleSchema = z.object({
|
||||
title: z.string().optional().default(""),
|
||||
})
|
||||
|
||||
/**
|
||||
* Intersection has to be used since you are not
|
||||
* allowed to merge two schemas where one uses
|
||||
* transform
|
||||
*/
|
||||
const linkAndTitleSchema = z.intersection(linkSchema, titleSchema)
|
||||
|
||||
/**
|
||||
* Same as above 👆
|
||||
*/
|
||||
export const menuItemSchema = z
|
||||
.intersection(
|
||||
linkAndTitleSchema,
|
||||
z
|
||||
.object({
|
||||
cardConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
linkConnection: linkConnectionRefs,
|
||||
see_all_link: z.object({
|
||||
linkConnection: linkConnectionRefs,
|
||||
}),
|
||||
cardConnection: z.object({
|
||||
edges: z
|
||||
.array(
|
||||
z.object({
|
||||
node: cardConnectionRefs,
|
||||
})
|
||||
)
|
||||
.max(1),
|
||||
}),
|
||||
submenu: z.array(
|
||||
z.object({
|
||||
links: z.array(
|
||||
z.object({ linkConnection: linkConnectionRefs })
|
||||
),
|
||||
})
|
||||
),
|
||||
node: cardBlockSchema,
|
||||
})
|
||||
),
|
||||
system: systemSchema,
|
||||
})
|
||||
)
|
||||
.length(1),
|
||||
}),
|
||||
})
|
||||
}),
|
||||
see_all_link: linkAndTitleSchema,
|
||||
submenu: z.array(
|
||||
z.object({
|
||||
links: z.array(linkAndTitleSchema),
|
||||
title: z.string().optional().default(""),
|
||||
})
|
||||
),
|
||||
})
|
||||
.transform((data) => {
|
||||
let card = null
|
||||
if (data.cardConnection.edges.length) {
|
||||
card = transformCardBlock(data.cardConnection.edges[0].node)
|
||||
}
|
||||
|
||||
return {
|
||||
card,
|
||||
seeAllLink: data.see_all_link,
|
||||
submenu: data.submenu,
|
||||
}
|
||||
})
|
||||
)
|
||||
.transform((data) => {
|
||||
return {
|
||||
...data,
|
||||
link: data.submenu.length ? null : data.link,
|
||||
seeAllLink: data.submenu.length ? data.seeAllLink : null,
|
||||
}
|
||||
})
|
||||
|
||||
export const headerSchema = z
|
||||
.object({
|
||||
all_header: z.object({
|
||||
items: z
|
||||
.array(
|
||||
z.object({
|
||||
menu_items: z.array(menuItemSchema),
|
||||
top_link: linkAndTitleSchema,
|
||||
})
|
||||
)
|
||||
.max(1),
|
||||
}),
|
||||
})
|
||||
.transform((data) => {
|
||||
if (!data.all_header.items.length) {
|
||||
console.info(`Zod Error - No header returned in request`)
|
||||
throw new ZodError([
|
||||
{
|
||||
code: ZodIssueCode.custom,
|
||||
fatal: true,
|
||||
message: "No header returned",
|
||||
path: ["all_header.items"],
|
||||
},
|
||||
])
|
||||
}
|
||||
|
||||
const header = data.all_header.items[0]
|
||||
return {
|
||||
header: {
|
||||
menuItems: header.menu_items,
|
||||
topLink: header.top_link,
|
||||
},
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user