Rules That Cross Fields, and Schemas That Cross Languages
Per-field checks miss rules that span fields, and shared schemas can lose them between languages. Two fixes for a Schema layer used by several services.
Somewhere in your codebase there are probably three definitions of what a loan is: one in a database migration, one in an API handler, one in the mobile client. They agree today. In six months one of them will quietly gain a field the other two never hear about. The Schema layer exists to make that story impossible, by giving every concept exactly one home.
Schema is layer one of nine, the lowest, with a volatility of 0.3. It holds four kinds of thing: the types that describe your domain, the validation rules and constraints on them, fixed values such as enumerations, and the contracts the other layers use to talk to each other. It describes data. It does not fetch or compute anything.
Everything above imports from it, so a change here travels further than a change anywhere else. The book's summary of the two bottom layers is that they must be highly stable, because an edit there reaches the whole system, and so they deserve the heaviest testing and the plainest code. Schema also has zero dependencies of its own. In practice it should move when the meaning of your data moves, and not much else.
The most useful habit in this layer is to write the shape of a thing once and generate everything else from it. In TypeScript with Zod, that looks like this:
import { z } from "zod";
export const LoanStatus = z.enum(["active", "returned", "overdue", "lost"]);
export const LoanSchema = z.object({
loanId: z.string().uuid(),
memberId: z.string().uuid(),
isbn: z.string().regex(/^\d{13}$/),
dueOn: z.string().date(),
status: LoanStatus,
});
export type Loan = z.infer<typeof LoanSchema>;
The runtime check and the compile-time type come from one declaration, so they cannot drift apart. Add a field and the type changes with it. Rename an enum value and every file still using the old one stops compiling. For a TypeScript codebase, this is why the book recommends Zod.
The book frames the choice around four questions: how much safety you get at compile time versus at runtime, how expressive the validation can be, whether other languages can read the schema, and how pleasant it is to work with. Four candidates come out of that:
The book's rule of thumb follows the shape of your organisation, not a league table. A TypeScript monorepo leans towards Zod, a polyglot service estate towards Protocol Buffers, and a public REST API pairs JSON Schema with Zod. The real question is who has to read the schema.
Three layouts appear in the book. Group by domain, so everything about loans sits in one folder and everything about members in another. Separate shared schemas, such as email formats and pagination, from domain-specific ones, so reuse has a clear boundary. And define a base entity schema (identifier, timestamps, version) that each entity extends, instead of repeating those fields in every file.
None of this is clever, and that is deliberate. The goal is that a new colleague can guess where a schema lives.
Treat the Schema layer as a dictionary, not a toolbox. Keep it small and free of dependencies, and spend your care on names and rules, because every layer above will repeat them. Next, see how validation and cross-language sharing build on it, and where it sits among the nine layers. The layer page has a short summary too.