Plan schema for Sanity and Astro
A price someone can choose: a subscription tier, a membership, a package. The core is the name, the price and what is in it; a yearly price, a highlight and offer terms are extensions.
- Core fields
- 6
- Extensions
- 3 (6 fields)
- schema.org
- Offer
- Licence
- MIT
Free, with no account. Same data in both editions: the Astro entry id is the Sanity slug.
Core fields
What every plan has. A slug is added in Sanity.
| Field | Type | Required | What it holds |
|---|---|---|---|
| name | string | Yes | Name |
| price | string | Yes | PriceAs shown, with the currency: €29, Free, From £8. |
| period | string | No | Perioda month, per person, once. |
| line | text | No | One lineWho it is for. |
| features | list of string | No | What is in it |
| action | link | Yes | Button |
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.
featured: Highlighted
The plan the page points to, with its badge. Fits software, memberships, services.
| Field | Type | Required | What it holds |
|---|---|---|---|
| featured | boolean | No | Highlight this plan |
| badge | string | No | BadgeMost chosen. |
offer: Offer terms
For a package or a deal: when it applies, until when, and the small print. Fits hotels, travel, venues.
| Field | Type | Required | What it holds |
|---|---|---|---|
| when | string | No | When it appliesSunday to Thursday, November to March. |
| validUntil | date | No | Valid until |
| terms | text | No | Terms |
yearly: Yearly price
The price when paid by the year, for the monthly/yearly switch. Fits software, memberships.
| Field | Type | Required | What it holds |
|---|---|---|---|
| yearlyPrice | string | No | Yearly pricePer month, paid yearly: €24. |
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/plan.jsonThe CLI needs a components.json in the project. No CLI? Copy the files below: they are all there is.
// Sanity schema: plan 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(32) }), defineField({ name: 'price', title: 'Price', description: 'As shown, with the currency: €29, Free, From £8.', type: 'string', validation: (rule) => rule.required().max(20) }), defineField({ name: 'period', title: 'Period', description: 'a month, per person, once.', type: 'string', validation: (rule) => rule.max(30) }), defineField({ name: 'line', title: 'One line', description: 'Who it is for.', type: 'text', rows: 2, validation: (rule) => rule.max(160) }), defineField({ name: 'features', title: 'What is in it', type: 'array', of: [defineArrayMember({ type: 'string', validation: (rule) => rule.max(80) })], options: { layout: 'tags' }, validation: (rule) => rule.max(10) }), defineField({ name: 'action', title: 'Button', type: 'object', fields: [defineField({ name: 'label', title: 'Label', type: 'string', validation: (rule) => rule.required().max(40) }), defineField({ name: 'href', title: 'Address', type: 'url', validation: (rule) => rule.required().uri({ allowRelative: true, scheme: ['http', 'https', 'mailto', 'tel'] }) })], validation: (rule) => rule.required() }),]; /** 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 price someone can choose: a subscription tier, a membership, a package. The core is the name, the price and what is in it; a yearly price, a highlight and offer terms are extensions. */export const plan = defineType({ name: 'plan', title: 'Plan', type: 'document', fields: [fields[0], slug, ...fields.slice(1)], preview: { select: { title: 'name', subtitle: 'price' } },}); /** The same fields as an object, for a page that keeps its own plans inline. */export const planEntry = defineType({ name: 'planEntry', title: 'Plan', type: 'object', fields, preview: { select: { title: 'name', subtitle: 'price' } },}); /** GROQ: one plan as the PlanRecord type (types/plan.ts) describes it. */export const planProjection = `{ "id": slug.current, name, price, period, line, "features": coalesce(features, []), "action": select(defined(action) => action{ label, href }) }`; /** GROQ: every plan, in order. */export const planListQuery = `*[_type == "plan" && defined(slug.current)] | order(_createdAt asc) ${planProjection}`;// TypeScript types: plan v1.// From themes.dev content models (https://www.themes.dev/sanity/models/), MIT. Your code now: edit it freely.import type { Link } from './kinds'; /** A price someone can choose: a subscription tier, a membership, a package. The core is the name, the price and what is in it; a yearly price, a highlight and offer terms are extensions. */export interface Plan { name: string; /** As shown, with the currency: €29, Free, From £8. */ price: string; /** a month, per person, once. */ period?: string; /** Who it is for. */ line?: string; features: string[]; action: Link;} /** What the GROQ projection returns: the fields and the id (the slug, the same as the Astro entry's id). */export type PlanRecord = Plan & { 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 { plan, planEntry } from './schemaTypes/plan'; export default defineConfig({ // … schema: { types: [plan, planEntry] },}); // In your site: the projection returns the Plan type in types/plan.ts.import { planListQuery } from './schemaTypes/plan';const plans = await client.fetch(planListQuery);In Astro
// src/content.config.tsimport { defineCollection } from 'astro:content';import { file } from 'astro/loaders';import { planSchema } from './models/plan'; const plans = defineCollection({ loader: file('./src/content/plans.json'), schema: planSchema,}); export const collections = { plans };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.