Location schema for Sanity and Astro
A place people go to: an office, a branch, a studio, a venue. The core is its name, where it is and a photo; opening hours, a direct line and a map are extensions.
- Core fields
- 6
- Extensions
- 3 (7 fields)
- schema.org
- Place
- Licence
- MIT
Free, with no account. Same data in both editions: the Astro entry id is the Sanity slug.
Core fields
What every location has. A slug is added in Sanity.
| Field | Type | Required | What it holds |
|---|---|---|---|
| name | string | Yes | Name |
| line | text | No | One line |
| address | string | No | Street addressStreet and number: 14 Mill Lane. |
| city | string | Yes | Town or city |
| postcode | string | No | Postcode |
| photo | image | No | PhotoThe building or the room, 4:3. |
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.
contact: Direct contact
A phone number and an address for this place, not the head office. Fits professional services, health, public services.
| Field | Type | Required | What it holds |
|---|---|---|---|
| phone | string | No | Phone |
| No |
hours: Opening hours
When it is open, as lines a visitor reads, and a note for the exceptions. Fits shops, studios, health, libraries.
| Field | Type | Required | What it holds |
|---|---|---|---|
| hours | list of string | No | HoursOne line each: Mon–Fri 08:00–18:00. |
| hoursNote | string | No | NoteBank holidays, closures. |
map: Map and directions
Where to put the pin, and how to get there. Fits venues, travel, studios.
| Field | Type | Required | What it holds |
|---|---|---|---|
| lat | number | No | Latitude |
| lng | number | No | Longitude |
| directions | text | No | Getting thereBy train, bus, car and bike, in a few lines. |
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/location.jsonThe CLI needs a components.json in the project. No CLI? Copy the files below: they are all there is.
// Sanity schema: location 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: 'name', title: 'Name', type: 'string', validation: (rule) => rule.required().max(60) }), defineField({ name: 'line', title: 'One line', type: 'text', rows: 2, validation: (rule) => rule.max(160) }), defineField({ name: 'address', title: 'Street address', description: 'Street and number: 14 Mill Lane.', type: 'string', validation: (rule) => rule.max(80) }), defineField({ name: 'city', title: 'Town or city', type: 'string', validation: (rule) => rule.required().max(40) }), defineField({ name: 'postcode', title: 'Postcode', type: 'string', validation: (rule) => rule.max(12) }), defineField({ name: 'photo', title: 'Photo', description: 'The building or the room, 4:3.', 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: 'name', maxLength: 64 }, validation: (rule) => rule.required() }); /** A place people go to: an office, a branch, a studio, a venue. The core is its name, where it is and a photo; opening hours, a direct line and a map are extensions. */export const location = defineType({ name: 'location', title: 'Location', type: 'document', fields: [fields[0], slug, ...fields.slice(1)], preview: { select: { title: 'name', subtitle: 'city', media: 'photo' } },}); /** The same fields as an object, for a page that keeps its own locations inline. */export const locationEntry = defineType({ name: 'locationEntry', title: 'Location', type: 'object', fields, preview: { select: { title: 'name', subtitle: 'city', media: 'photo' } },}); /** GROQ: one location as the LocationRecord type (types/location.ts) describes it. */export const locationProjection = `{ "id": slug.current, name, line, address, city, postcode, "photo": photo{ "src": asset->url, "alt": coalesce(alt, ""), "width": asset->metadata.dimensions.width, "height": asset->metadata.dimensions.height } }`; /** GROQ: every location, in order. */export const locationListQuery = `*[_type == "location" && defined(slug.current)] | order(_createdAt asc) ${locationProjection}`;// TypeScript types: location v1.// From themes.dev content models (https://www.themes.dev/sanity/models/), MIT. Your code now: edit it freely.import type { ImageRef } from './kinds'; /** A place people go to: an office, a branch, a studio, a venue. The core is its name, where it is and a photo; opening hours, a direct line and a map are extensions. */export interface Location { name: string; line?: string; /** Street and number: 14 Mill Lane. */ address?: string; city: string; postcode?: string; /** The building or the room, 4:3. */ photo?: ImageRef;} /** What the GROQ projection returns: the fields and the id (the slug, the same as the Astro entry's id). */export type LocationRecord = Location & { 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 { location, locationEntry } from './schemaTypes/location'; export default defineConfig({ // … schema: { types: [location, locationEntry] },}); // In your site: the projection returns the Location type in types/location.ts.import { locationListQuery } from './schemaTypes/location';const locations = await client.fetch(locationListQuery);In Astro
// src/content.config.tsimport { defineCollection } from 'astro:content';import { file } from 'astro/loaders';import { locationSchema } from './models/location'; const locations = defineCollection({ loader: file('./src/content/locations.json'), schema: locationSchema,}); export const collections = { locations };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.