373 lines
9.1 KiB
TypeScript
373 lines
9.1 KiB
TypeScript
import { z } from "zod"
|
|
|
|
import { Lang } from "@/constants/languages"
|
|
|
|
import { imageVaultAssetSchema } from "../schemas/imageVault"
|
|
|
|
import {
|
|
CardsGridEnum,
|
|
ContentBlocksTypenameEnum,
|
|
DynamicContentComponentEnum,
|
|
JoinLoyaltyContactTypenameEnum,
|
|
SidebarDynamicComponentEnum,
|
|
SidebarTypenameEnum,
|
|
} from "@/types/components/content/enums"
|
|
import { ImageVaultAsset } from "@/types/components/imageVault"
|
|
import { Embeds } from "@/types/requests/embeds"
|
|
import { PageLinkEnum } from "@/types/requests/pageLinks"
|
|
import { RTEEmbedsEnum } from "@/types/requests/rte"
|
|
import { EdgesWithTotalCount } from "@/types/requests/utils/edges"
|
|
import { RTEDocument } from "@/types/rte/node"
|
|
|
|
// Block schemas
|
|
export const contentPageBlockTextContent = z.object({
|
|
__typename: z.literal(ContentBlocksTypenameEnum.ContentPageBlocksContent),
|
|
content: z.object({
|
|
content: z.object({
|
|
embedded_itemsConnection: z.object({
|
|
edges: z.array(z.any()),
|
|
totalCount: z.number(),
|
|
}),
|
|
json: z.any(),
|
|
}),
|
|
}),
|
|
})
|
|
|
|
export const contentPageShortcuts = z.object({
|
|
__typename: z.literal(ContentBlocksTypenameEnum.ContentPageBlocksShortcuts),
|
|
shortcuts: z.object({
|
|
title: z.string().nullable(),
|
|
preamble: z.string().nullable(),
|
|
shortcuts: z.array(
|
|
z.object({
|
|
text: z.string().optional(),
|
|
openInNewTab: z.boolean(),
|
|
url: z.string(),
|
|
title: z.string(),
|
|
})
|
|
),
|
|
}),
|
|
})
|
|
|
|
export const contentPageDynamicContent = z.object({
|
|
__typename: z.literal(
|
|
ContentBlocksTypenameEnum.ContentPageBlocksDynamicContent
|
|
),
|
|
dynamic_content: z.object({
|
|
title: z.string().nullable(),
|
|
subtitle: z.string().nullable(),
|
|
component: z.nativeEnum(DynamicContentComponentEnum),
|
|
link: z
|
|
.object({
|
|
text: z.string(),
|
|
href: z.string(),
|
|
})
|
|
.optional(),
|
|
}),
|
|
})
|
|
|
|
export const cardBlock = z.object({
|
|
__typename: z.literal(CardsGridEnum.Card),
|
|
isContentCard: z.boolean(),
|
|
heading: z.string().nullable(),
|
|
body_text: z.string().nullable(),
|
|
background_image: z.any(),
|
|
scripted_top_title: z.string().nullable(),
|
|
primaryButton: z
|
|
.object({
|
|
openInNewTab: z.boolean(),
|
|
title: z.string(),
|
|
href: z.string(),
|
|
isExternal: z.boolean(),
|
|
})
|
|
.optional(),
|
|
secondaryButton: z
|
|
.object({
|
|
openInNewTab: z.boolean(),
|
|
title: z.string(),
|
|
href: z.string(),
|
|
isExternal: z.boolean(),
|
|
})
|
|
.optional(),
|
|
sidePeekButton: z
|
|
.object({
|
|
title: z.string(),
|
|
})
|
|
.optional(),
|
|
system: z.object({
|
|
locale: z.nativeEnum(Lang),
|
|
uid: z.string(),
|
|
}),
|
|
})
|
|
|
|
export const loyaltyCardBlock = z.object({
|
|
__typename: z.literal(CardsGridEnum.LoyaltyCard),
|
|
heading: z.string().nullable(),
|
|
body_text: z.string().nullable(),
|
|
image: z.any(),
|
|
link: z
|
|
.object({
|
|
openInNewTab: z.boolean(),
|
|
title: z.string(),
|
|
href: z.string(),
|
|
isExternal: z.boolean(),
|
|
})
|
|
.optional(),
|
|
system: z.object({
|
|
locale: z.nativeEnum(Lang),
|
|
uid: z.string(),
|
|
}),
|
|
})
|
|
|
|
const contentPageCardsItems = z.discriminatedUnion("__typename", [
|
|
loyaltyCardBlock,
|
|
cardBlock,
|
|
])
|
|
|
|
export const contentPageCards = z.object({
|
|
__typename: z.literal(ContentBlocksTypenameEnum.ContentPageBlocksCardsGrid),
|
|
cards_grid: z.object({
|
|
title: z.string().nullable(),
|
|
preamble: z.string().nullable(),
|
|
layout: z.enum(["twoColumnGrid", "threeColumnGrid", "twoPlusOne"]),
|
|
theme: z.enum(["one", "two", "three"]).nullable(),
|
|
cards: z.array(contentPageCardsItems),
|
|
}),
|
|
})
|
|
|
|
const contentPageBlockItem = z.discriminatedUnion("__typename", [
|
|
contentPageBlockTextContent,
|
|
contentPageCards,
|
|
contentPageDynamicContent,
|
|
contentPageShortcuts,
|
|
])
|
|
|
|
export const contentPageSidebarTextContent = z.object({
|
|
__typename: z.literal(SidebarTypenameEnum.ContentPageSidebarContent),
|
|
content: z.object({
|
|
content: z.object({
|
|
embedded_itemsConnection: z.object({
|
|
edges: z.array(z.any()),
|
|
totalCount: z.number(),
|
|
}),
|
|
json: z.any(),
|
|
}),
|
|
}),
|
|
})
|
|
|
|
export const contentPageJoinLoyaltyContact = z.object({
|
|
__typename: z.literal(
|
|
SidebarTypenameEnum.ContentPageSidebarJoinLoyaltyContact
|
|
),
|
|
join_loyalty_contact: z.object({
|
|
title: z.string().nullable(),
|
|
preamble: z.string().nullable(),
|
|
button: z
|
|
.object({
|
|
openInNewTab: z.boolean(),
|
|
title: z.string(),
|
|
href: z.string(),
|
|
isExternal: z.boolean(),
|
|
})
|
|
.nullable(),
|
|
contact: z.array(
|
|
z.object({
|
|
__typename: z.literal(
|
|
JoinLoyaltyContactTypenameEnum.ContentPageSidebarJoinLoyaltyContactBlockContactContact
|
|
),
|
|
contact: z.object({
|
|
display_text: z.string().nullable(),
|
|
contact_field: z.string(),
|
|
footnote: z.string().nullable(),
|
|
}),
|
|
})
|
|
),
|
|
}),
|
|
})
|
|
|
|
export const contentPageSidebarDynamicContent = z.object({
|
|
__typename: z.literal(SidebarTypenameEnum.ContentPageSidebarDynamicContent),
|
|
dynamic_content: z.object({
|
|
component: z.nativeEnum(SidebarDynamicComponentEnum),
|
|
}),
|
|
})
|
|
|
|
const contentPageSidebarItem = z.discriminatedUnion("__typename", [
|
|
contentPageSidebarTextContent,
|
|
contentPageSidebarDynamicContent,
|
|
contentPageJoinLoyaltyContact,
|
|
])
|
|
|
|
// Content Page Schema and types
|
|
export const validateContentPageSchema = z.object({
|
|
content_page: z.object({
|
|
title: z.string(),
|
|
header: z.object({
|
|
heading: z.string(),
|
|
preamble: z.string(),
|
|
}),
|
|
hero_image: imageVaultAssetSchema.nullable().optional(),
|
|
blocks: z.array(contentPageBlockItem).nullable(),
|
|
sidebar: z.array(contentPageSidebarItem).nullable(),
|
|
system: z.object({
|
|
uid: z.string(),
|
|
locale: z.nativeEnum(Lang),
|
|
created_at: z.string(),
|
|
updated_at: z.string(),
|
|
}),
|
|
}),
|
|
})
|
|
|
|
const pageConnectionRefs = z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: z.object({
|
|
__typename: z.nativeEnum(PageLinkEnum),
|
|
system: z.object({
|
|
content_type_uid: z.string(),
|
|
uid: z.string(),
|
|
}),
|
|
}),
|
|
})
|
|
),
|
|
})
|
|
|
|
const rteConnectionRefs = z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: z.object({
|
|
__typename: z.nativeEnum(RTEEmbedsEnum),
|
|
system: z.object({
|
|
content_type_uid: z.string(),
|
|
uid: z.string(),
|
|
}),
|
|
}),
|
|
})
|
|
),
|
|
})
|
|
|
|
const cardBlockRefs = z.object({
|
|
__typename: z.literal(CardsGridEnum.Card),
|
|
primary_button: z
|
|
.object({
|
|
linkConnection: pageConnectionRefs,
|
|
})
|
|
.nullable(),
|
|
secondary_button: z
|
|
.object({
|
|
linkConnection: pageConnectionRefs,
|
|
})
|
|
.nullable(),
|
|
system: z.object({
|
|
content_type_uid: z.string(),
|
|
uid: z.string(),
|
|
}),
|
|
})
|
|
|
|
const loyaltyCardBlockRefs = z.object({
|
|
__typename: z.literal(CardsGridEnum.LoyaltyCard),
|
|
link: z
|
|
.object({
|
|
linkConnection: pageConnectionRefs,
|
|
})
|
|
.nullable(),
|
|
system: z.object({
|
|
content_type_uid: z.string(),
|
|
uid: z.string(),
|
|
}),
|
|
})
|
|
|
|
const cardGridCardsRef = z.discriminatedUnion("__typename", [
|
|
loyaltyCardBlockRefs,
|
|
cardBlockRefs,
|
|
])
|
|
|
|
const contentPageBlockTextContentRefs = z.object({
|
|
__typename: z.literal(ContentBlocksTypenameEnum.ContentPageBlocksContent),
|
|
content: z.object({
|
|
content: z.object({
|
|
embedded_itemsConnection: rteConnectionRefs,
|
|
}),
|
|
}),
|
|
})
|
|
|
|
const contentPageCardsRefs = z.object({
|
|
__typename: z.literal(ContentBlocksTypenameEnum.ContentPageBlocksCardsGrid),
|
|
cards_grid: z.object({
|
|
cardConnection: z.object({
|
|
edges: z.array(
|
|
z.object({
|
|
node: cardGridCardsRef,
|
|
})
|
|
),
|
|
}),
|
|
}),
|
|
})
|
|
|
|
const contentPageShortcutsRefs = z.object({
|
|
__typename: z.literal(ContentBlocksTypenameEnum.ContentPageBlocksShortcuts),
|
|
shortcuts: z.object({
|
|
shortcuts: z.array(
|
|
z.object({
|
|
linkConnection: rteConnectionRefs,
|
|
})
|
|
),
|
|
}),
|
|
})
|
|
|
|
const contentPageDynamicContentRefs = z.object({
|
|
__typename: z.literal(
|
|
ContentBlocksTypenameEnum.ContentPageBlocksDynamicContent
|
|
),
|
|
dynamic_content: z.object({
|
|
link: z.object({
|
|
pageConnection: pageConnectionRefs,
|
|
}),
|
|
}),
|
|
})
|
|
|
|
const contentPageBlockRefsItem = z.discriminatedUnion("__typename", [
|
|
contentPageBlockTextContentRefs,
|
|
contentPageShortcutsRefs,
|
|
contentPageCardsRefs,
|
|
contentPageDynamicContentRefs,
|
|
])
|
|
|
|
const contentPageSidebarTextContentRef = z.object({
|
|
__typename: z.literal(SidebarTypenameEnum.ContentPageSidebarContent),
|
|
content: z.object({
|
|
content: z.object({
|
|
embedded_itemsConnection: rteConnectionRefs,
|
|
}),
|
|
}),
|
|
})
|
|
|
|
const contentPageSidebarJoinLoyaltyContactRef = z.object({
|
|
__typename: z.literal(
|
|
SidebarTypenameEnum.ContentPageSidebarJoinLoyaltyContact
|
|
),
|
|
join_loyalty_contact: z.object({
|
|
button: z
|
|
.object({
|
|
linkConnection: pageConnectionRefs,
|
|
})
|
|
.nullable(),
|
|
}),
|
|
})
|
|
|
|
const contentPageSidebarRefsItem = z.discriminatedUnion("__typename", [
|
|
contentPageSidebarTextContentRef,
|
|
contentPageSidebarJoinLoyaltyContactRef,
|
|
])
|
|
|
|
export const validateContentPageRefsSchema = z.object({
|
|
content_page: z.object({
|
|
blocks: z.array(contentPageBlockRefsItem).nullable(),
|
|
sidebar: z.array(contentPageSidebarRefsItem).nullable(),
|
|
system: z.object({
|
|
content_type_uid: z.string(),
|
|
uid: z.string(),
|
|
}),
|
|
}),
|
|
})
|