fix: only zod validate output from trpc
This commit is contained in:
@@ -13,28 +13,6 @@ import { PageLinkEnum } from "@/types/requests/pageLinks"
|
||||
import { EdgesWithTotalCount } from "@/types/requests/utils/edges"
|
||||
import { RTEDocument } from "@/types/rte/node"
|
||||
|
||||
const pageLink = z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
system: z.object({
|
||||
uid: z.string(),
|
||||
locale: z.nativeEnum(Lang),
|
||||
}),
|
||||
web: z
|
||||
.object({
|
||||
original_url: z.string().nullable(),
|
||||
})
|
||||
.nullable()
|
||||
.optional(),
|
||||
url: z.string(),
|
||||
title: z.string(),
|
||||
__typename: z.string().optional(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
})
|
||||
|
||||
const loyaltyPageDynamicContent = z.object({
|
||||
__typename: z.literal(
|
||||
LoyaltyBlocksTypenameEnum.LoyaltyPageBlocksDynamicContent
|
||||
@@ -43,10 +21,13 @@ const loyaltyPageDynamicContent = z.object({
|
||||
title: z.string().nullable(),
|
||||
subtitle: z.string().nullable(),
|
||||
component: z.nativeEnum(LoyaltyComponentEnum),
|
||||
link: z.object({
|
||||
text: z.string().nullable(),
|
||||
pageConnection: pageLink,
|
||||
}),
|
||||
link: z
|
||||
.object({
|
||||
text: z.string().nullable(),
|
||||
href: z.string(),
|
||||
title: z.string(),
|
||||
})
|
||||
.optional(),
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -57,9 +38,10 @@ const loyaltyPageShortcuts = z.object({
|
||||
preamble: z.string().nullable(),
|
||||
shortcuts: z.array(
|
||||
z.object({
|
||||
linkConnection: pageLink,
|
||||
text: z.string().nullable(),
|
||||
open_in_new_tab: z.boolean(),
|
||||
text: z.string().optional(),
|
||||
openInNewTab: z.boolean(),
|
||||
url: z.string(),
|
||||
title: z.string(),
|
||||
})
|
||||
),
|
||||
}),
|
||||
@@ -70,31 +52,22 @@ const cardBlock = z.object({
|
||||
body_text: z.string().nullable(),
|
||||
background_image: z.any(),
|
||||
scripted_top_title: z.string().nullable(),
|
||||
primary_button: z
|
||||
primaryButton: z
|
||||
.object({
|
||||
is_contentstack_link: z.boolean(),
|
||||
cta_text: z.string().nullable(),
|
||||
open_in_new_tab: z.boolean(),
|
||||
external_link: z.object({
|
||||
title: z.string().nullable(),
|
||||
href: z.string().nullable(),
|
||||
}),
|
||||
linkConnection: pageLink,
|
||||
openInNewTab: z.boolean(),
|
||||
title: z.string(),
|
||||
href: z.string(),
|
||||
isExternal: z.boolean(),
|
||||
})
|
||||
.nullable(),
|
||||
secondary_button: z
|
||||
.optional(),
|
||||
secondaryButton: z
|
||||
.object({
|
||||
is_contentstack_link: z.boolean(),
|
||||
cta_text: z.string().nullable(),
|
||||
open_in_new_tab: z.boolean().nullable(),
|
||||
external_link: z.object({
|
||||
title: z.string().nullable(),
|
||||
href: z.string().nullable(),
|
||||
}),
|
||||
linkConnection: pageLink,
|
||||
openInNewTab: z.boolean(),
|
||||
title: z.string(),
|
||||
href: z.string(),
|
||||
isExternal: z.boolean(),
|
||||
})
|
||||
.nullable(),
|
||||
|
||||
.optional(),
|
||||
system: z.object({
|
||||
locale: z.nativeEnum(Lang),
|
||||
uid: z.string(),
|
||||
@@ -108,13 +81,7 @@ const loyaltyPageCards = z.object({
|
||||
preamble: z.string().nullable(),
|
||||
layout: z.enum(["twoColumnGrid", "threeColumnGrid", "twoPlusOne"]),
|
||||
theme: z.enum(["one", "two", "three"]).nullable(),
|
||||
cardConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: cardBlock,
|
||||
})
|
||||
),
|
||||
}),
|
||||
cards: z.array(cardBlock),
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -178,31 +145,17 @@ const loyaltyPageSidebarItem = z.discriminatedUnion("__typename", [
|
||||
])
|
||||
|
||||
export const validateLoyaltyPageSchema = z.object({
|
||||
loyalty_page: z.object({
|
||||
title: z.string(),
|
||||
heading: z.string().nullable(),
|
||||
blocks: z.array(loyaltyPageBlockItem).nullable(),
|
||||
sidebar: z.array(loyaltyPageSidebarItem).nullable(),
|
||||
system: z.object({ uid: z.string() }),
|
||||
}),
|
||||
heading: z.string().nullable(),
|
||||
blocks: z.array(loyaltyPageBlockItem).nullable(),
|
||||
sidebar: z.array(loyaltyPageSidebarItem).nullable(),
|
||||
system: z.object({ uid: z.string() }),
|
||||
})
|
||||
|
||||
// Block types
|
||||
type DynamicContentRaw = z.infer<typeof loyaltyPageDynamicContent>
|
||||
|
||||
export type DynamicContent = Omit<DynamicContentRaw, "dynamic_content"> & {
|
||||
dynamic_content: Omit<DynamicContentRaw["dynamic_content"], "link"> & {
|
||||
link:
|
||||
| {
|
||||
href: string
|
||||
title: string
|
||||
text?: string
|
||||
}
|
||||
| undefined
|
||||
}
|
||||
}
|
||||
export type DynamicContent = z.infer<typeof loyaltyPageDynamicContent>
|
||||
|
||||
type BlockContentRaw = z.infer<typeof loyaltyPageBlockTextContent>
|
||||
|
||||
export interface RteBlockContent extends BlockContentRaw {
|
||||
content: {
|
||||
content: {
|
||||
@@ -214,44 +167,11 @@ export interface RteBlockContent extends BlockContentRaw {
|
||||
|
||||
type CardsGridRaw = z.infer<typeof loyaltyPageCards>
|
||||
|
||||
export type CardsRaw =
|
||||
CardsGridRaw["cards_grid"]["cardConnection"]["edges"][number]["node"]
|
||||
export type CardsRaw = CardsGridRaw["cards_grid"]["cards"][number]
|
||||
|
||||
export type CardsGrid = Omit<CardsGridRaw, "cards_grid"> & {
|
||||
cards_grid: Omit<CardsGridRaw["cards_grid"], "cardConnection"> & {
|
||||
cards: (Omit<CardsRaw, "primaryButton" | "secondaryButton"> & {
|
||||
primaryButton:
|
||||
| {
|
||||
openInNewTab: boolean
|
||||
title: string
|
||||
href: string
|
||||
isExternal: boolean
|
||||
}
|
||||
| undefined
|
||||
secondaryButton:
|
||||
| {
|
||||
openInNewTab: boolean
|
||||
title: string
|
||||
href: string
|
||||
isExternal: boolean
|
||||
}
|
||||
| undefined
|
||||
})[]
|
||||
}
|
||||
}
|
||||
export type CardsGrid = z.infer<typeof loyaltyPageCards>
|
||||
|
||||
type ShortcutsRaw = z.infer<typeof loyaltyPageShortcuts>
|
||||
|
||||
export type Shortcuts = Omit<ShortcutsRaw, "shortcuts"> & {
|
||||
shortcuts: Omit<ShortcutsRaw["shortcuts"], "shortcuts"> & {
|
||||
shortcuts: {
|
||||
text?: string
|
||||
openInNewTab: boolean
|
||||
url: string
|
||||
title: string
|
||||
}[]
|
||||
}
|
||||
}
|
||||
export type Shortcuts = z.infer<typeof loyaltyPageShortcuts>
|
||||
|
||||
export type Block = RteBlockContent | DynamicContent | Shortcuts | CardsGrid
|
||||
|
||||
@@ -268,12 +188,9 @@ export type RteSidebarContent = Omit<SidebarContentRaw, "content"> & {
|
||||
}
|
||||
export type JoinLoyaltyContact = z.infer<typeof loyaltyPageJoinLoyaltyContact>
|
||||
export type Sidebar = JoinLoyaltyContact | RteSidebarContent
|
||||
type LoyaltyPageDataRaw = z.infer<typeof validateLoyaltyPageSchema>
|
||||
|
||||
export type LoyaltyPageDataRaw = z.infer<typeof validateLoyaltyPageSchema>
|
||||
|
||||
type LoyaltyPageRaw = LoyaltyPageDataRaw["loyalty_page"]
|
||||
|
||||
export type LoyaltyPage = Omit<LoyaltyPageRaw, "blocks" | "sidebar"> & {
|
||||
export type LoyaltyPage = Omit<LoyaltyPageDataRaw, "blocks" | "sidebar"> & {
|
||||
blocks: Block[]
|
||||
sidebar: Sidebar[]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user