The schema an editor's form is built from
import { defineArrayMember, defineField, defineType } from "@sanity/types";
import {
badgeEntrances,
badgeNames,
buttonFeels,
heroInteractions,
heroMotions,
isolations,
} from "../choices";
// One document holds the three sections. Fields say what the content is and which behaviour
// the editor wants, never how it looks. Each effect is a choice from a list, not an on/off
// switch, so a new behaviour can be added later without migrating content.
export const homepage = defineType({
name: "homepage",
type: "document",
fields: [
defineField({
name: "hero",
type: "object",
fields: [
defineField({
name: "headline",
type: "string",
validation: (rule) => rule.required().max(40),
}),
defineField({
name: "subhead",
type: "text",
rows: 2,
validation: (rule) => rule.required().max(160),
}),
defineField({
name: "interaction",
type: "string",
description: "What a visitor can do in the hero.",
options: { list: [...heroInteractions], layout: "radio" },
initialValue: "deploymentChoices",
validation: (rule) => rule.required(),
}),
defineField({
name: "motion",
type: "string",
description: "How the hero picture moves on its own.",
options: { list: [...heroMotions], layout: "radio" },
initialValue: "still",
validation: (rule) => rule.required(),
}),
defineField({
name: "deploymentChoices",
type: "array",
description: "Shown in this order. The first one is selected when the page loads.",
hidden: ({ parent }) => parent?.interaction !== "deploymentChoices",
of: [
defineArrayMember({
name: "deploymentChoice",
type: "object",
fields: [
defineField({
name: "label",
type: "string",
validation: (rule) => rule.required().max(24),
}),
defineField({
name: "summary",
type: "string",
description: "One short sentence shown under the picture.",
validation: (rule) => rule.required().max(60),
}),
defineField({
name: "isolation",
type: "string",
description: "Decides how the boundary is drawn.",
options: { list: [...isolations], layout: "radio" },
validation: (rule) => rule.required(),
}),
],
}),
],
validation: (rule) => rule.min(2).max(4),
}),
],
}),
defineField({
name: "demoButton",
type: "object",
fields: [
defineField({
name: "label",
type: "string",
validation: (rule) => rule.required().max(24),
}),
defineField({
name: "feel",
type: "string",
options: { list: [...buttonFeels], layout: "radio" },
initialValue: "press",
validation: (rule) => rule.required(),
}),
],
}),
defineField({
name: "security",
type: "object",
fields: [
defineField({
name: "heading",
type: "string",
validation: (rule) => rule.required().max(60),
}),
defineField({
name: "body",
type: "text",
rows: 3,
validation: (rule) => rule.required().max(200),
}),
defineField({
name: "badges",
type: "array",
description: "Shown in this order.",
of: [defineArrayMember({ type: "string" })],
options: { list: badgeNames.map((name) => ({ title: name, value: name })) },
validation: (rule) => rule.required().min(1).unique(),
}),
defineField({
name: "entrance",
type: "string",
description: "How the badges arrive when a visitor scrolls to them.",
options: { list: [...badgeEntrances], layout: "radio" },
initialValue: "stamp",
validation: (rule) => rule.required(),
}),
],
}),
],
});