Lesson 1 · 16 min · Free
Sites as data (the spec)
Why the AI should edit a JSON description of the site, not raw HTML, and how to validate it.
The most important decision in an AI site builder: the AI never writes code that runs. It writes a small JSON document, the spec, that describes the site. Your own trusted code turns the spec into pages.
- Safe: the AI can't inject scripts, because the spec has no place for them.
- Consistent: every site uses your polished components, so quality stays high.
- Editable: small changes ('make the headline shorter') are small JSON edits, cheap and reliable.
export type Section =
| { type: "features"; title: string; items: { title: string; text: string }[] }
| { type: "faq"; title: string; items: { q: string; a: string }[] }
| { type: "contact"; title: string };
export type SiteSpec = {
brand: { name: string; primary: string };
hero: { title: string; sub: string; cta: string };
sections: Section[];
};Never trust the model's output
Even with a strict schema, validate everything before saving: right types, sensible lengths, colours that are real hex values, unknown fields dropped. If validation fails, keep the previous version.
import type { SiteSpec } from "./spec";
const HEX = /^#[0-9a-f]{6}$/i;
const str = (v: unknown, max: number) => (typeof v === "string" ? v.trim().slice(0, max) : "");
export function validate(raw: any): SiteSpec | null {
const name = str(raw?.brand?.name, 60);
const primary = HEX.test(raw?.brand?.primary) ? raw.brand.primary : "#3b82f6";
const title = str(raw?.hero?.title, 90);
if (!name || !title) return null;
const sections = (Array.isArray(raw?.sections) ? raw.sections : []).slice(0, 12).flatMap((s: any) => {
if (s?.type === "faq") return [{ type: "faq", title: str(s.title, 80), items: (s.items ?? []).slice(0, 10).map((i: any) => ({ q: str(i.q, 160), a: str(i.a, 600) })) }];
if (s?.type === "contact") return [{ type: "contact", title: str(s.title, 80) }];
return []; // unknown types are dropped
});
return { brand: { name, primary }, hero: { title, sub: str(raw?.hero?.sub, 240), cta: str(raw?.hero?.cta, 30) || "Get in touch" }, sections };
}Build it with Claude Code
Create lib/spec.ts with a TypeScript SiteSpec type (brand name and primary colour, hero title/sub/cta, and a sections array with features, faq and contact types) and lib/validate.ts with a validate function that trims and length-limits every string, only accepts 6-digit hex colours, drops unknown section types and returns null if the brand name or hero title is missing.