import type { CollectionEntry, SchemaContext } from 'astro:content'; import { z } from 'astro:content'; import { attributionSchema } from './attribution'; import { eventDateSchema } from './event-date'; import { isTeamKey, TEAM_KEYS } from './team-keys'; /** Typed participation kinds an event can represent. */ export const PARTICIPATION_TYPES = [ 'talk', 'livestream', 'podcast', 'booth', 'paper', 'panel', ] as const; /** Whether Thymian is presenting at the event or merely attending. */ export const PARTICIPATION_MODES = ['presenting', 'attending'] as const; export type ParticipationType = (typeof PARTICIPATION_TYPES)[number]; export type ParticipationMode = (typeof PARTICIPATION_MODES)[number]; /** * The `events` collection schema, in Astro's function form so the optional * `logo` field can use the content-collection `image()` helper (which is only * in scope here). NOTE: this is a `SchemaContext` FACTORY, not a plain Zod * schema — pass it to `defineCollection({ schema: eventsSchema })` (Astro * invokes it), and in tests resolve it once via `eventsSchema({ image })` * before calling `.safeParse(...)`. */ export const eventsSchema = ({ image }: SchemaContext) => z .object({ title: z .string() .trim() .min(1, 'An event `title` must not be empty or whitespace-only.'), participation: z.enum(PARTICIPATION_TYPES), mode: z.enum(PARTICIPATION_MODES), speakers: z .array(z.string()) .default([]) .superRefine((keys, ctx) => { const bad = keys.filter((k) => !isTeamKey(k)); if (bad.length > 0) { ctx.addIssue({ code: 'custom', path: ['speakers'], message: `Unknown speaker key(s): ${bad.join(', ')}. Valid team keys: ${TEAM_KEYS.join(', ')}.`, }); } const duplicates = [ ...new Set(keys.filter((k, i) => keys.indexOf(k) !== i)), ]; if (duplicates.length > 0) { ctx.addIssue({ code: 'custom', path: ['speakers'], message: `Duplicate speaker key(s): ${duplicates.join(', ')}.`, }); } }), location: z.string().trim().optional(), online: z.boolean().optional(), date: eventDateSchema, attribution: attributionSchema.optional(), registerUrl: z.string().trim().url().optional(), resourceUrl: z.string().trim().url().optional(), logo: image().optional(), }) .refine( (e) => { // `location` is already schema-trimmed, so a whitespace-only value is ''. const hasLocation = e.location !== undefined && e.location.length > 0; const isOnline = e.online === true; // Place is physical XOR online — exactly one. return hasLocation !== isOnline; }, { message: 'An event must have exactly one place: set a physical `location` OR `online: true` — not both, not neither.', }, ); export type EventData = CollectionEntry<'events'>['data'];