/** * What an admin fills in for each homepage section (WEBSITE-1.0-PLAN §2: "every section stores its * data, validated with a zod schema per section type"). Never tied to a template: every template * draws every variant from the same data (§3). Lists (ministries, leaders, events, messages) come * from their own tables; the section only holds its heading and intro. * * Images are media ids here; the published snapshot turns them into `{ url, alt }`. */ import { z } from 'zod'; import type { SectionType } from './variants.js'; const text = (max: number) => z.string().trim().max(max); const optional = (max: number) => text(max).nullable().default(null); const mediaId = z.string().uuid(); /** A link on the site (`/visit`) or elsewhere (https only). */ const href = z.string().trim().max(500).refine((v) => /^\/(?!\/)/.test(v) || /^https:\/\//.test(v), 'A page of this site (/visit) or an https:// address'); const button = z.object({ label: text(60).min(1), href }).nullable().default(null); export const SECTION_SCHEMAS = { hero: z.object({ headline: text(120).min(1), subline: optional(300), /** `photo` and `split` use the first; `collage` up to three. */ images: z.array(mediaId).max(3).default([]), /** `video`: a short muted loop (an uploaded .mp4) shown over its still (the first image). */ videoId: mediaId.nullable().default(null), button, }), serviceTimes: z.object({ heading: optional(80), /** E.g. "Kids church during the morning service". Times and address come from the church profile. */ note: optional(300), }), welcome: z.object({ heading: optional(120), /** The mission line, above the introduction in every variant (§3). */ mission: optional(200), body: text(4000).default(''), imageId: mediaId.nullable().default(null), pastor: z.object({ name: text(120).min(1), role: optional(80), quote: optional(400), photoId: mediaId.nullable().default(null) }).nullable().default(null), }), ministries: z.object({ heading: optional(80), intro: optional(400) }), team: z.object({ heading: optional(80), intro: optional(400) }), events: z.object({ heading: optional(80), count: z.int().min(2).max(12).default(3) }), latestMessage: z.object({ heading: optional(80) }), invitation: z.object({ heading: text(120).min(1), text: optional(400), button, imageId: mediaId.nullable().default(null) }), } as const satisfies Record; export type SectionData = { [S in SectionType]: z.infer<(typeof SECTION_SCHEMAS)[S]> }; /** Blocks for the other pages and custom pages (§3, "Pages from blocks"); more come with the admin. */ export const BLOCK_SCHEMAS = { text: z.object({ heading: optional(120), body: text(20000) }), 'image-text': z.object({ heading: optional(120), body: text(4000).default(''), imageId: mediaId.nullable().default(null), button }), quote: z.object({ quote: text(600).min(1), by: optional(120) }), cta: z.object({ heading: text(120).min(1), text: optional(400), button }), } as const; export type BlockType = keyof typeof BLOCK_SCHEMAS; export const isBlockType = (t: string): t is BlockType => t in BLOCK_SCHEMAS; /** A section's or block's data, checked; null when it doesn't fit (it's then left out, never shown broken). */ export function parseSectionData(type: string, data: unknown): Record | null { const schema = (SECTION_SCHEMAS as Record)[type] ?? (BLOCK_SCHEMAS as Record)[type]; if (!schema) return null; const r = schema.safeParse(data ?? {}); return r.success ? (r.data as Record) : null; }