Skip to content

Question schema for Sanity and Astro

A question people ask and its answer. Most themes keep these inline on a page; as a collection, every page can pick its own.

Core fields
3
Extensions
3 (3 fields)
schema.org
Question
Licence
MIT

Free, with no account. Same data in both editions: the Astro entry id is the Sanity slug.

Core fields

What every question has. A slug is added in Sanity.

FieldTypeRequiredWhat it holds
questionstringYesQuestion
answertextYesAnswerPlain text; an empty line starts a new paragraph.
ordernumberNoOrderLower comes first.

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 page the question belongs to (a trip, a practice area, a course), as a reference to another collection. The theme names the collection. Fits travel, professional services, education.

FieldTypeRequiredWhat it holds
subjectreferenceNoAbout

featured: Featured

Marks the few questions the home page or a product page shows; the help page shows them all. Fits apps, shops.

FieldTypeRequiredWhat it holds
featuredbooleanNoShow on the home page

group: Group

A heading the question is listed under on a long help page: Booking, Payment, On the day. Fits shops, travel, events.

FieldTypeRequiredWhat it holds
groupstringYesGroup

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.

Extensions

The core is always there. Each extension only adds fields.

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

Terminal
npx shadcn@latest add https://www.themes.dev/sanity/r/faq.json

The CLI needs a components.json in the project. No CLI? Copy the files below: they are all there is.

schemaTypes/faq.tsTypeScript
// Sanity schema: faq 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: 'question', title: 'Question', type: 'string', validation: (rule) => rule.required().max(120) }),  defineField({ name: 'answer', title: 'Answer', description: 'Plain text; an empty line starts a new paragraph.', type: 'text', rows: 4, validation: (rule) => rule.required().max(800) }),  defineField({ name: 'order', title: 'Order', description: 'Lower comes first.', type: 'number', validation: (rule) => rule.integer() }),]; /** 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: 'question', maxLength: 64 }, validation: (rule) => rule.required() }); /** A question people ask and its answer. Most themes keep these inline on a page; as a collection, every page can pick its own. */export const faq = defineType({  name: 'faq',  title: 'Question',  type: 'document',  fields: [fields[0], slug, ...fields.slice(1)],  orderings: [{ title: 'Order', name: 'order', by: [{ field: 'order', direction: 'asc' }] }],  preview: { select: { title: 'question', subtitle: 'answer' } },}); /** The same fields as an object, for a page that keeps its own questions inline. */export const faqEntry = defineType({  name: 'faqEntry',  title: 'Question',  type: 'object',  fields,  preview: { select: { title: 'question', subtitle: 'answer' } },}); /** GROQ: one faq as the FaqRecord type (types/faq.ts) describes it. */export const faqProjection = `{ "id": slug.current, question, answer, order }`; /** GROQ: every faq, in order. */export const faqListQuery = `*[_type == "faq" && defined(slug.current)] | order(order asc, _createdAt asc) ${faqProjection}`;
types/faq.tsTypeScript
// TypeScript types: faq v1.// From themes.dev content models (https://www.themes.dev/sanity/models/), MIT. Your code now: edit it freely. /** A question people ask and its answer. Most themes keep these inline on a page; as a collection, every page can pick its own. */export interface Faq {  question: string;  /** Plain text; an empty line starts a new paragraph. */  answer: string;  /** Lower comes first. */  order?: number;} /** What the GROQ projection returns: the fields and the id (the slug, the same as the Astro entry's id). */export type FaqRecord = Faq & { id: string };
types/kinds.tsTypeScript
// 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.tsTypeScript
// sanity.config.tsimport { faq, faqEntry } from './schemaTypes/faq'; export default defineConfig({  // …  schema: { types: [faq, faqEntry] },}); // In your site: the projection returns the Question type in types/faq.ts.import { faqListQuery } from './schemaTypes/faq';const questions = await client.fetch(faqListQuery);

In Astro

src/content.config.tsTypeScript
// src/content.config.tsimport { defineCollection } from 'astro:content';import { file } from 'astro/loaders';import { faqSchema } from './models/faq'; const questions = defineCollection({  loader: file('./src/content/questions.json'),  schema: faqSchema,}); export const collections = { questions };

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.