Testimonial schema for Sanity and Astro
What a customer, client or guest said, and who said it. The core is the quote and the person; a rating, the visit and what it was about are extensions.
- Core fields
- 4
- Extensions
- 3 (5 fields)
- schema.org
- Review
- Licence
- MIT
Free, with no account. Same data in both editions: the Astro entry id is the Sanity slug.
Core fields
What every testimonial has. A slug is added in Sanity.
| Field | Type | Required | What it holds |
|---|---|---|---|
| quote | text | Yes | QuoteTheir words, without quotation marks: the page adds them. |
| name | string | Yes | Name |
| role | string | No | Who they areRole and company, or what brought them: Booked the Long Room. |
| photo | image | No | PhotoSquare. Decorative: the name beside it says who it is. |
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.
about: About what
The thing the testimonial is about (a trip, a park, an insurer, a product), as a reference to another collection. The theme names the collection. Fits travel, comparison, shops.
| Field | Type | Required | What it holds |
|---|---|---|---|
| subject | reference | Yes | About |
stars: Star rating
A score of one to five stars, as app stores and review sites give it. A score out of ten is a different extension, not a setting of this one. Fits apps, travel, local services.
| Field | Type | Required | What it holds |
|---|---|---|---|
| stars | number | Yes | Stars |
visit: Visit
For a guest review: a headline, where the guest came from and when they went. Fits travel, hospitality, venues.
| Field | Type | Required | What it holds |
|---|---|---|---|
| title | string | No | Headline |
| from | string | No | FromWhere they came from: Leeds. |
| when | string | No | WhenAs the page shows it: May 2026. |
References point at product in the code here. Change it to the type in your own schema.
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/testimonial.jsonThe CLI needs a components.json in the project. No CLI? Copy the files below: they are all there is.
// Sanity schema: testimonial 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: 'quote', title: 'Quote', description: 'Their words, without quotation marks: the page adds them.', type: 'text', rows: 4, validation: (rule) => rule.required().max(400) }), defineField({ name: 'name', title: 'Name', type: 'string', validation: (rule) => rule.required().max(60) }), defineField({ name: 'role', title: 'Who they are', description: 'Role and company, or what brought them: Booked the Long Room.', type: 'string', validation: (rule) => rule.max(80) }), defineField({ name: 'photo', title: 'Photo', description: 'Square. Decorative: the name beside it says who it is.', 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() }); /** What a customer, client or guest said, and who said it. The core is the quote and the person; a rating, the visit and what it was about are extensions. */export const testimonial = defineType({ name: 'testimonial', title: 'Testimonial', type: 'document', fields: [fields[0], slug, ...fields.slice(1)], preview: { select: { title: 'name', subtitle: 'quote', media: 'photo' } },}); /** The same fields as an object, for a page that keeps its own testimonials inline. */export const testimonialEntry = defineType({ name: 'testimonialEntry', title: 'Testimonial', type: 'object', fields, preview: { select: { title: 'name', subtitle: 'quote', media: 'photo' } },}); /** GROQ: one testimonial as the TestimonialRecord type (types/testimonial.ts) describes it. */export const testimonialProjection = `{ "id": slug.current, quote, name, role, "photo": photo{ "src": asset->url, "alt": coalesce(alt, ""), "width": asset->metadata.dimensions.width, "height": asset->metadata.dimensions.height } }`; /** GROQ: every testimonial, in order. */export const testimonialListQuery = `*[_type == "testimonial" && defined(slug.current)] | order(_createdAt asc) ${testimonialProjection}`;// TypeScript types: testimonial v1.// From themes.dev content models (https://www.themes.dev/sanity/models/), MIT. Your code now: edit it freely.import type { ImageRef } from './kinds'; /** What a customer, client or guest said, and who said it. The core is the quote and the person; a rating, the visit and what it was about are extensions. */export interface Testimonial { /** Their words, without quotation marks: the page adds them. */ quote: string; name: string; /** Role and company, or what brought them: Booked the Long Room. */ role?: string; /** Square. Decorative: the name beside it says who it is. */ photo?: ImageRef;} /** What the GROQ projection returns: the fields and the id (the slug, the same as the Astro entry's id). */export type TestimonialRecord = Testimonial & { 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 { testimonial, testimonialEntry } from './schemaTypes/testimonial'; export default defineConfig({ // … schema: { types: [testimonial, testimonialEntry] },}); // In your site: the projection returns the Testimonial type in types/testimonial.ts.import { testimonialListQuery } from './schemaTypes/testimonial';const testimonials = await client.fetch(testimonialListQuery);In Astro
// src/content.config.tsimport { defineCollection } from 'astro:content';import { file } from 'astro/loaders';import { testimonialSchema } from './models/testimonial'; const testimonials = defineCollection({ loader: file('./src/content/testimonials.json'), schema: testimonialSchema,}); export const collections = { testimonials };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.