Event schema for Sanity and Astro
Something on at a date and time: a talk, a tasting, a class, a gig. The core is what, when and where; tickets, a programme slot and a weekly repeat are extensions.
- Core fields
- 6
- Extensions
- 3 (9 fields)
- schema.org
- Event
- Licence
- MIT
Free, with no account. Same data in both editions: the Astro entry id is the Sanity slug.
Core fields
What every event has. A slug is added in Sanity.
| Field | Type | Required | What it holds |
|---|---|---|---|
| title | string | Yes | Title |
| date | date | Yes | Date |
| start | time | No | Starts24-hour: 19:30. |
| where | string | No | WhereThe room or the place: The Long Room. |
| line | text | No | One line |
| image | image | No | Image3:2. |
Extensions
Fields for one kind of site, on top of the core. An extension never changes a core field, and there are no settings: a different need is a different extension.
programme: Programme slot
For a programme or a conference: when it ends, when doors open, the room or stage, and the series it belongs to. Fits conferences, theatres, festivals.
| Field | Type | Required | What it holds |
|---|---|---|---|
| end | time | No | Ends |
| doors | time | No | Doors open |
| room | string | No | Room or stage |
| series | string | No | Series |
recurring: Weekly
An event that comes back every week (a class, a supper club): the weekday and the last date. Fits studios, restaurants, community.
| Field | Type | Required | What it holds |
|---|---|---|---|
| weekday | one of Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday | Yes | Every |
| until | date | No | UntilThe last date; empty while it runs. |
tickets: Tickets
What it costs, how many places there are, and where to book. Fits hospitality, culture, venues.
| Field | Type | Required | What it holds |
|---|---|---|---|
| price | string | No | PriceAs shown: £18, Free, Pay what you can. |
| seats | number | No | Places |
| bookUrl | url | No | Booking link |
Get the code
Plain TypeScript with no package of ours: the files become your code. Tested in a new Sanity Studio and a new Astro project.
For your Studio: the document type, the same fields as an inline object, a GROQ projection and a list query, and the TypeScript type the projection returns.
Install with the shadcn CLI
npx shadcn@latest add https://www.themes.dev/sanity/r/event.jsonThe CLI needs a components.json in the project. No CLI? Copy the files below: they are all there is.
// Sanity schema: event v1.// From themes.dev content models (https://www.themes.dev/sanity/models/), MIT. Your code now: edit it freely.import { defineArrayMember, defineField, defineType } from 'sanity'; const fields = [ defineField({ name: 'title', title: 'Title', type: 'string', validation: (rule) => rule.required().max(80) }), defineField({ name: 'date', title: 'Date', type: 'date', validation: (rule) => rule.required() }), defineField({ name: 'start', title: 'Starts', description: '24-hour: 19:30.', type: 'string', placeholder: '19:30', validation: (rule) => rule.regex(/^\d{2}:\d{2}$/, { name: 'time (HH:MM)' }) }), defineField({ name: 'where', title: 'Where', description: 'The room or the place: The Long Room.', type: 'string', validation: (rule) => rule.max(60) }), defineField({ name: 'line', title: 'One line', type: 'text', rows: 2, validation: (rule) => rule.max(200) }), defineField({ name: 'image', title: 'Image', description: '3:2.', type: 'image', options: { hotspot: true }, fields: [defineField({ name: 'alt', title: 'Alternative text', type: 'string', description: 'What it shows, for people who can’t see it. Empty when the text beside it says it.', validation: (rule) => rule.max(180) })] }),]; /** The id other entries refer to, and the Astro edition's entry id: the same string in both. */const slug = defineField({ name: 'slug', title: 'Slug', type: 'slug', description: 'Lowercase letters, digits and hyphens.', options: { source: 'title', maxLength: 64 }, validation: (rule) => rule.required() }); /** Something on at a date and time: a talk, a tasting, a class, a gig. The core is what, when and where; tickets, a programme slot and a weekly repeat are extensions. */export const event = defineType({ name: 'event', title: 'Event', type: 'document', fields: [fields[0], slug, ...fields.slice(1)], orderings: [{ title: 'Order', name: 'date', by: [{ field: 'date', direction: 'asc' }] }], preview: { select: { title: 'title', subtitle: 'date', media: 'image' } },}); /** The same fields as an object, for a page that keeps its own events inline. */export const eventEntry = defineType({ name: 'eventEntry', title: 'Event', type: 'object', fields, preview: { select: { title: 'title', subtitle: 'date', media: 'image' } },}); /** GROQ: one event as the EventRecord type (types/event.ts) describes it. */export const eventProjection = `{ "id": slug.current, title, date, start, where, line, "image": image{ "src": asset->url, "alt": coalesce(alt, ""), "width": asset->metadata.dimensions.width, "height": asset->metadata.dimensions.height } }`; /** GROQ: every event, in order. */export const eventListQuery = `*[_type == "event" && defined(slug.current)] | order(date asc, _createdAt asc) ${eventProjection}`;// TypeScript types: event v1.// From themes.dev content models (https://www.themes.dev/sanity/models/), MIT. Your code now: edit it freely.import type { ImageRef } from './kinds'; /** Something on at a date and time: a talk, a tasting, a class, a gig. The core is what, when and where; tickets, a programme slot and a weekly repeat are extensions. */export interface Event { title: string; date: string; /** 24-hour: 19:30. */ start?: string; /** The room or the place: The Long Room. */ where?: string; line?: string; /** 3:2. */ image?: ImageRef;} /** What the GROQ projection returns: the fields and the id (the slug, the same as the Astro entry's id). */export type EventRecord = Event & { id: string };// Shared shapes of the generated content models./** An image: the path or URL without size suffix, and its alternative text (empty when the text beside it says it). */export interface ImageRef { src: string; alt: string; /** Intrinsic size, so the page reserves the box before the file arrives. */ width: number; height: number;} export interface Link { label: string; href: string;} /** Portable Text as Sanity stores it: blocks, and images with their alt text. */export type RichText = { _type: string; _key: string; [key: string]: unknown }[];Use it
In Sanity
// sanity.config.tsimport { event, eventEntry } from './schemaTypes/event'; export default defineConfig({ // … schema: { types: [event, eventEntry] },}); // In your site: the projection returns the Event type in types/event.ts.import { eventListQuery } from './schemaTypes/event';const events = await client.fetch(eventListQuery);In Astro
// src/content.config.tsimport { defineCollection } from 'astro:content';import { file } from 'astro/loaders';import { eventSchema } from './models/event'; const events = defineCollection({ loader: file('./src/content/events.json'), schema: eventSchema,}); export const collections = { events };An image is always { src, alt, width, height } in both editions: the GROQ projection reads the size from the asset, and in Astro it sits in your data, so pages never shift while they load.