The Schema Layer: Your Most Stable Code
The Schema layer is LVD's lowest and most stable layer. See what belongs in it, how to pick a schema language, and how to lay out its folders.
A library has two services that handle loans. The TypeScript one rejects a loan whose due date falls before the day it was borrowed. The Java one, written a year later by another team, stores it without complaint. Both teams read the same wiki page. The page could not be executed, and the only schema that could lived in one language.
Most validation is per field: this is a UUID, that is thirteen digits, this date parses. The loan bug is different. Neither date is wrong alone. The mistake only exists in the relationship between them, and a field-by-field check cannot see relationships.
A refinement can. It runs after the shape is confirmed and looks at the whole object:
import { z } from "zod";
export const LoanSchema = z
.object({
memberId: z.string().uuid(),
isbn: z.string().regex(/^\d{13}$/),
borrowedOn: z.coerce.date(),
dueOn: z.coerce.date(),
})
.refine((loan) => loan.dueOn > loan.borrowedOn, {
message: "The due date must be after the borrow date",
path: ["dueOn"],
});
Where should that rule live? In the Schema layer, next to the shape it constrains. It reads nothing from a database and calls nothing, so it is as pure as the type definition itself, and it belongs in the most stable code in the system (L1 sits at ν = 0.3 in the book). Put it in a handler and every other entry point has to remember it. Put it beside the type and anything that parses a loan gets it for free.
The moment a second language joins, the schema has to travel. The book's own scenario is a TypeScript backend sharing types with Java services, and it weighs several mechanisms. I will not walk through them. The principle behind all of them is the one worth keeping: generate the types in each language from one definition, and never translate by hand. A hand-translated type is correct on the day it is written and drifts afterwards, quietly.
Generation has a blind spot, though. A neutral format such as JSON Schema describes shape well (types, required fields, patterns, ranges). It has no natural way to say "this date must be later than that one". The rule from the previous section is exactly the kind of thing that disappears in transit, which is how the Java service came to accept a loan that was due before it was borrowed.
The practical answer is to share the rules as examples. Keep one small file of cases that every language replays in its own test suite:
[
{ "name": "due after borrow", "valid": true, "loan": { "borrowedOn": "2026-03-01", "dueOn": "2026-03-15" } },
{ "name": "due before borrow", "valid": false, "failsAt": "dueOn", "loan": { "borrowedOn": "2026-03-15", "dueOn": "2026-03-01" } }
]
Each implementation loads the file, runs its own validator over every case, and asserts the verdict and the failing field. Add a case whenever a bug teaches you something. The file becomes the contract that no generator can express, and a new language can prove it conforms before it ships.
Add one more guard. In CI, regenerate the types and fail the build if the output differs from what is committed. That turns drift from a surprise into a red check.
A schema is a promise made to every service that reads it. Keep cross-field rules in L1 beside the shape, generate each language's types from a single definition, and share the rules that generators cannot carry as executable examples. For more on why this layer is meant to change so rarely, see the Schema layer article and the L1 overview, or read how the lowest layers are tested in Part 1 of layer-specific testing.